-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPasLS.Parser.pas
More file actions
325 lines (285 loc) · 8.91 KB
/
PasLS.Parser.pas
File metadata and controls
325 lines (285 loc) · 8.91 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
// Pascal Language Server
// Copyright 2023 Michael Van Canneyt
// This file is part of Pascal Language Server.
// Pascal Language Server is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Pascal Language Server is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Pascal Language Server. If not, see
// <https://www.gnu.org/licenses/>.
unit PasLS.Parser;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, PParser, PScanner, PasTree, CodeCache, Types;
// Detect presence of OnError etc.
{$IF Declared(TPasParserErrorHandler)}
{$DEFINE MULTIERROR}
{$ENDIF}
Type
{ TCodeToolsFileResolver }
TCodeToolsFileResolver = Class(TFileResolver)
Private
FBuffer: TCodeBuffer;
Protected
function CreateLineReaderFromBuffer(aBuffer: TCodeBuffer): TLineReader;
Public
Constructor Create(aBuffer : TCodeBuffer); reintroduce;
function FindSourceFile(const AName: string): TLineReader; override;
Property Buffer : TCodeBuffer read FBuffer;
end;
{ TSimpleEngine }
TSimpleEngine = class(TPasTreeContainer)
public
function CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
override;
function FindElement(const AName: String): TPasElement; override;
end;
{ TSourceParser }
TReportParserErrorEvent = Procedure (Sender : TObject; Const aError, aFileName : string; aCode, aLine, aCol : Integer) Of Object;
TSourceParser = Class(TObject)
private
FOnError: TReportParserErrorEvent;
FParser: TPasParser;
FCode: TCodeBuffer;
FCommandLine: TStringDynArray;
FCPUTarget: String;
FOptions: TParseSourceOptions;
FOSTarget: String;
{$IFDEF MULTIERROR}
procedure DoError(Sender: TObject; const aContext: TRecoveryContext;
var aAllowRecovery: Boolean);
{$ENDIF}
Public
Function ParseSource: TPasModule;
Property CommandLine : TStringDynArray Read FCommandLine Write FCommandLine;
Property Code : TCodeBuffer Read FCode Write FCode;
Property OSTarget : String Read FOSTarget Write FOSTarget;
Property CPUTarget : String Read FCPUTarget Write FCPUTarget;
Property Options : TParseSourceOptions Read FOptions Write FOptions;
Property OnError : TReportParserErrorEvent Read FOnError Write FOnError;
end;
implementation
uses CodeToolManager;
{ Checks syntax for code buffer and publishes errors as diagnostics }
{ TSimpleEngine }
function TSimpleEngine.CreateElement(AClass: TPTreeElement; const AName: String;
AParent: TPasElement; AVisibility: TPasMemberVisibility;
const ASourceFilename: String; ASourceLinenumber: Integer): TPasElement;
begin
Result := AClass.Create(AName, AParent);
Result.Visibility := AVisibility;
Result.SourceFilename := ASourceFilename;
Result.SourceLinenumber := ASourceLinenumber;
end;
function TSimpleEngine.FindElement(const AName: String): TPasElement;
begin
// We could try to use codetools for this.
Result := nil;
end;
{ TSourceParser }
{$IFDEF MULTIERROR}
procedure TSourceParser.DoError(Sender: TObject;
const aContext: TRecoveryContext; var aAllowRecovery: Boolean);
var
aCode : Integer;
begin
aAllowRecovery:=True;
If Assigned(FOnError) then
begin
if aContext.Error is EParserError then
aCode:=EParserError(aContext.Error).ErrNo
else
aCode:=-1;
With FParser.CurSourcePos do
FOnError(Self,aContext.Error.Message,FileName,aCode, Row,Column);
end;
end;
{$ENDIF}
function TSourceParser.ParseSource: TPasModule;
var
FileResolver: TBaseFileResolver;
Filename: String;
Scanner: TPascalScanner;
procedure ProcessCmdLinePart(S : String);
var
l,Len: Integer;
begin
if (S='') then
exit;
Len:=Length(S);
if (s[1] = '-') and (len>1) then
begin
case s[2] of
'd': // -d define
Scanner.AddDefine(UpperCase(Copy(s, 3, Len)));
'u': // -u undefine
Scanner.RemoveDefine(UpperCase(Copy(s, 3, Len)));
'F': // -F
if (len>2) and (s[3] = 'i') then // -Fi include path
FileResolver.AddIncludePath(Copy(s, 4, Len));
'I': // -I include path
FileResolver.AddIncludePath(Copy(s, 3, Len));
'S': // -S mode
if (len>2) then
begin
l:=3;
While L<=Len do
begin
case S[l] of
'c' : Scanner.Options:=Scanner.Options+[po_cassignments];
'd' : Scanner.SetCompilerMode('DELPHI');
'2' : Scanner.SetCompilerMode('OBJFPC');
'h' : ; // do nothing
end;
inc(l);
end;
end;
'M' :
begin
delete(S,1,2);
Scanner.SetCompilerMode(S);
end;
end;
end else
if Filename <> '' then
raise ENotSupportedException.Create(SErrMultipleSourceFiles)
else
Filename := s;
end;
var
S: String;
Engine : TSimpleEngine;
aCode : integer;
begin
if DefaultFileResolverClass=Nil then
raise ENotImplemented.Create(SErrFileSystemNotSupported);
Result := nil;
FileResolver := nil;
Scanner := nil;
FParser := nil;
Engine:=Nil;
try
Engine:=TSimpleEngine.Create;
FileResolver := TCodeToolsFileResolver.Create(Code);
{$ifdef HasStreams}
if FileResolver is TFileResolver then
TFileResolver(FileResolver).UseStreams:=poUseStreams in Options;
{$endif}
Scanner := TPascalScanner.Create(FileResolver);
Scanner.LogEvents:=Engine.ScannerLogEvents;
Scanner.OnLog:=Engine.Onlog;
if not (poSkipDefaultDefs in Options) then
begin
Scanner.AddDefine('FPK');
Scanner.AddDefine('FPC');
// TargetOS
s := UpperCase(OSTarget);
Scanner.AddDefine(s);
Case s of
'LINUX' : Scanner.AddDefine('UNIX');
'DARWIN' :
begin
Scanner.AddDefine('DARWIN');
Scanner.AddDefine('UNIX');
end;
'FREEBSD' :
begin
Scanner.AddDefine('BSD');
Scanner.AddDefine('UNIX');
end;
'NETBSD' :
begin
Scanner.AddDefine('BSD');
Scanner.AddDefine('UNIX');
end;
'SUNOS' :
begin
Scanner.AddDefine('SOLARIS');
Scanner.AddDefine('UNIX');
end;
'GO32V2' : Scanner.AddDefine('DPMI');
'BEOS' : Scanner.AddDefine('UNIX');
'QNX' : Scanner.AddDefine('UNIX');
'AROS' : Scanner.AddDefine('HASAMIGA');
'MORPHOS' : Scanner.AddDefine('HASAMIGA');
'AMIGA' : Scanner.AddDefine('HASAMIGA');
end;
// TargetCPU
s := UpperCase(CPUTarget);
Scanner.AddDefine('CPU'+s);
if (s='X86_64') then
Scanner.AddDefine('CPU64')
else
Scanner.AddDefine('CPU32');
end;
FParser := TPasParser.Create(Scanner, FileResolver, Engine);
if (poSkipDefaultDefs in Options) then
FParser.ImplicitUses.Clear;
Filename := '';
FParser.LogEvents:=Engine.ParserLogEvents;
FParser.OnLog:=Engine.Onlog;
{$IFDEF MULTIERROR}
FParser.MaxErrorCount:=50;
FParser.OnError:=@DoError;
{$ENDIF}
For S in CommandLine do
ProcessCmdLinePart(S);
if Filename = '' then
raise Exception.Create(SErrNoSourceGiven);
FileResolver.AddIncludePath(ExtractFilePath(FileName));
Scanner.OpenFile(Code.FileName);
try
FParser.ParseMain(Result);
except
on E : Exception do
begin
FreeAndNil(Result);
if Assigned(FOnError) then
With FParser.CurSourcePos do
begin
aCode:=-1;
{$IFDEF MULTIERROR}
if E is EParserError then
aCode:=EParserError(E).ErrNo;
{$ENDIF}
FOnError(Self,E.Message,FileName,aCode,Row,Column);
end;
end;
end;
finally
FreeAndNil(FParser);
Scanner.Free;
FileResolver.Free;
end;
end;
{ TCodeToolsFileResolver }
constructor TCodeToolsFileResolver.Create(aBuffer: TCodeBuffer);
begin
Inherited Create;
FBuffer:=aBuffer;
end;
function TCodeToolsFileResolver.CreateLineReaderFromBuffer(aBuffer : TCodeBuffer) : TLineReader;
Var
SLR : TStreamLineReader;
begin
SLR:=TStreamLineReader.Create(aBuffer.FileName);
SLR.InitFromString(aBuffer.Source);
Result:=SLR;
end;
function TCodeToolsFileResolver.FindSourceFile(const AName: string
): TLineReader;
begin
if SameFileName(aName,FBuffer.Filename) then
Result:=CreateLineReaderFromBuffer(FBuffer)
else
Result:=inherited FindSourceFile(AName);
end;
end.