-
Notifications
You must be signed in to change notification settings - Fork 0
/
barf_commonlang_scanner.reflex
778 lines (721 loc) · 26.5 KB
/
barf_commonlang_scanner.reflex
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
771
772
773
774
775
776
777
778
// 2006.11.09 - Copyright Victor Dods - Licensed under Apache 2.0
%targets cpp
/* ***************************************************************************
/* cpp scanner header-file-related directives
/* **************************************************************************/
%target.cpp.header_filename "barf_commonlang_scanner.hpp"
%target.cpp.implementation_filename "barf_commonlang_scanner.cpp"
%target.cpp.top_of_header_file %{
#if !defined(BARF_COMMONLANG_SCANNER_HPP_)
#define BARF_COMMONLANG_SCANNER_HPP_
#include "barf_commonlang.hpp"
#include <ostream>
#include "barf_inputbase.hpp"
namespace Barf {
namespace Ast {
class Base;
} // end of namespace Ast
namespace CommonLang {
%}
%target.cpp.class_name Scanner
%target.cpp.class_inheritance { protected InputBase }
%target.cpp.top_of_class {
struct Token
{
enum Type
{
BAD_END_OF_FILE = 0x100,
BAD_TOKEN,
CHAR_LITERAL,
DIRECTIVE_ADD_CODESPEC,
DIRECTIVE_ADD_OPTIONAL_DIRECTIVE,
DIRECTIVE_ADD_REQUIRED_DIRECTIVE,
DIRECTIVE_CASE_INSENSITIVE,
DIRECTIVE_DEFAULT,
DIRECTIVE_DEFAULT_PARSE_NONTERMINAL,
DIRECTIVE_DUMB_CODE_BLOCK,
DIRECTIVE_EMPTY,
DIRECTIVE_END,
DIRECTIVE_ERROR,
DIRECTIVE_ID,
DIRECTIVE_LOOKAHEAD,
DIRECTIVE_MACRO,
DIRECTIVE_NONTERMINAL,
DIRECTIVE_PREC,
DIRECTIVE_START_IN_STATE_MACHINE,
DIRECTIVE_STATE_MACHINE,
DIRECTIVE_STRICT_CODE_BLOCK,
DIRECTIVE_STRING,
DIRECTIVE_TARGET,
DIRECTIVE_TARGETS,
DIRECTIVE_TERMINAL,
DIRECTIVE_TYPE,
DIRECTIVE_UNGREEDY,
DUMB_CODE_BLOCK,
END_OF_FILE,
END_PREAMBLE,
ID,
NEWLINE,
REGEX,
STRICT_CODE_BLOCK,
STRING_LITERAL,
COUNT_PLUS_0x100
}; // end of enum Scanner::Token::Type
}; // end of struct Scanner::Token
}
%target.cpp.scan_method_parameters { Ast::Base *&token }
%target.cpp.bottom_of_class {
using InputBase::IsOpen;
using InputBase::GetFiLoc;
using InputBase::InputName;
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;
private:
Token::Type ParseDirective (string const &accepted_string, Ast::Base *&token);
bool m_is_in_preamble;
Uint32 m_regex_paren_level;
Uint32 m_regex_bracket_level;
Uint32 m_code_block_bracket_level;
StateMachine::Name m_return_state;
}
%target.cpp.bottom_of_header_file %{
ostream &operator << (ostream &stream, Scanner::Token::Type scanner_token_type);
} // end of namespace CommonLang
} // end of namespace Barf
#endif // !defined(BARF_COMMONLANG_SCANNER_HPP_)
%}
/* ***************************************************************************
/* cpp scanner implementation-file-related directives
/* **************************************************************************/
%target.cpp.top_of_implementation_file %{
#include <cstdlib>
#include <sstream>
#include "barf_ast.hpp"
#include "barf_util.hpp"
namespace Barf {
namespace CommonLang {
ostream &operator << (ostream &stream, Scanner::Token::Type scanner_token_type)
{
static string const s_scanner_token_type_string[Scanner::Token::COUNT_PLUS_0x100 - 0x100] =
{
"BAD_END_OF_FILE",
"BAD_TOKEN",
"CHAR_LITERAL",
"DIRECTIVE_ADD_CODESPEC",
"DIRECTIVE_ADD_OPTIONAL_DIRECTIVE",
"DIRECTIVE_ADD_REQUIRED_DIRECTIVE",
"DIRECTIVE_CASE_INSENSITIVE",
"DIRECTIVE_DEFAULT",
"DIRECTIVE_DEFAULT_PARSE_NONTERMINAL",
"DIRECTIVE_DUMB_CODE_BLOCK",
"DIRECTIVE_EMPTY",
"DIRECTIVE_END",
"DIRECTIVE_ERROR",
"DIRECTIVE_ID",
"DIRECTIVE_LOOKAHEAD",
"DIRECTIVE_MACRO",
"DIRECTIVE_NONTERMINAL",
"DIRECTIVE_PREC",
"DIRECTIVE_START_IN_STATE_MACHINE",
"DIRECTIVE_STATE_MACHINE",
"DIRECTIVE_STRICT_CODE_BLOCK",
"DIRECTIVE_STRING",
"DIRECTIVE_TARGET",
"DIRECTIVE_TARGETS",
"DIRECTIVE_TERMINAL",
"DIRECTIVE_TYPE",
"DIRECTIVE_UNGREEDY",
"DUMB_CODE_BLOCK",
"END_OF_FILE",
"END_PREAMBLE",
"ID",
"NEWLINE",
"REGEX",
"STRICT_CODE_BLOCK",
"STRING_LITERAL"
};
if (scanner_token_type < 0x100)
{
assert(scanner_token_type >= 0);
return stream << CharLiteral(Uint8(scanner_token_type));
}
else
{
assert(scanner_token_type < Scanner::Token::COUNT_PLUS_0x100);
return stream << s_scanner_token_type_string[scanner_token_type - 0x100];
}
}
bool Scanner::OpenFile (string const &input_filename)
{
bool open_success = InputBase::OpenFile(input_filename);
if (open_success)
{
ResetForNewInput();
IstreamIterator(istream_iterator<char>(In()));
// unlimited readahead (read the whole file immediately)
InputReadahead(0);
}
return open_success;
}
void Scanner::OpenString (string const &input_string, string const &input_name, bool use_line_numbers)
{
InputBase::OpenString(input_string, input_name, use_line_numbers);
ResetForNewInput();
IstreamIterator(istream_iterator<char>(In()));
// unlimited readahead (read the whole file immediately)
InputReadahead(0);
}
void Scanner::OpenUsingStream (istream *input_stream, string const &input_name, bool use_line_numbers)
{
InputBase::OpenUsingStream(input_stream, input_name, use_line_numbers);
ResetForNewInput();
IstreamIterator(istream_iterator<char>(In()));
// unlimited readahead (read the whole file immediately)
InputReadahead(0);
}
Scanner::Token::Type Scanner::ParseDirective (string const &accepted_string, Ast::Base *&token)
{
assert(token == NULL);
if (accepted_string == "%add_codespec") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_ADD_CODESPEC; }
if (accepted_string == "%add_optional_directive") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_ADD_OPTIONAL_DIRECTIVE; }
if (accepted_string == "%add_required_directive") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_ADD_REQUIRED_DIRECTIVE; }
if (accepted_string == "%case_insensitive") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_CASE_INSENSITIVE; }
if (accepted_string == "%default") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_DEFAULT; }
if (accepted_string == "%default_parse_nonterminal") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_DEFAULT_PARSE_NONTERMINAL; }
if (accepted_string == "%dumb_code_block") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_DUMB_CODE_BLOCK; }
if (accepted_string == "%empty") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_EMPTY; }
if (accepted_string == "%end") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_END; }
if (accepted_string == "%error") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_ERROR; }
if (accepted_string == "%identifier") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_ID; }
if (accepted_string == "%lookahead") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_LOOKAHEAD; }
if (accepted_string == "%macro") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_MACRO; }
if (accepted_string == "%nonterminal") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_NONTERMINAL; }
if (accepted_string == "%prec") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_PREC; }
if (accepted_string == "%start_with_state_machine") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_START_IN_STATE_MACHINE; }
if (accepted_string == "%state_machine") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_STATE_MACHINE; }
if (accepted_string == "%strict_code_block") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_STRICT_CODE_BLOCK; }
if (accepted_string == "%string") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_STRING; }
if (accepted_string == "%target") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_TARGET; }
if (accepted_string == "%targets") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_TARGETS; }
if (accepted_string == "%terminal") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_TERMINAL; }
if (accepted_string == "%type") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_TYPE; }
if (accepted_string == "%ungreedy") { token = new Ast::ThrowAway(GetFiLoc()); return Token::DIRECTIVE_UNGREEDY; }
EmitError("invalid directive \"" + accepted_string + "\"", GetFiLoc());
return Token::BAD_TOKEN;
}
%}
%target.cpp.top_of_scan_method_actions %{
assert(token == NULL);
%}
%target.cpp.bottom_of_scan_method_actions %{
assert(false && "you didn't handle EOF properly");
return Token::BAD_END_OF_FILE;
%}
%target.cpp.bottom_of_implementation_file %{
} // end of namespace CommonLang
} // end of namespace Barf
%}
/* ***************************************************************************
/* cpp scanner I/O parameters
/* **************************************************************************/
%target.cpp.return_type "Scanner::Token::Type"
%target.cpp.rejection_actions {
EmitError("unrecognized character " + CharLiteral(rejected_atom), GetFiLoc());
}
%target.cpp.reset_for_new_input_actions {
m_is_in_preamble = true;
m_regex_paren_level = 0;
m_code_block_bracket_level = 0;
}
/* ***************************************************************************
/* cpp scanner misc directives
/* **************************************************************************/
%target.cpp.generate_debug_spew_code
%target.cpp.debug_spew_prefix {"CommonLang::Scanner" << (GetFiLoc().IsValid() ? " ("+GetFiLoc().AsString()+")" : g_empty_string) << ":"}
/* ***************************************************************************
/* target-independent scanner directives
/* **************************************************************************/
// a single octal (base 8) digit
%macro OCT_DIGIT ([0-7])
// an octal escape char (e.g. \42)
%macro OCT_CHAR (\\{OCT_DIGIT}+)
// a single hexadecimal (base 16) digit
%macro HEX_DIGIT ([0-9A-Fa-f])
// a hexadecimal escape char (e.g. \xF9)
%macro HEX_CHAR (\\x{HEX_DIGIT}+)
// an escaped char literal (e.g. '\t', '\0' or even '\j')
%macro CHAR_ESC_CHAR (\\[[:print:]])
// everything that doesn't need to be escaped in a char literal (e.g. 'p')
%macro CHAR_NORMAL_CHAR ([^[:cntrl:]"'\\])
// single-char escape codes for a string literal (e.g. \n is newline)
%macro STRING_ESC_CHAR (\\[[:print:]])
// malformed single-char escape codes for a string literal (e.g. a backslash followed by a tab)
%macro STRING_BAD_ESC_CHAR (\\[^[:print:]])
// everything that doesn't need to be escaped in a string literal
%macro STRING_NORMAL_CHAR ([^[:cntrl:]"\\]|\t|\n)
// id (same as C-style, but can't start or end with an underscore)
%macro ID ([A-Za-z]([_A-Za-z0-9]*[A-Za-z0-9])?)
// all operators that should be returned by ascii value
%macro OPERATOR ([.:;|\[\]!])
// non-newline whitespace
%macro WHITESPACE ([ \t])
// newline
%macro NEWLINE (\n)
// any char
%macro ANY (\n|.)
// end-of-file condition
%macro END_OF_FILE (\z)
%start_with_state_machine MAIN
%%
/* ***************************************************************************
/* state machines and constituent regex rules
/* **************************************************************************/
%state_machine MAIN
:
(/[*]) // opening delimiter for a block comment
%target.cpp {
m_return_state = StateMachine::MAIN;
SwitchToStateMachine(StateMachine::BLOCK_COMMENT);
}
|
(//.*) // line comment (not including the newline)
%target.cpp { }
|
(') // opening quote for a char literal
%target.cpp {
SwitchToStateMachine(StateMachine::CHAR_LITERAL_GUTS);
}
|
(") // opening quote for a string literal
%target.cpp {
token = new Ast::String(GetFiLoc());
SwitchToStateMachine(StateMachine::STRING_LITERAL_GUTS);
}
|
(\() // opening delimiter for a regular expression
%target.cpp {
token = new Ast::String(GetFiLoc());
SwitchToStateMachine(StateMachine::REGULAR_EXPRESSION);
}
|
(%\{) // opening delimiter for a dumb code block
%target.cpp {
token = new Ast::DumbCodeBlock(GetFiLoc());
SwitchToStateMachine(StateMachine::DUMB_CODE_BLOCK);
}
|
(\{) // opening delimiter for a strict code block
%target.cpp {
token = new Ast::StrictCodeBlock(GetFiLoc());
SwitchToStateMachine(StateMachine::STRICT_CODE_BLOCK);
}
|
(%{ID}) // a directive
%target.cpp {
return Scanner::ParseDirective(accepted_string, token);
}
|
(%%)
%target.cpp {
token = new Ast::ThrowAway(GetFiLoc());
m_is_in_preamble = false;
return Token::END_PREAMBLE;
}
|
({ID})
%target.cpp {
token = new Ast::Id(accepted_string, GetFiLoc());
return Token::ID;
}
|
({OPERATOR})
%target.cpp {
return Token::Type(accepted_string[0]);
}
|
({WHITESPACE}) %target.cpp { /* ignore all whitespace */ }
|
({NEWLINE})
%target.cpp {
IncrementLineNumber();
if (m_is_in_preamble)
return Token::NEWLINE;
}
|
({END_OF_FILE})
%target.cpp {
return Token::END_OF_FILE;
}
|
(.)
%target.cpp {
EmitError("unexpected character " + CharLiteral(accepted_string[0]), GetFiLoc());
return Token::BAD_TOKEN;
}
;
%state_machine BLOCK_COMMENT %ungreedy
:
({ANY}*[*]/) // everything up through the closing delimiter of a block comment
%target.cpp {
IncrementLineNumber(NewlineCount(accepted_string));
if (token != NULL)
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
SwitchToStateMachine(m_return_state);
}
|
({ANY}*{END_OF_FILE}) // everything up through EOF
%target.cpp {
IncrementLineNumber(NewlineCount(accepted_string));
EmitWarning("unterminated block comment", GetFiLoc());
delete token;
token = NULL;
return Token::END_OF_FILE;
}
;
%state_machine CHAR_LITERAL_GUTS
:
({OCT_CHAR})
%target.cpp {
assert(accepted_string.length() >= 2);
assert(accepted_string[0] == '\\');
Uint32 value = strtol(accepted_string.c_str()+1, NULL, 8);
if (value >= 0x100)
EmitError("octal character literal value out of range (" + accepted_string + ")", GetFiLoc());
token = new Ast::Char(Uint8(value), GetFiLoc());
SwitchToStateMachine(StateMachine::CHAR_LITERAL_END);
}
|
({HEX_CHAR})
%target.cpp {
assert(accepted_string.length() >= 3);
assert(accepted_string[0] == '\\');
assert(accepted_string[1] == 'x');
Uint32 value = strtol(accepted_string.c_str()+2, NULL, 16);
if (value >= 0x100)
EmitError("hexadecimal character literal value out of range (" + accepted_string + ")", GetFiLoc());
token = new Ast::Char(Uint8(value), GetFiLoc());
SwitchToStateMachine(StateMachine::CHAR_LITERAL_END);
}
|
({CHAR_ESC_CHAR})
%target.cpp {
assert(accepted_string.length() == 2);
assert(accepted_string[0] == '\\');
Ast::Char *ch = new Ast::Char(Uint8(accepted_string[1]), GetFiLoc());
ch->Escape();
token = ch;
SwitchToStateMachine(StateMachine::CHAR_LITERAL_END);
}
|
({CHAR_NORMAL_CHAR})
%target.cpp {
assert(accepted_string.length() == 1);
token = new Ast::Char(Uint8(accepted_string[0]), GetFiLoc());
SwitchToStateMachine(StateMachine::CHAR_LITERAL_END);
}
|
(\\?{END_OF_FILE}) // end of file (which may be preceded by a backslash)
%target.cpp {
EmitError("unterminated character literal", GetFiLoc());
return Token::END_OF_FILE;
}
|
({ANY}) // anything else just means it's malformed
%target.cpp {
EmitError("unexpected character " + CharLiteral(accepted_string[0]) + " in character literal", GetFiLoc());
if (accepted_string[0] == '\n')
IncrementLineNumber();
token = new Ast::Char(Uint8(accepted_string[0]), GetFiLoc());
SwitchToStateMachine(StateMachine::CHAR_LITERAL_END);
}
;
%state_machine CHAR_LITERAL_END
:
(') // closing single-quote
%target.cpp {
assert(token != NULL);
SwitchToStateMachine(StateMachine::MAIN);
return Token::CHAR_LITERAL;
}
|
(\\?{END_OF_FILE}) // end of file (which may be preceded by a backslash)
%target.cpp {
EmitError("unterminated character literal", GetFiLoc());
assert(token != NULL);
delete token;
token = NULL;
SwitchToStateMachine(StateMachine::MAIN);
return Token::END_OF_FILE;
}
|
({ANY}) // anything else just means it's malformed
%target.cpp {
EmitError("malformed character literal", GetFiLoc());
if (accepted_string[0] == '\n')
IncrementLineNumber();
assert(token != NULL);
delete token;
token = NULL;
SwitchToStateMachine(StateMachine::MAIN);
return Token::BAD_TOKEN;
}
;
%state_machine STRING_LITERAL_GUTS %ungreedy
:
(([^\\]|\\{ANY})*") // everything up through the first unescaped double quote
%target.cpp {
assert(token != NULL);
IncrementLineNumber(NewlineCount(accepted_string));
// get rid of the trailing endquote
assert(accepted_string.length() >= 1);
accepted_string.resize(accepted_string.length()-1);
// escape the string in-place and handle the return code
EscapeStringStatus status = EscapeString(accepted_string);
switch (status.m_return_code)
{
case ESRC_SUCCESS:
// awesome
break;
case ESRC_UNEXPECTED_EOI:
assert(false && "the formulation of the regex should prevent this");
break;
case ESRC_MALFORMED_HEX_CHAR:
EmitError(
"\\x with no trailing hex digits",
FiLoc(token->GetFiLoc().Filename(),
token->GetFiLoc().LineNumber() + status.m_line_number_offset));
break;
case ESRC_HEX_ESCAPE_SEQUENCE_OUT_OF_RANGE:
case ESRC_OCTAL_ESCAPE_SEQUENCE_OUT_OF_RANGE:
EmitError(
"hex/octal escape sequence out of range",
FiLoc(token->GetFiLoc().Filename(),
token->GetFiLoc().LineNumber() + status.m_line_number_offset));
break;
}
Dsc<Ast::String *>(token)->AppendText(accepted_string);
SwitchToStateMachine(StateMachine::MAIN);
return Token::STRING_LITERAL;
}
|
(([^\\]|\\{ANY})*\\?{END_OF_FILE}) // everything up through EOF (which may be preceded by a hanging backslash)
%target.cpp {
EmitError("unterminated string literal", GetFiLoc());
IncrementLineNumber(NewlineCount(accepted_string));
assert(token != NULL);
delete token;
token = NULL;
return Token::END_OF_FILE;
}
;
%state_machine REGULAR_EXPRESSION
:
(\)) // closing delimiter for a regular expression
%target.cpp {
assert(token != NULL);
if (m_regex_paren_level == 0)
{
SwitchToStateMachine(StateMachine::MAIN);
return Token::REGEX;
}
--m_regex_paren_level;
Dsc<Ast::String *>(token)->AppendText(accepted_string);
}
|
(\() // opening delimeter for a sub-expression
%target.cpp {
assert(token != NULL);
++m_regex_paren_level;
Dsc<Ast::String *>(token)->AppendText(accepted_string);
}
|
(\[) // opening delimiter for a bracket expression
%target.cpp {
assert(token != NULL);
Dsc<Ast::String *>(token)->AppendText(accepted_string);
m_regex_bracket_level = 0;
SwitchToStateMachine(StateMachine::REGULAR_EXPRESSION_BRACKET_EXPRESSION);
}
|
(([^()\[\]\\]|\\{ANY})+) // any not-()[]-or-backslash, or any escaped char
%target.cpp {
assert(token != NULL);
IncrementLineNumber(NewlineCount(accepted_string));
Dsc<Ast::String *>(token)->AppendText(accepted_string);
}
|
(\\?{END_OF_FILE}) // end of file (which may be preceded by a backslash)
%target.cpp {
assert(token != NULL);
delete token;
token = NULL;
EmitError("unterminated regular expression", GetFiLoc());
return Token::END_OF_FILE;
}
;
%state_machine REGULAR_EXPRESSION_BRACKET_EXPRESSION
:
(\]) // closing delimiter for bracket expression (or char class)
%target.cpp {
assert(token != NULL);
Dsc<Ast::String *>(token)->AppendText(accepted_string);
if (m_regex_bracket_level == 0)
SwitchToStateMachine(StateMachine::REGULAR_EXPRESSION);
else
--m_regex_bracket_level;
}
|
(\[) // opening delimiter for char class
%target.cpp {
assert(token != NULL);
Dsc<Ast::String *>(token)->AppendText(accepted_string);
++m_regex_bracket_level;
}
|
(([^\[\]\\]|\\{ANY})+) // any non-delimiter non-backslash or any escaped char
%target.cpp {
assert(token != NULL);
IncrementLineNumber(NewlineCount(accepted_string));
Dsc<Ast::String *>(token)->AppendText(accepted_string);
}
|
(\\?{END_OF_FILE}) // end of file (which may be preceded by a backslash)
%target.cpp {
assert(token != NULL);
delete token;
token = NULL;
EmitError("unterminated bracket expression inside regular expression", GetFiLoc());
return Token::END_OF_FILE;
}
;
%state_machine DUMB_CODE_BLOCK %ungreedy
:
({ANY}*%\}) // everything up through the closing %}
%target.cpp {
assert(token != NULL);
assert(accepted_string.length() >= 2);
// take off the %} at the end
accepted_string.resize(accepted_string.length()-2);
IncrementLineNumber(NewlineCount(accepted_string));
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
SwitchToStateMachine(StateMachine::MAIN);
return Token::DUMB_CODE_BLOCK;
}
|
({ANY}*{END_OF_FILE}) // everything up through EOF
%target.cpp {
EmitError("unterminated dumb code block (looking for %} delimiter)", GetFiLoc());
IncrementLineNumber(NewlineCount(accepted_string));
assert(token != NULL);
delete token;
token = NULL;
return Token::END_OF_FILE;
}
;
%state_machine STRICT_CODE_BLOCK
:
(\}) // closing delimiter for a code block
%target.cpp {
assert(token != NULL);
if (m_code_block_bracket_level == 0)
{
SwitchToStateMachine(StateMachine::MAIN);
return Token::STRICT_CODE_BLOCK;
}
--m_code_block_bracket_level;
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
}
|
(\{) // opening delimiter for a code block
%target.cpp {
assert(token != NULL);
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
++m_code_block_bracket_level;
}
|
(') // opening quote for a char literal
%target.cpp {
assert(token != NULL);
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
SwitchToStateMachine(StateMachine::CHAR_LITERAL_INSIDE_STRICT_CODE_BLOCK);
}
|
(") // opening quote for a string literal
%target.cpp {
assert(token != NULL);
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
SwitchToStateMachine(StateMachine::STRING_LITERAL_INSIDE_STRICT_CODE_BLOCK);
}
|
(/[*]) // opening delimiter for a block comment
%target.cpp {
assert(token != NULL);
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
m_return_state = StateMachine::STRICT_CODE_BLOCK;
SwitchToStateMachine(StateMachine::BLOCK_COMMENT);
}
|
(//.*) // line comment (not including the newline)
%target.cpp {
assert(token != NULL);
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
}
|
([^{}'"/]+|.) // any non-delimiter char or just any single char
%target.cpp {
assert(token != NULL);
IncrementLineNumber(NewlineCount(accepted_string));
Dsc<Ast::CodeBlock *>(token)->AppendText(accepted_string);
}
|
({END_OF_FILE}) // end of file
%target.cpp {
assert(token != NULL);
delete token;
token = NULL;
EmitError("unterminated strict code block", GetFiLoc());
return Token::END_OF_FILE;
}
;
%state_machine CHAR_LITERAL_INSIDE_STRICT_CODE_BLOCK %ungreedy
:
(([^\\]|\\{ANY})*') // everything up through the first unescaped single quote
%target.cpp {
assert(token != NULL);
IncrementLineNumber(NewlineCount(accepted_string));
Dsc<Ast::StrictCodeBlock *>(token)->AppendText(accepted_string);
SwitchToStateMachine(StateMachine::STRICT_CODE_BLOCK);
}
|
(([^\\]|\\{ANY})*\\?{END_OF_FILE}) // everything up through EOF (which may be preceded by a hanging backslash)
%target.cpp {
assert(token != NULL);
delete token;
token = NULL;
EmitError("unterminated character literal in strict code block", GetFiLoc());
IncrementLineNumber(NewlineCount(accepted_string));
return Token::END_OF_FILE;
}
;
%state_machine STRING_LITERAL_INSIDE_STRICT_CODE_BLOCK %ungreedy
:
(([^\\]|\\{ANY})*") // everything up through the first unescaped double quote
%target.cpp {
assert(token != NULL);
Dsc<Ast::StrictCodeBlock *>(token)->AppendText(accepted_string);
IncrementLineNumber(NewlineCount(accepted_string));
SwitchToStateMachine(StateMachine::STRICT_CODE_BLOCK);
}
|
(([^\\]|\\{ANY})*\\?{END_OF_FILE}) // everything up through EOF (which may be preceded by a hanging backslash)
%target.cpp {
assert(token != NULL);
delete token;
token = NULL;
EmitError("unterminated string literal in strict code block", GetFiLoc());
IncrementLineNumber(NewlineCount(accepted_string));
return Token::END_OF_FILE;
}
;