/
parser.g
404 lines (371 loc) · 8.85 KB
/
parser.g
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* parser.g - Grammar for parsing tokens retrieved by the lexer
*
* Copyright (c) 1999 Ryan C. Gordon and Gregory S. Read.
*/
header
{
#include "PostParser.h"
}
options
{
language = "Cpp"; // We want to generate C++ output
}
class BasicParser extends Parser;
options
{
// Import tokens from the lexer
importVocab = Basic;
// Turn off default error handling
defaultErrorHandler=false;
}
{
private:
// Here's the post parser that we'll be passing parsed data to.
PostParser *pPostParser;
bool blnSkipToEnd;
}
start[PostParser *pNewPostParser]:
/* Description
* This function is called to start the parsing
* Parameters
* none
* Returns
* none
*/
{
// Save pointer to the parser being passed to our start method.
pPostParser = pNewPostParser;
// Don't skip to end of line by default
blnSkipToEnd = false;
}
(statement
{blnSkipToEnd}? (~(NEWLINE | EOF))
(NEWLINE)?)*
;
// Our global parser exception handler
exception catch [ParserException ex]
{
pPostParser->ReportError(&ex);
}
statement:
/* Description
* We come here each time we need to start parsing a single vb statement.
* Parameters
* none
* Returns
* none
*/
(dim_statement
// We'll get a proc_declaration if no scope identifier preceded the
// declaration. We shall assume public then.
| proc_declaration[SCOPE_PUBLIC]
| scope_statement
| identifier_statement
| end_statement
| for_statement
| next_statement
| print_statement)?
(COMMENT)?
;
// Exception handler for failed statements.
exception catch [ParserException ex]
{
pPostParser->ReportError(&ex);
// We want to skip to the end of the line if we got an error
blnSkipToEnd = true;
}
scope_statement
/* Description
* We get here if the statement starts with a scope identifier. From here
* we will test it for a variable declaration, or a procedure declaration.
* We also will pass the scope identifier that was specified along with
* the call.
* Parameters
* none
* Returns
* none
*/
{
BasicScope scope;
}:
(PUBLIC
{
scope = SCOPE_PUBLIC;
}
| PRIVATE
{
scope = SCOPE_PRIVATE;
}
| FRIEND
{
scope = SCOPE_FRIEND;
})
(var_declaration[scope]
| proc_declaration[scope])
;
dim_statement:
/* Description
* We'll get here if we've hit any statement with a DIM keyword
* Parameters
* none
* Returns
* none
*/
// Immediately spawn to the variable declaration method. The SCOPE_DIM
// can indicate a different scope depending on what the context of
// the DIM statement is.
DIM var_declaration[SCOPE_DIM]
;
proc_declaration[BasicScope Scope]:
/* Description
* We'll either get here as a root statement, or spawn to here from the
* 'scope_statement' function.
* Parameters
* Scope - Specifies the scope of the procedure declaration
* Returns
* none
*/
(STATIC)? (SUB IDENTIFIER decl_arglist)
| (FUNCTION IDENTIFIER decl_arglist (AS IDENTIFIER)?)
;
identifier_statement:
/* Description
* We'll get here on any statement that starts with an identifier.
* From here we decide whether it's an assignment statement, or
* a procedure call.
* Parameters
* none
* Returns
* none
*/
id:IDENTIFIER
(assignment_statement[id] | procedure_call[id])
;
for_statement:
/* Description
* This method processes a FOR statement
* Parameters
* none
* Returns
* none
*/
FOR
(IDENTIFIER EQUAL expression TO expression (STEP expression)?)
| (EACH IDENTIFIER IN IDENTIFIER)
;
next_statement:
/* Description
* Can't have a for statement without a next statement, eh?
* Parameters
* none
* Returns
* none
*/
NEXT (IDENTIFIER)?
;
end_statement:
/* Description
* Any statement beginning with 'end' will start here
* Parameters
* none
* Returns
* none
*/
END
(SUB | FUNCTION | IF | PROPERTY | SELECT | TYPE | WITH)?
;
print_statement:
/* Description
* The totally ambiguous and sucky print statement
* Parameters
* none
* Returns
* none
*/
PRINT (POUND NUMBER COMMA)?
( (SPC_FUNC LPAREN expression RPAREN)
| (TAB_FUNC LPAREN expression RPAREN)
| COMMA
| SEMICOLON
| expression)+
;
/* Branch statements */
decl_arglist:
/* Description
* This defines a set of one or more arguments in a procedure declaration.
* Parameters
* none
* Returns
* none
*/
LPAREN
(decl_arg (COMMA decl_arg)*)?
RPAREN
;
decl_arg:
/* Description
* This is the specification for a single argument in a procedure
* declaration.
* Parameters
* none
* Returns
* none
*/
(OPTIONAL)? (BYVAL | BYREF)? (PARAMARRAY)? IDENTIFIER
(LPAREN RPAREN)? AS IDENTIFIER (EQUAL expression)?
;
var_declaration[BasicScope Scope]
/* Description
* We come here when one or more variables are being declared...either
* using a scope identifier, or with the DIM statement.
* Parameters
* none
* Returns
* none
*/
{
// Initialize
RefToken rtVarName;
RefToken rtDataType;
Boolean blnWithEvents;
Boolean blnAsNew;
}:
var_decl[rtVarName, rtDataType, blnWithEvents, blnAsNew]
{
// If we get here, we'll have a valid variable declaration
pPostParser->VarDecl(Scope, rtVarName, rtDataType, blnWithEvents, blnAsNew);
}
(COMMA var_declaration[Scope])?
;
var_decl[RefToken &VarName, RefToken &DataType, Boolean &WithEvents,
Boolean &AsNew]
/* Description
* Represents a single variable declaration only. All parameters that
* are passed are set by this function.
* Parameters
* VarName - Token that represents the variable name being declared.
* DataType - Token that represents the datatype of the variable.
* WithEvents - Was the variable declared as 'WithEvents'?
* AsNew - Was the variable instantiated as a new object?
* Returns
* none
*/
// Initialization before processing variable declaration.
{
WithEvents = FALSE;
AsNew = FALSE;
}:
(withevents_token:WITHEVENTS)?
{
// If we found a 'WithEvents' token
if(withevents_token != NULL) WithEvents = TRUE;
}
varname_token:IDENTIFIER
{
// Retain identifier token
VarName = varname_token;
}
(LPAREN (subscripts)? RPAREN)?
(
// "As" or "As New" is acceptable
((AS NEW) => (AS NEW)
{
// If "As New" was specified
AsNew = TRUE;
}
| AS)
datatype_token:IDENTIFIER
{
// Save our datatype that was specified
DataType = datatype_token;
}
)?
;
subscripts:
/* Description
* We'll get here if we expect an array subscript range.
* Parameters
* none
* Returns
* none
*/
(expression TO) => (lower:expression TO upper:expression)
| upper2:expression (subscripts)?
;
assignment_statement[RefToken VarName]:
/* Description
* Just a plain old assignment statement.
* Parameters
* VarName - Token that represents the variable name being assigned to.
* Returns
* none
*/
EQUAL expr:expression
;
procedure_call[RefToken FuncName]:
/* Description
* Just a plain old procedure call.
* Parameters
* none
* Returns
* none
*/
(expression (COMMA expression)*)?
;
function_call[RefToken FuncName]:
/* Description
* Just a plain old function call. A function call differs from a
* procedure call in that the arguments are surrounded by parenthesis.
* Parameters
* none
* Returns
* none
*/
LPAREN expression (COMMA expression)* RPAREN
;
expression:
/* Description
* Represents a valid numeric or string expression
* Parameters
* none
* Returns
* none
*/
(unary_operator atom binary_operator) =>
(unary_operator atom binary_operator expression)
| (unary_operator atom)
| (atom binary_operator) => (atom binary_operator expression)
| atom
;
atom:
/* Description
* This is a single non-operator component of an expression.
* Parameters
* none
* Returns
* none
*/
(IDENTIFIER LPAREN) => (funcname:IDENTIFIER function_call[funcname])
| IDENTIFIER | NUMBER | STRING | (LPAREN expression RPAREN)
;
unary_operator:
/* Description
* Any operator that only requires an atom following the operator.
* Parameters
* none
* Returns
* none
*/
PLUS | MINUS
;
binary_operator:
/* Description
* Any operator that requires two atoms with the operator, located
* before and after it.
* Parameters
* none
* Returns
* none
*/
PLUS | MINUS | MULT | DIV
;