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