Go back to 3.X semantics for >, % and error actions. The > operator also embeds
[external/ragel.git] / test / clang4.rl
1 /*
2  * @LANG: indep
3  * @NEEDS_EOF: yes
4  */
5
6 ptr tokstart;
7 ptr tokend;
8 char array[32];
9 int pos;
10 int line;
11 %%
12 pos = 0;
13 line = 1;
14 %%{
15         machine clang;
16
17         # Function to buffer a character.
18         action bufChar { array[pos] = fc; pos = pos + 1; }
19
20         # Function to clear the buffer.
21         action clearBuf { pos = 0; }
22
23         # Functions to dump tokens as they are matched.
24         action ident {
25                 prints "ident(";
26                 printi line;
27                 prints ",";
28                 printi pos;
29                 prints "): ";
30                 printb array;
31                 prints "\n";
32         }
33         action literal {
34                 prints "literal(";
35                 printi line;
36                 prints ",";
37                 printi pos;
38                 prints "): ";
39                 printb array;
40                 prints "\n";
41         }
42         action float {
43                 prints "float(";
44                 printi line;
45                 prints ",";
46                 printi pos;
47                 prints "): ";
48                 printb array;
49                 prints "\n";
50         }
51         action integer {
52                 prints "int(";
53                 printi line;
54                 prints ",";
55                 printi pos;
56                 prints "): ";
57                 printb array;
58                 prints "\n";
59         }
60         action hex {
61                 prints "hex(";
62                 printi line;
63                 prints ",";
64                 printi pos;
65                 prints "): ";
66                 printb array;
67                 prints "\n";
68         }
69         action symbol {
70                 prints "symbol(";
71                 printi line;
72                 prints ",";
73                 printi pos;
74                 prints "): ";
75                 printb array;
76                 prints "\n";
77         }
78
79         # Alpha numberic characters or underscore.
80         alnumu = alnum | '_';
81
82         # Alpha charactres or underscore.
83         alphau = alpha | '_';
84
85         # Symbols. Upon entering clear the buffer. On all transitions
86         # buffer a character. Upon leaving dump the symbol.
87         symbol = ( punct - [_'"] ) >clearBuf $bufChar %symbol;
88
89         # Identifier. Upon entering clear the buffer. On all transitions
90         # buffer a character. Upon leaving, dump the identifier.
91         ident = (alphau . alnumu*) >clearBuf $bufChar %ident;
92
93         # Match single characters inside literal strings. Or match 
94         # an escape sequence. Buffers the charater matched.
95         sliteralChar =
96                         ( extend - ['\\] ) @bufChar |
97                         ( '\\' . extend @bufChar );
98         dliteralChar =
99                         ( extend - ["\\] ) @bufChar |
100                         ( '\\' . extend @bufChar );
101
102         # Single quote and double quota literals. At the start clear
103         # the buffer. Upon leaving dump the literal.
104         sliteral = ('\'' @clearBuf . sliteralChar* . '\'' ) %literal;
105         dliteral = ('"' @clearBuf . dliteralChar* . '"' ) %literal;
106         literal = sliteral | dliteral;
107
108         # Whitespace is standard ws, newlines and control codes.
109         whitespace = any - 33 .. 126;
110
111         # Describe both c style comments and c++ style comments. The
112         # priority bump on tne terminator of the comments brings us
113         # out of the extend* which matches everything.
114         ccComment = '//' . extend* $0 . '\n' @1;
115         cComment = '/!' . extend* $0 . '!/' @1;
116
117         # Match an integer. We don't bother clearing the buf or filling it.
118         # The float machine overlaps with int and it will do it.
119         integer = digit+ %integer;
120
121         # Match a float. Upon entering the machine clear the buf, buffer
122         # characters on every trans and dump the float upon leaving.
123         float =  ( digit+ . '.' . digit+ ) >clearBuf $bufChar %float;
124
125         # Match a hex. Upon entering the hex part, clear the buf, buffer characters
126         # on every trans and dump the hex on leaving transitions.
127         hex = '0x' . xdigit+ >clearBuf $bufChar %hex;
128
129         # Or together all the lanuage elements.
130         fin = ( ccComment |
131                 cComment |
132                 symbol |
133                 ident |
134                 literal |
135                 whitespace |
136                 integer |
137                 float |
138                 hex );
139
140         # Star the language elements. It is critical in this type of application
141         # that we decrease the priority of out transitions before doing so. This
142         # is so that when we see 'aa' we stay in the fin machine to match an ident
143         # of length two and not wrap around to the front to match two idents of 
144         # length one.
145         clang_main = ( fin $1 %0 )*;
146
147         # This machine matches everything, taking note of newlines.
148         newline = ( any | '\n' @{ line = line + 1; } )*;
149
150         # The final fsm is the lexer intersected with the newline machine which
151         # will count lines for us. Since the newline machine accepts everything,
152         # the strings accepted is goverened by the clang_main machine, onto which
153         # the newline machine overlays line counting.
154         main := clang_main & newline;
155 }%%
156 /* _____INPUT_____
157 "999 0xaAFF99 99.99 /!\n!/ 'lksdj' //\n\"\n\nliteral\n\n\n\"0x00aba foobardd.ddsf 0x0.9\n"
158 "wordwithnum00asdf\n000wordfollowsnum,makes new symbol\n\nfinishing early /! unfinished ...\n"
159 _____INPUT_____ */
160 /* _____OUTPUT_____
161 int(1,3): 999
162 hex(1,6): aAFF99
163 float(1,5): 99.99
164 literal(2,5): lksdj
165 literal(8,12): 
166
167 literal
168
169
170
171 hex(8,5): 00aba
172 ident(8,8): foobardd
173 symbol(8,1): .
174 ident(8,4): ddsf
175 hex(8,1): 0
176 symbol(8,1): .
177 int(8,1): 9
178 ACCEPT
179 ident(1,17): wordwithnum00asdf
180 int(2,3): 000
181 ident(2,14): wordfollowsnum
182 symbol(2,1): ,
183 ident(2,5): makes
184 ident(2,3): new
185 ident(2,6): symbol
186 ident(4,9): finishing
187 ident(4,5): early
188 FAIL
189 _____OUTPUT_____ */
190