-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexprparser.pas
258 lines (234 loc) · 7.33 KB
/
exprparser.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
{
This program 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; version 2 of the License.
This program 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.
}
// Copyright (c) 2010 - J. Aldo G. de Freitas Junior
Unit
ExprParser;
Interface
Uses
Classes,
SysUtils,
ExprScanner,
ExprNodes;
Type
TExprParser = Class
Private
fOwnsSource : Boolean;
fSource : TExprTokenIterator;
Public
Constructor Create(Const aSource : TExprTokenIterator; Const aOwnsSource : Boolean = True); Virtual;
Destructor Destroy; Override;
Procedure Mark(Const aNode : TExprNode);
Function ParseUnaryMinus(Const aPrevious : TExprNode): TExprNode;
Function ParseUnaryNot(Const aPrevious : TExprNode): TExprNode;
Function ParseFunctionCall(Const aPrevious : TExprNode): TExprNode;
Function ParseStringLiteral(Const aPrevious : TExprNode): TExprNode;
Function ParseNumberLiteral(Const aPrevious : TExprNode): TExprNode;
Function ParseLiteral(Const aPrevious : TExprNode): TExprNode;
Function ParseTerm(Const aPrevious : TExprNode): TExprNode;
Function ParseMulExpression(Const aPrevious : TExprNode): TExprNode;
Function ParseAddExpression(Const aPrevious : TExprNode): TExprNode;
Function ParseExpression(Const aPrevious : TExprNode): TExprNode;
Function ParseExpressionList(Const aPrevious : TExprNode): TExprNode;
Function ParseParameters: TExprNode;
Function ParseSource: TExprNode;
End;
Implementation
Constructor TExprParser.Create(Const aSource : TExprTokenIterator; Const aOwnsSource : Boolean = True);
Begin
Inherited Create;
fOwnsSource := aOwnsSource;
fSource := aSource;
End;
Destructor TExprParser.Destroy;
Begin
If fOwnsSource Then
fSource.Free;
Inherited Destroy;
End;
Procedure TExprParser.Mark(Const aNode : TExprNode);
Begin
aNode.Row := fSource.Row;
aNode.Col := fSource.Col;
End;
Function TExprParser.ParseUnaryMinus(Const aPrevious : TExprNode): TExprNode;
Var
lUnaryMinus : TUnaryMinusNode;
Begin
// Debug WriteLn('ParseUnaryMinus');
lUnaryMinus := TUnaryMinusNode.Create(aPrevious);
Mark(lUnaryMinus);
fSource.Consume('-');
ParseTerm(lUnaryMinus);
Result := lUnaryMinus;
End;
Function TExprParser.ParseUnaryNot(Const aPrevious : TExprNode): TExprNode;
Var
lUnaryNot : TUnaryNotNode;
Begin
// Debug WriteLn('ParseUnaryNot');
lUnaryNot := TUnaryNotNode.Create(aPrevious);
Mark(lUnaryNot);
fSource.Consume('not');
ParseTerm(lUnaryNot);
Result := lUnaryNot;
End;
Function TExprParser.ParseFunctionCall(Const aPrevious : TExprNode): TExprNode;
Var
lFunctionCall : TFunctionCallNode;
Begin
// Debug WriteLn('ParseFunctionCall');
lFunctionCall := TFunctionCallNode.Create(aPrevious);
Mark(lFunctionCall);
lFunctionCall.Name := fSource.Extract(tkExprIdentifier);
If fSource.Consume('(', False) Then
Begin
If Not(fSource.Expected(')')) Then
lFunctionCall.Parameters := ParseExpressionList(lFunctionCall) As TExpressionListNode;
fSource.Consume(')');
End
Else
lFunctionCall.Parameters := Nil;
Result := lFunctionCall;
End;
Function TExprParser.ParseStringLiteral(Const aPrevious : TExprNode): TExprNode;
Var
lStringLiteral : TStringLiteralNode;
Begin
// Debug WriteLn('ParseStringLiteral');
lStringLiteral := TStringLiteralNode.Create(aPrevious);
Mark(lStringLiteral);
lStringLiteral.Value := fSource.Extract(tkExprLiteralString);
// Debug WriteLn(lStringLiteral.Value);
Result := lStringLiteral;
End;
Function TExprParser.ParseNumberLiteral(Const aPrevious : TExprNode): TExprNode;
Var
lNumberLiteral : TNumberLiteralNode;
Begin
// Debug WriteLn('ParseNumberLiteral');
lNumberLiteral := TNumberLiteralNode.Create(aPrevious);
Mark(lNumberLiteral);
lNumberLiteral.Value := StrToFloat(fSource.Extract(tkExprLiteralNumber));
// Debug WriteLn(lNumberLiteral.Value);
Result := lNumberLiteral;
End;
Function TExprParser.ParseLiteral(Const aPrevious : TExprNode): TExprNode;
Begin
// Debug WriteLn('ParseLiteral');
If fSource.Expected(tkExprLiteralString) Then
Result := ParseStringLiteral(aPrevious)
Else If fSource.Expected(tkExprLiteralNumber) Then
Result := ParseNumberLiteral(aPrevious)
Else
fSource.RaiseError('Expected string or number.');
End;
Function TExprParser.ParseTerm(Const aPrevious : TExprNode): TExprNode;
Begin
// Debug WriteLn('ParseTerm');
If fSource.Expected('-') Then
Result := ParseUnaryMinus(aPrevious)
Else If fSource.Expected('not') Then
Result := ParseUnaryNot(aPrevious)
Else If fSource.Expected(tkExprIdentifier) Then
Result := ParseFunctionCall(aPrevious)
Else If fSource.Expected('(') Then
Begin
fSource.Consume('(');
Result := ParseExpression(aPrevious);
fSource.Consume(')');
End
Else
Result := ParseLiteral(aPrevious);
End;
Function TExprParser.ParseMulExpression(Const aPrevious : TExprNode): TExprNode;
Var
lMul : TMulExpressionNode;
Begin
// Debug WriteLn('ParseMulExpression');
lMul := TMulExpressionNode.Create(aPrevious);
lMul.Operation := '';
ParseTerm(lMul);
If fSource.Expected(['*', '/', 'div', 'mod', 'and', '^']) Then
Begin
Mark(lMul);
lMul.Operation := fSource.Extract(tkExprWord);
ParseMulExpression(lMul);
End;
Result := lMul;
End;
Function TExprParser.ParseAddExpression(Const aPrevious : TExprNode): TExprNode;
Var
lAdd : TAddExpressionNode;
Begin
// Debug WriteLn('ParseAddExpression');
lAdd := TAddExpressionNode.Create(aPrevious);
lAdd.Operation := '';
ParseMulExpression(lAdd);
If fSource.Expected(['+', '-', 'or', 'xor']) Then
Begin
Mark(lAdd);
lAdd.Operation := fSource.Extract(tkExprWord);
ParseAddExpression(lAdd);
End;
Result := lAdd;
End;
Function TExprParser.ParseExpression(Const aPrevious : TExprNode): TExprNode;
Var
lCompare : TExpressionNode;
Begin
// Debug WriteLn('ParseExpression');
lCompare := TExpressionNode.Create(aPrevious);
lCompare.Operation := '';
ParseAddExpression(lCompare);
If fSource.Expected(['=', '<', '>', '<=', '>=', '<>']) Then
Begin
Mark(lCompare);
lCompare.Operation := fSource.Extract(tkExprWord);
ParseAddExpression(lCompare);
End;
Result := lCompare;
End;
Function TExprParser.ParseExpressionList(Const aPrevious : TExprNode): TExprNode;
Var
lExpressionList : TExpressionListNode;
Begin
// Debug WriteLn('ParseExpressionList');
lExpressionList := TExpressionListNode.Create(aPrevious);
Mark(lExpressionList);
Repeat
ParseExpression(lExpressionList);
Until Not(fSource.Consume(',', False));
Result := lExpressionList;
End;
Function TExprParser.ParseParameters: TExprNode;
Var
lSource : TSourceNode;
Begin
// Debug WriteLn('ParseSource');
lSource := TSourceNode.Create(Nil);
Mark(lSource);
Repeat
ParseExpression(lSource);
Until Not(fSource.Consume(',', False));
fSource.Consume(tkExprEOF);
Result := lSource;
End;
Function TExprParser.ParseSource: TExprNode;
Var
lSource : TSourceNode;
Begin
// Debug WriteLn('ParseSource');
lSource := TSourceNode.Create(Nil);
Mark(lSource);
ParseExpression(lSource);
fSource.Consume(tkExprEOF);
Result := lSource;
End;
End.