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