-
Notifications
You must be signed in to change notification settings - Fork 0
/
barf_preprocessor_parser.trison
513 lines (457 loc) · 14.7 KB
/
barf_preprocessor_parser.trison
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// 2006.10.15 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
%target.cpp.header_filename "barf_preprocessor_parser.hpp"
%target.cpp.implementation_filename "barf_preprocessor_parser.cpp"
%target.cpp.top_of_header_file %{
#if !defined(BARF_PREPROCESSOR_PARSER_HPP_)
#define BARF_PREPROCESSOR_PARSER_HPP_
#include "barf_preprocessor.hpp"
#include "barf_filoc.hpp"
namespace Barf {
namespace Ast {
class Base;
} // end of namespace Ast
namespace Preprocessor {
class Scanner;
%}
%target.cpp.class_name Parser
%target.cpp.bottom_of_class {
FiLoc const &GetFiLoc () const;
bool ScannerDebugSpewIsEnabled () const;
std::ostream *ScannerDebugSpewStream ();
void SetScannerDebugSpewStream (std::ostream *debug_spew_stream);
bool OpenFile (string const &input_filename);
void OpenString (string const &input_string, string const &input_name, bool use_line_numbers = false);
void OpenUsingStream (istream *input_stream, string const &input_name, bool use_line_numbers);
private:
Scanner *m_scanner;
}
%target.cpp.bottom_of_header_file %{
} // end of namespace Preprocessor
} // end of namespace Barf
#endif // !defined(BARF_PREPROCESSOR_PARSER_HPP_)
%}
%target.cpp.top_of_implementation_file %{
#include "barf_preprocessor_ast.hpp"
#include "barf_preprocessor_scanner.hpp"
namespace Barf {
namespace Preprocessor {
%}
%target.cpp.constructor_actions {
m_scanner = new Scanner();
}
%target.cpp.destructor_actions {
delete m_scanner;
m_scanner = NULL;
}
%target.cpp.top_of_parse_method_actions %{
EmitExecutionMessage("starting preprocessor parser");
%}
%target.cpp.bottom_of_parse_method_actions %{
if (parse_return_code == PRC_SUCCESS)
EmitExecutionMessage("preprocessor parse was successful");
%}
%target.cpp.bottom_of_implementation_file %{
FiLoc const &Parser::GetFiLoc () const
{
assert(m_scanner != NULL);
return m_scanner->GetFiLoc();
}
bool Parser::ScannerDebugSpewIsEnabled () const
{
return m_scanner->DebugSpewIsEnabled();
}
std::ostream *Parser::ScannerDebugSpewStream ()
{
return m_scanner->DebugSpewStream();
}
void Parser::SetScannerDebugSpewStream (std::ostream *debug_spew_stream)
{
m_scanner->SetDebugSpewStream(debug_spew_stream);
}
bool Parser::OpenFile (string const &input_filename)
{
assert(m_scanner != NULL);
ResetForNewInput();
EmitExecutionMessage("opening file \"" + input_filename + "\" for input");
bool scanner_open_file_succeeded = m_scanner->OpenFile(input_filename);
if (scanner_open_file_succeeded)
EmitExecutionMessage("opened file \"" + input_filename + "\" successfully");
return scanner_open_file_succeeded;
}
void Parser::OpenString (string const &input_string, string const &input_name, bool use_line_numbers)
{
assert(m_scanner != NULL);
ResetForNewInput();
return m_scanner->OpenString(input_string, input_name, use_line_numbers);
}
void Parser::OpenUsingStream (istream *input_stream, string const &input_name, bool use_line_numbers)
{
assert(m_scanner != NULL);
ResetForNewInput();
return m_scanner->OpenUsingStream(input_stream, input_name, use_line_numbers);
}
} // end of namespace Preprocessor
} // end of namespace Barf
%}
%target.cpp.token_data_type "Ast::Base *"
%target.cpp.token_data_default "NULL"
%target.cpp.custom_token_data_type_cast "Dsc"
%target.cpp.throw_away_token_actions {
delete token_data;
}
%target.cpp.scan_actions {
assert(m_scanner != NULL);
return m_scanner->Scan();
}
%target.cpp.generate_debug_spew_code
%target.cpp.debug_spew_prefix {"Preprocessor::Parser" << (GetFiLoc().IsValid() ? " ("+GetFiLoc().AsString()+")" : g_empty_string) << ":"}
%terminal TEXT %type.cpp "Text *"
%terminal START_CODE END_CODE
%terminal CODE_LINE CODE_NEWLINE
%terminal ID %type.cpp "Ast::Id *"
%terminal DUMP_SYMBOL_TABLE
%terminal IF ELSE ELSE_IF END_IF
%terminal UNDEFINE
%terminal DECLARE_ARRAY DECLARE_MAP
%terminal DEFINE END_DEFINE
%terminal LOOP END_LOOP
%terminal FOR_EACH END_FOR_EACH
%terminal INCLUDE SANDBOX_INCLUDE
%terminal WARNING ERROR FATAL_ERROR
%terminal SIZEOF
%terminal IS_DEFINED
%terminal INTEGER_LITERAL %type.cpp "Integer *"
%terminal STRING_LITERAL %type.cpp "Text *"
%terminal KEYWORD_INT KEYWORD_STRING
%terminal STRING_LENGTH
%terminal TO_CHARACTER_LITERAL
%terminal TO_STRING_LITERAL
%terminal '(' ')' '[' ']' ',' '?'
%terminal '.' '+' '-' '*' '/' '%'
%terminal '!' '&' '|' '=' '<' '>'
%terminal BAD_TOKEN
%prec.left %default
%prec.left LOGICAL_OR
%prec.left LOGICAL_AND
%prec.left EQUALITY
%prec.left COMPARISON
%prec.left CONCATENATION
%prec.left ADDITION
%prec.left MULTIPLICATION
%prec.right UNARY
%default_parse_nonterminal body
%%
%nonterminal body %type.cpp "Body *"
:
%empty
%target.cpp {
return new Body();
}
|
TEXT:text
%target.cpp {
Body *body = new Body();
body->Append(text);
return body;
}
|
body:body executable:executable
%target.cpp {
if (executable != NULL)
body->Append(executable);
return body;
}
|
body:body executable:executable TEXT:text
%target.cpp {
if (executable != NULL)
body->Append(executable);
body->Append(text);
return body;
}
;
%nonterminal executable %type.cpp "ExecutableAst *"
:
code:code
%target.cpp {
return code;
}
|
conditional_series:conditional
%target.cpp {
return conditional;
}
|
define:define body:body end_define
%target.cpp {
define->SetBody(body);
return define;
}
|
loop:loop body:body end_loop
%target.cpp {
loop->SetBody(body);
return loop;
}
|
for_each:for_each body:body end_for_each
%target.cpp {
for_each->SetBody(body);
return for_each;
}
;
%nonterminal code %type.cpp "ExecutableAst *"
:
START_CODE code_body:code_body END_CODE %target.cpp { return code_body; }
|
CODE_LINE code_body:code_body CODE_NEWLINE %target.cpp { return code_body; }
;
%nonterminal code_body %type.cpp "ExecutableAst *"
:
%empty
%target.cpp { return NULL; }
|
expression:expression
%target.cpp { return expression; }
|
DUMP_SYMBOL_TABLE '(' ')'
%target.cpp { return new DumpSymbolTable(); }
|
UNDEFINE '(' ID:id ')'
%target.cpp { return new Undefine(id); }
|
DECLARE_ARRAY '(' ID:id ')'
%target.cpp { return new DeclareArray(id); }
|
DECLARE_MAP '(' ID:id ')'
%target.cpp { return new DeclareMap(id); }
|
INCLUDE '(' expression:include_filename_expression ')'
%target.cpp { return new Include(include_filename_expression, false); }
|
SANDBOX_INCLUDE '(' expression:include_filename_expression ')'
%target.cpp { return new Include(include_filename_expression, true); }
|
WARNING '(' expression:message_expression ')'
%target.cpp { return new Message(message_expression, Message::WARNING); }
|
ERROR '(' expression:message_expression ')'
%target.cpp { return new Message(message_expression, Message::ERROR); }
|
FATAL_ERROR '(' expression:message_expression ')'
%target.cpp { return new Message(message_expression, Message::FATAL_ERROR); }
;
%nonterminal conditional_series %type.cpp "Conditional *"
:
if_statement:conditional body:if_body conditional_series_end:else_body
%target.cpp {
conditional->SetIfBody(if_body);
conditional->SetElseBody(else_body);
return conditional;
}
;
%nonterminal conditional_series_end %type.cpp "Body *"
:
end_if %target.cpp { return NULL; }
|
else_statement body:body end_if %target.cpp { return body; }
|
else_if_statement:conditional body:if_body conditional_series_end:else_body
%target.cpp {
conditional->SetIfBody(if_body);
conditional->SetElseBody(else_body);
Body *body = new Body();
body->Append(conditional);
return body;
}
;
%nonterminal if_statement %type.cpp "Conditional *"
:
START_CODE IF '(' expression:expression ')' END_CODE
%target.cpp { return new Conditional(expression); }
|
CODE_LINE IF '(' expression:expression ')' CODE_NEWLINE
%target.cpp { return new Conditional(expression); }
;
%nonterminal else_statement
:
START_CODE ELSE END_CODE %target.cpp { return NULL; }
|
CODE_LINE ELSE CODE_NEWLINE %target.cpp { return NULL; }
;
%nonterminal else_if_statement %type.cpp "Conditional *"
:
START_CODE ELSE_IF '(' expression:expression ')' END_CODE
%target.cpp { return new Conditional(expression); }
|
CODE_LINE ELSE_IF '(' expression:expression ')' CODE_NEWLINE
%target.cpp { return new Conditional(expression); }
;
%nonterminal end_if
:
START_CODE END_IF END_CODE %target.cpp { return NULL; }
|
CODE_LINE END_IF CODE_NEWLINE %target.cpp { return NULL; }
;
%nonterminal define %type.cpp "Define *"
:
define_scalar:define %target.cpp { return define; }
|
define_array_element:define %target.cpp { return define; }
|
define_map_element:define %target.cpp { return define; }
;
%nonterminal define_scalar %type.cpp "Define *"
:
START_CODE DEFINE '(' ID:id ')' END_CODE
%target.cpp { return new Define(id); }
|
CODE_LINE DEFINE '(' ID:id ')' CODE_NEWLINE
%target.cpp { return new Define(id); }
;
%nonterminal define_array_element %type.cpp "Define *"
:
START_CODE DEFINE '(' ID:id '[' ']' ')' END_CODE
%target.cpp { return new DefineArrayElement(id); }
|
CODE_LINE DEFINE '(' ID:id '[' ']' ')' CODE_NEWLINE
%target.cpp { return new DefineArrayElement(id); }
;
%nonterminal define_map_element %type.cpp "Define *"
:
START_CODE DEFINE '(' ID:id '[' STRING_LITERAL:key ']' ')' END_CODE
%target.cpp { return new DefineMapElement(id, key); }
|
CODE_LINE DEFINE '(' ID:id '[' STRING_LITERAL:key ']' ')' CODE_NEWLINE
%target.cpp { return new DefineMapElement(id, key); }
;
%nonterminal end_define
:
START_CODE END_DEFINE END_CODE %target.cpp { return NULL; }
|
CODE_LINE END_DEFINE CODE_NEWLINE %target.cpp { return NULL; }
;
%nonterminal loop %type.cpp "Loop *"
:
START_CODE LOOP '(' ID:iterator_id ',' expression:iteration_count_expression ')' END_CODE
%target.cpp { return new Loop(iterator_id, iteration_count_expression); }
|
CODE_LINE LOOP '(' ID:iterator_id ',' expression:iteration_count_expression ')' CODE_NEWLINE
%target.cpp { return new Loop(iterator_id, iteration_count_expression); }
;
%nonterminal end_loop
:
START_CODE END_LOOP END_CODE %target.cpp { return NULL; }
|
CODE_LINE END_LOOP CODE_NEWLINE %target.cpp { return NULL; }
;
%nonterminal for_each %type.cpp "ForEach *"
:
START_CODE FOR_EACH '(' ID:key_id ',' ID:map_id ')' END_CODE
%target.cpp { return new ForEach(key_id, map_id); }
|
CODE_LINE FOR_EACH '(' ID:key_id ',' ID:map_id ')' CODE_NEWLINE
%target.cpp { return new ForEach(key_id, map_id); }
;
%nonterminal end_for_each
:
START_CODE END_FOR_EACH END_CODE %target.cpp { return NULL; }
|
CODE_LINE END_FOR_EACH CODE_NEWLINE %target.cpp { return NULL; }
;
%nonterminal expression %type.cpp "Expression *"
:
STRING_LITERAL:str
%target.cpp { return str; }
|
INTEGER_LITERAL:integer
%target.cpp { return integer; }
|
SIZEOF '(' ID:id ')'
%target.cpp { return new Sizeof(id); }
|
KEYWORD_INT '(' expression:expression ')'
%target.cpp { return new Operation(Operation::INT_CAST, expression); }
|
KEYWORD_STRING '(' expression:expression ')'
%target.cpp { return new Operation(Operation::STRING_CAST, expression); }
|
STRING_LENGTH '(' expression:expression ')'
%target.cpp { return new Operation(Operation::STRING_LENGTH, expression); }
|
TO_CHARACTER_LITERAL '(' expression:character_index_expression ')'
%target.cpp { return new Operation(Operation::TO_CHARACTER_LITERAL, character_index_expression); }
|
TO_STRING_LITERAL '(' expression:string_expression ')'
%target.cpp { return new Operation(Operation::TO_STRING_LITERAL, string_expression); }
|
IS_DEFINED '(' ID:id ')'
%target.cpp { return new IsDefined(id, NULL); }
|
IS_DEFINED '(' ID:id '[' expression:element_index_expression ']' ')'
%target.cpp { return new IsDefined(id, element_index_expression); }
|
ID:id
%target.cpp { return new Dereference(id, NULL, DEREFERENCE_ALWAYS); }
|
ID:id '[' expression:element_index_expression ']'
%target.cpp { return new Dereference(id, element_index_expression, DEREFERENCE_ALWAYS); }
|
ID:id '?'
%target.cpp { return new Dereference(id, NULL, DEREFERENCE_IFF_DEFINED); }
|
ID:id '[' expression:element_index_expression ']' '?'
%target.cpp { return new Dereference(id, element_index_expression, DEREFERENCE_IFF_DEFINED); }
|
expression:left '.' expression:right %prec CONCATENATION
%target.cpp { return new Operation(left, Operation::CONCATENATE, right); }
|
expression:left '|' '|' expression:right %prec LOGICAL_OR
%target.cpp { return new Operation(left, Operation::LOGICAL_OR, right); }
|
expression:left '&' '&' expression:right %prec LOGICAL_AND
%target.cpp { return new Operation(left, Operation::LOGICAL_AND, right); }
|
expression:left '=' '=' expression:right %prec EQUALITY
%target.cpp { return new Operation(left, Operation::EQUAL, right); }
|
expression:left '!' '=' expression:right %prec EQUALITY
%target.cpp { return new Operation(left, Operation::NOT_EQUAL, right); }
|
expression:left '<' expression:right %prec COMPARISON
%target.cpp { return new Operation(left, Operation::LESS_THAN, right); }
|
expression:left '<' '=' expression:right %prec COMPARISON
%target.cpp { return new Operation(left, Operation::LESS_THAN_OR_EQUAL, right); }
|
expression:left '>' expression:right %prec COMPARISON
%target.cpp { return new Operation(left, Operation::GREATER_THAN, right); }
|
expression:left '>' '=' expression:right %prec COMPARISON
%target.cpp { return new Operation(left, Operation::GREATER_THAN_OR_EQUAL, right); }
|
expression:left '+' expression:right %prec ADDITION
%target.cpp { return new Operation(left, Operation::PLUS, right); }
|
expression:left '-' expression:right %prec ADDITION
%target.cpp { return new Operation(left, Operation::MINUS, right); }
|
expression:left '*' expression:right %prec MULTIPLICATION
%target.cpp { return new Operation(left, Operation::MULTIPLY, right); }
|
expression:left '/' expression:right %prec MULTIPLICATION
%target.cpp { return new Operation(left, Operation::DIVIDE, right); }
|
expression:left '%' expression:right %prec MULTIPLICATION
%target.cpp { return new Operation(left, Operation::REMAINDER, right); }
|
'-' expression:expression %prec UNARY
%target.cpp { return new Operation(Operation::NEGATIVE, expression); }
|
'!' expression:expression %prec UNARY
%target.cpp { return new Operation(Operation::LOGICAL_NOT, expression); }
|
'(' expression:expression ')'
%target.cpp { return expression; }
;