nitcc: add Rejected tokens
[nit.git] / contrib / nitcc / src / nitcc.sablecc
1 // The grammar of nitcc (subset of sablecc4)
2 Grammar nitcc;
3
4 Lexer
5
6 // Identifier, used for production, expressions, alternatives, etc.
7 id = ('a'..'z')('a'..'z'|'0'..'9'|'_')*;
8
9 // A printable character (inside strings)
10 ch = ' ' .. '~';
11
12 // Literal strings
13 str = '\'' (ch-'\\'-'\''|'\\'ch)* '\'';
14
15 // A char by decimal ascii
16 ch_dec = '#' ('0'..'9')+ ;
17
18 // A single-line comment
19 comment = '//' ch* '\n'?;
20
21 any = '\t'..'~';
22 not_star = any - '*';
23 not_star_not_slash = not_star - '/';
24
25 ccomment = '/*' not_star* ('*' (not_star_not_slash not_star*)?)* '*/';
26
27 unknown_keyword = 'A..Z'('A'..'Z'|'a'..'z'|'0'..'9'|'_')*;
28
29 // Igndored stufs
30 blank = '\n' | '\t' | ' ' | comment | ccomment;
31
32 Parser
33
34 Ignored blank;
35
36 grammar = 'Grammar' id ';' lexer_part? parser_part? reject?;
37
38 reject = 'XXX' unknown_keyword ';';
39
40 lexer_part = 'Lexer' expr*;
41
42 expr = id '=' re ';';
43
44 // No priority (yet) so just decompose
45 re =
46         {alter:} re '|' re1 |
47         re1 ;
48
49 re1 {-> re} =
50         {minus:} re1 '-' re2 |
51         re2 ;
52
53 re2 {-> re} =
54         {conc:} re2 re3 |
55         re3 ;
56
57 re3 {-> re} =
58         {ques:} re3 '?' |
59         {star:} re3 '*' |
60         {plus:} re3 '+' |
61         {id:} id |
62         {str:} str |
63         {par:} '(' re ')' |
64         {class:} str '.' '.' str |
65         {any:} 'Any' |
66         {ch_dec:} ch_dec ;
67
68
69 parser_part = 'Parser' ign? rej? prod*;
70
71 ign = 'Ignored' id ';' ;
72
73 rej = 'Rejected' elem_list ';' ;
74
75 prod = id ptrans? '=' alts ';';
76
77 ptrans = '{' '->' id '}';
78 atrans = '{' '->' id '}';
79
80 alts =
81         {more:} alts '|' alt |
82         {one:} alt ;
83
84 alt = altid? nelem* atrans?;
85
86 altid = '{' id ':' '}' | '{' id '}';
87
88 nelem = elem | elemid elem;
89
90 elemid = '[' id ':' ']' | '[' id ']' ':';
91
92 elem_list =
93         {more:} elem_list ',' elem |
94         {one:} elem ;
95
96 elem =
97         {id:} id |
98         {str:} str |
99         {star:} elem '*' |
100         {ques:} elem '?' |
101         {plus:} elem '+' ;