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