A fix to the documentation makefile from John D. Mitchell.
[ragel.git] / test / element3.rl
blob66435f41a1679a0c002c72a9edde76a6d1b326d9
1 /*
2  * @LANG: obj-c
3  */
5 #include <stdio.h>
6 #include <objc/Object.h>
8 struct LangEl
10         int key;
11         char *name;
14 @interface Fsm : Object
16 @public
17         int cs;
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;
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;
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;
37 @end;
40 @implementation Fsm
42 %%{
43         machine Fsm;
45         alphtype int;
46         getkey fpc->key;
48         action a1 {}
49         action a2 {}
50         action a3 {}
52         main := ( 1 2* 3  ) 
53                         ${printf("%s\n", fpc->name);} 
54                         %/{printf("accept\n");};
55 }%%
57 %% write data;
59 - (int) initFsm;
61         %% write init;
62         return 0;
65 - (int) executeWithData:( struct LangEl *)_data len:(int)_len;
67         struct LangEl *p = _data;
68         struct LangEl *pe = _data + _len;
69         struct LangEl *eof = pe;
70         %% write exec;
72         if ( self->cs == Fsm_error ) 
73                 return -1;
74         return ( self->cs >= Fsm_first_final ) ? 1 : 0;
77 - (int) finish;
79         if ( self->cs == Fsm_error ) 
80                 return -1;
81         return ( self->cs >= Fsm_first_final ) ? 1 : 0;
85 @end
87 int main()
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];
103         return 0;
106 @interface Fsm2 : Object
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;
114         @protected
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;
126 @end
128 @implementation Fsm2
129 - (int)
130 executeWithElements:(int)elements
131 length:(unsigned)length;
133         return 0;
135 @end
137 #ifdef _____OUTPUT_____
139 two-a
140 two-b
141 two-c
142 three
143 accept
144 #endif