Scanners now ensure that a pattern's leaving actions are executed.
[ragel.git] / test / high1.rl
blob9179c893429364eb778e6a581504f51b53068e80
1 /*
2  * @LANG: c
3  * @ALLOW_GENFLAGS: -T0 -T1 -G0 -G1 -G2
4  */
6 /**
7  * Test a high character to make sure signedness 
8  * isn't messing us up.
9  */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 struct high
17         int cs;
20 %%{
21         machine high;
22         variable cs fsm->cs;
24         # We Want the header portion.
25         alphtype unsigned int;
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 }%%
35 %% write data;
37 void high_init( struct high *fsm )
39         %% write init;
42 void high_execute( struct high *fsm, const unsigned int *_data, int _len )
44         const unsigned int *p = _data;
45         const unsigned int *pe = _data+_len;
47         %% write exec;
50 int high_finish( struct high *fsm )
52         if ( fsm->cs == high_error )
53                 return -1;
54         if ( fsm->cs >= high_first_final )
55                 return 1;
56         return 0;
59 struct high high;
61 #define BUFSIZE 1024
62 char cbuf[BUFSIZE];
63 unsigned int buf[BUFSIZE];
64 int buflen = 0;
65 char numbuf[9];
66 int numlen = 0;
68 struct tokenizer
70         int cs;
73 %%{
74         machine tokenizer;
75         variable cs fsm->cs;
77         action bufdigit {
78                 if ( numlen < 8 )
79                         numbuf[numlen++] = fc;
80         }
82         action writeDigit {
83                 /* Null terminate the buffer storing the number and reset. */
84                 numbuf[numlen] = 0;
85                 numlen = 0;
87                 /* Store the number in the buf. If the buf is full then 
88                  * flush and reset the buffer. */
89                 buf[buflen++] = strtoul( numbuf, 0, 16 );
90                 if ( buflen == BUFSIZE ) {
91                         high_execute( &high, buf, BUFSIZE );
92                         buflen = 0;
93                 }
94         }
96         action finish {
97                 if ( buflen > 0 )
98                         high_execute( &high, buf, buflen );
99                 if ( high_finish( &high ) > 0 )
100                         printf("ACCEPT\n");
101                 else
102                         printf("FAIL\n");
103         }
105         num = ( digit | 'a'..'f' )+ $bufdigit %writeDigit;
106         main := ( num $1 %0 | space )* %/finish;
109 %% write data;
111 void tokenizer_init( struct tokenizer *fsm )
113         %% write init;
116 void tokenizer_execute( struct tokenizer *fsm, const char *_data, int _len )
118         const char *p = _data;
119         const char *pe = _data+_len;
120         const char *eof = pe;
122         %% write exec;
125 int tokenizer_finish( struct tokenizer *fsm )
127         if ( fsm->cs == tokenizer_error )
128                 return -1;
129         if ( fsm->cs >= tokenizer_first_final )
130                 return 1;
131         return 0;
134 struct tokenizer tok;
136 void test( char *cbuf )
138         int len = strlen( cbuf );
139         high_init( &high );
140         tokenizer_init( &tok );
141         tokenizer_execute( &tok, cbuf, len );
142         if ( tokenizer_finish( &tok ) <= 0 )
143                 printf("Tokenizer FAIL\n");
146 char data[] =
147         "10 20 30 40 50 200 300 400 \n"
148         "d0000000 f0000000 fd000000 fe000000\n"
149         "ff000000 ffffffffffffffffffffffffff\n"
150         "ff\n";
152 int main()
154         test( data );
155         return 0;
158 #ifdef _____OUTPUT_____
159 else
160 gothigh1
161 gothigh1
162 gothigh1
163 gothigh1
164 gothigh1
165 gothigh2
166 gothigh1
167 gothigh2
168 gothigh1
169 gothigh2
170 gothigh1
171 gothigh2
172 gothigh1
173 gothigh2
174 gothigh2
175 gothigh2
176 else
177 else
178 gothigh1
179 ACCEPT
180 #endif