This repository was archived by the owner on Jun 15, 2019. It is now read-only.
forked from zaach/lex-parser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlex.y
912 lines (793 loc) · 26.8 KB
/
lex.y
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
%code imports %{
import XRegExp from '@gerhobbelt/xregexp'; // for helping out the `%options xregexp` in the lexer
import helpers from 'jison-helpers-lib';
import fs from 'fs';
%}
%start lex
/* Jison lexer file format grammar */
%nonassoc '/' '/!'
%left '*' '+' '?' RANGE_REGEX
%left '|'
%left '('
%%
lex
: init definitions rules_and_epilogue EOF
{
$$ = $rules_and_epilogue;
$$.macros = $definitions.macros;
$$.startConditions = $definitions.startConditions;
$$.unknownDecls = $definitions.unknownDecls;
// if there are any options, add them all, otherwise set options to NULL:
// can't check for 'empty object' by `if (yy.options) ...` so we do it this way:
for (var k in yy.options) {
$$.options = yy.options;
break;
}
if (yy.actionInclude) {
var asrc = yy.actionInclude.join('\n\n');
// Only a non-empty action code chunk should actually make it through:
if (asrc.trim() !== '') {
$$.actionInclude = asrc;
}
}
delete yy.options;
delete yy.actionInclude;
return $$;
}
| init definitions error EOF
{
yyerror(rmCommonWS`
There's an error in your lexer regex rules or epilogue.
Maybe you did not correctly separate the lexer sections with a '%%'
on an otherwise empty line?
The lexer spec file should have this structure:
definitions
%%
rules
%% // <-- optional!
extra_module_code // <-- optional epilogue!
Erroneous code:
${yylexer.prettyPrintRange(@error)}
Technical error report:
${$error.errStr}
`);
}
;
rules_and_epilogue
: '%%' rules '%%' extra_lexer_module_code
{
if ($extra_lexer_module_code.trim() !== '') {
$$ = { rules: $rules, moduleInclude: $extra_lexer_module_code };
} else {
$$ = { rules: $rules };
}
}
| '%%' error rules '%%' extra_lexer_module_code
{
yyerror(rmCommonWS`
There's probably an error in one or more of your lexer regex rules.
The lexer rule spec should have this structure:
regex action_code
where 'regex' is a lex-style regex expression (see the
jison and jison-lex documentation) which is intended to match a chunk
of the input to lex, while the 'action_code' block is the JS code
which will be invoked when the regex is matched. The 'action_code' block
may be any (indented!) set of JS statements, optionally surrounded
by '{...}' curly braces or otherwise enclosed in a '%{...%}' block.
Erroneous code:
${yylexer.prettyPrintRange(@error)}
Technical error report:
${$error.errStr}
`);
}
| '%%' rules '%%' error
{
yyerror(rmCommonWS`
There's an error in your lexer epilogue a.k.a. 'extra_module_code' block.
Erroneous code:
${yylexer.prettyPrintRange(@error)}
Technical error report:
${$error.errStr}
`);
}
| '%%' error rules
{
yyerror(rmCommonWS`
There's probably an error in one or more of your lexer regex rules.
The lexer rule spec should have this structure:
regex action_code
where 'regex' is a lex-style regex expression (see the
jison and jison-lex documentation) which is intended to match a chunk
of the input to lex, while the 'action_code' block is the JS code
which will be invoked when the regex is matched. The 'action_code' block
may be any (indented!) set of JS statements, optionally surrounded
by '{...}' curly braces or otherwise enclosed in a '%{...%}' block.
Erroneous code:
${yylexer.prettyPrintRange(@error)}
Technical error report:
${$error.errStr}
`);
}
| '%%' rules
/* Note: an empty rules set is allowed when you are setting up an `%options custom_lexer` */
{
$$ = { rules: $rules };
}
| ε
/* Note: an empty rules set is allowed when you are setting up an `%options custom_lexer` */
{
$$ = { rules: [] };
}
;
// because JISON doesn't support mid-rule actions,
// we set up `yy` using this empty rule at the start:
init
: ε
{
yy.actionInclude = [];
if (!yy.options) yy.options = {};
}
;
definitions
: definitions definition
{
$$ = $definitions;
if ($definition != null) {
if ('length' in $definition) {
$$.macros[$definition[0]] = $definition[1];
} else if ($definition.type === 'names') {
for (var name in $definition.names) {
$$.startConditions[name] = $definition.names[name];
}
} else if ($definition.type === 'unknown') {
$$.unknownDecls.push($definition.body);
}
}
}
| ε
{
$$ = {
macros: {}, // { hash table }
startConditions: {}, // { hash table }
unknownDecls: [] // [ array of [key,value] pairs }
};
}
;
definition
: NAME regex
{ $$ = [$NAME, $regex]; }
| START_INC names_inclusive
{ $$ = $names_inclusive; }
| START_EXC names_exclusive
{ $$ = $names_exclusive; }
| action
{
var rv = checkActionBlock($action, @action);
if (rv) {
yyerror(rmCommonWS`
The '%{...%}' lexer setup action code section does not compile: ${rv}
Erroneous area:
${yylexer.prettyPrintRange(@action)}
`);
}
yy.actionInclude.push($action);
$$ = null;
}
| options
{ $$ = null; }
| UNKNOWN_DECL
{ $$ = {type: 'unknown', body: $1}; }
| IMPORT import_name import_path
{ $$ = {type: 'imports', name: $import_name, path: $import_path}; }
| IMPORT import_name error
{
yyerror(rmCommonWS`
You did not specify a legal file path for the '%import' initialization code statement, which must have the format:
%import qualifier_name file_path
Erroneous code:
${yylexer.prettyPrintRange(@error, @IMPORT)}
Technical error report:
${$error.errStr}
`);
}
| IMPORT error
{
yyerror(rmCommonWS`
%import name or source filename missing maybe?
Note: each '%import'-ed initialization code section must be qualified by a name, e.g. 'required' before the import path itself:
%import qualifier_name file_path
Erroneous code:
${yylexer.prettyPrintRange(@error, @IMPORT)}
Technical error report:
${$error.errStr}
`);
}
| INIT_CODE init_code_name action
{
var rv = checkActionBlock($action, @action);
if (rv) {
yyerror(rmCommonWS`
The '%code ${$init_code_name}' action code section does not compile: ${rv}
Erroneous area:
${yylexer.prettyPrintRange(@action, @INIT_CODE)}
`);
}
$$ = {
type: 'codesection',
qualifier: $init_code_name,
include: $action
};
}
| INIT_CODE error action
{
yyerror(rmCommonWS`
Each '%code' initialization code section must be qualified by a name, e.g. 'required' before the action code itself:
%code qualifier_name {action code}
Erroneous code:
${yylexer.prettyPrintRange(@error, @INIT_CODE, @action)}
Technical error report:
${$error.errStr}
`);
}
;
init_code_name
: NAME
{ $$ = $NAME; }
| STRING_LIT
{ $$ = $STRING_LIT; }
;
import_name
: NAME
{ $$ = $NAME; }
| STRING_LIT
{ $$ = $STRING_LIT; }
;
import_path
: NAME
{ $$ = $NAME; }
| STRING_LIT
{ $$ = $STRING_LIT; }
;
names_inclusive
: START_COND
{ $$ = {type: 'names', names: {}}; $$.names[$START_COND] = 0; }
| names_inclusive START_COND
{ $$ = $names_inclusive; $$.names[$START_COND] = 0; }
;
names_exclusive
: START_COND
{ $$ = {type: 'names', names: {}}; $$.names[$START_COND] = 1; }
| names_exclusive START_COND
{ $$ = $names_exclusive; $$.names[$START_COND] = 1; }
;
rules
: rules rules_collective
{ $$ = $rules.concat($rules_collective); }
| ε
{ $$ = []; }
;
rules_collective
: start_conditions rule
{
if ($start_conditions) {
$rule.unshift($start_conditions);
}
$$ = [$rule];
}
| start_conditions '{' rule_block '}'
{
if ($start_conditions) {
$rule_block.forEach(function (d) {
d.unshift($start_conditions);
});
}
$$ = $rule_block;
}
| start_conditions '{' error '}'
{
yyerror(rmCommonWS`
Seems you made a mistake while specifying one of the lexer rules inside
the start condition
<${$start_conditions.join(',')}> { rules... }
block.
Erroneous area:
${yylexer.prettyPrintRange(yylexer.mergeLocationInfo(##start_conditions, ##4), @start_conditions)}
Technical error report:
${$error.errStr}
`);
}
| start_conditions '{' error
{
yyerror(rmCommonWS`
Seems you did not correctly bracket a lexer rules set inside
the start condition
<${$start_conditions.join(',')}> { rules... }
as a terminating curly brace '}' could not be found.
Erroneous area:
${yylexer.prettyPrintRange(@error, @start_conditions)}
Technical error report:
${$error.errStr}
`);
}
;
rule_block
: rule_block rule
{ $$ = $rule_block; $$.push($rule); }
| ε
{ $$ = []; }
;
rule
: regex action
{
var rv = checkActionBlock($action, @action);
if (rv) {
yyerror(rmCommonWS`
The rule's action code section does not compile: ${rv}
Erroneous area:
${yylexer.prettyPrintRange(@action)}
`);
}
$$ = [$regex, $action];
}
| regex error
{
$$ = [$regex, $error];
yyerror(rmCommonWS`
Lexer rule regex action code declaration error?
Erroneous code:
${yylexer.prettyPrintRange(@error, @regex)}
Technical error report:
${$error.errStr}
`);
}
;
action
: ACTION_START action_body BRACKET_MISSING
{
yyerror(rmCommonWS`
Missing curly braces: seems you did not correctly bracket a lexer rule action block in curly braces: '{ ... }'.
Offending action body:
${yylexer.prettyPrintRange(@BRACKET_MISSING, @1)}
`);
}
| ACTION_START action_body BRACKET_SURPLUS
{
yyerror(rmCommonWS`
Too many curly braces: seems you did not correctly bracket a lexer rule action block in curly braces: '{ ... }'.
Offending action body:
${yylexer.prettyPrintRange(@BRACKET_SURPLUS, @1)}
`);
}
| ACTION_START action_body ACTION_END
{
var s = $action_body.trim();
// remove outermost set of braces UNLESS there's
// a curly brace in there anywhere: in that case
// we should leave it up to the sophisticated
// code analyzer to simplify the code!
//
// This is a very rough check as it will also look
// inside code comments, which should not have
// any influence.
//
// Nevertheless: this is a *safe* transform!
if (s[0] === '{' && s.indexOf('}') === s.length - 1) {
$$ = s.substring(1, s.length - 1).trim();
} else {
$$ = s;
}
}
;
action_body
: action_body ACTION
{ $$ = $action_body + '\n\n' + $ACTION + '\n\n'; }
| action_body ACTION_BODY
{ $$ = $action_body + $ACTION_BODY; }
| action_body ACTION_BODY_C_COMMENT
{ $$ = $action_body + $ACTION_BODY_C_COMMENT; }
| action_body ACTION_BODY_CPP_COMMENT
{ $$ = $action_body + $ACTION_BODY_CPP_COMMENT; }
| action_body ACTION_BODY_WHITESPACE
{ $$ = $action_body + $ACTION_BODY_WHITESPACE; }
| action_body include_macro_code
{ $$ = $action_body + '\n\n' + $include_macro_code + '\n\n'; }
| action_body INCLUDE_PLACEMENT_ERROR
{
yyerror(rmCommonWS`
You may place the '%include' instruction only at the start/front of a line.
It's use is not permitted at this position:
${yylexer.prettyPrintRange(@INCLUDE_PLACEMENT_ERROR, @action_body)}
`);
}
| action_body error
{
yyerror(rmCommonWS`
Seems you did not correctly match curly braces '{ ... }' in a lexer rule action block.
Erroneous code:
${yylexer.prettyPrintRange(@error, @action_body)}
Technical error report:
${$error.errStr}
`);
}
| ε
{ $$ = ''; }
;
start_conditions
: '<' name_list '>'
{ $$ = $name_list; }
| '<' name_list error
{
yyerror(rmCommonWS`
Seems you did not correctly terminate the start condition set <${$name_list.join(',')},???> with a terminating '>'
Erroneous code:
${yylexer.prettyPrintRange(@error, @1)}
Technical error report:
${$error.errStr}
`);
}
| '<' '*' '>'
{ $$ = ['*']; }
| ε
;
name_list
: NAME
{ $$ = [$NAME]; }
| name_list ',' NAME
{ $$ = $name_list; $$.push($NAME); }
;
regex
: nonempty_regex_list[re]
{
// Detect if the regex ends with a pure (Unicode) word;
// we *do* consider escaped characters which are 'alphanumeric'
// to be equivalent to their non-escaped version, hence these are
// all valid 'words' for the 'easy keyword rules' option:
//
// - hello_kitty
// - γεια_σου_γατούλα
// - \u03B3\u03B5\u03B9\u03B1_\u03C3\u03BF\u03C5_\u03B3\u03B1\u03C4\u03BF\u03CD\u03BB\u03B1
//
// http://stackoverflow.com/questions/7885096/how-do-i-decode-a-string-with-escaped-unicode#12869914
//
// As we only check the *tail*, we also accept these as
// 'easy keywords':
//
// - %options
// - %foo-bar
// - +++a:b:c1
//
// Note the dash in that last example: there the code will consider
// `bar` to be the keyword, which is fine with us as we're only
// interested in the trailing boundary and patching that one for
// the `easy_keyword_rules` option.
$$ = $re;
if (yy.options.easy_keyword_rules) {
// We need to 'protect' `eval` here as keywords are allowed
// to contain double-quotes and other leading cruft.
// `eval` *does* gobble some escapes (such as `\b`) but
// we protect against that through a simple replace regex:
// we're not interested in the special escapes' exact value
// anyway.
// It will also catch escaped escapes (`\\`), which are not
// word characters either, so no need to worry about
// `eval(str)` 'correctly' converting convoluted constructs
// like '\\\\\\\\\\b' in here.
$$ = $$
.replace(/\\\\/g, '.')
.replace(/"/g, '.')
.replace(/\\c[A-Z]/g, '.')
.replace(/\\[^xu0-9]/g, '.');
try {
// Convert Unicode escapes and other escapes to their literal characters
// BEFORE we go and check whether this item is subject to the
// `easy_keyword_rules` option.
$$ = JSON.parse('"' + $$ + '"');
}
catch (ex) {
yyparser.warn('easy-keyword-rule FAIL on eval: ', ex);
// make the next keyword test fail:
$$ = '.';
}
// a 'keyword' starts with an alphanumeric character,
// followed by zero or more alphanumerics or digits:
var re = new XRegExp('\\w[\\w\\d]*$');
if (XRegExp.match($$, re)) {
$$ = $re + "\\b";
} else {
$$ = $re;
}
}
}
;
regex_list
: regex_list '|' regex_concat
{ $$ = $1 + '|' + $3; }
| regex_list '|'
{ $$ = $1 + '|'; }
| regex_concat
{ $$ = $1; }
| ε
{ $$ = ''; }
;
nonempty_regex_list
: nonempty_regex_list '|' regex_concat
{ $$ = $1 + '|' + $3; }
| nonempty_regex_list '|'
{ $$ = $1 + '|'; }
| '|' regex_concat
{ $$ = '|' + $2; }
| regex_concat
{ $$ = $1; }
;
regex_concat
: regex_concat regex_base
{ $$ = $1 + $2; }
| regex_base
{ $$ = $1; }
;
regex_base
: '(' regex_list ')'
{ $$ = '(' + $regex_list + ')'; }
| SPECIAL_GROUP regex_list ')'
{ $$ = $SPECIAL_GROUP + $regex_list + ')'; }
| '(' regex_list error
{
yyerror(rmCommonWS`
Seems you did not correctly bracket a lex rule regex part in '(...)' braces.
Unterminated regex part:
${yylexer.prettyPrintRange(@error, @1)}
Technical error report:
${$error.errStr}
`);
}
| SPECIAL_GROUP regex_list error
{
yyerror(rmCommonWS`
Seems you did not correctly bracket a lex rule regex part in '(...)' braces.
Unterminated regex part:
${yylexer.prettyPrintRange(@error, @SPECIAL_GROUP)}
Technical error report:
${$error.errStr}
`);
}
| regex_base '+'
{ $$ = $regex_base + '+'; }
| regex_base '*'
{ $$ = $regex_base + '*'; }
| regex_base '?'
{ $$ = $regex_base + '?'; }
| '/' regex_base
{ $$ = '(?=' + $regex_base + ')'; }
| '/!' regex_base
{ $$ = '(?!' + $regex_base + ')'; }
| name_expansion
| regex_base range_regex
{ $$ = $1 + $2; }
| any_group_regex
| '.'
{ $$ = '.'; }
| '^'
{ $$ = '^'; }
| '$'
{ $$ = '$'; }
| string
| escape_char
;
name_expansion
: NAME_BRACE
;
any_group_regex
: REGEX_SET_START regex_set REGEX_SET_END
{ $$ = $REGEX_SET_START + $regex_set + $REGEX_SET_END; }
| REGEX_SET_START regex_set error
{
yyerror(rmCommonWS`
Seems you did not correctly bracket a lex rule regex set in '[...]' brackets.
Unterminated regex set:
${yylexer.prettyPrintRange(@error, @REGEX_SET_START)}
Technical error report:
${$error.errStr}
`);
}
;
regex_set
: regex_set regex_set_atom
{ $$ = $regex_set + $regex_set_atom; }
| regex_set_atom
;
regex_set_atom
: REGEX_SET
| name_expansion
{
if (XRegExp._getUnicodeProperty($name_expansion.replace(/[{}]/g, ''))
&& $name_expansion.toUpperCase() !== $name_expansion
) {
// treat this as part of an XRegExp `\p{...}` Unicode 'General Category' Property cf. http://unicode.org/reports/tr18/#Categories
$$ = $name_expansion;
} else {
$$ = $name_expansion;
}
//yyparser.log("name expansion for: ", { name: $name_expansion, redux: $name_expansion.replace(/[{}]/g, ''), output: $$ });
}
;
escape_char
: ESCAPE_CHAR
{ $$ = $ESCAPE_CHAR; }
;
range_regex
: RANGE_REGEX
{ $$ = $RANGE_REGEX; }
;
string
: STRING_LIT
{ $$ = prepareString($STRING_LIT); }
| CHARACTER_LIT
;
options
: OPTIONS option_list OPTIONS_END
{ $$ = null; }
;
option_list
: option option_list
{ $$ = null; }
| option
{ $$ = null; }
;
option
: NAME[option]
{ yy.options[$option] = true; }
| NAME[option] '=' OPTION_STRING_VALUE[value]
{ yy.options[$option] = $value; }
| NAME[option] '=' OPTION_VALUE[value]
{ yy.options[$option] = parseValue($value); }
| NAME[option] '=' NAME[value]
{ yy.options[$option] = parseValue($value); }
| NAME[option] '=' error
{
// TODO ...
yyerror(rmCommonWS`
Internal error: option "${$option}" value assignment failure.
Erroneous area:
${yylexer.prettyPrintRange(@error, @option)}
Technical error report:
${$error.errStr}
`);
}
| error
{
// TODO ...
yyerror(rmCommonWS`
Expected a valid option name (with optional value assignment).
Erroneous area:
${yylexer.prettyPrintRange(@error)}
Technical error report:
${$error.errStr}
`);
}
;
extra_lexer_module_code
: optional_module_code_chunk
{
var rv = checkActionBlock($optional_module_code_chunk, @optional_module_code_chunk);
if (rv) {
yyerror(rmCommonWS`
The extra lexer module code section (a.k.a. 'epilogue') does not compile: ${rv}
Erroneous area:
${yylexer.prettyPrintRange(@optional_module_code_chunk)}
`);
}
$$ = $optional_module_code_chunk;
}
| extra_lexer_module_code include_macro_code optional_module_code_chunk
{
// Each of the 3 chunks should be parse-able as a JS snippet on its own.
//
// Note: we have already checked the first section in a previous reduction
// of this rule, so we don't need to check that one again!
var rv = checkActionBlock($include_macro_code, @include_macro_code);
if (rv) {
yyerror(rmCommonWS`
The source code %include-d into the extra lexer module code section (a.k.a. 'epilogue') does not compile: ${rv}
Erroneous area:
${yylexer.prettyPrintRange(@include_macro_code)}
`);
}
rv = checkActionBlock($optional_module_code_chunk, @optional_module_code_chunk);
if (rv) {
yyerror(rmCommonWS`
The extra lexer module code section (a.k.a. 'epilogue') does not compile: ${rv}
Erroneous area:
${yylexer.prettyPrintRange(@optional_module_code_chunk)}
`);
}
$$ = $extra_lexer_module_code + $include_macro_code + $optional_module_code_chunk;
}
;
include_macro_code
: INCLUDE PATH
{
var fileContent = fs.readFileSync($PATH, { encoding: 'utf-8' });
// And no, we don't support nested '%include':
$$ = '\n// Included by Jison: ' + $PATH + ':\n\n' + fileContent + '\n\n// End Of Include by Jison: ' + $PATH + '\n\n';
}
| INCLUDE error
{
yyerror(rmCommonWS`
%include MUST be followed by a valid file path.
Erroneous path:
${yylexer.prettyPrintRange(@error, @INCLUDE)}
Technical error report:
${$error.errStr}
`);
}
;
module_code_chunk
: CODE
{ $$ = $CODE; }
| module_code_chunk CODE
{ $$ = $module_code_chunk + $CODE; }
| error CODE
{
// TODO ...
yyerror(rmCommonWS`
Module code declaration error?
Erroneous code:
${yylexer.prettyPrintRange(@error)}
Technical error report:
${$error.errStr}
`);
}
;
optional_module_code_chunk
: module_code_chunk
{ $$ = $module_code_chunk; }
| ε
{ $$ = ''; }
;
%%
var rmCommonWS = helpers.rmCommonWS;
var checkActionBlock = helpers.checkActionBlock;
function encodeRE(s) {
return s.replace(/([.*+?^${}()|\[\]\/\\])/g, '\\$1').replace(/\\\\u([a-fA-F0-9]{4})/g, '\\u$1');
}
function prepareString(s) {
// unescape slashes
s = s.replace(/\\\\/g, "\\");
s = encodeRE(s);
return s;
}
// convert string value to number or boolean value, when possible
// (and when this is more or less obviously the intent)
// otherwise produce the string itself as value.
function parseValue(v) {
if (v === 'false') {
return false;
}
if (v === 'true') {
return true;
}
// http://stackoverflow.com/questions/175739/is-there-a-built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
// Note that the `v` check ensures that we do not convert `undefined`, `null` and `''` (empty string!)
if (v && !isNaN(v)) {
var rv = +v;
if (isFinite(rv)) {
return rv;
}
}
return v;
}
parser.warn = function p_warn() {
console.warn.apply(console, arguments);
};
parser.log = function p_log() {
console.log.apply(console, arguments);
};
parser.pre_parse = function p_lex() {
if (parser.yydebug) parser.log('pre_parse:', arguments);
};
parser.yy.pre_parse = function p_lex() {
if (parser.yydebug) parser.log('pre_parse YY:', arguments);
};
parser.yy.post_lex = function p_lex() {
if (parser.yydebug) parser.log('post_lex:', arguments);
};