d8db2b4aa85db2a5f52a37ab39c92a2c7eae1dd6
[henge/apc.git] / src / lexer_fsm.rl
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include "parser.tab.h"
6 #include "apc.h"
7 #include <unistdio.h>
8 #include <unistr.h>
9 extern //lexer.c
10 void lexer_pushtok(int, YYSTYPE);
11 /* Public */
12 int lexer_setdirection(uint8_t*, int);
13 int lexer_lexstring(uint8_t*, int);
14 int lexer_setstr(uint8_t*, int);
15
16 %%{
17 machine lexstring;
18
19 # set up yylval and tok_t to be pushed to stack
20 action push_ref { te = NULL; errno = 0;
21 yylval.ref = strtoll((char*)ts,(char**)&te,16);
22 if (errno | (te != NULL))
23 { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
24 if (te != NULL)
25 { while (str++ < te)
26 fputc(' ', stderr);
27 fputc('^', stderr);
28 }
29 exit(1);
30 }
31 lexer_pushtok(REF, yylval); ntok++;
32 }
33 action push_link { lexer_pushtok(LINK,(YYSTYPE)0); ntok++; }
34 action push_val { te = NULL; errno = 0;
35 yylval.val = strtoll((char*)ts,(char**)&te,10);
36 if (errno)
37 { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
38 exit(1);
39 }
40 lexer_pushtok(NUM, yylval);
41 }
42 action push_name { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
43 lexer_pushtok(NAME, yylval);
44 ntok++;
45 }
46 action push_map { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
47 yylval.str = (uint8_t*) '~';
48 lexer_pushtok(MOPEN, yylval);
49 ntok++;
50 }
51 action set_ts { printf("Lexer_lexstring:: action:set_ts. ts = %s\n", p); ts = p; }
52 action push_SS { printf("Lexer_lexstring:: action:push_SS. p = %s\n",p);
53 yylval.str = (uint8_t*) "SS";
54 lexer_pushtok(SS, yylval);
55 ntok++;
56 }
57 action push_S { printf("Lexer_lexstring:: action:push_S. p = %s\n", p);
58 yylval.val = 0;
59 lexer_pushtok(D, yylval);
60 }
61 action push_SW { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p);
62 yylval.val = 1;
63 lexer_pushtok(D, yylval);
64 }
65 action push_W { printf("Lexer_lexstring:: action:push_W. p = %s\n", p);
66 yylval.val = 2;
67 lexer_pushtok(D, yylval);
68 }
69 action push_NW { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p);
70 yylval.val = 3;
71 lexer_pushtok(D, yylval);
72 }
73 action push_N { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
74 yylval.val = 4;
75 lexer_pushtok(D, yylval);
76 }
77 action push_NE { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p);
78 yylval.val = 5;
79 lexer_pushtok(D, yylval);
80 }
81 action push_E { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
82 yylval.val = 6;
83 lexer_pushtok(D, yylval);
84 }
85 action push_SE { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
86 yylval.val = 7;
87 lexer_pushtok(D, yylval);
88 }
89 action push_height { printf("Lexer_lexstring:: action:push_height p = %s\n", p);
90 }
91 action push_width { printf("Lexer_lexstring:: action:push_height p = %s\n", p);
92 }
93 #action lex_error { printf("input error: character %c in filename %s is invalid\n p = %s\n", fc, str, p);}
94 action p { printf("Lexer_lexstring:: p = %s\n", p);}
95
96 N = 'N' %push_N;
97 W = 'W' %push_W;
98 S = 'S' %push_S;
99 E = 'E' %push_E;
100 NW = 'NW' %push_NW;
101 NE = 'NE' %push_NW;
102 SW = 'SW' %push_SW;
103 SE = 'SE' %push_SE;
104
105 #what goes in between tokens in a filename
106 tok_delimiter = [_];
107
108 #types of tokes a filename can contain
109 direction = (N | W | S | E | NW | NE | SW | SE) ;
110 #make sure 0x123123 doesnt get mistaken for a ref
111 dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts %push_val;
112 link = '#' %push_link;
113 SS = ('+SS' %to(push_SS)) | ('+SS' %to(push_SS) link ) ;
114 ref = '0x' >set_ts alnum+ %push_ref;
115 val = digit+ >set_ts %push_val ;
116 name = (lower+ >set_ts) %push_name ;
117 map = '+MAP' %to(push_map);
118 tok = (name | val | ref | dimensions | map | link | SS | direction);
119
120
121 main := (tok tok_delimiter)+ tok [\0];
122
123 write data nofinal noerror noprefix;
124
125 }%%
126
127 int lexer_lexstring
128 ( uint8_t* str,
129 int size
130 )
131 { uint8_t *p;
132 uint8_t *ts, *pe, *te;
133 int cs, ntok;//, tok_t;
134
135 ntok = 0;
136 p = ts = str;
137 pe = p + size + 1;
138
139 printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
140
141 %%write init;
142 %%write exec;
143
144 printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
145
146 return ntok;
147 }
148
149
150 /**************************/
151 /****Abandon All Hope******/
152 /**************************/
153 /*** ***/
154 /*** ***/
155 /*** ***/
156 /*** ***/
157
158
159 #if 0
160
161 %%{
162 machine setdirection;
163
164 action ret_north {printf("Lexer_setdirection:: direction is north, returning 4\n"); return 4;; }
165 action ret_west { printf("Lexer_setdirection:: direction is west, returning 2\n");return 2;}
166 action ret_east { printf("Lexer_setdirection:: direction is east, returning 6\n");return 6;}
167 action ret_south { printf("Lexer_setdirection:: direction is south, returning 0\n");return 0;}
168 action ret_northeast { printf("Lexer_setdirection:: direction is northeast, returning 5\n");return 5 ;}
169 action ret_northwest { printf("Lexer_setdirection:: direction is northwest, returning 3\n");return 3;}
170 action ret_southeast { printf("Lexer_setdirection:: direction is southeast, returning 7\n");return 7;}
171 action ret_southwest { printf("Lexer_setdirection:: direction is southwest, returning 1\n");return 1;}
172
173 def = [_\0] %to(ret_south);
174 N = 'N'[_\0] %to(ret_north);
175 W = 'W' [_\0] %to(ret_west);
176 S = 'S' [_\0] %to(ret_south);
177 E = 'E' [_\0] %to(ret_east);
178 NW = 'NW' [_\0] %to(ret_northwest);
179 NE = 'NE' [_\0] %to(ret_northeast);
180 SW = 'SW' [_\0] %to(ret_southwest);
181 SE = 'SE' [_\0] %to(ret_southeast);
182
183 direction = (N | W | S | E | NW | NE | SW | SE | def);
184
185 main := direction;
186
187 write data nofinal noprefix noerror;
188
189
190 }%%
191
192
193 int
194 lexer_setdirection
195 (uint8_t* str, int size)
196 { uint8_t *p, *pe, *eof;
197 int cs;
198
199
200 p = str;
201 pe = str + size + 1;
202
203 printf("|--- Begin lexer_setdirection str = %s, p = %s, pe = %s ---|\n", str,p, pe);
204
205 %%write init;
206 %%write exec noend;
207
208 printf("|--- Error in: lexer_setdirection ---|\n");
209
210 return -1;
211 }
212
213
214
215 %%{
216 machine setstr;
217
218
219 action lex_setvlink {printf("Lexer_setstr:: Returning setvlink filetype for %s\n", str); type = 5; newstrt = lexer_lexsetvlink(str); fbreak;}
220 action lex_elevlink {printf("Lexer_setstr:: Returning elevlink filetype for %s\n", str); type = 6; newstrt = lexer_lexelevlink(str); fbreak;}
221 action lex_setmodel {printf("Lexer_setstr:: Returning setmodel filetype\n"); newstrt = lexer_lexsetmodel(str); type = 1; fbreak;}
222 action lex_setmap {printf("Lexer_setstr:: Returning setmap filetype\n"); newstrt = lexer_lexsetmap(str); type = 2; fbreak;}
223 action lex_elemodel {printf("Lexer_setstr:: Returning elemodel filetype for %s\n", str); newstrt = lexer_lexelemodel(str); type = 3; fbreak;}
224 action lex_elemap {printf("Lexer_setstr:: Returning elemap filetype for %s\n", str); newstrt = lexer_lexelemap(str); type = 4; fbreak;}
225 action lex_setolink { printf("Lexer_setstr:: Returning setolink filetype\n"); type = 8; newstrt = lexer_lexsetolink(str); fbreak;}
226 action lex_eleolink { printf("Lexer_setstr:: Returning eleolink filetype\n"); type = 7; newstrt = lexer_lexeleolink(str); fbreak;}
227 action p {printf("p = %s \n",p);}
228 action name_error {printf("In %s, there is a syntactic error. Make sure your set/element names dont conflict with the reserved keywords.\n", str);}
229
230
231 N = 'N';
232 W = 'W';
233 S = 'S';
234 E = 'E';
235 NW = 'NW';
236 NE = 'NE';
237 SW = 'SW';
238 SE = 'SE';
239
240 SS = 'SS';
241 direction = (N | W | S | E | NW | NE | SW | SE) $p;
242
243 SSD = SS direction;
244
245
246
247 name = alpha+ $p - SSD $p;
248 num = digit+ $p;
249 ref = '0x' $p alnum+ $p;
250
251
252 set_label = name | (name '_' ref);
253 ele_label = name | (name '_' ref);
254
255 model_types = (name) | (name '_' num '_' num) | (name '_' num);
256
257
258 set_model = set_label '_' SS %to(lex_setmodel);
259 set_map = set_label '_' '~' %to(lex_setmap);
260 ele_model = set_label '_' ele_label '_' SS %to(lex_elemodel);
261 ele_map = set_label '_' ele_label '_' '~' %to(lex_elemap);
262 set_olink = ref %to(lex_setolink) [\0] ;
263 ele_olink = set_label '_' '~' '_' ref [\0] %to(lex_eleolink);
264 set_vlink = set_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_setvlink);
265 ele_vlink = set_label '_' ele_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_elevlink);
266
267 main := (ele_map | set_model | set_map |ele_model | ele_vlink | set_vlink | set_olink | ele_olink);
268
269 write data;
270
271
272 }%%
273
274 int
275 lexer_setstr
276 (uint8_t* str, int size)
277 { uint8_t *p, *pe, *eof;
278 int cs, type, newstrt;
279
280 type = newstrt = 0;
281
282 p = str;
283 pe = str + size + 1;
284
285 printf("|--- Begin lexer_setstr with str = %s, p = %s, pe = %s ---|\n", str,p, pe);
286
287 %%write init;
288 %%write exec noend;
289
290 printf("|--- End lexer_setstr. Incrementing str by %d, type is %d ---|\n", newstrt, type);
291
292 return newstrt;
293 }
294
295 #endif
296
297
298 /* %%{ */
299 /* machine file_matcher; */
300
301 /* action call_ml { ts = p; fgoto set_hw ;} */
302 /* action call_tl { return 0;} */
303 /* action set_height {height = ttov(p, p-ts+1); ts = p;} */
304 /* action set_width { width = ttov(p, p-ts+1);} */
305 /* action call_lmf {lexer_lexmapfile(height, width); } */
306 /* action lex_error {printf("input error: character %c in filename %s is invalid\n = %s\n", fc, str, p);} */
307
308 /* #This machine determines the type of file we are lexing */
309 /* #and calls the appropriate machine to handle it. */
310
311 /* #TODO add mapping name */
312 /* width = digit+ %set_width; */
313 /* height = digit+ %set_height; */
314
315 /* set_hw := height . '_' . width [\0] %to(call_lmf); */
316
317 /* tok_segment = alnum; */
318 /* map_end = 'm' . '_' %to(call_ml); */
319 /* tok_end = alnum+ . [\0] %to(call_tl); */
320
321 /* file_matcher := (tok_segment+ . '_' )+ ( map_end | tok_end ); */
322
323 /* write data; */
324 /* }%% */
325
326 /* int */
327 /* lexer_matchfile */
328 /* (char* str, int size) */
329 /* { *p, *pe; */
330 /* char* ts; */
331 /* int cs, ntok, height, width; */
332
333 /* p = str; */
334 /* pe = p + size; */
335 /* height = width = 0; */
336
337 /* printf("Checking if filename is a map file:: filename = %s, p = %c, pe = %c\n", str, *p, *pe); */
338
339 /* %%write init; */
340 /* %%write exec noend; */
341
342 /* printf("Ending lexer_ismapfile on %s\n", str); */
343
344 /* return ntok; */
345 /* } */
346
347 /* %%{ */
348 /* machine vartype; */
349
350 /* action isele {return 0;} */
351 /* action ismodel {return 1;} */
352
353 /* set_name = alpha+; */
354 /* ele_name = alpha+; */
355 /* model_name = alpha+; */
356
357 /* ele = set_name '_' model_name '_' ele_name %isele; */
358 /* model = set_name '_' model_name [\0] %ismodel; */
359
360
361 /* ismodel := (ele | model); */
362
363 /* write data; */
364
365 /* }%% */
366
367 /* int */
368 /* lexer_ismodel */
369 /* (uint8_t* str, int size) */
370 /* { uint8_t *p, *pe, *eof; */
371 /* int cs; */
372
373 /* p = str; */
374 /* pe = p + size + 1; */
375
376 /* %%write init; */
377 /* %%write exec; */
378
379
380 /* } */