Scanners now ensure that a pattern's leaving actions are executed.
[ragel.git] / test / forder2.rl
blobd92f888d00e03de16533cfac04b67aa72622f1f2
1 /*
2  * @LANG: c
3  */
5 #include <stdio.h>
6 #include <string.h>
8 /*
9  * After the fact start and ending transitions. Behaves like constructors of
10  * and destructors in c++.
11  */
13 struct forder
15         int cs;
18 %%{
19         machine forder;
20         variable cs fsm->cs;
22         inner = 'inner'
23                 >{printf("enter inner\n");}
24                 ${printf("inside inner\n");}
25                 %{printf("leave inner\n");}
26         ;
28         outter = inner
29                 >{printf("enter outter\n");}
30                 ${printf("inside outter\n");}
31                 %{printf("leave outter\n");}
32         ;
34         main := outter . '\n';
35 }%%
37 %% write data;
39 void forder_init( struct forder *fsm )
41         %% write init;
44 void forder_execute( struct forder *fsm, const char *_data, int _len )
46         const char *p = _data;
47         const char *pe = _data+_len;
49         %% write exec;
52 int forder_finish( struct forder *fsm )
54         if ( fsm->cs == forder_error )
55                 return -1;
56         if ( fsm->cs >= forder_first_final )
57                 return 1;
58         return 0;
61 struct forder fsm;
63 void test( char *buf )
65         int len = strlen( buf );
66         forder_init( &fsm );
67         forder_execute( &fsm, buf, len );
68         if ( forder_finish( &fsm ) > 0 )
69                 printf("ACCEPT\n");
70         else
71                 printf("FAIL\n");
75 int main()
77         test( "inner\n");
79         test(
80                 "inner\n"
81                 "foobar\n"
82         );
84         test( "" );
85         test( "\n" );
86         test( "inn\n" );
88         return 0;
91 #ifdef _____OUTPUT_____
92 enter outter
93 enter inner
94 inside inner
95 inside outter
96 inside inner
97 inside outter
98 inside inner
99 inside outter
100 inside inner
101 inside outter
102 inside inner
103 inside outter
104 leave inner
105 leave outter
106 ACCEPT
107 enter outter
108 enter inner
109 inside inner
110 inside outter
111 inside inner
112 inside outter
113 inside inner
114 inside outter
115 inside inner
116 inside outter
117 inside inner
118 inside outter
119 leave inner
120 leave outter
121 FAIL
122 FAIL
123 FAIL
124 enter outter
125 enter inner
126 inside inner
127 inside outter
128 inside inner
129 inside outter
130 inside inner
131 inside outter
132 FAIL
133 #endif