nitcc: introduce nitcc
[nit.git] / contrib / nitcc / 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? prod*;
70
71 ign = 'Ignored' id ';' ;
72
73 prod = id ptrans? '=' alts ';';
74
75 ptrans = '{' '->' id '}';
76 atrans = '{' '->' id '}';
77
78 alts =
79         {more:} alts '|' alt |
80         {one:} alt ;
81
82 alt = altid? nelem* atrans?;
83
84 altid = '{' id ':' '}' | '{' id '}';
85
86 nelem = elem | elemid elem;
87
88 elemid = '[' id ':' ']' | '[' id ']' ':';
89
90 elem =
91         {id:} id |
92         {str:} str |
93         {star:} elem '*' |
94         {ques:} elem '?' |
95         {plus:} elem '+' ;