fb535a0ac68f3e94e2adc628fcf6b84ffb3fe5d7
[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 char by hexadecimal ascii
19 ch_hex = '#' ('x'|'X') ('0'..'9'|'a'..'z'|'A'..'Z')+ ;
20
21 // A single-line comment
22 comment = '//' ch* '\n'?;
23
24 any = '\t'..'~';
25 not_star = any - '*';
26 not_star_not_slash = not_star - '/';
27
28 ccomment = '/*' not_star* ('*' (not_star_not_slash not_star*)?)* '*/';
29
30 unknown_keyword = 'A'..'Z'('A'..'Z'|'a'..'z'|'0'..'9'|'_')*;
31
32 // Igndored stufs
33 blank = '\r' | '\n' | '\t' | ' ' | comment | ccomment;
34
35 Parser
36
37 Ignored blank;
38 Rejected unknown_keyword;
39
40 grammar = 'Grammar' id ';' lexer_part? parser_part? tree_part?;
41
42 lexer_part = 'Lexer' expr*;
43
44 expr = id '=' re ';';
45
46 // No priority (yet) so just decompose
47 re =
48         {alter:} re '|' re1 |
49         re1 ;
50
51 re1 {-> re} =
52         {minus:} re1 '-' re2 |
53         {except:} re1 'Except' re2 |
54         {and:} re1 'And' re2 |
55         re2 ;
56
57 re2 {-> re} =
58         {conc:} re2 re3 |
59         re3 ;
60
61 re3 {-> re} =
62         {ques:} re3 '?' |
63         {star:} re3 '*' |
64         {plus:} re3 '+' |
65         {shortest:} 'Shortest' '(' re ')' |
66         {longest:} 'Longest' '(' re ')' |
67         {id:} id |
68         {par:} '(' re ')' |
69         {class:} text '.' '.' text |
70         {openclass:} text '.' '.' '.' |
71         {any:} 'Any' |
72         {end:} 'End' |
73         {text:} text ;
74
75 text {-> re} =
76         {str:} str |
77         {ch_dec:} ch_dec |
78         {ch_hex:} ch_hex ;
79
80 parser_part = 'Parser' ign? rej? prod*;
81
82 ign = 'Ignored' elem_list ';' ;
83
84 rej = 'Rejected' elem_list ';' ;
85
86 prod = id ptrans? '=' alts priority* ';';
87
88 ptrans = '{' '->' id '}';
89 atrans = '{' '->' '}';
90
91 alts =
92         {more:} alts '|' alt |
93         {one:} alt ;
94
95 alt = altid? nelem* atrans?;
96
97 altid = '{' id ':' '}' | '{' id '}';
98
99 nelem = elem | elemid elem;
100
101 elemid = '[' id ':' ']' | '[' id ']' ':';
102
103 elem_list =
104         {more:} elem_list ',' elem |
105         {one:} elem ;
106
107 elem =
108         {id:} id |
109         {str:} text |
110         {star:} elem '*' |
111         {ques:} elem '?' |
112         {plus:} elem '+' |
113         {empty:} 'Empty' ;
114
115 priority =
116         {left:} 'Left' alts |
117         {right:} 'Right' alts |
118         {unary:} 'Unary' alts ;
119
120 tree_part = 'Tree' prod*;