-
Notifications
You must be signed in to change notification settings - Fork 0
/
barf_regex_parser.trison
770 lines (699 loc) · 25.5 KB
/
barf_regex_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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// 2006.01.30 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
%target.cpp.header_filename "barf_regex_parser.hpp"
%target.cpp.implementation_filename "barf_regex_parser.cpp"
%target.cpp.top_of_header_file %{
#if !defined(BARF_REGEX_PARSER_HPP_)
#define BARF_REGEX_PARSER_HPP_
#include "barf_regex.hpp"
#include <istream>
#include "barf_inputbase.hpp"
#include "barf_regex_ast.hpp"
#include "barf_util.hpp"
namespace Barf {
namespace Regex {
%}
%target.cpp.class_name Parser
%target.cpp.class_inheritance { protected InputBase }
%target.cpp.parse_method_access "private"
%target.cpp.bottom_of_class {
using InputBase::IsOpen;
using InputBase::GetFiLoc;
using InputBase::InputName;
// must override these (instead of just "using" them), so that
// ResetForNewInput can be called.
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);
using InputBase::Close;
//bool ScannerDebugSpew () const { return false; } // TODO: not implemented yet
//void ScannerDebugSpew (bool debug_spew) { } // TODO: not implemented yet
bool ScannerDebugSpewIsEnabled () const { return false; } // TODO: not implemented yet
std::ostream *ScannerDebugSpewStream () const { return NULL; } // TODO: not implemented yet
void SetScannerDebugSpewStream (std::ostream *debug_spew_stream) { } // TODO: not implemented yet
// this method will throw a std::string if a macro is used without
// providing a macro map, if an undefined macro is referenced, or
// if it encounters an error while parsing the regex.
ParserReturnCode Parse (RegularExpression **parsed_regex, RegularExpressionMap *macro_map = NULL);
private:
mutable RegularExpressionMap *m_macro_map;
// little kludgey thing to keep track of if the last char
// was a backslash which is going to escape something (this
// is needed because the scanner is doing part of the parser's
// job; parsing of hex chars, e.g. \xA7)
bool m_active_backslash;
}
%target.cpp.bottom_of_header_file %{
} // end of namespace Regex
} // end of namespace Barf
#endif // !defined(BARF_REGEX_PARSER_HPP_)
%}
%target.cpp.top_of_implementation_file %{
#include <sstream>
namespace Barf {
namespace Regex {
bool IsHexDigit (Uint8 const c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}
Uint8 HexDigitValue (Uint8 c)
{
assert(IsHexDigit(c));
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'A' && c <= 'F')
c += 0xA - 'A';
else if (c >= 'a' && c <= 'f')
c += 0xA - 'a';
return c;
}
%}
%target.cpp.superclass_and_member_constructors { InputBase() }
%target.cpp.constructor_actions {
m_macro_map = NULL;
m_active_backslash = false;
}
%target.cpp.destructor_actions {
assert(m_macro_map == NULL);
}
%target.cpp.enable_scan_actions_exceptions
%target.cpp.enable_reduction_rule_exceptions
%target.cpp.bottom_of_implementation_file %{
bool Parser::OpenFile (string const &input_filename)
{
ResetForNewInput();
return InputBase::OpenFile(input_filename);
}
void Parser::OpenString (string const &input_string, string const &input_name, bool use_line_numbers)
{
ResetForNewInput();
return InputBase::OpenString(input_string, input_name, use_line_numbers);
}
void Parser::OpenUsingStream (istream *input_stream, string const &input_name, bool use_line_numbers)
{
ResetForNewInput();
return InputBase::OpenUsingStream(input_stream, input_name, use_line_numbers);
}
Parser::ParserReturnCode Parser::Parse (RegularExpression **parsed_regex, RegularExpressionMap *macro_map)
{
assert(parsed_regex != NULL);
assert(m_macro_map == NULL);
try {
Ast::Base *parsed_tree_root = NULL;
m_macro_map = macro_map;
ParserReturnCode retval = Parse(&parsed_tree_root);
*parsed_regex = Dsc<RegularExpression *>(parsed_tree_root);
m_macro_map = NULL;
return retval;
} catch (string const &exception) {
*parsed_regex = NULL;
m_macro_map = NULL;
throw exception;
}
}
} // end of namespace Regex
} // 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 {
while (true)
{
int c;
c = In().get();
if (c == EOF)
{
m_active_backslash = false;
return Token(Terminal::END_);
}
if (c >= '0' && c <= '9')
{
m_active_backslash = false;
return Token(Terminal::DIGIT, new Char(c));
}
else if ((c >= 'A' && c <= 'Z')
||
(c >= 'a' && c <= 'z'))
{
m_active_backslash = false;
return Token(Terminal::ALPHA, new Char(c));
}
assert(c >= 0 && c < 256);
switch (c)
{
// backslash needs to be handled in a special way, since
// it can delimit a hex char (e.g. \xA7).
//
// the desired behavior is that in the string "\\xA7" the
// sequence of returned chars is '\' '\' 'x' 'A' '7' (i.e.
// "\\" takes precedence over "\xA7") whereas in the string
// "\xA7" the sequence a single character having hex value A7.
case '\\':
{
// if there's an active backslash, we'll want to just
// return the backslash, because the active backslash
// (the previous character) will escape it.
if (m_active_backslash)
{
m_active_backslash = false;
return Token::Id('\\');
}
// if the next char isn't 'x' (i.e. we will not be
// parsing a hex char), then this backslash is active.
if (In().peek() != 'x')
{
m_active_backslash = true;
return Token::Id('\\');
}
assert(!m_active_backslash);
// eat the 'x'
c = In().get();
assert(c == 'x');
// we're expecting exactly two hex digits after the \x
Uint8 hex_value = 0;
for (Uint8 i = 0; i < 2; ++i)
{
c = In().get();
if (c == EOF)
THROW_STRING("encountered end of input while trying to scan hex char");
if (!IsHexDigit(c))
THROW_STRING("expected exactly two hex digits after \"\\x\"");
hex_value <<= 4;
hex_value |= HexDigitValue(c);
}
// (for now) prevent usage of the null char \x00, since
// the scanner implementation uses '\0' as a sentinel value.
if (hex_value == 0)
THROW_STRING("can't use hex char \\x00");
return Token(Terminal::HEX_CHAR, new Char(hex_value));
}
// other control chars
case '^':
case '$':
case '*':
case '+':
case '?':
case '{':
case '}':
case '[':
case ']':
case '(':
case ')':
case '.':
case ',':
case '|':
case '-':
case ':':
m_active_backslash = false;
return Token::Id(c);
// non-alphanumeric normal chars
case ' ':
case '!':
case '"':
case '#':
case '%':
case '&':
case '\'':
case '/':
case ';':
case '<':
case '=':
case '>':
case '@':
case '_':
case '`':
case '~':
m_active_backslash = false;
return Token(Terminal::CHAR, new Char(c));
// anything else is an invalid char
default:
m_active_backslash = false;
THROW_STRING("invalid char (use hex char " << CharLiteral(c, false) << " instead)");
break;
}
}
}
%target.cpp.reset_for_new_input_actions {
m_macro_map = NULL;
m_active_backslash = false;
}
%target.cpp.generate_debug_spew_code
%target.cpp.debug_spew_prefix {"Regex::Parser" << (GetFiLoc().IsValid() ? " ("+GetFiLoc().AsString()+")" : g_empty_string) << ":"}
// ///////////////////////////////////////////////////////////////////////////
// grammar metadata
// ///////////////////////////////////////////////////////////////////////////
%terminal BAD_TOKEN
%terminal ALPHA %type.cpp "Char *"
%terminal CHAR %type.cpp "Char *"
%terminal DIGIT %type.cpp "Char *"
%terminal HEX_CHAR %type.cpp "Char *"
// NOTE: quotes around examples for clarity
%terminal '|' // branch operator; e.g. "re1|re2" matches "re1" or "re2"
%terminal '(' ')' // associative grouping delimiters
%terminal '?' // "a?" matches "" or "a"
%terminal '*' // "a*" matches "" or "a" or "aa" or "aaa" etc
%terminal '+' // "a+" matches "a" or "aa" or "aaa" etc
%terminal '{' ',' '}' // "a{3}" matches "aaa"
// "a{3,}" matches "aaa" or "aaaa" or "aaaaa" etc
// "a{3,4}" matches "aaa" or "aaaa"
%terminal '^' // "^blah" matches "blah" only at the beginning of a line
%terminal '$' // "blah$" matches "blah" only at the end of a line
%terminal '.' // "." matches any non-newline char
%terminal '\\' // escape char (but also delimits a hex char such as \xA7
// which is handled entirely by the scanner)
%terminal '[' ']' // bracket expression delimiters
%terminal '-' // bracket range delimiter
%terminal ':' // char class delimiter
// "[abc]" matches "a" or "b" or "c"
// "[a-e]" matches "a" or "b" or "c" or "d" or "e"
// "[[:digit:]]" matches "0" or "1" or "2" or "3" or "4"
// or "5" or "6" or "7" or "8" or "9"
// other char classes include "alnum", "alpha", etc
%prec.left %default
%default_parse_nonterminal regex_then_end
%%
// ///////////////////////////////////////////////////////////////////////////
// grammar specification
// ///////////////////////////////////////////////////////////////////////////
%nonterminal regex_then_end %type.cpp "RegularExpression *"
:
regex_unterminated:regex branch:branch %end
%target.cpp {
for (RegularExpression::size_type i = 0; i < regex->size(); ++i)
if (Regex::NodesAreEqual(branch, regex->Element(i)))
THROW_STRING("duplicated branch");
regex->Append(branch);
return regex;
}
|
branch:branch %end
%target.cpp {
RegularExpression *regex = new RegularExpression();
regex->Append(branch);
return regex;
}
;
%nonterminal regex_parenthesized %type.cpp "RegularExpression *"
:
'(' regex_unterminated:regex branch:branch ')'
%target.cpp {
for (RegularExpression::size_type i = 0; i < regex->size(); ++i)
if (Regex::NodesAreEqual(branch, regex->Element(i)))
THROW_STRING("duplicated branch");
regex->Append(branch);
return regex;
}
|
'(' branch:branch ')'
%target.cpp {
RegularExpression *regex = new RegularExpression();
regex->Append(branch);
return regex;
}
;
%nonterminal regex_unterminated %type.cpp "RegularExpression *"
:
regex_unterminated:regex branch:branch '|'
%target.cpp {
for (RegularExpression::size_type i = 0; i < regex->size(); ++i)
if (Regex::NodesAreEqual(branch, regex->Element(i)))
THROW_STRING("duplicated branch");
regex->Append(branch);
return regex;
}
|
branch:branch '|'
%target.cpp {
RegularExpression *regex = new RegularExpression();
regex->Append(branch);
return regex;
}
;
// %nonterminal regex %type.cpp "RegularExpression *"
// :
// regex:regex '|' branch:branch
// %target.cpp {
// for (RegularExpression::size_type i = 0; i < regex->size(); ++i)
// if (Regex::NodesAreEqual(branch, regex->Element(i)))
// THROW_STRING("duplicated branch");
//
// regex->Append(branch);
// return regex;
// }
// |
// branch:branch
// %target.cpp {
// RegularExpression *regex = new RegularExpression();
// regex->Append(branch);
// return regex;
// }
// ;
%nonterminal branch %type.cpp "Branch *"
:
branch_which_didnt_just_accept_an_atom:branch %target.cpp { return branch; }
| branch_which_just_accepted_an_atom:branch %target.cpp { return branch; }
| %empty %target.cpp { return new Branch(); }
;
%nonterminal branch_which_didnt_just_accept_an_atom %type.cpp "Branch *"
:
branch_which_just_accepted_an_atom:branch bound:bound
%target.cpp {
branch->AddBound(bound);
return branch;
}
;
%nonterminal branch_which_just_accepted_an_atom %type.cpp "Branch *"
:
branch_which_just_accepted_an_atom:branch atom:atom
%target.cpp {
branch->AddAtom(atom);
return branch;
}
|
branch_which_didnt_just_accept_an_atom:branch atom:atom
%target.cpp {
branch->AddAtom(atom);
return branch;
}
|
atom:atom
%target.cpp {
Branch *branch = new Branch();
branch->AddAtom(atom);
return branch;
}
;
// ///////////////////////////////////////////////////////////////////////////
// atom and bound rules (which together make a piece)
// ///////////////////////////////////////////////////////////////////////////
%nonterminal atom %type.cpp "Atom *"
:
'{' id:macro_name '}'
%target.cpp {
assert(macro_name != NULL);
if (m_macro_map == NULL)
THROW_STRING("undefined macro \"" + macro_name->GetText() + "\" (no macro map was provided)");
RegularExpression *macro_regex = m_macro_map->Element(macro_name->GetText());
if (macro_regex == NULL)
THROW_STRING("undefined macro \"" + macro_name->GetText() + "\"");
return macro_regex;
}
| regex_parenthesized:regex %target.cpp { return regex; }
// | '(' ')' %target.cpp { return new RegularExpression(); }
| '^' %target.cpp { return new ConditionalChar(CT_BEGINNING_OF_LINE); }
| '$' %target.cpp { return new ConditionalChar(CT_END_OF_LINE); }
| '.' %target.cpp { return new BracketCharSet('\n', true); }
| atom_normal_char:ch %target.cpp { return ch; }
| '\\' atom_normal_char:ch
%target.cpp {
if (ch->GetChar() == '0')
THROW_STRING("can't use \\0");
Atom *escaped = ch->Escaped();
// if escaping changed the char (i.e. it new'ed something), delete the old
if (ch != escaped)
delete ch;
return escaped;
}
| '\\' atom_control_char:ch %target.cpp { return ch; }
| HEX_CHAR:ch %target.cpp { return ch; }
| bracket_expression:exp %target.cpp { return exp; }
;
%nonterminal bound %type.cpp "Bound *"
:
'*' %target.cpp { return new Bound(0, Bound::NO_UPPER_BOUND); }
|
'+' %target.cpp { return new Bound(1, Bound::NO_UPPER_BOUND); }
|
'?' %target.cpp { return new Bound(0, 1); }
|
'{' integer:exact_bound '}'
%target.cpp {
assert(exact_bound->Value() >= 0);
Bound *bound = new Bound(exact_bound->Value(), exact_bound->Value());
delete exact_bound;
return bound;
}
|
'{' integer:lower_bound ',' '}'
%target.cpp {
assert(lower_bound->Value() >= 0);
return new Bound(lower_bound->Value(), Bound::NO_UPPER_BOUND);
}
|
'{' integer:lower_bound ',' integer:upper_bound '}'
%target.cpp {
assert(lower_bound->Value() >= 0);
assert(upper_bound->Value() >= 0);
if (lower_bound->Value() > upper_bound->Value() ||
(lower_bound->Value() == 0 && upper_bound->Value() == 0))
{
ostringstream out;
out << "invalid bound {" << lower_bound->Value() << ',' << upper_bound->Value() << '}';
throw out.str();
}
else if (lower_bound->Value() > Bound::MaximumBoundValue())
{
ostringstream out;
out << "bound value " << lower_bound->Value() << " is out of range (max 255)";
throw out.str();
}
else if (upper_bound->Value() > Bound::MaximumBoundValue())
{
ostringstream out;
out << "bound value " << upper_bound->Value() << " is out of range (max 255)";
throw out.str();
}
Bound *bound = new Bound(lower_bound->Value(), upper_bound->Value());
delete lower_bound;
delete upper_bound;
return bound;
}
;
// ///////////////////////////////////////////////////////////////////////////
// bracket expression rules
// ///////////////////////////////////////////////////////////////////////////
%nonterminal bracket_expression %type.cpp "Atom *"
:
'[' bracket_char_set:bracket_char_set ']'
%target.cpp {
if (bracket_char_set->IsEmpty())
THROW_STRING("invalid empty bracket expression");
return bracket_char_set;
}
|
'[' '^' bracket_char_set:bracket_char_set ']'
%target.cpp {
if (bracket_char_set->IsEmpty())
THROW_STRING("invalid empty bracket expression");
bracket_char_set->Negate();
return bracket_char_set;
}
;
%nonterminal bracket_char_set %type.cpp "BracketCharSet *"
:
bracket_char_set:bracket_char_set bracket_expression_char:ch
%target.cpp {
bracket_char_set->AddChar(ch->GetChar());
delete ch;
return bracket_char_set;
}
|
bracket_char_set:bracket_char_set bracket_expression_char:begin_range '-' bracket_expression_char:end_range
%target.cpp {
if (end_range->GetChar() < begin_range->GetChar())
THROW_STRING("invalid bracketed range [" << CharLiteral(begin_range->GetChar(), false) << '-' << CharLiteral(end_range->GetChar(), false) << ']');
bracket_char_set->AddCharRange(
begin_range->GetChar(),
end_range->GetChar());
delete begin_range;
delete end_range;
return bracket_char_set;
}
|
bracket_char_set:bracket_char_set '[' ':' id:id ':' ']'
%target.cpp {
bracket_char_set->AddCharClass(id->GetText());
delete id;
return bracket_char_set;
}
|
%empty
%target.cpp {
BracketCharSet *bracket_char_set = new BracketCharSet();
return bracket_char_set;
}
;
%nonterminal bracket_expression_char %type.cpp "Char *"
: // normal chars don't require any escaping.
bracket_expression_normal_char:normal_char %target.cpp { return normal_char; }
| // some normal chars can be escaped (like '\n')
'\\' bracket_expression_normal_char:normal_char %target.cpp { normal_char->EscapeInsideBracketExpression(); return normal_char; }
| // control chars require escaping.
'\\' bracket_expression_control_char:control_char %target.cpp { return control_char; }
| // hex chars can't be escaped
HEX_CHAR:hex_char %target.cpp { return hex_char; }
;
// ///////////////////////////////////////////////////////////////////////////
// grammar primitive rules
// ///////////////////////////////////////////////////////////////////////////
// these chars are special only in atom parsing (not inside
// a bound or a bracket expression). all others (with the exception
// of backslash, the escape char) are treated as normal chars.
// no char here should be present in atom_normal_char,
// and the union of atom_normal_char, atom_control_char
// and backslash should constitute all valid chars.
%nonterminal atom_control_char %type.cpp "Char *"
:
// branch operator
'|' %target.cpp { return new Char('|'); }
| // associative grouping delimiter (open)
'(' %target.cpp { return new Char('('); }
| // associative grouping delimiter (close)
')' %target.cpp { return new Char(')'); }
| // bound delimiter (open)
'{' %target.cpp { return new Char('{'); }
| // bound delimiter (close)
'}' %target.cpp { return new Char('}'); }
| // bracket expression delimiter (open)
'[' %target.cpp { return new Char('['); }
| // bracket expression delimiter (close)
']' %target.cpp { return new Char(']'); }
| // indicates 0 or 1 of the previous atom
'?' %target.cpp { return new Char('?'); }
| // indicates 0 or more of the previous atom
'*' %target.cpp { return new Char('*'); }
| // indicates 1 or more of the previous atom
'+' %target.cpp { return new Char('+'); }
| // match-any-non-newline char
'.' %target.cpp { return new Char('.'); }
| // match-the-beginning-of-a-line epsilon operator
'^' %target.cpp { return new Char('^'); }
| // match-the-end-of-a-line epsilon operator
'$' %target.cpp { return new Char('$'); }
| // backslash -- for escaping other chars
'\\' %target.cpp { return new Char('\\'); }
;
// these chars have no special meaning in atom parsing (not
// inside a bound or a bracket expression). all others (with the exception
// of backslash, the escape char) are treated as control chars.
// no char here should be present in atom_control_char,
// and the union of atom_normal_char, atom_control_char
// and backslash should constitute all valid chars.
%nonterminal atom_normal_char %type.cpp "Char *"
:
ALPHA:alpha %target.cpp { return alpha; }
| DIGIT:digit %target.cpp { return digit; }
| CHAR:ch %target.cpp { return ch; }
| ',' %target.cpp { return new Char(','); }
| '-' %target.cpp { return new Char('-'); }
| ':' %target.cpp { return new Char(':'); }
;
// these chars are special only inside bracket expressions. all others
// (with the exception of backslash, the escape char) are treated as
// normal chars. no char here should be present in
// bracket_expression_normal_char, and the union of
// bracket_expression_normal_char, bracket_expression_control_char
// and backslash should constitute all valid chars.
%nonterminal bracket_expression_control_char %type.cpp "Char *"
:
// delimits a bracket atom range
'-' %target.cpp { return new Char('-'); }
| // indicates bracket char set negation
'^' %target.cpp { return new Char('^'); }
| // indicates the beginning of a char class expression
'[' %target.cpp { return new Char('['); }
| // indicates the end of the bracket expression (or char class exp)
']' %target.cpp { return new Char(']'); }
| // backslash -- for escaping other chars
'\\' %target.cpp { return new Char('\\'); }
;
// these chars have no special meaning inside bracket expressions. all
// others (with the exception of backslash, the escape char) are treated
// as normal chars. no char here should be present in
// bracket_expression_normal_char, and the union of
// bracket_expression_normal_char, bracket_expression_control_char
// and backslash should constitute all valid chars.
%nonterminal bracket_expression_normal_char %type.cpp "Char *"
:
ALPHA:alpha %target.cpp { return alpha; }
| DIGIT:digit %target.cpp { return digit; }
| CHAR:ch %target.cpp { return ch; }
| '|' %target.cpp { return new Char('|'); }
| ':' %target.cpp { return new Char(':'); }
| '?' %target.cpp { return new Char('?'); }
| '*' %target.cpp { return new Char('*'); }
| '+' %target.cpp { return new Char('+'); }
| '.' %target.cpp { return new Char('.'); }
| '$' %target.cpp { return new Char('$'); }
| ',' %target.cpp { return new Char(','); }
| '(' %target.cpp { return new Char('('); }
| ')' %target.cpp { return new Char(')'); }
| '{' %target.cpp { return new Char('{'); }
| '}' %target.cpp { return new Char('}'); }
;
%nonterminal id %type.cpp "Ast::Id *"
:
id:id ALPHA:alpha
%target.cpp {
assert(id != NULL);
id->AppendChar(alpha->GetChar());
delete alpha;
return id;
}
|
id:id CHAR:ch
%target.cpp {
assert(id != NULL);
id->AppendChar(ch->GetChar());
delete ch;
return id;
}
|
id:id DIGIT:digit
%target.cpp {
assert(id != NULL);
id->AppendChar(digit->GetChar());
delete digit;
return id;
}
|
ALPHA:alpha
%target.cpp {
string temp;
temp += alpha->GetChar();
Ast::Id *id = new Ast::Id(temp, FiLoc::ms_invalid);
delete alpha;
return id;
}
|
CHAR:ch
%target.cpp {
string temp;
temp += ch->GetChar();
Ast::Id *id = new Ast::Id(temp, FiLoc::ms_invalid);
delete ch;
return id;
}
;
%nonterminal integer %type.cpp "Ast::SignedInteger *"
:
integer:integer DIGIT:digit
%target.cpp {
integer->ShiftAndAdd(digit->GetChar() - '0');
if (integer->Value() > 255)
integer->SetValue(255);
delete digit;
return integer;
}
|
DIGIT:digit
%target.cpp {
Ast::SignedInteger *integer = new Ast::SignedInteger(digit->GetChar() - '0', FiLoc::ms_invalid);
delete digit;
return integer;
}
;