Introcuded the "eof" variable for indicating the end of file. The p variable is
[external/ragel.git] / test / high1.rl
1 /*
2  * @LANG: c
3  * @ALLOW_GENFLAGS: -T0 -T1 -G0 -G1 -G2
4  */
5
6 /**
7  * Test a high character to make sure signedness 
8  * isn't messing us up.
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 struct high
16 {
17         int cs;
18 };
19
20 %%{
21         machine high;
22         variable cs fsm->cs;
23
24         # We Want the header portion.
25         alphtype unsigned int;
26
27         main := ( 
28                         0x20 .. 0xefffffff   @1 @{printf("gothigh1\n");} | 
29                         0xf0000000           @1 @{printf("gothigh1\n");} | 
30                         0x200 .. 0xfe000000  @1 @{printf("gothigh2\n");} |
31                         any                  @0 @{printf("else\n");}
32                 )*;
33 }%%
34
35 %% write data;
36
37 void high_init( struct high *fsm )
38 {
39         %% write init;
40 }
41
42 void high_execute( struct high *fsm, const unsigned int *_data, int _len )
43 {
44         const unsigned int *p = _data;
45         const unsigned int *pe = _data+_len;
46
47         %% write exec;
48 }
49
50 int high_finish( struct high *fsm )
51 {
52         %% write eof;
53
54         if ( fsm->cs == high_error )
55                 return -1;
56         if ( fsm->cs >= high_first_final )
57                 return 1;
58         return 0;
59 }
60
61 struct high high;
62
63 #define BUFSIZE 1024
64 char cbuf[BUFSIZE];
65 unsigned int buf[BUFSIZE];
66 int buflen = 0;
67 char numbuf[9];
68 int numlen = 0;
69
70 struct tokenizer
71 {
72         int cs;
73 };
74
75 %%{
76         machine tokenizer;
77         variable cs fsm->cs;
78
79         action bufdigit {
80                 if ( numlen < 8 )
81                         numbuf[numlen++] = fc;
82         }
83
84         action writeDigit {
85                 /* Null terminate the buffer storing the number and reset. */
86                 numbuf[numlen] = 0;
87                 numlen = 0;
88
89                 /* Store the number in the buf. If the buf is full then 
90                  * flush and reset the buffer. */
91                 buf[buflen++] = strtoul( numbuf, 0, 16 );
92                 if ( buflen == BUFSIZE ) {
93                         high_execute( &high, buf, BUFSIZE );
94                         buflen = 0;
95                 }
96         }
97
98         action finish {
99                 if ( buflen > 0 )
100                         high_execute( &high, buf, buflen );
101                 if ( high_finish( &high ) > 0 )
102                         printf("ACCEPT\n");
103                 else
104                         printf("FAIL\n");
105         }
106
107         num = ( digit | 'a'..'f' )+ $bufdigit %writeDigit;
108         main := ( num $1 %0 | space )* %/finish;
109 }%%
110
111 %% write data;
112
113 void tokenizer_init( struct tokenizer *fsm )
114 {
115         %% write init;
116 }
117
118 void tokenizer_execute( struct tokenizer *fsm, const char *_data, int _len )
119 {
120         const char *p = _data;
121         const char *pe = _data+_len;
122         const char *eof = pe;
123
124         %% write exec;
125 }
126
127 int tokenizer_finish( struct tokenizer *fsm )
128 {
129         if ( fsm->cs == tokenizer_error )
130                 return -1;
131         if ( fsm->cs >= tokenizer_first_final )
132                 return 1;
133         return 0;
134 }
135
136 struct tokenizer tok;
137
138 void test( char *cbuf )
139 {
140         int len = strlen( cbuf );
141         high_init( &high );
142         tokenizer_init( &tok );
143         tokenizer_execute( &tok, cbuf, len );
144         if ( tokenizer_finish( &tok ) <= 0 )
145                 printf("Tokenizer FAIL\n");
146 }
147
148 char data[] =
149         "10 20 30 40 50 200 300 400 \n"
150         "d0000000 f0000000 fd000000 fe000000\n"
151         "ff000000 ffffffffffffffffffffffffff\n"
152         "ff\n";
153
154 int main()
155 {
156         test( data );
157         return 0;
158 }
159
160 #ifdef _____OUTPUT_____
161 else
162 gothigh1
163 gothigh1
164 gothigh1
165 gothigh1
166 gothigh1
167 gothigh2
168 gothigh1
169 gothigh2
170 gothigh1
171 gothigh2
172 gothigh1
173 gothigh2
174 gothigh1
175 gothigh2
176 gothigh2
177 gothigh2
178 else
179 else
180 gothigh1
181 ACCEPT
182 #endif