update(add) packaging directory and spec file from OBSTF:Private, OBS
[external/ragel.git] / test / clang1.rl
1 /*
2  * @LANG: c
3  * A mini C-like language scanner.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #define IDENT_BUFLEN 256
9
10 %%{
11         machine clang;
12
13         # Function to buffer a character.
14         action bufChar {
15                 if ( identLen < IDENT_BUFLEN ) {
16                         identBuf[identLen] = fc;
17                         identLen += 1;
18                 }
19         }
20
21         # Function to clear the buffer.
22         action clearBuf {
23                 identLen = 0;
24         }
25
26         # Functions to dump tokens as they are matched.
27         action ident {
28                 identBuf[identLen] = 0;
29                 printf("ident(%i): %s\n", curLine, identBuf);
30         }
31         action literal {
32                 identBuf[identLen] = 0;
33                 printf("literal(%i): %s\n", curLine, identBuf);
34         }
35         action float {
36                 identBuf[identLen] = 0;
37                 printf("float(%i): %s\n", curLine, identBuf);
38         }
39         action int {
40                 identBuf[identLen] = 0;
41                 printf("int(%i): %s\n", curLine, identBuf);
42         }
43         action hex {
44                 identBuf[identLen] = 0;
45                 printf("hex(%i): 0x%s\n", curLine, identBuf);
46         }
47         action symbol {
48                 identBuf[identLen] = 0;
49                 printf("symbol(%i): %s\n", curLine, identBuf);
50         }
51
52         # Alpha numberic characters or underscore.
53         alnumu = alnum | '_';
54
55         # Alpha charactres or underscore.
56         alphau = alpha | '_';
57
58         # Symbols. Upon entering clear the buffer. On all transitions
59         # buffer a character. Upon leaving dump the symbol.
60         symbol = ( punct - [_'"] ) >clearBuf $bufChar %symbol;
61
62         # Identifier. Upon entering clear the buffer. On all transitions
63         # buffer a character. Upon leaving, dump the identifier.
64         ident = (alphau . alnumu*) >clearBuf $bufChar %ident;
65
66         # Match single characters inside literal strings. Or match 
67         # an escape sequence. Buffers the charater matched.
68         sliteralChar =
69                         ( extend - ['\\] ) @bufChar |
70                         ( '\\' . extend @bufChar );
71         dliteralChar =
72                         ( extend - ["\\] ) @bufChar |
73                         ( '\\' . extend @bufChar );
74
75         # Single quote and double quota literals. At the start clear
76         # the buffer. Upon leaving dump the literal.
77         sliteral = ('\'' @clearBuf . sliteralChar* . '\'' ) %literal;
78         dliteral = ('"' @clearBuf . dliteralChar* . '"' ) %literal;
79         literal = sliteral | dliteral;
80
81         # Whitespace is standard ws, newlines and control codes.
82         whitespace = any - 0x21..0x7e;
83
84         # Describe both c style comments and c++ style comments. The
85         # priority bump on tne terminator of the comments brings us
86         # out of the extend* which matches everything.
87         ccComment = '//' . extend* $0 . '\n' @1;
88         cComment = '/*' . extend* $0 . '*/' @1;
89
90         # Match an integer. We don't bother clearing the buf or filling it.
91         # The float machine overlaps with int and it will do it.
92         int = digit+ %int;
93
94         # Match a float. Upon entering the machine clear the buf, buffer
95         # characters on every trans and dump the float upon leaving.
96         float =  ( digit+ . '.' . digit+ ) >clearBuf $bufChar %float;
97
98         # Match a hex. Upon entering the hex part, clear the buf, buffer characters
99         # on every trans and dump the hex on leaving transitions.
100         hex = '0x' . xdigit+ >clearBuf $bufChar %hex;
101
102         # Or together all the lanuage elements.
103         fin = ( ccComment |
104                 cComment |
105                 symbol |
106                 ident |
107                 literal |
108                 whitespace |
109                 int |
110                 float |
111                 hex );
112
113         # Star the language elements. It is critical in this type of application
114         # that we decrease the priority of out transitions before doing so. This
115         # is so that when we see 'aa' we stay in the fin machine to match an ident
116         # of length two and not wrap around to the front to match two idents of 
117         # length one.
118         clang_main = ( fin $1 %0 )*;
119
120         # This machine matches everything, taking note of newlines.
121         newline = ( any | '\n' @{ curLine += 1; } )*;
122
123         # The final fsm is the lexer intersected with the newline machine which
124         # will count lines for us. Since the newline machine accepts everything,
125         # the strings accepted is goverened by the clang_main machine, onto which
126         # the newline machine overlays line counting.
127         main := clang_main & newline;
128 }%%
129
130 #include <stdio.h>
131
132 %% write data noerror;
133
134
135 char data[] = 
136         "/*\n"
137         " *  Copyright\n"
138         " */\n"
139         "\n"
140         "/*  Aapl.\n"
141         " */\n"
142         "\f\n"
143         "#define _AAPL_RESIZE_H\n"
144         "\n"
145         "#include <assert.h>\n"
146         "\n"
147         "#ifdef AAPL_NAMESPACE\n"
148         "namespace Aapl {\n"
149         "#endif\n"
150         "#define LIN_DEFAULT_STEP 256\n"
151         "#define EXPN_UP( existing, needed ) \\\n"
152         "               need > eng ? (ned<<1) : eing\n"
153         "       \n"
154         "\n"
155         "/*@}*/\n"
156         "#undef EXPN_UP\n"
157         "#ifdef AAPL_NAMESPACE\n"
158         "#endif /* _AAPL_RESIZE_H */\n";
159
160 void test( char *buf )
161 {
162         int len = strlen( buf );
163         char *p = buf, *pe = buf + len;
164         char *eof = pe;
165         char identBuf[IDENT_BUFLEN+1];
166         int identLen;
167         int curLine;
168         int cs;
169
170         identLen = 0;
171         curLine = 1;
172
173         %% write init;
174         %% write exec;
175
176         if ( cs >= clang_first_final )
177                 printf("ACCEPT\n");
178         else
179                 printf("FAIL\n");
180 }
181
182 int main()
183 {
184         test( 
185                 "999 0xaAFF99 99.99 /*\n"
186                 "*/ 'lksdj' //\n"
187                 "\"\n"
188                 "\n"
189                 "literal\n"
190                 "\n"
191                 "\n"
192                 "\"0x00aba foobardd.ddsf 0x0.9\n" );
193         test( 
194                 "wordwithnum00asdf\n"
195                 "000wordfollowsnum,makes new symbol\n"
196                 "\n"
197                 "finishing early /* unfinished ...\n" );
198         test( data );
199         return 0;
200 }
201
202 #ifdef _____OUTPUT_____
203 int(1): 999
204 hex(1): 0xaAFF99
205 float(1): 99.99
206 literal(2): lksdj
207 literal(8): 
208
209 literal
210
211
212
213 hex(8): 0x00aba
214 ident(8): foobardd
215 symbol(8): .
216 ident(8): ddsf
217 hex(8): 0x0
218 symbol(8): .
219 int(8): 9
220 ACCEPT
221 ident(1): wordwithnum00asdf
222 int(2): 000
223 ident(2): wordfollowsnum
224 symbol(2): ,
225 ident(2): makes
226 ident(2): new
227 ident(2): symbol
228 ident(4): finishing
229 ident(4): early
230 FAIL
231 symbol(8): #
232 ident(8): define
233 ident(8): _AAPL_RESIZE_H
234 symbol(10): #
235 ident(10): include
236 symbol(10): <
237 ident(10): assert
238 symbol(10): .
239 ident(10): h
240 symbol(10): >
241 symbol(12): #
242 ident(12): ifdef
243 ident(12): AAPL_NAMESPACE
244 ident(13): namespace
245 ident(13): Aapl
246 symbol(13): {
247 symbol(14): #
248 ident(14): endif
249 symbol(15): #
250 ident(15): define
251 ident(15): LIN_DEFAULT_STEP
252 int(15): 256
253 symbol(16): #
254 ident(16): define
255 ident(16): EXPN_UP
256 symbol(16): (
257 ident(16): existing
258 symbol(16): ,
259 ident(16): needed
260 symbol(16): )
261 symbol(16): \
262 ident(17): need
263 symbol(17): >
264 ident(17): eng
265 symbol(17): ?
266 symbol(17): (
267 ident(17): ned
268 symbol(17): <
269 symbol(17): <
270 int(17): 1
271 symbol(17): )
272 symbol(17): :
273 ident(17): eing
274 symbol(21): #
275 ident(21): undef
276 ident(21): EXPN_UP
277 symbol(22): #
278 ident(22): ifdef
279 ident(22): AAPL_NAMESPACE
280 symbol(23): #
281 ident(23): endif
282 ACCEPT
283 #endif