-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.pas
More file actions
248 lines (200 loc) · 5.83 KB
/
lexer.pas
File metadata and controls
248 lines (200 loc) · 5.83 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
unit Lexer;
{$mode ObjFPC}{$H+}
interface
uses
Classes,
SysUtils,
Generics.Collections,
RTTI,
TypInfo,
tokens;
const
MAX_DEPTH = 250;
type
TLexer = class
private
pFunList: TList;
pRecursionDepth: integer;
function ParseString(aArgText: string): string;
function ParseInt(aArgText: string): integer;
procedure ParseNamedVar(aFormDef: TFormDef; aName, aValue: string;
aStringStack: TNamedStrs; aIntStack: TNamedInts);
public
function eval(aFormToEval: string; aCurrentLine: integer;
aStringStack: TNamedStrs; aIntStack: TNamedInts): TFormDef;
constructor Create();
destructor Destroy(); override;
end;
implementation
constructor TLexer.Create();
begin
Self.pFunList := TList.Create;
Self.pRecursionDepth := 0;
end;
destructor TLexer.Destroy();
begin
if (Assigned(pFunList)) then Self.pFunList.Free;
end;
function TLexer.ParseString(aArgText: string): string;
begin
Result := aArgText.Trim(['"']);
end;
function TLexer.ParseInt(aArgText: string): integer;
begin
Result := StrToInt(aArgText);
end;
procedure TLexer.ParseNamedVar(aFormDef: TFormDef; aName, aValue: string;
aStringStack: TNamedStrs; aIntStack: TNamedInts);
begin
case aFormDef.FunToEval of
TOperation.defs:
begin
if aStringStack.ContainsKey(aName) then
begin
aIntStack.Remove(aName);
end;
aStringStack.Add(aName, ParseString(aValue));
//aFormDef.Argumenst.Add(aName);
end;
TOperation.defi:
begin
if aIntStack.ContainsKey(aName) then
begin
aIntStack.Remove(aName);
end;
aIntStack.Add(aName, ParseInt(aValue));
//aFormDef.Argumenst.Add(aName);
end;
else
raise Exception.Create('The we cannot parse ' + aName);
end;
end;
function TLexer.eval(aFormToEval: string; aCurrentLine: integer;
aStringStack: TNamedStrs; aIntStack: TNamedInts): TFormDef;
var
lOpenParanIndex: integer;
lCloseParanIndex: integer;
lQuoteStartIndex: integer;
lQuoteEndIndex: integer;
{ #todo : Can I use something instead of an index to find out where in the enum I am? }
lEnumIndex: integer;
lFormDef: TFormDef;
lOPType: ^TTypeInfo;
lOPString: string;
lArgOffset: integer;
lArgText: string;
lArgsSplit: TStringArray;
lStringVal: string;
{
lNestedLexer: TLexer;
lNestedForm: TFormDef;
lNestedParanOpen: integer;
lNestedParanClose: integer;
}
// iterators
iOperation: TOperation;
iCurrArgOffset: integer;
iQuote: integer;
{iNested: integer;}
iArgName: string;
begin
lFormDef := TFormDef.Create;
lOpenParanIndex := aFormToEval.IndexOf(char(TSymbol.openParan));
lEnumIndex := 0;
lArgText := '';
// First we find the `(`
if (lOpenParanIndex >= 0) then
begin
lFormDef.OpenParam := TSymbol.openParan;
// Reverse lookup!
// now we find the `)`
lCloseParanIndex := aFormToEval.LastIndexOf(char(TSymbol.closeParan));
if (lCloseParanIndex <= 0) then
raise Exception.Create(Format(
'No closing parantheses found on the current line! %d', [aCurrentline]));
// now after the `(` should come a function
for iOperation in TOperation do
begin
lOPType := TypeInfo(iOperation);
lOPString := GetEnumName(lOPType, lEnumIndex);
Inc(lEnumIndex);
if (aFormToEval.Contains(lOPString)) then
begin
lFormDef.FunToEval := iOperation;
break;
end;
end;
{
now we get the args!
}
// get a buffer and read until the closing parameter!
// len + len(lOpStr) + whitespace!
lArgOffset := lOpenParanIndex + Length(lOPString) + 1;
for iCurrArgOffset := lArgOffset to lCloseParanIndex - 1 do
begin
AppendStr(lArgText, aFormToEval.Chars[iCurrArgOffset]);
end;
if (lArgText = string.Empty) then
raise Exception.Create(Format(
'No arguments passed to function %s on line %d', [lOPString, aCurrentLine]));
{ #todo: recusive call to the lexer for nested functions
if (lArgText.Contains(char(TSymbol.openParan))) and
(lArgText.Contains(char(TSymbol.closeParan))) then
begin
// check recursion depth
if (aRecursionDepth >= MAX_DEPTH) then
raise Exception.Create('Reached max recursion depth!');
lStringVal := string.Empty;
lNestedParanOpen := lArgText.IndexOf(char(TSymbol.openParan));
lNestedParanClose := lArgText.LastIndexOf(char(TSymbol.closeParan));
for iNested := lNestedParanOpen to lNestedParanClose + 1 do
begin
lStringVal := lStringVal + lArgText[iNested];
end;
lNestedLexer := TLexer.Create;
lNestedForm := lNestedLexer.eval(lStringVal, aCurrentLine,
aStringStack, aIntStack, aCallStack, aRecursionDepth);
aCallStack[aRecursionDepth] := lNestedForm;
Inc(aRecursionDepth);
lNestedLexer.Free;
end;
}
// now we split on whitespace and try to parse the args
lArgsSplit := lArgText.Split(' ');
case lFormDef.FunToEval of
TOperation.defi:
begin
Self.ParseNamedVar(
lFormDef,
Trim(lArgsSplit[1]),
Trim(lArgsSplit[2]),
aStringStack,
aIntStack);
end;
TOperation.defs:
begin
lStringVal := string.Empty;
lQuoteStartIndex := lArgText.IndexOf('"');
lQuoteEndIndex := lArgText.LastIndexOf('"');
for iQuote := lQuoteStartIndex to lQuoteEndIndex do
begin
lStringVal := lStringVal + lArgText[iQuote];
end;
Self.ParseNamedVar(
lFormDef,
Trim(lArgsSplit[1]),
lStringVal,
aStringStack,
aIntStack);
end
else
for iArgName in lArgsSplit do
begin
if not (iArgName = string.Empty) then lFormDef.Argumenst.Add(iArgName);
end;
end;
{ #todo : limit amount of max args you can pass to a func }
Result := lFormDef;
end;
end;
end.