-
Notifications
You must be signed in to change notification settings - Fork 50
/
CastaliaSimplePasPar.pas
6211 lines (5788 loc) · 133 KB
/
CastaliaSimplePasPar.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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{---------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.mozilla.org/NPL/NPL-1_1Final.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: mwSimplePasPar.pas, released November 14, 1999.
The Initial Developer of the Original Code is Martin Waldenburg
Portions created by Martin Waldenburg are Copyright (C) 1998, 1999 Martin
Waldenburg.
All Rights Reserved.
Portions CopyRight by Robert Zierer.
Contributor(s): James Jacobson, Dean Hill, Vladimir Churbanov___________________.
Last Modified: 2002/01/16
Current Version: 1.02
Notes: This program is an early beginning of a Pascal parser.
I'd like to invite the Delphi community to develop it further and to create
a fully featured Object Pascal parser.
Modification history:
Jacob Thurman between 20040301 and 20020401
Made ready for Delphi 8:
Added new directives and keywords: static, sealed, final, operator, unsafe.
Added parsing for custom attributes (based on ECMA C# specification).
Added support for nested types in class declarations.
Jeff Rafter between 20020116 and 20020302
Added AncestorId and AncestorIdList back in, but now treat them as Qualified
Identifiers per Daniel Rolf's fix. The separation from QualifiedIdentifierList
is need for descendent classes.
Added VarName and VarNameList back in for descendent classes, fixed to correctly
use Identifiers as in Daniel's verison
Removed fInJunk flags (they were never used, only set)
Pruned uses clause to remove windows dependency. This required changing
"TPoint" to "TTokenPoint". TTokenPoint was declared in mwPasLexTypes
Daniel Rolf between 20010723 and 20020116
Made ready for Delphi 6
ciClassClass for "class function" etc.
ciClassTypeEnd marks end of a class declaration (I needed that for the delphi-objectif-connector)
ciEnumeratedTypeItem for items of enumerations
ciDirectiveXXX for the platform, deprecated, varargs, local
ciForwardDeclaration for "forward" (until now it has been read but no event)
ciIndexSpecifier for properties
ciObjectTypeEnd marks end of an object declaration
ciObjectProperty property for objects
ciObjectPropertySpecifiers property for objects
ciPropertyDefault marking default of property
ciDispIDSpecifier for dispid
patched some functions for implementing the above things and patching the following bugs/improv.:
ObjectProperty handling overriden properties
ProgramFile, UnitFile getting Identifier instead of dropping it
InterfaceHeritage: Qualified identifiers
bugs in variant records
typedconstant failed with complex set constants. simple patch using ConstantExpression
German localization for the two string constants. Define GERMAN for german string constants.
Greg Chapman on 20010522
Better handling of defaut array property
Separate handling of X and Y in property Pixels[X, Y: Integer through identifier "event"
corrected spelling of "ForwardDeclaration"
James Jacobson on 20010223
semi colon before finalization fix
James Jacobson on 20010223
RecordConstant Fix
Martin waldenburg on 2000107
Even Faster lexer implementation !!!!
James Jacobson on 20010107
Improper handling of the construct
property TheName: Integer read FTheRecord.One.Two; (stop at second point)
where one and two are "qualifiable" structures.
James Jacobson on 20001221
Stops at the second const.
property Anchor[const Section: string; const Ident:string]: string read
changed TmwSimplePasPar.PropertyParameterList
On behalf of Martin Waldenburg and James Jacobson
Correction in array property Handling (Matin and James) 07/12/2000
Use of ExId instead of TokenId in ExportsElements (James) 07/12/2000
Reverting to old behavior in Statementlist [PtintegerConst put back in] (James) 07/12/2000
Xavier Masson InnerCircleProject : XM : 08/11/2000
Integration of the new version delivered by Martin Waldenburg with the modification I made described just below
Xavier Masson InnerCircleProject : XM : 07/15/2000
Added "states/events " for spaces( SkipSpace;) CRLFco (SkipCRLFco) and
CRLF (SkipCRLF) this way the parser can give a complete view on code allowing
"perfect" code reconstruction.
(I fully now that this is not what a standard parser will do but I think it is more usefull this way ;) )
go to www.innercircleproject.com for more explanations or express your critisism ;)
previous modifications not logged sorry ;)
Known Issues:
-----------------------------------------------------------------------------}
{----------------------------------------------------------------------------
Last Modified: 05/22/2001
Current Version: 1.1
official version
Maintained by InnerCircle
http://www.innercircleproject.org
02/07/2001
added property handling in Object types
changed handling of forward declarations in ExportedHeading method
-----------------------------------------------------------------------------}
unit CastaliaSimplePasPar;
interface
uses
//!! pruned uses
SysUtils,
Classes,
CastaliaPasLexTypes,
CastaliaPasLex,
CastaliaSimplePasParTypes;
{$INCLUDE CastaliaParserDefines.inc}
{$IFDEF GERMAN} // DR 2002-01-16
resourcestring
rsExpected = '''%s'' erwartet, aber ''%s'' gefunden';
rsEndOfFile = 'Dateiende';
{$ELSE}
resourcestring
rsExpected = '''%s'' expected found ''%s''';
rsEndOfFile = 'end of file';
{$ENDIF}
const
ClassMethodDirectiveEnum = [ptAbstract, ptCdecl, ptDynamic, ptMessage, ptOverride,
ptOverload, ptPascal, ptRegister, ptReintroduce, ptSafeCall, ptStdCall,
ptVirtual,
ptDeprecated, ptLibrary, ptPlatform // DR 2001-10-20
{$IFDEF D8_NEWER}
, ptStatic //JThurman 2004-11-10
{$ENDIF}
{$IFDEF D9_NEWER}
, ptInline
{$ENDIF}
]; //XM 2002-01-29
type
ESyntaxError = class(Exception)
private //jdj 7/18/1999
FPosXY: TTokenPoint;
protected
public
constructor Create(const Msg: string);
constructor CreateFmt(const Msg: string; const Args: array of const);
constructor CreatePos(const Msg: string; aPosXY: TTokenPoint);
property PosXY: TTokenPoint read FPosXY write FPosXY;
end;
PDefineRec = ^TDefineRec;
TDefineRec = record
Defined: Boolean;
StartCount: Integer;
Next: PDefineRec;
end;
type
TmwSimplePasPar = class(TObject)
private
FOnMessage: TMessageEvent;
fLexer: TmwPasLex;
fOwnStream: Boolean;
fStream: TCustomMemoryStream;
fInterfaceOnly: Boolean;
fLastNoJunkPos: Integer;
fLastNoJunkLen: Integer;
FUseDefines: Boolean;
FDefines: TStrings;
AheadParse: TmwSimplePasPar;
FTopDefineRec: PDefineRec;
procedure EnterDefineBlock(ADefined: Boolean);
procedure ExitDefineBlock;
procedure ClearDefines;
procedure InitAhead;
protected
FDefineStack: Integer;
fInRound: Boolean;
// !! removed fInJunk
procedure Expected(Sym: TptTokenKind); virtual;
procedure ExpectedEx(Sym: TptTokenKind); virtual;
procedure ExpectedFatal(Sym: TptTokenKind); virtual;
procedure HandlePtCompDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtDefineDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtElseDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtEndIfDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtIfDefDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtIfNDefDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtIfOptDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtIncludeDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtResourceDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtUndefDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtIfDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtIfEndDirect(Sender: TmwBasePasLex); virtual;
procedure HandlePtElseIfDirect(Sender: TmwBasePasLex); virtual;
procedure NextToken; virtual;
procedure SkipJunk; virtual;
procedure TerminateStream; virtual;
procedure SEMICOLON; virtual;
function GetExID: TptTokenKind; virtual;
function GetTokenID: TptTokenKind; virtual;
function GetGenID: TptTokenKind; virtual;
procedure AccessSpecifier; virtual;
procedure AdditiveOperator; virtual;
procedure AncestorIdList; virtual; // !! Added ancestorIdList back in...
procedure AncestorId; virtual; // !! Added ancestorId back in...
procedure AnonymousMethod; virtual;
procedure AnonymousMethodType; virtual;
procedure ArrayConstant; virtual;
procedure ArrayType; virtual;
procedure AsmStatement; virtual;
procedure Block; virtual;
procedure CaseLabel; virtual;
procedure CaseSelector; virtual;
procedure CaseStatement; virtual;
procedure CharString; virtual;
procedure ClassField; virtual;
procedure ClassForward; virtual;
procedure ClassFunctionHeading; virtual;
procedure ClassHeritage; virtual;
procedure ClassMemberList; virtual;
procedure ClassMethodDirective; virtual;
procedure ClassMethodHeading; virtual;
procedure ClassMethodOrProperty; virtual;
procedure ClassMethodResolution; virtual;
procedure ClassProcedureHeading; virtual;
procedure ClassClass; virtual;
procedure ClassProperty; virtual;
procedure ClassReferenceType; virtual;
procedure ClassType; virtual;
procedure ClassTypeEnd; virtual; // DR 2001-07-31
procedure ClassVisibility; virtual;
procedure CompoundStatement; virtual;
procedure ConstantColon; virtual;
procedure ConstantDeclaration; virtual;
procedure ConstantEqual; virtual;
procedure ConstantExpression; virtual;
procedure ConstantName; virtual;
//JR added constant type
procedure ConstantType; virtual;
procedure ConstantValue; virtual;
procedure ConstantValueTyped; virtual;
procedure ConstParameter; virtual;
procedure ConstructorHeading; virtual;
procedure ConstructorName; virtual;
procedure ConstSection; virtual;
procedure ContainsClause; virtual;
procedure ContainsExpression; virtual;
procedure ContainsIdentifier; virtual;
procedure ContainsStatement; virtual;
{$IFDEF D8_NEWER}
procedure CustomAttribute; virtual; //JThurman 2004-03-03
{$ENDIF}
procedure DeclarationSection; virtual;
procedure Designator; virtual;
procedure DestructorHeading; virtual;
procedure DestructorName; virtual;
procedure Directive16Bit; virtual;
procedure DirectiveBinding; virtual;
procedure DirectiveCalling; virtual;
procedure DirectiveDeprecated; virtual; // DR 2001-10-20
procedure DirectiveLibrary; virtual; // DR 2001-10-20
procedure DirectiveLocal; virtual; // DR 2001-11-14
procedure DirectivePlatform; virtual; // DR 2001-10-20
procedure DirectiveVarargs; virtual; // DR 2001-11-14
procedure DispInterfaceForward; virtual;
procedure DispIDSpecifier; virtual; // DR 2001-07-26
procedure EmptyStatement; virtual;
procedure EnumeratedType; virtual;
procedure EnumeratedTypeItem; virtual; // DR 2001-10-29
procedure ExceptBlock; virtual;
procedure ExceptionBlockElseBranch; virtual;
procedure ExceptionClassTypeIdentifier; virtual;
procedure ExceptionHandler; virtual;
procedure ExceptionHandlerList; virtual;
procedure ExceptionIdentifier; virtual;
procedure ExceptionVariable; virtual;
procedure ExplicitType; virtual; // !! changed spelling to "Explicit"
procedure ExportedHeading; virtual;
procedure ExportsClause; virtual;
procedure ExportsElement; virtual;
procedure Expression; virtual;
procedure ExpressionList; virtual;
procedure ExternalDirective; virtual;
procedure ExternalDirectiveThree; virtual;
procedure ExternalDirectiveTwo; virtual;
procedure Factor; virtual;
procedure FieldDeclaration; virtual;
procedure FieldList; virtual;
procedure FieldNameList; virtual;
procedure FieldName; virtual;
procedure FileType; virtual;
procedure FormalParameterList; virtual;
procedure FormalParameterSection; virtual;
procedure ForStatement; virtual;
procedure ForwardDeclaration; virtual; {GLC: corrected spelling}
procedure FunctionHeading; virtual;
procedure FunctionMethodDeclaration; virtual;
procedure FunctionMethodName; virtual;
procedure FunctionProcedureBlock; virtual;
procedure FunctionProcedureName; virtual;
procedure Identifier; virtual;
procedure IdentifierList; virtual;
procedure IfStatement; virtual;
procedure ImplementationSection; virtual;
procedure IncludeFile; virtual;
procedure IndexSpecifier; virtual; // DR 2001-07-26
procedure InheritedStatement; virtual;
procedure InitializationSection; virtual;
procedure InlineStatement; virtual;
procedure InParameter; virtual;
procedure InterfaceDeclaration; virtual;
procedure InterfaceForward; virtual;
procedure InterfaceGUID; virtual;
procedure InterfaceHeritage; virtual;
procedure InterfaceMemberList; virtual;
procedure InterfaceSection; virtual;
procedure InterfaceType; virtual;
procedure LabelDeclarationSection; virtual;
procedure LabeledStatement; virtual;
procedure LabelId; virtual;
procedure LibraryFile; virtual;
procedure MainUsedUnitExpression; virtual;
procedure MainUsedUnitName; virtual;
procedure MainUsedUnitStatement; virtual;
procedure MainUsesClause; virtual;
procedure MultiplicativeOperator; virtual;
procedure NewFormalParameterType; virtual;
procedure Number; virtual;
procedure ObjectConstructorHeading; virtual;
procedure ObjectDestructorHeading; virtual;
procedure ObjectField; virtual;
procedure ObjectForward; virtual;
procedure ObjectFunctionHeading; virtual;
procedure ObjectHeritage; virtual;
procedure ObjectMemberList; virtual;
procedure ObjectMethodDirective; virtual;
procedure ObjectMethodHeading; virtual;
procedure ObjectNameOfMethod; virtual;
procedure ObjectProperty; virtual;
procedure ObjectPropertySpecifiers; virtual;
procedure ObjectProcedureHeading; virtual;
procedure ObjectType; virtual;
procedure ObjectTypeEnd; virtual; // DR 2001-08-07
procedure ObjectVisibility; virtual;
procedure OldFormalParameterType; virtual;
procedure OrdinalIdentifier; virtual;
procedure OrdinalType; virtual;
procedure OutParameter; virtual;
procedure PackageFile; virtual;
procedure ParameterFormal; virtual;
procedure ParameterName; virtual;
procedure ParameterNameList; virtual;
procedure ParseFile; virtual;
procedure PointerType; virtual;
procedure ProceduralDirective; virtual;
procedure ProceduralType; virtual;
procedure ProcedureDeclarationSection; virtual;
procedure ProcedureHeading; virtual;
procedure ProcedureMethodDeclaration; virtual;
procedure ProcedureMethodName; virtual;
procedure ProgramBlock; virtual;
procedure ProgramFile; virtual;
procedure PropertyDefault; virtual;
procedure PropertyInterface; virtual;
procedure PropertyName; virtual;
procedure PropertyParameterConst; virtual;
procedure PropertyParameterList; virtual;
procedure PropertySpecifiers; virtual;
procedure QualifiedIdentifier; virtual;
procedure QualifiedIdentifierList; virtual;
procedure RaiseStatement; virtual;
procedure ReadAccessIdentifier; virtual;
procedure RealIdentifier; virtual;
procedure RealType; virtual;
procedure RecordConstant; virtual;
procedure RecordFieldConstant; virtual;
procedure RecordType; virtual;
procedure RecordVariant; virtual;
procedure RelativeOperator; virtual;
procedure RepeatStatement; virtual;
procedure RequiresClause; virtual;
procedure RequiresIdentifier; virtual;
procedure ResolutionInterfaceName; virtual;
procedure ResourceDeclaration; virtual;
procedure ReturnType; virtual;
procedure SetConstructor; virtual;
procedure SetElement; virtual;
procedure SetType; virtual;
procedure SimpleExpression; virtual;
procedure SimpleStatement; virtual;
procedure SimpleType; virtual;
procedure SkipAnsiComment; virtual;
procedure SkipBorComment; virtual;
procedure SkipSlashesComment; virtual;
procedure SkipSpace; virtual; //XM Jul-2000
procedure SkipCRLFco; virtual; //XM Jul-2000
procedure SkipCRLF; virtual; //XM Jul-2000
procedure Statement; virtual;
procedure StatementList; virtual;
procedure StorageExpression; virtual;
procedure StorageIdentifier; virtual;
procedure StorageDefault; virtual;
procedure StorageNoDefault; virtual;
procedure StorageSpecifier; virtual;
procedure StorageStored; virtual;
procedure StringIdentifier; virtual;
procedure StringStatement; virtual;
procedure StringType; virtual;
procedure StructuredType; virtual;
procedure SubrangeType; virtual;
procedure TagField; virtual;
procedure TagFieldName; virtual;
procedure TagFieldTypeName; virtual;
procedure Term; virtual;
procedure TryStatement; virtual;
procedure TypedConstant; virtual;
procedure TypeDeclaration; virtual;
procedure TypeId; virtual;
procedure TypeKind; virtual;
procedure TypeName; virtual;
//generics
procedure TypeArgs; virtual;
procedure TypeParams; virtual;
procedure TypeParamDecl; virtual;
procedure TypeParamDeclList; virtual;
procedure TypeParamList; virtual;
procedure ConstraintList; virtual;
procedure Constraint; virtual;
//end generics
procedure TypeSection; virtual;
procedure UnitFile; virtual;
procedure UnitId; virtual;
procedure UnitName; virtual;
procedure UsedUnitName; virtual;
procedure UsedUnitsList; virtual;
procedure UsesClause; virtual;
procedure VarAbsolute; virtual;
procedure VarEqual; virtual;
procedure VarDeclaration; virtual;
procedure Variable; virtual;
procedure VariableList; virtual;
procedure VariableReference; virtual;
procedure VariableTwo; virtual;
procedure VariantIdentifier; virtual;
procedure VariantSection; virtual;
procedure VarParameter; virtual;
procedure VarName; virtual; //!! Added VarName and VarNameList back in...
procedure VarNameList; virtual;
procedure VarSection; virtual;
procedure VisibilityAutomated; virtual;
procedure VisibilityPrivate; virtual;
procedure VisibilityProtected; virtual;
procedure VisibilityPublic; virtual;
procedure VisibilityPublished; virtual;
procedure VisibilityUnknown; virtual;
procedure WhileStatement; virtual;
procedure WithStatement; virtual;
procedure WriteAccessIdentifier; virtual;
{$IFDEF D8_NEWER}//JThurman 2004-03-21
{This is the syntax for custom attributes, based quite strictly on the
ECMA syntax specifications for C#, but with a Delphi expression being
used at the bottom as opposed to a C# expression}
procedure GlobalAttributes;
procedure GlobalAttributeSections;
procedure GlobalAttributeSection;
procedure GlobalAttributeTargetSpecifier;
procedure GlobalAttributeTarget;
procedure Attributes;
procedure AttributeSections;
procedure AttributeSection;
procedure AttributeTargetSpecifier;
procedure AttributeTarget;
procedure AttributeList;
procedure Attribute;
procedure AttributeName;
procedure AttributeArguments;
procedure PositionalArgumentList;
procedure PositionalArgument;
procedure NamedArgumentList;
procedure NamedArgument;
procedure AttributeArgumentExpression;
{$ENDIF}
property ExID: TptTokenKind read GetExID;
property GenID: TptTokenKind read GetGenID;
property TokenID: TptTokenKind read GetTokenID;
public
constructor Create;
destructor Destroy; override;
procedure SynError(Error: TmwParseError); virtual;
procedure Run(UnitName: string; SourceStream: TCustomMemoryStream); virtual;
procedure InitDefines;
procedure AddDefine(const ADefine: string);
procedure RemoveDefine(const ADefine: string);
function IsDefined(const ADefine: string): Boolean;
property InterfaceOnly: Boolean read fInterfaceOnly write fInterfaceOnly;
property Lexer: TmwPasLex read fLexer;
property OnMessage: TMessageEvent read FOnMessage write FOnMessage;
property LastNoJunkPos: Integer read fLastNoJunkPos;
property LastNoJunkLen: Integer read fLastNoJunkLen;
property UseDefines: Boolean read FUseDefines write FUseDefines;
published
end;
implementation
uses Windows;
{ ESyntaxError }
constructor ESyntaxError.Create(const Msg: string);
begin
// !! changed initialization for TTokenPoint
FPosXY.X:= -1;
FPosXY.Y:= -1;
inherited Create(Msg);
end;
constructor ESyntaxError.CreateFmt(const Msg: string; const Args: array of const);
begin
// !! changed initialization for TTokenPoint
FPosXY.X:= -1;
FPosXY.Y:= -1;
inherited CreateFmt(Msg, Args);
end;
constructor ESyntaxError.CreatePos(const Msg: string; aPosXY: TTokenPoint);
begin
Message := Msg;
FPosXY := aPosXY;
end;
{ TmwSimplePasPar }
(* DR 2002-01-16
const
cnExpected = 'Expected ''%s'' found ''%s''';
// cnOrExpected = 'Expected ''%s'' or ''%s'' found ''%s''';
cnEndOfFile = 'end of file'; {jdj 7/22/1999}
// cnIntegerOverflow = 'Integer constant too large'; {jdj 7/22/1999}
*)
{range checks a ptIntegerConst-slightly faster than StrToInt}
{function IsValidInteger(const S: string): Boolean; jdj 7/22/1999
var jdj removed 02/07/2001
C: Integer;
N: Integer;
begin
Val(S, N, C);
Result := (C = 0);
end;}
procedure TmwSimplePasPar.ForwardDeclaration;
begin {jdj added method 02/07/2001}
NextToken;
SEMICOLON;
end;
procedure TmwSimplePasPar.ObjectProperty;
begin {jdj added method 02/07/2001}
// DR 2001-08-07 -> changed. for array-property override failure
Expected(ptProperty);
PropertyName;
case TokenID of
ptColon, ptSquareOpen:
begin
PropertyInterface;
end;
end;
ObjectPropertySpecifiers;
case ExID of
ptDefault:
begin
PropertyDefault; //DR 2001-07-16
SEMICOLON;
end;
end;
end;
procedure TmwSimplePasPar.ObjectPropertySpecifiers;
begin {jdj added method 02/07/2001}
if ExID = ptIndex then
begin
IndexSpecifier; // DR 2001-08-07
end;
while ExID in [ptRead, ptReadOnly, ptWrite, ptWriteOnly] do
begin
AccessSpecifier;
end;
while ExID in [ptDefault, ptNoDefault, ptStored] do
begin
StorageSpecifier;
end;
SEMICOLON;
end;
procedure TmwSimplePasPar.Run(UnitName: string; SourceStream: TCustomMemoryStream);
begin
fStream := nil;
fOwnStream := False;
{if SourceStream = nil then
begin
fStream := TMemoryStream.Create;
fOwnStream := True;
fStream.LoadFromFile(UnitName);
end
else}
fStream := SourceStream;
TerminateStream;
fLexer.Origin := fStream.Memory;
ParseFile;
if fOwnStream then
fStream.Free;
end;
constructor TmwSimplePasPar.Create;
begin
inherited Create;
fLexer := TmwPasLex.Create;
fLexer.OnCompDirect := HandlePtCompDirect;
fLexer.OnDefineDirect := HandlePtDefineDirect;
fLexer.OnElseDirect := HandlePtElseDirect;
fLexer.OnEndIfDirect := HandlePtEndIfDirect;
fLexer.OnIfDefDirect := HandlePtIfDefDirect;
fLexer.OnIfNDefDirect := HandlePtIfNDefDirect;
fLexer.OnIfOptDirect := HandlePtIfOptDirect;
fLexer.OnIncludeDirect := HandlePtIncludeDirect;
fLexer.OnResourceDirect := HandlePtResourceDirect;
fLexer.OnUnDefDirect := HandlePtUndefDirect;
fLexer.OnIfDirect := HandlePtIfDirect;
fLexer.OnIfEndDirect := HandlePtIfEndDirect;
fLexer.OnElseIfDirect := HandlePtElseIfDirect;
FDefines := TStringList.Create;
with TStringList(FDefines) do
begin
Sorted := True;
Duplicates := dupIgnore;
end;
InitDefines;
FDefineStack := 0;
FUseDefines := False;
end;
destructor TmwSimplePasPar.Destroy;
begin
ClearDefines; //Must do this here to avoid a memory leak
FDefines.Free;
AheadParse.Free;
fLexer.Free;
inherited Destroy;
end;
{next two check for ptNull and ExpectedFatal for an EOF Error}
procedure TmwSimplePasPar.Expected(Sym: TptTokenKind);
begin
if Sym <> Lexer.TokenID then
begin
if TokenID = ptNull then
ExpectedFatal(Sym) {jdj 7/22/1999}
else
begin
if Assigned(FOnMessage) then
FOnMessage(Self, meError, Format(rsExpected, [TokenName(Sym), fLexer.Token]),
fLexer.PosXY.X, fLexer.PosXY.Y);
end;
end
else
NextToken;
end;
procedure TmwSimplePasPar.ExpectedEx(Sym: TptTokenKind);
begin
if Sym <> Lexer.ExID then
begin
if Lexer.TokenID = ptNull then
ExpectedFatal(Sym) {jdj 7/22/1999}
else if Assigned(FOnMessage) then
FOnMessage(Self, meError, Format(rsExpected, ['EX:' + TokenName(Sym), fLexer.Token]),
fLexer.PosXY.X, fLexer.PosXY.Y);
end
else
NextToken;
end;
{Replace Token with cnEndOfFile if TokenId = ptnull}
procedure TmwSimplePasPar.ExpectedFatal(Sym: TptTokenKind);
var
tS: string;
begin
if Sym <> Lexer.TokenID then
begin
{--jdj 7/22/1999--}
if Lexer.TokenId = ptNull then
tS := rsEndOfFile
else
tS := fLexer.Token;
{--jdj 7/22/1999--}
raise ESyntaxError.CreatePos(Format(rsExpected, [TokenName(Sym), tS]), fLexer.PosXY);
end
else
NextToken;
end;
procedure TmwSimplePasPar.HandlePtCompDirect(Sender: TmwBasePasLex);
begin
if Assigned(FOnMessage) then
FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// Sender.NextNoJunk;
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtDefineDirect(Sender: TmwBasePasLex);
begin
// if Assigned(FOnMessage) then
// FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// if FUseDefines then
// AddDefine(Lexer.DirectiveParam);
// else
// Sender.NextNoJunk;
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtElseDirect(Sender: TmwBasePasLex);
begin
// if Assigned(FOnMessage) then
// FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// if FUseDefines then
// begin
// if FTopDefineRec <> nil then
// begin
// if FTopDefineRec^.Defined then
// Inc(FDefineStack)
// else
// if FDefineStack > 0 then
// Dec(FDefineStack);
// end;
// end;
// Sender.NextNoJunk;
if Sender = Lexer then
NextToken
else
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtElseIfDirect(Sender: TmwBasePasLex);
var
Param: string;
Def: string;
begin
// if FUseDefines then
// begin
// if FTopDefineRec <> nil then
// begin
// if FTopDefineRec^.Defined then
// Inc(FDefineStack)
// else
// begin
// if FDefineStack > 0 then
// Dec(FDefineStack);
// Param := Sender.DirectiveParam;
// if Pos('DEFINED', Param) = 1 then
// begin
// Def := Copy(Param, 9, Length(Param) - 9);
// EnterDefineBlock(IsDefined(Def));
// end;
// end;
// end;
// end;
if Sender = Lexer then
NextToken
else
Sender.Next;
end;
procedure TmwSimplePasPar.HandlePtEndIfDirect(Sender: TmwBasePasLex);
begin
// if Assigned(FOnMessage) then
// FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// if FUseDefines then
// begin
// ExitDefineBlock;
// end;
// Sender.NextNoJunk;
if Sender = Lexer then
NextToken
else
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtIfDefDirect(Sender: TmwBasePasLex);
begin
// if Assigned(FOnMessage) then
// FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// if FUseDefines then
// begin
// EnterDefineBlock(IsDefined(Sender.DirectiveParam));
// end;
// Sender.NextNoJunk;
if Sender = Lexer then
NextToken
else
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtIfDirect(Sender: TmwBasePasLex);
var
Def: string;
Param: string;
begin
// Param := Sender.DirectiveParam;
// if FUseDefines then
// begin
// if Pos('DEFINED', Param) = 1 then
// begin
// Def := Copy(Param, 9, Length(Param) - 9);
// EnterDefineBlock(IsDefined(Def));
// end;
// end;
if Sender = Lexer then
NextToken
else
Sender.Next;
end;
procedure TmwSimplePasPar.HandlePtIfEndDirect(Sender: TmwBasePasLex);
begin
// if FUseDefines then
// ExitDefineBlock;
if Sender = Lexer then
NextToken
else
Sender.Next;
end;
procedure TmwSimplePasPar.HandlePtIfNDefDirect(Sender: TmwBasePasLex);
begin
// if Assigned(FOnMessage) then
// FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// if FUseDefines then
// begin
// EnterDefineBlock(not IsDefined(Sender.DirectiveParam));
// end;
// Sender.NextNoJunk;
if Sender = Lexer then
NextToken
else
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtIfOptDirect(Sender: TmwBasePasLex);
begin
if Assigned(FOnMessage) then
FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// Sender.NextNoJunk;
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtIncludeDirect(Sender: TmwBasePasLex);
begin
if Assigned(FOnMessage) then
FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// Sender.NextNoJunk;
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtResourceDirect(Sender: TmwBasePasLex);
begin
if Assigned(FOnMessage) then
FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// Sender.NextNoJunk;
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.HandlePtUndefDirect(Sender: TmwBasePasLex);
begin
// if Assigned(FOnMessage) then
// FOnMessage(Self, meNotSupported, 'Currently not supported ' + fLexer.Token, fLexer.PosXY.X, fLexer.PosXY.Y);
// if FUseDefines then
// RemoveDefine(Lexer.DirectiveParam);
// Sender.NextNoJunk;
Sender.Next; //XM Jul-2000
{ ToDo }
end;
procedure TmwSimplePasPar.NextToken;
begin
// if FUseDefines then
// begin
// repeat
// FLexer.Next;
// until (FDefineStack = 0) or (TokenID = ptNull);
// SkipJunk;
// end else
// begin
FLexer.NextNoJunk;
//fLexer.Next;
//SkipJunk;
// end;
end;
procedure TmwSimplePasPar.SkipJunk;
begin
if Lexer.IsJunk then
begin
case TokenID of
ptAnsiComment:
begin
SkipAnsiComment;
end;
ptBorComment:
begin
SkipBorComment;
end;
ptSlashesComment:
begin
SkipSlashesComment;
end;
ptSpace:
begin
SkipSpace; //XM Jul-2000
end;