Initialize Tizen 2.3
[external/ragel.git] / test / erract5.rl
1 /*
2  * @LANG: obj-c
3  */
4
5 /*
6  * Test error actions.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <objc/Object.h>
12
13
14 @interface ErrAct : Object
15 {
16 @public
17         int cs;
18 };
19
20 // Initialize the machine. Invokes any init statement blocks. Returns 0
21 // if the machine begins in a non-accepting state and 1 if the machine
22 // begins in an accepting state.
23 - (int) initFsm;
24
25 // Execute the machine on a block of data. Returns -1 if after processing
26 // the data, the machine is in the error state and can never accept, 0 if
27 // the machine is in a non-accepting state and 1 if the machine is in an
28 // accepting state.
29 - (void) executeWithData:(const char *)data len:(int)len;
30
31 // Indicate that there is no more data. Returns -1 if the machine finishes
32 // in the error state and does not accept, 0 if the machine finishes
33 // in any other non-accepting state and 1 if the machine finishes in an
34 // accepting state.
35 - (int) finish;
36
37 @end
38
39 @implementation ErrAct
40
41 %%{
42         machine ErrAct;
43
44         action expect_digit_plus_minus { printf(" DIGIT PLUS MINUS\n"); } 
45         action expect_digit { printf(" DIGIT\n"); } 
46         action expect_digit_decimal { printf(" DIGIT DECIMAL\n"); }
47
48         float = (
49                 ( 
50                         [\-+] >!expect_digit_plus_minus %!expect_digit |
51                         ""
52                 )
53                 ( [0-9] [0-9]* $!expect_digit_decimal )
54                 ( '.' [0-9]+ $!expect_digit )? 
55         );
56
57         main := float '\n';
58 }%%
59
60 %% write data;
61
62 - (int) initFsm;
63 {
64         %% write init;
65         return 1;
66 }
67
68 - (void) executeWithData:(const char *)_data len:(int)_len;
69 {
70         const char *p = _data;
71         const char *pe = _data + _len;
72         const char *eof = pe;
73         %% write exec;
74 }
75
76 - (int) finish;
77 {
78         if ( cs == ErrAct_error )
79                 return -1;
80         else if ( cs >= ErrAct_first_final )
81                 return 1;
82         return 0;
83 }
84
85
86 @end
87
88 #define BUFSIZE 1024
89
90 void test( char *buf )
91 {
92         ErrAct *errAct = [[ErrAct alloc] init];
93         [errAct initFsm];
94         [errAct executeWithData:buf len:strlen(buf)];
95         if ( [errAct finish] > 0 )
96                 printf("ACCEPT\n");
97         else
98                 printf("FAIL\n");
99 }
100
101 int main()
102 {
103         test( "1\n" );
104         test( "+1\n" );
105         test( "-1\n" );
106         test( "1.1\n" );
107         test( "+1.1\n" );
108         test( "-1.1\n" );
109         test( "a\n" );
110         test( "-\n" );
111         test( "+\n" );
112         test( "-a\n" );
113         test( "+b\n" );
114         test( "1.\n" );
115         test( "1d\n" );
116         test( "1.d\n" );
117         test( "1.1d\n" );
118         return 0;
119 }
120
121 #ifdef _____OUTPUT_____
122 ACCEPT
123 ACCEPT
124 ACCEPT
125 ACCEPT
126 ACCEPT
127 ACCEPT
128  DIGIT PLUS MINUS
129 FAIL
130  DIGIT
131 FAIL
132  DIGIT
133 FAIL
134  DIGIT
135 FAIL
136  DIGIT
137 FAIL
138  DIGIT
139 FAIL
140  DIGIT DECIMAL
141 FAIL
142  DIGIT
143 FAIL
144  DIGIT
145 FAIL
146 #endif