Initialize Tizen 2.3
[external/ragel.git] / test / element1.rl
1 /*
2  * @LANG: c++
3  */
4
5 #include <iostream>
6 using namespace std;
7
8 struct LangEl
9 {
10         int key;
11         const char *name;
12 };
13
14 struct Fsm
15 {
16         int cs;
17
18         // Initialize the machine. Invokes any init statement blocks. Returns 0
19         // if the machine begins in a non-accepting state and 1 if the machine
20         // begins in an accepting state.
21         int init( );
22
23         // Execute the machine on a block of data. Returns -1 if after processing
24         // the data, the machine is in the error state and can never accept, 0 if
25         // the machine is in a non-accepting state and 1 if the machine is in an
26         // accepting state.
27         int execute(  LangEl *data, int len );
28
29         // Indicate that there is no more data. Returns -1 if the machine finishes
30         // in the error state and does not accept, 0 if the machine finishes
31         // in any other non-accepting state and 1 if the machine finishes in an
32         // accepting state.
33         int finish( );
34
35 };
36
37 %%{
38         machine Fsm;
39
40         alphtype int;
41         getkey fpc->key;
42         variable eof eof_marker;
43
44         action a1 {}
45         action a2 {}
46         action a3 {}
47
48         main := ( 1 2* 3  ) 
49                         ${cout << fpc->name << endl;} 
50                         %/{cout << "accept" << endl;};
51 }%%
52
53 %% write data;
54
55 int Fsm::init( )
56 {
57         %% write init;
58         return 0;
59 }
60
61 int Fsm::execute( LangEl *data, int len )
62 {
63         LangEl *p = data;
64         LangEl *pe = data + len;
65         LangEl *eof_marker = pe;
66         %% write exec;
67
68         if ( cs == Fsm_error )
69                 return -1;
70         if ( cs >= Fsm_first_final )
71                 return 1;
72         return 0;
73 }
74
75 int Fsm::finish( )
76 {
77         if ( cs == Fsm_error )
78                 return -1;
79         if ( cs >= Fsm_first_final )
80                 return 1;
81         return 0;
82 }
83
84 int main( )
85 {
86         static Fsm fsm;
87         static LangEl lel[] = { 
88                 {1, "one"}, 
89                 {2, "two-a"}, 
90                 {2, "two-b"}, 
91                 {2, "two-c"}, 
92                 {3, "three"}
93         };
94
95         fsm.init();
96         fsm.execute( lel, 5 );
97         fsm.finish();
98         return 0;
99 }
100
101 #ifdef _____OUTPUT_____
102 one
103 two-a
104 two-b
105 two-c
106 three
107 accept
108 #endif