fixes
[henge/apc.git] / src / lexer.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 lex_error { printf("input error: character %c in filename %s is invalid\n p = %s\n", fc, str, p);}
90 action p { printf("Lexer_lexstring:: p = %s\n", p);}
91
92 N = 'N' %push_N;
93 W = 'W' %push_W;
94 S = 'S' %push_S;
95 E = 'E' %push_E;
96 NW = 'NW' %push_NW;
97 NE = 'NE' %push_NW;
98 SW = 'SW' %push_SW;
99 SE = 'SE' %push_SE;
100
101 #what goes in between tokens in a filename
102 tok_delimiter = [_];
103
104 #types of tokes a filename can contain
105 direction = (N | W | S | E | NW | NE | SW | SE) ;
106 #make sure 0x123123 doesnt get mistaken for a ref
107 dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts %push_val;
108 link = '#' %push_link;
109 SS = ('+SS' %to(push_SS)) | ('+SS' %to(push_SS) link ) ;
110 ref = '0x' >set_ts alnum+ %push_ref;
111 val = digit+ >set_ts %push_val ;
112 name = (lower+ >set_ts) %push_name ;
113 map = '+MAP' %to(push_map);
114 tok = (name | val | ref | dimensions | map | link | SS | direction);
115
116
117 main := (tok tok_delimiter)+ tok [\0];
118
119 write data nofinal noerror noprefix;
120
121 }%%
122
123 int lexer_lexstring
124 ( uint8_t* str,
125 int size
126 )
127 { uint8_t *p;
128 uint8_t *ts, *pe, *te;
129 int cs, ntok;//, tok_t;
130
131 ntok = 0;
132 p = ts = str;
133 pe = p + size + 1;
134
135 printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
136
137 %%write init;
138 %%write exec;
139
140 printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
141
142 return ntok;
143 }
144
145
146 /**************************/
147 /****Abandon All Hope******/
148 /**************************/
149 /*** ***/
150 /*** ***/
151 /*** ***/
152 /*** ***/
153
154
155 #if 0
156
157 %%{
158 machine setdirection;
159
160 action ret_north {printf("Lexer_setdirection:: direction is north, returning 4\n"); return 4;; }
161 action ret_west { printf("Lexer_setdirection:: direction is west, returning 2\n");return 2;}
162 action ret_east { printf("Lexer_setdirection:: direction is east, returning 6\n");return 6;}
163 action ret_south { printf("Lexer_setdirection:: direction is south, returning 0\n");return 0;}
164 action ret_northeast { printf("Lexer_setdirection:: direction is northeast, returning 5\n");return 5 ;}
165 action ret_northwest { printf("Lexer_setdirection:: direction is northwest, returning 3\n");return 3;}
166 action ret_southeast { printf("Lexer_setdirection:: direction is southeast, returning 7\n");return 7;}
167 action ret_southwest { printf("Lexer_setdirection:: direction is southwest, returning 1\n");return 1;}
168
169 def = [_\0] %to(ret_south);
170 N = 'N'[_\0] %to(ret_north);
171 W = 'W' [_\0] %to(ret_west);
172 S = 'S' [_\0] %to(ret_south);
173 E = 'E' [_\0] %to(ret_east);
174 NW = 'NW' [_\0] %to(ret_northwest);
175 NE = 'NE' [_\0] %to(ret_northeast);
176 SW = 'SW' [_\0] %to(ret_southwest);
177 SE = 'SE' [_\0] %to(ret_southeast);
178
179 direction = (N | W | S | E | NW | NE | SW | SE | def);
180
181 main := direction;
182
183 write data nofinal noprefix noerror;
184
185
186 }%%
187
188
189 int
190 lexer_setdirection
191 (uint8_t* str, int size)
192 { uint8_t *p, *pe, *eof;
193 int cs;
194
195
196 p = str;
197 pe = str + size + 1;
198
199 printf("|--- Begin lexer_setdirection str = %s, p = %s, pe = %s ---|\n", str,p, pe);
200
201 %%write init;
202 %%write exec noend;
203
204 printf("|--- Error in: lexer_setdirection ---|\n");
205
206 return -1;
207 }
208
209
210
211 %%{
212 machine setstr;
213
214
215 action lex_setvlink {printf("Lexer_setstr:: Returning setvlink filetype for %s\n", str); type = 5; newstrt = lexer_lexsetvlink(str); fbreak;}
216 action lex_elevlink {printf("Lexer_setstr:: Returning elevlink filetype for %s\n", str); type = 6; newstrt = lexer_lexelevlink(str); fbreak;}
217 action lex_setmodel {printf("Lexer_setstr:: Returning setmodel filetype\n"); newstrt = lexer_lexsetmodel(str); type = 1; fbreak;}
218 action lex_setmap {printf("Lexer_setstr:: Returning setmap filetype\n"); newstrt = lexer_lexsetmap(str); type = 2; fbreak;}
219 action lex_elemodel {printf("Lexer_setstr:: Returning elemodel filetype for %s\n", str); newstrt = lexer_lexelemodel(str); type = 3; fbreak;}
220 action lex_elemap {printf("Lexer_setstr:: Returning elemap filetype for %s\n", str); newstrt = lexer_lexelemap(str); type = 4; fbreak;}
221 action lex_setolink { printf("Lexer_setstr:: Returning setolink filetype\n"); type = 8; newstrt = lexer_lexsetolink(str); fbreak;}
222 action lex_eleolink { printf("Lexer_setstr:: Returning eleolink filetype\n"); type = 7; newstrt = lexer_lexeleolink(str); fbreak;}
223 action p {printf("p = %s \n",p);}
224 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);}
225
226
227 N = 'N';
228 W = 'W';
229 S = 'S';
230 E = 'E';
231 NW = 'NW';
232 NE = 'NE';
233 SW = 'SW';
234 SE = 'SE';
235
236 SS = 'SS';
237 direction = (N | W | S | E | NW | NE | SW | SE) $p;
238
239 SSD = SS direction;
240
241
242
243 name = alpha+ $p - SSD $p;
244 num = digit+ $p;
245 ref = '0x' $p alnum+ $p;
246
247
248 set_label = name | (name '_' ref);
249 ele_label = name | (name '_' ref);
250
251 model_types = (name) | (name '_' num '_' num) | (name '_' num);
252
253
254 set_model = set_label '_' SS %to(lex_setmodel);
255 set_map = set_label '_' '~' %to(lex_setmap);
256 ele_model = set_label '_' ele_label '_' SS %to(lex_elemodel);
257 ele_map = set_label '_' ele_label '_' '~' %to(lex_elemap);
258 set_olink = ref %to(lex_setolink) [\0] ;
259 ele_olink = set_label '_' '~' '_' ref [\0] %to(lex_eleolink);
260 set_vlink = set_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_setvlink);
261 ele_vlink = set_label '_' ele_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_elevlink);
262
263 main := (ele_map | set_model | set_map |ele_model | ele_vlink | set_vlink | set_olink | ele_olink);
264
265 write data;
266
267
268 }%%
269
270 int
271 lexer_setstr
272 (uint8_t* str, int size)
273 { uint8_t *p, *pe, *eof;
274 int cs, type, newstrt;
275
276 type = newstrt = 0;
277
278 p = str;
279 pe = str + size + 1;
280
281 printf("|--- Begin lexer_setstr with str = %s, p = %s, pe = %s ---|\n", str,p, pe);
282
283 %%write init;
284 %%write exec noend;
285
286 printf("|--- End lexer_setstr. Incrementing str by %d, type is %d ---|\n", newstrt, type);
287
288 return newstrt;
289 }
290
291 #endif
292
293
294 /* %%{ */
295 /* machine file_matcher; */
296
297 /* action call_ml { ts = p; fgoto set_hw ;} */
298 /* action call_tl { return 0;} */
299 /* action set_height {height = ttov(p, p-ts+1); ts = p;} */
300 /* action set_width { width = ttov(p, p-ts+1);} */
301 /* action call_lmf {lexer_lexmapfile(height, width); } */
302 /* action lex_error {printf("input error: character %c in filename %s is invalid\n = %s\n", fc, str, p);} */
303
304 /* #This machine determines the type of file we are lexing */
305 /* #and calls the appropriate machine to handle it. */
306
307 /* #TODO add mapping name */
308 /* width = digit+ %set_width; */
309 /* height = digit+ %set_height; */
310
311 /* set_hw := height . '_' . width [\0] %to(call_lmf); */
312
313 /* tok_segment = alnum; */
314 /* map_end = 'm' . '_' %to(call_ml); */
315 /* tok_end = alnum+ . [\0] %to(call_tl); */
316
317 /* file_matcher := (tok_segment+ . '_' )+ ( map_end | tok_end ); */
318
319 /* write data; */
320 /* }%% */
321
322 /* int */
323 /* lexer_matchfile */
324 /* (char* str, int size) */
325 /* { *p, *pe; */
326 /* char* ts; */
327 /* int cs, ntok, height, width; */
328
329 /* p = str; */
330 /* pe = p + size; */
331 /* height = width = 0; */
332
333 /* printf("Checking if filename is a map file:: filename = %s, p = %c, pe = %c\n", str, *p, *pe); */
334
335 /* %%write init; */
336 /* %%write exec noend; */
337
338 /* printf("Ending lexer_ismapfile on %s\n", str); */
339
340 /* return ntok; */
341 /* } */
342
343 /* %%{ */
344 /* machine vartype; */
345
346 /* action isele {return 0;} */
347 /* action ismodel {return 1;} */
348
349 /* set_name = alpha+; */
350 /* ele_name = alpha+; */
351 /* model_name = alpha+; */
352
353 /* ele = set_name '_' model_name '_' ele_name %isele; */
354 /* model = set_name '_' model_name [\0] %ismodel; */
355
356
357 /* ismodel := (ele | model); */
358
359 /* write data; */
360
361 /* }%% */
362
363 /* int */
364 /* lexer_ismodel */
365 /* (uint8_t* str, int size) */
366 /* { uint8_t *p, *pe, *eof; */
367 /* int cs; */
368
369 /* p = str; */
370 /* pe = p + size + 1; */
371
372 /* %%write init; */
373 /* %%write exec; */
374
375
376 /* } */