-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScannerUnit.pas
568 lines (496 loc) · 14 KB
/
ScannerUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/// Þíèò ñêàííåðà è ïðåïðîöåññîðà, âñ¸ ïðîèñõîäèò çà îäèí ïðîõîä
unit ScannerUnit;
interface
uses
System,
System.Text,
System.IO,
System.Collections,
System.Collections.Generic,
CommonUnit;
type
ScanReader = class
public
FileName: string;
Reader: TextReader;
Line: integer;
Column: integer;
codepage: Encoding;
constructor Create(fname: string; codepage: Text.Encoding);
begin
FileName := Path.GetFullPath(fname);
self.codepage := codepage;
try
self.Reader := new StreamReader(fname, codepage ) as TextReader;
except
on FileNotFoundException do raise new Exception('Íå íàéäåí ôàéë ' + fname);
end;
self.line := 1;
self.column := 0;
end;
function Read: integer;
begin
var c := reader.Read;
column += 1;
if c = 13 then
begin
line += 1;
column := 0;
end;
Result := c;
end;
function Peek: integer;
begin
Result := reader.Peek;
end;
end;
Token = class(Location)
public
Kind: TokenKind;
Data: object;
constructor Create(sr: ScanReader);
begin
FileName := sr.FileName;
DefineBeginPos(sr.line, sr.column)
end;
procedure EndCreate(sr: ScanReader);
begin
DefineEndPos(sr.line, sr.column)
end;
end;
GScanner = class
private
SaveToPos: TextPos;
// ïðåïðîöåññîð
IfStack: Stack<boolean>;
AllowAddToken := True;
DefineList: List<string>;
_tokens: List<Token>;
function TryExecScanDirective(Name, Value: string; Pos: TextPos; fname: string): boolean;
procedure Scan(fname: string);
public
property Tokens: List<Token> read _tokens;
constructor Create(fname: string; DefineList: List<string>);
end;
ScannerException = class(Exception)
Loc: Location;
constructor Create(message: string; TxtPos: TextPos);
begin
inherited Create(message);
self.Loc := new Location;
self.Loc.LocCopyFrom(TxtPos);
end;
end;
implementation
constructor GScanner.Create(fname: string; DefineList: List<string>);
begin
self.DefineList := new List<string>;
foreach s: string in DefineList do self.DefineList.Add(s);
_tokens := new List<Token>;
IfStack := new Stack<boolean>;
IfStack.Push(true);
Scan(fname);
end;
function GScanner.TryExecScanDirective(Name, Value: string; Pos: TextPos; fname: string): boolean;
begin
Result := False;
if Name.ToLower = 'include' then
begin
SaveToPos := Pos;
Scan(Value);
SaveToPos := nil;
Result := True;
end
else if Name.ToLower = 'define' then
begin
Value := Value.ToLower;
if AllowAddToken then
begin
if not DefineList.Contains(Value) then
DefineList.Add(Value)
else raise new ScannerException('èìÿ ' + Value + ' óæå áûëî îïðåäåëåíî', Pos);
end;
Result := True;
end
else if Name.ToLower = 'undef' then
begin
Value := Value.ToLower;
if AllowAddToken then
begin
if DefineList.Contains(Value) then
DefineList.Remove(Value)
else raise new ScannerException('ñòèðàíèå íå ñóùåñòâîþùåãî èìåíè ' + Value, Pos);
end;
Result := True;
end
else if Name.ToLower = 'ifdef' then
begin
Value := Value.ToLower;
if AllowAddToken then
begin
if DefineList.Contains(Value) then
AllowAddToken := True
else AllowAddToken := False;
end;
IfStack.Push(AllowAddToken);
Result := True;
end
else if Name.ToLower = 'ifndef' then
begin
Value := Value.ToLower;
if AllowAddToken then
begin
if not DefineList.Contains(Value) then
AllowAddToken := True
else AllowAddToken := False;
end;
IfStack.Push(AllowAddToken);
Result := True;
end
else if Name.ToLower = 'else' then
begin
AllowAddToken := not IfStack.Peek;
Result := True;
end
else if Name.ToLower = 'endif' then
begin
IfStack.Pop;
AllowAddToken := IfStack.Peek;
Result := True;
end
else if Name.ToLower = 'warning' then
begin
if AllowAddToken then
begin
OutputProc( new MessageInfo( string.Format('{2}<{0},{1}> warning: {3}', Pos.Line, Pos.Column, fname, Value), Warning, Pos));
end;
Result := True;
end
else if Name.ToLower = 'error' then
begin
if AllowAddToken then
begin
raise new ScannerException( string.Format('{2}<{0},{1}> error: {3}', Pos.Line, Pos.Column, fname, Value), pos );
end;
Result := True;
end;
end;
procedure GScanner.Scan(fname: string);
begin
var sr := new ScanReader(fname, Options.Codepage);
while sr.Peek <> -1 do
begin
var tk := new Token(sr); // Ñîçäàåòñÿ Token è çàïèñûâàåòñÿ â í¸ì òåêóùèé ñòîëáèê è ëèíèÿ
tk.Kind := NO_TOKEN; // Åñëè äàëåå òèï íå èçìåíèòñÿ, äîáàâëÿòü ñîçäàííûé òîêåí íå áóäåì
var ch := char(sr.Peek);
// ïóñòîòà
if char.IsWhiteSpace(ch) then
sr.Read
// {{ÁÓÊÂÀ}+[_]}{{ÁÓÊÂÀ}+[_]+{ÖÈÔÐÀ}}*
else if char.IsLetter(ch) or (ch = '_') then
begin
var sb := new StringBuilder;
while char.IsLetter(ch) or (ch = '_') or ((sb.Length > 0) and char.IsDigit(ch)) do
begin
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
end;
if keyword_dict.ContainsKey(sb.ToString.ToLower) then
begin
tk.Kind := TokenKind(keyword_dict[sb.ToString.ToLower]);
end
else
begin
tk.Kind := TokenKind.STRING_IDENT;
tk.Data := sb.ToString;
end;
end
// {ÖÈÔÐÀ}*|{ÖÈÔÐÀ}*.{ÖÈÔÐÀ}+
else if char.IsDigit(ch) then
begin
var sb := new StringBuilder;
var DotFound := False;
while char.IsDigit(ch) or ((sb.Length > 0) and (ch = '.') and not DotFound) do
begin
if ch = '.' then DotFound := True;
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
end;
if DotFound then
begin
tk.Kind := TokenKind.FLOAT_KIND;
tk.Data := StrToFloat(sb.ToString);
end
else
begin
tk.Kind := TokenKind.INTEGER_KIND;
tk.Data := Convert.ToInt32(sb.ToString);
end;
end
//
else if ch = #39 then
begin
sr.Read;
var sb := new StringBuilder;
ch := char(sr.Peek);
repeat
if sr.Peek = -1 then
raise new ScannerException('Îáíàðóæåíà íåçàêàí÷èâàþùàÿñÿ ñòðîêà!', new TextPos(sr.Line, sr.Column));
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
until ch = #39;
sr.Read;
tk.Kind := TokenKind.STRING_KIND;
tk.Data := sb.ToString;
end
//
else if ch = '{' then
begin
sr.Read;
if char(sr.Read) = '$' then
begin
var sb := new StringBuilder;
ch := char(sr.Peek);
while char.IsLetter(ch) or (ch = '_') or (ch = '.') or ((sb.Length > 0) and char.IsDigit(ch)) do
begin
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
end;
var CompilerDirectiveName := sb.ToString;
var CompilerDirectiveValue := '';
if char(sr.Peek) <> '}' then
begin
if char(sr.Read) <> ' ' then
raise new ScannerException('Îæèäàëñÿ ïðîáåë', new TextPos(sr.Line, sr.Column));
sb := new StringBuilder;
ch := char(sr.Peek);
if ch = #39 then
begin
sr.Read;
ch := char(sr.Peek);
repeat
if sr.Peek = -1 then
raise new ScannerException('Îáíàðóæåíà íåçàêàí÷èâàþùàÿñÿ ñòðîêà!', new TextPos(sr.Line, sr.Column));
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
until ch = #39;
sr.Read;
end
else
while char.IsLetter(ch) do
begin
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
end;
if char(sr.Read) <> '}' then
raise new ScannerException('Îæèäàëîñü "}"', new TextPos(sr.Line, sr.Column));
CompilerDirectiveValue := sb.ToString;
end
else sr.Read();
if (CompilerDirectiveName <> '') then
if not TryExecScanDirective(CompilerDirectiveName, CompilerDirectiveValue, new TextPos(tk.BeginLine, tk.BeginColumn), fname) then
begin
tk.EndCreate(sr);
Options.CompilerDirectives.Add(new CompilerDirective(CompilerDirectiveName, CompilerDirectiveValue, tk))
end;
end
else
begin
while char(sr.Peek) <> '}' do
begin
// íà ñëó÷àé êîãäà â â ñêîáêàõ {} äðóãîé êîììåíòàðèé //
if char(sr.Read) = '/' then
if char(sr.Read) = '/' then
while (sr.Peek <> -1) and (sr.Read <> 13) do
if sr.Peek = -1 then
raise new ScannerException('Îáíàðóæåí íåçàêàí÷èâàþùèéñÿ êîììåíòàðèé òèïà ôèãóðíûå ñêîáêè', new TextPos(sr.Line, sr.Column));
end;
sr.Read;
end;
end
else
case ch of
'+':
begin
sr.Read;
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := ADD_ASSIGN_OP;
end
else
tk.Kind := ADD_OP;
end;
'-':
begin
sr.Read;
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := SUB_ASSIGN_OP;
end
else
tk.Kind := SUB_OP;
end;
'*':
begin
sr.Read;
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := MUL_ASSIGN_OP;
end
else
tk.Kind := MUL_OP;
end;
'/':
begin
sr.Read;
if char(sr.Peek) = '/' then
while (sr.Peek <> -1) and (sr.Read <> 13) do
else
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := DIV_ASSIGN_OP;
end
else
tk.Kind := DIV_OP;
end;
':':
begin
sr.Read;
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := ASSIGN;
end
else
tk.Kind := COLON;
end;
'=':
begin
sr.Read;
tk.Kind := TK_EQUAL;
end;
'<':
begin
sr.Read;
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := TK_LESS_THAN_OR_EQUAL;
end
else if char(sr.Peek) = '>' then
begin
sr.Read;
tk.Kind := TK_NO_EQUAL;
end
else
tk.Kind := TK_LESS_THAN;
end;
'>':
begin
sr.Read;
if char(sr.Peek) = '=' then
begin
sr.Read;
tk.Kind := TK_GREATER_THAN_OR_EQUAL;
end
else
tk.Kind := TK_GREATER_THAN;
end;
'(':
begin
sr.Read;
tk.Kind := PAREN_OPEN;
end;
')':
begin
sr.Read;
tk.Kind := PAREN_CLOSE;
end;
'[':
begin
sr.Read;
tk.Kind := SQUARE_BRACKET_OPEN;
end;
']':
begin
sr.Read;
tk.Kind := SQUARE_BRACKET_CLOSE;
end;
';':
begin
sr.Read;
tk.Kind := SEMI;
end;
'.':
begin
sr.Read;
tk.Kind := DOT;
end;
',':
begin
sr.Read;
tk.Kind := COMMA;
end;
'&':
begin
sr.Read;
var sb := new StringBuilder;
ch := char(sr.Peek);
while char.IsLetter(ch) or (ch = '_') or ((sb.Length > 0) and char.IsDigit(ch)) do
begin
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
end;
if sb.Length = 0 then
raise new ScannerException('îæèäàëîñü êëþ÷åâîå ñëîâî ñ êîòîðîãî íàäî ñíÿòü àòòðèáóò êëþ÷åâîãî ñëîâà', new TextPos(tk.BeginLine, tk.BeginColumn));
if not keyword_dict.ContainsKey(sb.ToString.ToLower) then
raise new ScannerException('ñíèìàåòñÿ àòðèáóò êëþ÷åâîãî ñëîâà ñ íå êëþ÷åâîãî ñëîâà', new TextPos(tk.BeginLine, tk.BeginColumn));
tk.Kind := TokenKind.STRING_IDENT;
tk.Data := sb.ToString;
end;
'#':
begin
sr.Read;
tk.Kind := CHAR_KIND;
var sb := new StringBuilder;
ch := char(sr.Peek);
while char.IsDigit(ch) do
begin
sb.Append(ch);
sr.Read;
ch := char(sr.Peek);
end;
var int: integer;
if not integer.TryParse(sb.ToString, int) then
raise new ScannerException('Îæèäàëñÿ áàéò 0..255', new TextPos(sr.Line, sr.Column));
tk.Data := int;
end;
else raise new ScannerException('Ñêàííåð îáíàðóæèë íåïðåäâèäåííûé ñèìâîë "' + ch + '"', new TextPos(sr.Line, sr.Column));
end;
if tk.Kind <> NO_TOKEN then
begin
tk.EndCreate(sr);
if AllowAddToken then
if SaveToPos = nil then
_tokens.Add(tk)
else
begin
tk.DefineBeginPos(SaveToPos.Line, SaveToPos.Column);
tk.DefineEndPos(SaveToPos.Line, SaveToPos.Column);
_tokens.Add(tk)
end;
end;
end;
end;
end.