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