-
Notifications
You must be signed in to change notification settings - Fork 9
/
GS.Language.Tokenizer.Pascal.pas
486 lines (431 loc) · 15.5 KB
/
GS.Language.Tokenizer.Pascal.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
unit GS.Language.Tokenizer.Pascal;
{$IFDEF FPC}
{$mode delphi}{$H+}
{$ENDIF FPC}
interface
uses
Classes, SysUtils, ContNrs,
GS.Language.WordTokenizer,
GS.Language.Compiler;
type
//Main symbol type.
TPascalTokenType = ( pttword,
pttsymbol,
pttnumberConstInt,
pttnumberConstFloat,
pttstringConst,
pttLF,
pttCR,
pttComment,
pttCompilationDirective
);
//Sub symbol type : pttsymbol can have those subsymbol type. pttNA if non applicable.
TPascalSymbolTokenType = (pttNA,pttComma,pttSemiColon,ptt2Point,pttPlus,pttMinus,pttMul,pttDiv,pttEqual,pttPointer);
//langage specific symbol type.
TPascalNativeKeyWord = ( kwUnknown, kwPprogram, kwProcedure, kwFunction, kwMethod, kwBegin, kwEnd, kwConst, kwVar, kwType);
TPascalNativeType = ( ntUnknown, ntByte, ntInt, ntDouble, ntString, ntExtended);
TPascalNativeTypeExt = ( nteUnknown, nteArray, nteRec, nteClass, nteExtendedBeyond);
Const
CST_TPascalNativeKeyWord : array[TPascalNativeKeyWord] of string = ('unknown', 'program', 'procedure', 'function', 'method', 'begin', 'end', 'const', 'var', 'type');
CST_TPascalNativeType : array[TPascalNativeType] of string = ( 'Unknown', 'byte', 'integer', 'double', 'string' , 'ExtendedType');
CST_TPascalNativeTypeExt : array[TPascalNativeTypeExt] of string = ( 'Unknown', 'array', 'record', 'class', 'eExtendedBeyondType');
Type
{ TPascalTokenItem }
TPascalTokenItem = class
private
FColEnd: integer;
FColStart: integer;
FItem: string;
FLineEnd: integer;
FLineStart: integer;
FType: TPascalTokenType;
FSymbolType: TPascalSymbolTokenType;
protected
public
constructor create(_PascaltokType : TPascalTokenType; _item : String; _lineStart : integer; _colStart : integer; _lineEnd : integer; _colEnd : integer; const SymbolTokenType : TPascalSymbolTokenType = pttNA); virtual;
property PascalTokenType : TPascalTokenType read FType;
property PascalSymbolTokenType : TPascalSymbolTokenType read FSymbolType;
property item : string read FItem;
property lineStart : integer read FLineStart;
property colStart : integer read FColStart;
property lineEnd : integer read FLineEnd;
property colEnd : integer read FColEnd;
end;
{ TPascalTokenizer }
TPascalTokenizer = class(TWordTokenizer)
private
function GetPascalTokenCount: integer;
function GetPascalTokenItem(index : integer): TPascalTokenItem;
protected
FPascalTokens : TObjectList;
FIndex : Integer;
public
constructor Create; override;
destructor Destroy; override;
procedure Tokenize(const _source : String); override;
function SetCursorTo(token: TPascalTokenItem) : TPascalTokenItem;
function IsLast : boolean;
function First : TPascalTokenItem;
function Current : TPascalTokenItem;
function Next : TPascalTokenItem;
function Previous : TPascalTokenItem;
property PascalTokens[index : integer] : TPascalTokenItem read GetPascalTokenItem;
property PascalTokenCount : integer read GetPascalTokenCount;
end;
TPascalTokenItemArray = Array of TPascalTokenItem;
procedure SetPascalStyleKeySymbols(var _symbols : DyArrayOfChar);
implementation
procedure SetPascalStyleKeySymbols(var _symbols: DyArrayOfChar);
begin
_symbols := DyArrayOfChar.Create(' ',';',':','=','+','-','*','[',']','/','%','&','{','}',',','(',')','''','.',#13,#10,'^');
end;
{ TPascalTokenItem }
constructor TPascalTokenItem.create(_PascaltokType: TPascalTokenType;
_item: String; _lineStart: integer; _colStart: integer; _lineEnd: integer;
_colEnd: integer; const SymbolTokenType : TPascalSymbolTokenType);
begin
inherited create;
FColEnd := _colEnd;
FColStart := _colStart;
FItem := lowercase(_item);
FLineEnd := _lineEnd;
FLineStart := _lineStart;
FType := _PascaltokType;
FSymbolType := SymbolTokenType;
end;
{ TPascalTokenizer }
function TPascalTokenizer.GetPascalTokenCount: integer;
begin
result := FPascalTokens.count;
end;
function TPascalTokenizer.GetPascalTokenItem(index : integer): TPascalTokenItem;
begin
result := TPascalTokenItem(FPascalTokens[index]);
end;
function TPascalTokenizer.IsLast: boolean;
begin
Result := FIndex = PascalTokenCount-1;
end;
constructor TPascalTokenizer.Create;
begin
inherited Create;
SetPascalStyleKeySymbols(KeySymbols);
FPascalTokens := TObjectList.Create;
FIndex := 0;
end;
function TPascalTokenizer.Current: TPascalTokenItem;
begin
result := PascalTokens[FIndex];
end;
destructor TPascalTokenizer.Destroy;
begin
FreeAndNil(FPascalTokens);
inherited Destroy;
end;
procedure TPascalTokenizer.Tokenize(const _source: String);
var idx,j : integer;
temps : string;
tempc, templ : integer;
parity : integer;
pType : TPascalTokenType;
procedure AddStdPascalToken(_ptokentype : TPascalTokenType; const _SymbolTokenType : TPascalSymbolTokenType = pttNA);
begin
FPascalTokens.Add(TPascalTokenItem.Create(
_pTokenType,
tokens[idx].item,
tokens[idx].line,
tokens[idx].col,
tokens[idx].line,
tokens[idx].col,
_SymbolTokenType)
);
end;
begin
inherited Tokenize(_source);
idx := 0;
while (idx<tokenCount) do
begin
case tokens[idx].TokType of
TTokenType.ttsymbol:
begin
if tokens[idx].item = ';' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttSemiColon);
inc(idx);
end
else
if tokens[idx].item = ',' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttComma);
inc(idx);
end
else
if tokens[idx].item = ':' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.ptt2Point);
inc(idx);
end
else
if tokens[idx].item = '+' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttPlus);
inc(idx);
end
else
if tokens[idx].item = '-' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttMinus);
inc(idx);
end
else
if tokens[idx].item = '*' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttMul);
inc(idx);
end
else
// PLEASE : let this comment.
// if tokens[idx].item = '/' then
// begin
//Not here : shared symbol with comment. See in comments.
// end
// else
if tokens[idx].item = '=' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttEqual);
inc(idx);
end
else
if tokens[idx].item = '^' then
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttPointer);
inc(idx);
end
else
if tokens[idx].item = '/' then
begin
if Not(idx=tokenCount) and (tokens[idx+1].item ='/') then
begin
j := idx+1;
temps := '';
tempc := tokens[j].col;
templ := tokens[j].line;
while (tokens[j].TokType<>TTokenType.ttCR) or (j = tokenCount) do
begin
inc(j);
temps := temps + tokens[j].item;
tempc := tokens[j].col;
templ := tokens[j].line;
end;
FPascalTokens.Add(TPascalTokenItem.Create(
TPascalTokenType.pttComment,
temps,
tokens[idx].line,
tokens[idx].col,
templ,
tempc)
);
idx := j;
end
else
begin
AddStdPascalToken(TPascalTokenType.pttsymbol,TPascalSymbolTokenType.pttDiv);
inc(idx);
end;
end
else
if tokens[idx].item = '{' then
begin
pType := TPascalTokenType.pttComment;
if Not(idx=tokenCount) then
begin
j := idx+1;
parity := 1;
temps := '';
tempc := tokens[j].col;
templ := tokens[j].line;
while (parity>0) and (j < tokenCount) do
begin
if tokens[j].item = '{' then
inc(parity)
else
if tokens[j].item = '}' then
dec(parity)
else
if (tokens[j].item = '$') and (j=idx+1) then
pType := TPascalTokenType.pttComment
else
begin
temps := temps + tokens[j].item;
tempc := tokens[j].col;
templ := tokens[j].line;
end;
inc(j);
end;
if j<tokenCount then
begin
FPascalTokens.Add(TPascalTokenItem.Create(
pType,
temps,
tokens[idx].line,
tokens[idx].col,
templ,
tempc)
);
end;
idx := j;
end
else
begin
AddStdPascalToken(TPascalTokenType.pttsymbol);
inc(idx);
end;
end
else
if tokens[idx].item = '''' then
begin
if idx<tokenCount then
begin
j := idx+1;
parity:= 1;
temps := '';
tempc := tokens[j].col;
templ := tokens[j].line;
while ((parity>0) and (j < tokenCount-1)) do
begin
if (tokens[j].item='''') And (tokens[j+1].item='''') then
begin
temps := temps + tokens[j].item;
tempc := tokens[j].col;
templ := tokens[j].line;
j := j+1;
end
else
if tokens[j].item='''' then
begin
parity := 0;
end
else
begin
temps := temps + tokens[j].item;
tempc := tokens[j].col;
templ := tokens[j].line;
end;
inc(j);
end;
FPascalTokens.Add(TPascalTokenItem.Create(
TPascalTokenType.pttstringConst,
temps,
tokens[idx].line,
tokens[idx].col,
templ,
tempc)
);
idx := j;
end
else
begin
AddStdPascalToken(TPascalTokenType.pttsymbol);
inc(idx);
end;
end
else
begin
AddStdPascalToken(TPascalTokenType.pttsymbol);
inc(idx);
end;
end;
TTokenType.ttCR:
begin
// AddStdPascalToken(TPascalTokenType.pttCR); Useless in pascal ?
inc(idx);
end;
TTokenType.ttLF:
begin
// AddStdPascalToken(TPascalTokenType.pttLF);
inc(idx);
end;
TTokenType.ttnumber:
begin
//const float number writen as "x." (c-style)
if (idx<tokenCount-1) and (tokens[idx+1].item ='.') and (tokens[idx+2].TokType<>TTokenType.ttnumber) then
begin
FPascalTokens.Add(TPascalTokenItem.Create(
TPascalTokenType.pttnumberConstFloat,
tokens[idx].item+tokens[idx+1].item+'0',
tokens[idx].line,
tokens[idx].col,
tokens[idx+1].line,
tokens[idx+1].col)
);
idx :=idx+2
end
else
//const float number writen as "x.y"
if (idx<tokenCount-1) and (tokens[idx+1].item ='.') and (tokens[idx+2].TokType=TTokenType.ttnumber) then
begin
FPascalTokens.Add(TPascalTokenItem.Create(
TPascalTokenType.pttnumberConstFloat,
tokens[idx].item+tokens[idx+1].item+tokens[idx+2].item,
tokens[idx].line,
tokens[idx].col,
tokens[idx+2].line,
tokens[idx+2].col)
);
idx :=idx+3
end
else
begin //no point, it is a const.
AddStdPascalToken(TPascalTokenType.pttnumberConstInt);
inc(idx);
end;
end;
TTokenType.ttword:
begin
AddStdPascalToken(TPascalTokenType.pttword);
inc(idx);
end;
else
begin
inc(idx);
end;
end;
end;
end;
function TPascalTokenizer.First: TPascalTokenItem;
begin
FIndex := -1;
result := Next;
end;
function TPascalTokenizer.Next: TPascalTokenItem;
begin
Assert(Findex<PascalTokenCount-1);
inc(FIndex);
While (FIndex<PascalTokenCount) and
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttCompilationDirective) or
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttComment) or
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttLF) or
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttCR) or
(PascalTokens[FIndex].item = ' ') do
inc(FIndex);
result := PascalTokens[FIndex];
end;
function TPascalTokenizer.Previous: TPascalTokenItem;
begin
dec(FIndex);
While (FIndex>0) and
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttCompilationDirective) or
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttComment) or
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttLF) or
(PascalTokens[FIndex].PascalTokenType = TPascalTokenType.pttCR) or
(PascalTokens[FIndex].item = ' ') do
Dec(FIndex);
result := PascalTokens[FIndex];
end;
function TPascalTokenizer.SetCursorTo(token: TPascalTokenItem) : TPascalTokenItem;
begin
assert(assigned(token));
FIndex := FPascalTokens.IndexOf(token);
result := token;
end;
end.