Import from my private repository. Snapshot after version 5.16, immediately
[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 curstate 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 curstate 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
123         %% write exec;
124 }
125
126 int tokenizer_finish( struct tokenizer *fsm )
127 {
128         %% write eof;
129
130         if ( fsm->cs == tokenizer_error )
131                 return -1;
132         if ( fsm->cs >= tokenizer_first_final )
133                 return 1;
134         return 0;
135 }
136
137 struct tokenizer tok;
138
139 void test( char *cbuf )
140 {
141         int len = strlen( cbuf );
142         high_init( &high );
143         tokenizer_init( &tok );
144         tokenizer_execute( &tok, cbuf, len );
145         if ( tokenizer_finish( &tok ) <= 0 )
146                 printf("Tokenizer FAIL\n");
147 }
148
149 char data[] =
150         "10 20 30 40 50 200 300 400 \n"
151         "d0000000 f0000000 fd000000 fe000000\n"
152         "ff000000 ffffffffffffffffffffffffff\n"
153         "ff\n";
154
155 int main()
156 {
157         test( data );
158         return 0;
159 }
160
161 #ifdef _____OUTPUT_____
162 else
163 gothigh1
164 gothigh1
165 gothigh1
166 gothigh1
167 gothigh1
168 gothigh2
169 gothigh1
170 gothigh2
171 gothigh1
172 gothigh2
173 gothigh1
174 gothigh2
175 gothigh1
176 gothigh2
177 gothigh2
178 gothigh2
179 else
180 else
181 gothigh1
182 ACCEPT
183 #endif