Merge: doc: fixed some typos and other misc. corrections
[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         {prefixes:} 'Prefixes' '(' re ')' |
68         {id:} id |
69         {par:} '(' re ')' |
70         {class:} text '.' '.' text |
71         {openclass:} text '.' '.' '.' |
72         {any:} 'Any' |
73         {end:} 'End' |
74         {text:} text ;
75
76 text {-> re} =
77         {str:} str |
78         {ch_dec:} ch_dec |
79         {ch_hex:} ch_hex ;
80
81 parser_part = 'Parser' ign? rej? prod*;
82
83 ign = 'Ignored' elem_list ';' ;
84
85 rej = 'Rejected' elem_list ';' ;
86
87 prod = id ptrans? '=' alts priority* ';';
88
89 ptrans = '{' '->' id '}';
90 atrans = '{' '->' '}';
91
92 alts =
93         {more:} alts '|' alt |
94         {one:} alt ;
95
96 alt = altid? nelem* atrans?;
97
98 altid = '{' id ':' '}' | '{' id '}';
99
100 nelem = elem | elemid elem;
101
102 elemid = '[' id ':' ']' | '[' id ']' ':';
103
104 elem_list =
105         {more:} elem_list ',' elem |
106         {one:} elem ;
107
108 elem =
109         {id:} id |
110         {str:} text |
111         {star:} elem '*' |
112         {ques:} elem '?' |
113         {plus:} elem '+' |
114         {empty:} 'Empty' ;
115
116 priority =
117         {left:} 'Left' alts |
118         {right:} 'Right' alts |
119         {unary:} 'Unary' alts ;
120
121 tree_part = 'Tree' prod*;