-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAIN.PAS
316 lines (287 loc) · 7.61 KB
/
MAIN.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
Unit Main;
{$I define.inc}
Interface
Uses
Lexer, TypeDef;
Type
{$IFDEF CLASSIC}
TSize = TypeDef.TSize;
PSize = TypeDef.PSize;
{$ENDIF}
TRegisterTermRule = Procedure(Name: PChar);
TGetMode = Function: PChar;
TGetTokenKind = Function: PChar;
TGetTokenValue = Function: PChar;
TInsertToken = Procedure(Kind: PChar; Value: PChar);
TPopMode = Procedure;
PContext = ^TContext;
TContext = Record
RegisterTermRule: TRegisterTermRule;
GetMode: TGetMode;
GetTokenKind: TGetTokenKind;
GetTokenValue: TGetTokenValue;
InsertToken: TInsertToken;
PopMode: TPopMode;
End;
TInit = Procedure(Context: PContext);
TProcessToken = Procedure(Context: PContext);
Procedure RegisterTermRule(Name: PChar);
Function GetMode: PChar;
Function GetTokenKind: PChar;
Function GetTokenValue: PChar;
Procedure InsertToken(Kind: PChar; Value: PChar);
Procedure PopMode;
Function Parse(GrammarFile: String; SourceCodeFile: String; AstPathPrefix: String;
ErrorToken: PToken; ErrorMessages: PString; ErrorMessagesCount: PSize;
PlugInPath: String): String;
Procedure Run;
Implementation
Uses {$IFNDEF FPC}
{$IFDEF CLASSIC}
TypInfo
{$ELSE}
System.Rtti
{$ENDIF}
, {$ENDIF}
{$IFDEF FPC}
dynlibs,
{$ELSE}
{$IFDEF VINTAGE}
WINPROCS,
WINTYPES,
{$ELSE}
Windows,
{$ENDIF}
{$ENDIF}
Stack,
SysUtils,
TestUtil,
CPARSER,
Parser, GrmRUnit, ASTNode, ParseTr,
{$IFDEF USE_STRINGS}strings,{$ENDIF} STREAM, GLEXER, GPARSER, CLEXER, GRMRNODE, StrUtil;
Var
{$IFDEF FPC}
mHandle: TLibHandle;
{$ELSE}
mHandle: THandle;
{$ENDIF}
mContext: TContext;
mLexer: PCodeLexer;
Procedure TCodeLexer_Init(Var Self: PCodeLexer);
{$IFDEF VINTAGE} Far; {$ENDIF}
Var
mInit: TInit;
{$IFDEF VINTAGE}
p: TFarProc;
{$ENDIF}
Begin
{$IFDEF VINTAGE}
p := GetProcAddress(mHandle, 'Init');
mInit := TInit(p);
{$ELSE}
mInit := TInit(GetProcAddress(mHandle, 'Init'));
{$ENDIF}
mInit(@mContext);
End;
Procedure TCodeLexer_ProcessToken(Var Self: PCodeLexer);
{$IFDEF VINTAGE} Far; {$ENDIF}
Var
mProcessToken: TProcessToken;
{$IFDEF VINTAGE}
p: TFarProc;
{$ENDIF}
Begin
{$IFDEF VINTAGE}
p := GetProcAddress(mHandle, 'ProcessToken');
mProcessToken := TProcessToken(p);
{$ELSE}
mProcessToken := TProcessToken(GetProcAddress(mHandle, 'ProcessToken'));
{$ENDIF}
mProcessToken(@mContext);
End;
Procedure RegisterTermRule(Name: PChar);
Begin
TGrammarNode_RegisterTermRule(mLexer^.GrammarNode, Name);
End;
Function GetMode: PChar;
Begin
Result := PPChar(TStack_Top(mLexer^.Mode))^;
End;
Function GetTokenKind: PChar;
Begin
Result := TGrammarNode_GetTermRuleName(mLexer^.GrammarNode,
PTermRule(mLexer^.Parent.CurrentToken.Kind)^);
End;
Function GetTokenValue: PChar;
Begin
Result := mLexer^.Parent.CurrentToken.Value;
End;
Procedure InsertToken(Kind: PChar; Value: PChar);
Begin
PTermRule(mLexer^.Parent.CurrentToken.Kind)^ :=
TGrammarNode_GetTermRuleId(mLexer^.GrammarNode, Kind);
TLexer_Retract(PLexer(mLexer), StrLen(mLexer^.Parent.CurrentToken.Value) -
StrLen(Value));
FreeStr(mLexer^.Parent.CurrentToken.Value);
mLexer^.Parent.CurrentToken.Value := StrNew(Value);
End;
Procedure PopMode;
Begin
TCodeLexer_PopMode(PLexer(mLexer));
End;
Function Propmt(Prompt: PChar): String;
Begin
WriteLn(Prompt);
ReadLn(Result);
End;
Function Parse(GrammarFile: String; SourceCodeFile: String; AstPathPrefix: String;
ErrorToken: PToken; ErrorMessages: PString; ErrorMessagesCount: PSize;
PlugInPath: String): String;
Var
mGrammar: PStream;
mCode: PStream;
{$IFDEF VINTAGE}
p: PChar;
{$ENDIF}
mGrammarLexer: PLexer;
{$IFDEF VER150}
tInfo: PTypeInfo;
{$ENDIF}
mParser: PGrammarParser;
mViewer: PAstViewer;
mErrorNode: PNode;
e: PString;
Begin
TFileStream_Create(PFileStream(mGrammar), GrammarFile);
TFileStream_Create(PFileStream(mCode), SourceCodeFile);
mGrammarLexer := PLexer(GetGrammarLexer(mGrammar));
TGrammarParser_Create(mParser, mGrammarLexer, GrammarRule);
If Not TGrammarParser_Parse(mParser) Then
Begin
Result := Format('%s', [mParser^.Parent.Error]);
ErrorToken^.StartPos := mGrammarLexer^.CurrentToken.StartPos;
ErrorToken^.Value := strnew(mGrammarLexer^.CurrentToken.Value);
ErrorToken^.Error := strnew(mGrammarLexer^.CurrentToken.Error);
End;
TCodeLexer_Create(mLexer, mCode);
If PlugInPath <> '' Then
Begin
{$IFDEF FPC}
mHandle := LoadLibrary(PlugInPath);
{$ELSE}
{$IFDEF VINTAGE}
GetMem(p, Length(PlugInPath) + 1);
StrPCopy(p, PlugInPath);
mHandle := LoadLibrary(p);
FreeMem(p, Length(PlugInPath) + 1);
{$ELSE}
mHandle := LoadLibrary(PChar(PlugInPath));
{$ENDIF}
{$ENDIF}
mContext.RegisterTermRule := RegisterTermRule;
mContext.GetMode := GetMode;
mContext.GetTokenKind := GetTokenKind;
mContext.GetTokenValue := GetTokenValue;
mContext.InsertToken := InsertToken;
mContext.PopMode := PopMode;
mLexer^.OnInit := TCodeLexer_Init;
mLexer^.OnProcessToken := TCodeLexer_ProcessToken;
End;
TAstViewer_Create(mViewer, PLexer(mLexer));
mParser^.Ast^.VMT^.Accept(mParser^.Ast, mViewer^.As_IAstVisitor);
mViewer^.Level := 0;
ErrorMessagesCount^ := 0;
If mViewer^.Error Then
Begin
mErrorNode := mViewer^.ErrorMessages^.Top;
If mErrorNode <> nil Then
Begin
While mErrorNode^.Next <> nil Do
Begin
Inc(ErrorMessagesCount);
mErrorNode := mErrorNode^.Next;
End;
End;
GetMem(ErrorMessages, ErrorMessagesCount^ * SizeOf(PString));
e := ErrorMessages;
mErrorNode := mViewer^.ErrorMessages^.Top;
If mErrorNode <> nil Then
Begin
While mErrorNode^.Next <> nil Do
Begin
e^ := Format('%s', [PChar(mErrorNode^.Data^)]);
Inc(e);
mErrorNode := mErrorNode^.Next;
End;
End;
End;
TAstViewer_OutputParseTree(mViewer, AstPathPrefix);
If PlugInPath <> '' Then
Begin
FreeLibrary(mHandle);
End;
TParseTree_Destroy(mViewer^.FParseTree);
TAstViewer_Destroy(PParser(mViewer));
Dispose(mViewer);
TCodeLexer_Destroy(PLexer(mLexer));
Dispose(mLexer);
TGrammarParser_Destroy(PParser(mParser));
Dispose(mParser);
TGrammarLexer_Destroy(mGrammarLexer);
Dispose(mGrammarLexer);
Dispose(mGrammar);
Dispose(mCode);
End;
Procedure Run;
Var
mGrammarFilePath: String;
mSourceCodeFilePath: String;
mAstPathPrefix: String;
mPlugInPath: String;
mResult: String;
mErrorToken: TToken;
mErrorMessagesCount: TSize;
mErrorMessages: PString;
p: PString;
Begin
mPlugInPath := '';
If (ParamCount = 3) Or (ParamCount = 4) Then
Begin
mGrammarFilePath := ParamStr(1);
mSourceCodeFilePath := ParamStr(2);
mAstPathPrefix := ParamStr(3);
If ParamCount = 4 Then
Begin
mPlugInPath := ParamStr(4);
End;
End
Else
Begin
mGrammarFilePath := Propmt('Grammar File Path?');
mSourceCodeFilePath := Propmt('Source Code File Path?');
mAstPathPrefix := Propmt('AST Output Path Prefix?');
mPlugInPath := Propmt('PlugIn Library File Path?');
End;
mResult := Parse(mGrammarFilePath, mSourceCodeFilePath, mAstPathPrefix,
@mErrorToken, mErrorMessages, @mErrorMessagesCount, mPlugInPath);
If mResult = '' Then
WriteLn('ACCEPTED')
Else
Begin
WriteLn(Format('ERROR: Parser Message: %s', [mResult]));
WriteLn(Format('ERROR: Current Token at Pos = %d, Value = [%s], Message: %s',
[mErrorToken.StartPos, mErrorToken.Value, mErrorToken.Error]));
End;
If mErrorMessagesCount > 0 Then
Begin
p := mErrorMessages;
While mErrorMessagesCount > 0 Do
Begin
WriteLn(p^);
Inc(p);
Dec(mErrorMessagesCount);
End;
FreeMem(mErrorMessages, mErrorMessagesCount * SizeOf(PString));
End;
End;
End.