Removed arg passing from frontend to backend functions.
[ragel.git] / test / repetition.rl
blob328cfa9fd73ac2a8f7bcb06b53a5b59a74c91d29
1 /*
2  * @LANG: c++
3  */
5 /* Test repeptition operators. */
7 #include <iostream>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
12 using namespace std;
14 struct Rep
16         int cs;
18         int init( );
19         int execute( const char *data, int len );
20         int finish( );
23 %%{
24         machine Rep;
26         action begin { cout << "begin" << endl; }
27         action in { cout << "in" << endl; }
28         action end { cout << "end" << endl; }
30         a = 'a' >begin @in %end;
31         b = 'b' >begin @in %end;
32         c = 'c' >begin @in %end;
33         d = 'd' >begin @in %end;
35         main := 
36                 ( a {5} '\n' )* '-\n'
37                 ( b {,5} '\n' )* '-\n'
38                 ( c {5,} '\n' )* '-\n'
39                 ( d {2,5} '\n' )*;
40 }%%
42 %% write data;
44 int Rep::init( )
46         %% write init;
47         return 1;
50 int Rep::execute( const char *_data, int _len )
52         const char *p = _data;
53         const char *pe = _data+_len;
55         %% write exec;
57         if ( cs == Rep_error )
58                 return -1;
59         if ( cs >= Rep_first_final )
60                 return 1;
61         return 0;
64 int Rep::finish( )
66         if ( cs == Rep_error )
67                 return -1;
68         if ( cs >= Rep_first_final )
69                 return 1;
70         return 0;
73 void test( const char *buf )
75         Rep rep;
76         int len = strlen( buf );
77         rep.init();
78         rep.execute( buf, len );
79         if ( rep.finish() > 0 )
80                 printf("ACCEPT\n");
81         else
82                 printf("FAIL\n");
85 int main()
87         test(
88                 "aaaaa\n"
89                 "-\n"
90                 "\n"
91                 "b\n"
92                 "bb\n"
93                 "bbb\n"
94                 "bbbb\n"
95                 "bbbbb\n"
96                 "-\n"
97                 "ccccc\n"
98                 "ccccccc\n"
99                 "cccccccccc\n"
100                 "-\n"
101                 "dd\n"
102                 "ddd\n"
103                 "dddd\n"
104                 "ddddd\n"
105         );
107         test(
108                 "a\n"
109                 "-\n"
110                 "b\n"
111                 "-\n"
112                 "c\n"
113                 "-\n"
114                 "d\n"
115         );
117         return 0;
120 #ifdef _____OUTPUT_____
121 begin
124 begin
127 begin
130 begin
133 begin
136 begin
139 begin
142 begin
145 begin
148 begin
151 begin
154 begin
157 begin
160 begin
163 begin
166 begin
169 begin
172 begin
175 begin
178 begin
181 begin
184 begin
187 begin
190 begin
193 begin
196 begin
199 begin
202 begin
205 begin
208 begin
211 begin
214 begin
217 begin
220 begin
223 begin
226 begin
229 begin
232 begin
235 begin
238 begin
241 begin
244 begin
247 begin
250 begin
253 begin
256 begin
259 begin
262 begin
265 begin
268 begin
271 begin
274 begin
277 begin
280 begin
283 begin
286 begin
289 ACCEPT
290 begin
292 FAIL
293 #endif