Initialize Tizen 2.3
[external/ragel.git] / test / erract1.rl
1 /*
2  * @LANG: c++
3  */
4
5 /*
6  * Test error actions.
7  */
8
9 #include <iostream>
10 #include <stdio.h>
11 #include <string.h>
12
13 using namespace std;
14
15 struct ErrAct 
16 {
17         int cs;
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         int init( );
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         int execute( const char *data, 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
37 %%{
38         machine ErrAct;
39
40         action expect_digit_plus_minus { printf(" DIGIT PLUS MINUS\n"); } 
41         action expect_digit { printf(" DIGIT\n"); } 
42         action expect_digit_decimal { printf(" DIGIT DECIMAL\n"); }
43
44         float = (
45                 ( 
46                         [\-+] >err expect_digit_plus_minus %err expect_digit |
47                         ""
48                 )
49                 ( [0-9] [0-9]* $err expect_digit_decimal )
50                 ( '.' [0-9]+ $err expect_digit )? 
51         );
52
53         main := float '\n';
54 }%%
55
56 %% write data;
57
58 int ErrAct::init( )
59 {
60         %% write init;
61         return 0;
62 }
63
64 int ErrAct::execute( const char *_data, int _len )
65 {
66         const char *p = _data;
67         const char *pe = _data+_len;
68         const char *eof = pe;
69         %% write exec;
70
71         if ( cs == ErrAct_error )
72                 return -1;
73         if ( cs >= ErrAct_first_final )
74                 return 1;
75         return 0;
76 }
77
78 int ErrAct::finish( )
79 {
80         if ( cs == ErrAct_error )
81                 return -1;
82         if ( cs >= ErrAct_first_final )
83                 return 1;
84         return 0;
85 }
86
87 #define BUFSIZE 1024
88
89 void test( const char *buf )
90 {
91         ErrAct errAct;
92         errAct.init();
93         errAct.execute( buf, strlen(buf) );
94         if ( errAct.finish() > 0 )
95                 cout << "ACCEPT" << endl;
96         else
97                 cout << "FAIL" << endl;
98 }
99
100 int main()
101 {
102         test( "1\n" );
103         test( "+1\n" );
104         test( "-1\n" );
105         test( "1.1\n" );
106         test( "+1.1\n" );
107         test( "-1.1\n" );
108         test( "a\n" );
109         test( "-\n" );
110         test( "+\n" );
111         test( "-a\n" );
112         test( "+b\n" );
113         test( "1.\n" );
114         test( "1d\n" );
115         test( "1.d\n" );
116         test( "1.1d\n" );
117         return 0;
118 }
119
120 #ifdef _____OUTPUT_____
121 ACCEPT
122 ACCEPT
123 ACCEPT
124 ACCEPT
125 ACCEPT
126 ACCEPT
127  DIGIT PLUS MINUS
128 FAIL
129  DIGIT
130 FAIL
131  DIGIT
132 FAIL
133  DIGIT
134 FAIL
135  DIGIT
136 FAIL
137  DIGIT
138 FAIL
139  DIGIT DECIMAL
140 FAIL
141  DIGIT
142 FAIL
143  DIGIT
144 FAIL
145 #endif