/
ConsoleParser.dpr
216 lines (179 loc) · 6.5 KB
/
ConsoleParser.dpr
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
program ConsoleParser;
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
SysUtils,
Scanner in 'Parser\Scanner.pas',
Tokens in 'Parser\Tokens.pas',
ScannerKeywordsJumpTable in 'Parser\ScannerKeywordsJumpTable.pas',
Statements in 'Parser\Statements.pas',
Documents in 'Parser\Documents.pas',
Expressions in 'Parser\Expressions.pas',
TypesUtils in 'Parser\TypesUtils.pas',
ScannerHandlers in 'Parser\ScannerHandlers.pas',
Scopes in 'Parser\Scopes.pas',
Blocks in 'Parser\Blocks.pas';
var i, ParamStart, Errors, TotalErrors, TotalLines: Cardinal;
FileDocument: PDocument;
FileStream: TFileStream;
FileMemory: PAnsiChar;
DoImplictRealCatch: Boolean;
EmulatePJASS: Boolean;
procedure SetResult(Result: TErrorClass);
begin
if EmulatePJASS then
begin
case Result of
ecError, ecException:
ExitCode := 1;
end;
end
else
begin
if Integer(Result) > ExitCode then
ExitCode := Integer(Result);
end;
end;
procedure ReportErrors(Doc: PDocumentInfo; Name: String; var Number: Cardinal);
var
ErrorInfo: PErrorInfo;
begin
ErrorInfo := Doc.Errors;
while ErrorInfo <> nil do
begin
SetResult(ErrorInfo.ErrorClass);
if ErrorInfo.ErrorType = eiChildErrors then
ReportErrors(ErrorInfo.Child, Name + Doc.Name + ':', Number)
else
begin
if EmulatePJASS then
WriteLn(Doc.Name, ':' + IntToStr(ErrorInfo.Line + 1) + ': ' + ErrorInfo.ToString)
else
WriteLn('[' + ErrorClassNames[ErrorInfo.ErrorClass] + '] ' + Name + Doc.Name, '(' + IntToStr(ErrorInfo.Line + 1) + ': ' + IntToStr(Cardinal(ErrorInfo.Start) - Cardinal(ErrorInfo.LineStart) + 1) + '-' + IntToStr(Cardinal(ErrorInfo.Start) - Cardinal(ErrorInfo.LineStart) + 1 + ErrorInfo.Length) + '): ' + ErrorInfo.ToString);
Inc(Number);
end;
ErrorInfo := ErrorInfo.Next;
end;
end;
function IsCommandLine: Boolean;
var Cmd: String;
begin
Cmd := ParamStr(ParamStart);
Result := (Cmd = '--implicit-reals') or (Cmd = '-ir')
or (Cmd = '--report-leaks')
or (Cmd = '--return-bug') or (Cmd = '-rb')
or (Cmd = '--pjass')
or (Cmd = '--help');
end;
procedure ParseCommandLine;
var Cmd: String;
begin
Cmd := ParamStr(ParamStart);
if (Cmd = '--implicit-reals') or (Cmd = '-ir') then
DoImplictRealCatch := True
else if Cmd = '--report-leaks' then
ReportMemoryLeaksOnShutdown := True
else if Cmd = '--pjass' then
EmulatePJASS := True
else if (Cmd = '--return-bug') or (Cmd = '-rb') then
DoReturnBug := True
else if Cmd = '--help' then
begin
Writeln('JassParserCLI 0.1.12');
Writeln('-------------');
Writeln('JassParserCLI <options> <documents>');
Writeln('');
Writeln('--implicit-reals and -ir: Reports implicit conversion from integer constants to reals');
Writeln('--pjass: Emulate PJASS CLI');
Writeln('--report-leaks: Reports memory leaks at shutdown');
Writeln('--return-bug and -rb: This will emulate the return bug');
Writeln('--help: Shows this info');
Abort;
end;
Inc(ParamStart);
end;
begin
try
Init;
ExitCode := 0;
TotalErrors := 0;
TotalLines := 0;
DoImplictRealCatch := False;
DoReturnBug := False;
EmulatePJASS := False;
ParamStart := 1;
while IsCommandLine do
ParseCommandLine;
try
for i := ParamStart to ParamCount do
begin
Errors := 0;
ImplictRealCatch := DoImplictRealCatch and (LowerCase(ExtractFileName(ParamStr(i))) <> 'blizzard.j');
try
FileStream := TFileStream.Create(ParamStr(i), fmOpenRead);
FileMemory := nil;
try
GetMem(FileMemory, FileStream.Size + 1);
PAnsiChar(Cardinal(FileMemory) + FileStream.Size)^ := #0;
FileStream.Read(FileMemory^, FileStream.Size);
New(FileDocument);
FileDocument.Init;
FileDocument.Info := Parse(FileMemory);
FileDocument.Info.Name := ParamStr(i);
ParseDocument(FileDocument^);
ReportErrors(FileDocument.Info, '', Errors);
finally
FreeAndNil(FileStream);
FreeMem(FileMemory);
end;
DocumentList.Add(FileDocument);
except
on E:Exception do
begin
Errors := Errors + 1;
if EmulatePJASS then
WriteLn(ParamStr(i), ':' + IntToStr(Token.Line + 1) + ': ' + E.Classname, ': ', E.Message)
else
WriteLn('[Exception] ' + ParamStr(i), '(' + IntToStr(Token.Line + 1) + ': ' + IntToStr(Cardinal(Token.Start) - Cardinal(Token.LineStart) + 1) + '-' + IntToStr(Cardinal(Token.Stop) - Cardinal(Token.LineStart) + 1) + '): ' + E.Classname, ': ', E.Message);
SetResult(ecException);
end;
end;
TotalErrors := TotalErrors + Errors;
TotalLines := TotalLines + Token.Line;
if EmulatePJASS then
begin
if Errors = 0 then
WriteLn(Format('Parse successful: %8u lines: %s', [Token.Line, ParamStr(i)]))
else if Errors = 1 then
WriteLn(ParamStr(i), ' failed with ' + IntToStr(Errors) + ' error')
else if Errors > 1 then
WriteLn(ParamStr(i), ' failed with ' + IntToStr(Errors) + ' errors');
end;
end;
if EmulatePJASS then
begin
if TotalErrors = 0 then
WriteLn(Format('Parse successful: %8u lines: %s', [TotalLines, '<total>']))
else if TotalErrors = 1 then
WriteLn('Parse failed: ' + IntToStr(TotalErrors) + ' error total')
else if TotalErrors > 1 then
WriteLn('Parse failed: ' + IntToStr(TotalErrors) + ' errors total');
end;
for i := 1 to DocumentList.Count - 1 do
with PDocument(DocumentList[i])^ do
begin
Info.Free;
Free;
Dispose(DocumentList[i]);
end;
except
on E: Exception do
begin
MessageBox(0, PChar(E.Classname + ': ' + E.Message), 'JassParser', 0);
SetResult(ecException);
end;
end;
except
end;
end.