Initialize Tizen 2.3
[external/ragel.git] / test / element3.rl
1 /*
2  * @LANG: obj-c
3  */
4
5 #include <stdio.h>
6 #include <objc/Object.h>
7
8 struct LangEl
9 {
10         int key;
11         char *name;
12 };
13
14 @interface Fsm : 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 - (int) executeWithData:( struct LangEl *)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
40 @implementation Fsm
41
42 %%{
43         machine Fsm;
44
45         alphtype int;
46         getkey fpc->key;
47
48         action a1 {}
49         action a2 {}
50         action a3 {}
51
52         main := ( 1 2* 3  ) 
53                         ${printf("%s\n", fpc->name);} 
54                         %/{printf("accept\n");};
55 }%%
56
57 %% write data;
58
59 - (int) initFsm;
60 {
61         %% write init;
62         return 0;
63 }
64
65 - (int) executeWithData:( struct LangEl *)_data len:(int)_len;
66 {
67         struct LangEl *p = _data;
68         struct LangEl *pe = _data + _len;
69         struct LangEl *eof = pe;
70         %% write exec;
71
72         if ( self->cs == Fsm_error ) 
73                 return -1;
74         return ( self->cs >= Fsm_first_final ) ? 1 : 0;
75 }
76
77 - (int) finish;
78 {
79         if ( self->cs == Fsm_error ) 
80                 return -1;
81         return ( self->cs >= Fsm_first_final ) ? 1 : 0;
82 }
83
84
85 @end
86
87 int main()
88 {
89         static Fsm *fsm;
90         static struct LangEl lel[] = { 
91                 {1, "one"}, 
92                 {2, "two-a"}, 
93                 {2, "two-b"}, 
94                 {2, "two-c"}, 
95                 {3, "three"}
96         };
97         
98         fsm = [[Fsm alloc] init];
99         [fsm initFsm];
100         [fsm executeWithData:lel len:5];
101         [fsm finish];
102
103         return 0;
104 }
105
106 @interface Fsm2 : Object
107 {
108         // The current state may be read and written to from outside of the
109         // machine.  From within action code, curs is -1 and writing to it has no
110         // effect.
111         @public
112         int cs;
113
114         @protected
115
116 }
117
118 // Execute the machine on a block of data. Returns -1 if after processing
119 // the data, the machine is in the error state and can never accept, 0 if
120 // the machine is in a non-accepting state and 1 if the machine is in an
121 // accepting state.
122 - (int)
123 executeWithElements:(int) elements
124 length:(unsigned)length;
125
126 @end
127
128 @implementation Fsm2
129 - (int)
130 executeWithElements:(int)elements
131 length:(unsigned)length;
132 {
133         return 0;
134 }
135 @end
136
137 #ifdef _____OUTPUT_____
138 one
139 two-a
140 two-b
141 two-c
142 three
143 accept
144 #endif