Menu

[r9]: / trunk / src / Parser.pas  Maximize  Restore  History

Download this file

472 lines (427 with data), 11.6 kB

  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
unit Parser;
interface
uses
Global, Source;
{$I FlashPascal.inc}
type
TToken=(
// Pascal keywords
tkProgram,
tkUses, tkUnit, tkInterface, tkImplementation,
tkVar, tkType, tkConst,
tkBegin, tkEnd,
tkProcedure, tkFunction,
tkExternal,
tkClass, tkConstructor, tkAs,
tkProperty,
tkNil,
tkDiv, tkMod,
tkTrue,tkFalse,
tkIf, tkThen,
tkFor, tkTo, tkDownto, tkDo,
tkArray, tkOf,
tkObject,
tkInherited,
tkSelf,
tkCopy,
tkCase, tkElse,
tkRepeat, tkUntil,
tkWhile,
tkOr, tkAnd,
// built in functions
tkIntToStr, tkFloatToStr, tkSort,
// user defined symbol
tk_Symbol,
tk_Variable, tk_Parameter, tk_Property, tk_Method, tk_Prototype, tk_Class, tk_Constant,
// Symbols, etc...
tk_Equal, tk_Greater, tk_Lesser, tk_GE, tk_LE,
tk_Add, tk_Sub, tk_Mul, tk_Slash, tk_Range,
tk_Assign, tk_Comma, tk_Colon, tk_SemiColon, tk_Dot,
tk_LParen, tk_RParen,
tk_LBracket, tk_RBracket,
tk_String, tk_Number, tk_Float, tk_Ident,
tk_Comment, tk_LComment1, tk_RComment1, tk_LComment2, tk_RComment2,
tk_Switch1
);
TVariable=class(TSymbol)
Kind :TSymbol;
Owner :TSymbol; // nil for Global var...
{$IFDEF REG_VARS}
Reg :byte;
{$ENDIF}
end;
TConstant=class(TSymbol)
Code : string;
Kind : TSymbol;
end;
TParameter=class(TSymbol)
IsConst:boolean; // not used for now
ByRef :boolean;
Kind :TSymbol;
{$IFDEF REG_PARAM}
Reg :byte;
{$ENDIF}
NextParam:TParameter;
function Clone:TParameter;
end;
TProperty=class(TSymbol)
Kind : TSymbol;
Alias: string;
end;
TFunction=class(TSymbol)
params :TParameter;
count :integer;
regs :integer;
Kind :TSymbol; // function result
Return :TVariable;
destructor Destroy; override;
end;
TMethod=class(TFunction)
alias :string; // alias used for MovieClip.createTextField()
Parent :TParameter; // MovieClip is this case
code :string; // for UserClass only
Owner :TSymbol;
Symbols :TSymbol;
Externe :string; // Math.floor
destructor Destroy; override;
function LastParm: TParameter;
end;
TClass=class(TSymbol)
_inherite : TClass;
_constructor: TMethod;
_symbols : TSymbol;
destructor Destroy; override;
end;
TParser=class(TSource)
TokenType:TToken; // what kind of token is it ?
procedure AlphaToken;
procedure NumericToken;
procedure StringToken;
procedure SymbolToken;
procedure HexaToken;
procedure GetToken;
procedure NextToken; virtual;
function SkipToken(Token:TToken):boolean;
procedure DropToken(Token:TToken);
procedure DropIdent(const Ident:string);
function GetIdent:string;
function GetInteger:Integer;
function CheckSymbol(List:TSymbol):boolean;
function CheckScope(Symbol:TSymbol):boolean;
end;
var
Symbols :TSymbol; // chained list of symbols
Scopes :PScope;
Symbol :TSymbol; // token symbol
implementation
{ TParameter }
function TParameter.Clone:TParameter;
begin
Result:=TParameter.Create;
Result.IsConst:=IsConst;
Result.ByRef:=ByRef;
Result.Kind:=Kind;
{$IFDEF REG_PARAM}
Result.Reg:=Reg;
{$ENDIF}
Result.name:=name;
Result.realName:=realName;
end;
{ TClass }
destructor TClass.Destroy;
var
s:TSymbol;
begin
s:=_symbols;
while s<>nil do begin
_symbols:=s.NextSymbol;
s.Free;
s:=_symbols;
end;
inherited;
end;
{ TFunction }
destructor TFunction.Destroy;
var
p:TParameter;
begin
p:=Params;
while p<>nil do begin
Params:=p.NextParam;
p.Free;
p:=Params;
end;
inherited;
end;
{ TMethod }
destructor TMethod.Destroy;
var
s:TSymbol;
begin
s:=Symbols;
while s<>nil do begin
Symbols:=s.NextSymbol;
s.Free;
s:=Symbols;
end;
Parent.Free;
inherited;
end;
function TMethod.LastParm: TParameter;
begin
Result:=Params;
if Result<>nil then begin
while Result.NextParam<>nil do Result:=Result.NextParam;
end;
end;
{ TParser }
// read an alpha token
procedure TParser.AlphaToken;
var
scope:PScope;
begin
TokenType:=tk_Ident;
while upcase(NextChar) in ['_','A'..'Z','0'..'9'] do begin
Token:=Token+upcase(ReadChar);
end;
// check for reserved keyword
if Token='PROGRAM' then TokenType:=tkProgram else
if Token='USES' then TokenType:=tkUses else
if Token='UNIT' then TokenType:=tkUnit else
if Token='INTERFACE' then TokenType:=tkInterface else
if Token='IMPLEMENTATION' then TokenType:=tkImplementation else
if Token='VAR' then TokenType:=tkVar else
if Token='TYPE' then TokenType:=tkType else
if Token='CONST' then TokenType:=tkConst else
if Token='BEGIN' then TokenType:=tkBegin else
if Token='END' then TokenType:=tkEnd else
if Token='PROCEDURE' then TokenType:=tkProcedure else
if Token='FUNCTION' then TokenType:=tkFunction else
if Token='NIL' then TokenType:=tkNil else
if Token='DIV' then TokenType:=tkDiv else
if Token='MOD' then TokenType:=tkMod else
if Token='TRUE' then TokenType:=tkTrue else
if Token='FALSE' then TokenType:=tkFalse else
if Token='IF' then TokenType:=tkIf else
if Token='THEN' then TokenType:=tkThen else
if Token='FOR' then TokenType:=tkFor else
if Token='TO' then TokenType:=tkTo else
if Token='DOWNTO' then TokenType:=tkDownto else
if Token='DO' then TokenType:=tkDo else
if Token='ARRAY' then TokenType:=tkArray else
if Token='OF' then TokenType:=tkOf else
if Token='OBJECT' then TokenType:=tkObject else
if Token='INHERITED' then TokenType:=tkInherited else
if Token='SELF' then TokenType:=tkSelf else
if Token='COPY' then TokenType:=tkCopy else
if Token='CASE' then TokenType:=tkCase else
if Token='ELSE' then TokenType:=tkElse else
if Token='REPEAT' then TokenType:=tkRepeat else
if Token='UNTIL' then TokenType:=tkUntil else
if Token='WHILE' then TokenType:=tkWhile else
if Token='OR' then TokenType:=tkOr else
if Token='AND' then TokenType:=tkAnd else
if Token='INTTOSTR' then TokenType:=tkIntToStr else
if Token='FLOATTOSTR' then TokenType:=tkFloatToStr else
if Token='SORT' then TokenType:=tkSort else
if Token='EXTERNAL' then TokenType:=tkExternal else
if Token='CLASS' then TokenType:=tkClass else
if Token='CONSTRUCTOR' then TokenType:=tkConstructor else
if Token='AS' then TokenType:=tkAs else
if Token='PROPERTY' then TokenType:=tkProperty else
begin
// Check for local scope symbols
scope:=Scopes;
while scope<>nil do begin
if CheckScope(scope.Symbol) then exit;
scope:=scope.Next;
end;
// global scope also
CheckSymbol(Symbols);
end;
end;
// read a numeric token
procedure TParser.NumericToken;
begin
TokenType:=tk_Number;
while NextChar in ['0'..'9'] do begin
Token:=Token+ReadChar;
end;
(* !!! WARNING 0..1 is not a Float !
if NextChar='.' then begin
TokenType:=tk_Float;
Token:=Token+ReadChar;
while NextChar in ['0'..'9'] do begin
Token:=Token+ReadChar;
end;
end;
*)
end;
// read a string made of litterals and ascii chars
procedure TParser.StringToken;
begin
TokenType:=tk_String;
while NextChar in ['#',''''] do begin
if NextChar='#' then
Token:=Token+AsciiChar
else
Token:=Token+StringConst;
end;
end;
// get a special symbol
procedure TParser.SymbolToken;
begin
case ReadChar of
'=' : TokenType:=tk_Equal;
'>' : if SkipChar('=') then TokenType:=tk_GE else TokenType:=tk_Greater;
'<' : if SkipChar('=') then TokenType:=tk_LE else TokenType:=tk_Lesser;
'+' : TokenType:=tk_Add;
'-' : TokenType:=tk_Sub;
'*' : TokenType:=tk_Mul;
'.' : if SkipChar('.') then TokenType:=tk_Range else TokenType:=tk_Dot;
',' : TokenType:=tk_Comma;
':' : if SkipChar('=') then TokenType:=tk_Assign else TokenType:=tk_Colon;
';' : TokenType:=tk_SemiColon;
'(' : if SkipChar('*') then TokenType:=tk_LComment2 else TokenType:=tk_LParen;
')' : TokenType:=tk_RParen;
'{' : if SkipChar('$') then TokenType:=tk_Switch1 else TokenType:=tk_LComment1;
'}' : TokenType:=tk_RComment1;
'[' : TokenType:=tk_LBracket;
']' : TokenType:=tk_RBracket;
'/' : if SkipChar('/') then Tokentype:=tk_Comment else TokenType:=tk_Slash;
else Error('Unknow symbol '+LastChar);
end;
end;
// get an hexadecimal number
procedure TParser.HexaToken;
begin
TokenType:=tk_Number;
Token:=ReadChar;
while upcase(NextChar) in ['0'..'9','A'..'F'] do begin
Token:=Token+upcase(ReadChar);
end;
if Length(Token)>9 then Error('Ordinal overflow');
end;
// get the next token
procedure TParser.GetToken;
begin
while NextChar<=#32 do ReadChar;
SrcToken:='';
Token:='';
case upcase(NextChar) of
'_','A'..'Z' : AlphaToken;
'0'..'9' : NumericToken;
'$' : HexaToken;
'#','''' : StringToken;
else SymbolToken;
end;
end;
// get the next token, handle compiler switch & comments
procedure TParser.NextToken;
begin
GetToken;
case TokenType of
tk_Comment : begin
repeat until ReadChar in [#10,#13];
NextToken;
end;
tk_LComment1 : begin
repeat until ReadChar='}';
NextToken;
end;
tk_LComment2 : begin
repeat
repeat until ReadChar='*';
until NextChar=')';
ReadChar;
NextToken;
end;
end;
end;
// skip a token
function TParser.SkipToken(Token:TToken):boolean;
begin
Result:=TokenType=Token;
if Result then NextToken;
end;
// request a token
procedure TParser.DropToken(Token:TToken);
begin
if not SkipToken(Token) then
Error('Unexpected token (drop)');
end;
procedure TParser.DropIdent(const Ident:string);
begin
if Token<>Ident then Error('Expected '+Ident);
NextToken;
end;
// request an ident
function TParser.GetIdent:string;
begin
if TokenType<>tk_Ident then begin
// quick hack for parameters with a global name
if not (TokenType in [tk_Variable]) then
Error('Ident expected');
end;
Result:=Token;
NextToken;
end;
function TParser.GetInteger:integer;
begin
if TokenType<>tk_Number then Error('number expected');
if BitsCount(Token)>32 then Error('ordinal overflow');
Result:=StrToInt(Token);
NextToken;
end;
function TParser.CheckSymbol(List:TSymbol):boolean;
begin
Symbol:=List;
while Symbol<>nil do begin
if Symbol.name=Token then begin
if Symbol is TVariable then
TokenType:=tk_Variable
else
if Symbol is TConstant then
TokenType:=tk_Constant
else
if Symbol is TParameter then
TokenType:=tk_Parameter
else
if Symbol is TProperty then
TokenType:=tk_Property
else
if Symbol is TClass then
TokenType:=tk_Class
else
if Symbol is TMethod then
TokenType:=tk_Method
else
TokenType:=tk_Symbol;
Result:=True;
exit;
end;
Symbol:=Symbol.NextSymbol;
end;
Result:=False;
end;
function TParser.CheckScope(Symbol:TSymbol):boolean;
var
cl:TClass;
begin
Result:=False;
if Symbol is TClass then begin
cl:=TClass(Symbol);
while cl<>nil do begin
Result:=CheckSymbol(cl._symbols);
if Result then exit;
cl:=cl._inherite;
end;
end else
if Symbol is TMethod then begin
Result:=CheckSymbol(TMethod(Symbol).Symbols);
if Result=False then
Result:=CheckSymbol(TMethod(Symbol).params);
end else
Error('CheckScope for '+Symbol.ClassName);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.