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