Initialize Tizen 2.3
[external/ragel.git] / test / call3.rl
1 /*
2  * @LANG: obj-c
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <objc/Object.h>
8
9
10 int num = 0;
11
12 @interface CallTest : Object
13 {
14 @public 
15         /* State machine operation data. */
16         int cs, top, stack[32];
17 };
18
19 // Initialize the machine. Invokes any init statement blocks. Returns 0
20 // if the machine begins in a non-accepting state and 1 if the machine
21 // begins in an accepting state.
22 - (void) initFsm;
23
24 // Execute the machine on a block of data. Returns -1 if after processing
25 // the data, the machine is in the error state and can never accept, 0 if
26 // the machine is in a non-accepting state and 1 if the machine is in an
27 // accepting state.
28 - (void) executeWithData:(const char *)data len:(int)len;
29
30 // Indicate that there is no more data. Returns -1 if the machine finishes
31 // in the error state and does not accept, 0 if the machine finishes
32 // in any other non-accepting state and 1 if the machine finishes in an
33 // accepting state.
34 - (int) finish;
35
36 @end
37
38 @implementation CallTest
39
40 %%{
41         machine CallTest;
42
43         action check_num {
44                 if ( num & 1 )
45                         fcall odd;
46                 else
47                         fcall even;
48         }
49
50         # Test call and return functionality.
51         even := 'even' any @{fhold; fret;};
52         odd := 'odd' any @{fhold; fret;};
53         num = [0-9]+ ${ num = num * 10 + (fc - '0'); };
54         even_odd = num ' ' @check_num "\n";
55
56         # Test calls in out actions.
57         fail := !(any*);
58         out_acts = 'OA ok\n' | 
59                 'OA error1\n' |
60                 'OA error2\n';
61
62         main := even_odd | out_acts;
63 }%%
64
65 %% write data;
66
67 - (void) initFsm;
68 {
69         num = 0;
70         %% write init;
71 }
72
73 - (void) executeWithData:(const char *)data len:(int)len;
74 {
75         const char *p = data;
76         const char *pe = data + len;
77         %% write exec;
78 }
79
80 - (int) finish;
81 {
82         if ( cs == CallTest_error ) 
83                 return -1;
84         return ( cs >= CallTest_first_final ) ? 1 : 0;
85 }
86
87 @end
88
89 #define BUFSIZE 1024
90
91 void test( char *buf )
92 {   
93         CallTest *test = [[CallTest alloc] init];
94         [test initFsm];
95         [test executeWithData:buf len:strlen(buf)];
96         if ( [test finish] > 0 )
97                 printf( "ACCEPT\n" );
98         else
99                 printf( "FAIL\n" );
100 }
101
102 int main()
103 {
104         test( "78 even\n" );
105         test( "89 odd\n" );
106         test( "1 even\n" );
107         test( "0 odd\n" );
108         test( "OA ok\n" );
109         test( "OA error1\n" );
110         test( "OA error2\n" );
111         return 0;
112 }
113
114 #ifdef _____OUTPUT_____
115 ACCEPT
116 ACCEPT
117 FAIL
118 FAIL
119 ACCEPT
120 ACCEPT
121 ACCEPT
122 #endif