5 #include "parser.tab.h"
10 void lexer_pushtok(int, YYSTYPE);
12 int lexer_setdirection(uint8_t*, int);
13 int lexer_lexfile(const uint8_t*);
14 int lexer_lexstring(const uint8_t*, int);
15 int lexer_setstr(uint8_t*, int);
21 #define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (YYSTYPE*)(L), apc_cstate)
22 #define LEXTOK(T,L) do { \
26 #define LEXFACE(F) do { \
28 LEXTOK(FACING, &lval.face); \
31 /* Lexstring is the main lexer for APC and is generated by ragel. It lexes file names of files
32 that have been scanned and pushes their types and values into the tok_stack, which yyparse
33 eventually calls during parsing. */
38 # set up yylval and tok_t to be pushed to stack
39 action push_ref { te = NULL; errno = 0;
40 lval.ref = strtoll((char*)ts,(char**)&te,16);
41 if (errno | (te != NULL))
42 { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
50 LEXTOK(REF, &lval.ref);
52 action push_link { lval.val = 0;
53 PUSHTOK(LINK, &lval.val); }
54 action push_val { te = NULL; errno = 0;
55 lval.val = strtoll((char*)ts,(char**)&te,10);
57 { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
60 LEXTOK(NUM, &lval.val);
62 action push_name { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
65 action push_map { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
68 action set_ts { printf("Lexer_lexstring:: action:set_ts. ts = %s\n", p); ts = p; }
69 action push_SS { printf("Lexer_lexstring:: action:push_SS. p = %s\n",p);
72 action push_S { printf("Lexer_lexstring:: action:push_S. p = %s\n", p);
75 action push_SW { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p);
78 action push_W { printf("Lexer_lexstring:: action:push_W. p = %s\n", p);
81 action push_NW { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p);
84 action push_N { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
87 action push_NE { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p);
90 action push_E { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
93 action push_SE { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
96 action ref_error { printf("ref from %s to %s has an inappropriate amount of hex digits, it must have eight.\n", ts, p);
99 action p { printf("Lexer_lexstring:: p = %s\n", p);}
112 direction = (N | W | S | E | NW | NE | SW | SE) ;
113 dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts %push_val;
114 link = '#' %push_link;
115 SS = ('+SS' %to(push_SS)) | ('+SS' %to(push_SS) link ) ;
116 ref = '0x' >set_ts alnum{8} $err(ref_error) %push_ref ;
117 val = digit+ >set_ts %push_val ;
118 name = (lower+ >set_ts) (lower | digit)+ %push_name ;
119 map = '+MAP' %to(push_map);
120 tok = (name | val | ref | dimensions | map | link | SS | direction);
123 main := (tok tok_delimiter)+ tok [\0];
125 write data nofinal noerror noprefix;
130 ( const uint8_t* str,
134 const uint8_t *ts, *pe, *te, *eof;
140 pe = eof = p + size + 1;
143 printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
148 printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
153 /* Lexical analysis of a file
154 Strips a filename to its base name, then sends it to lexer_lexstring before
155 pushing a PATH token with the filename
156 Returns the number of tokens pushed to the parser.
159 ( uint8_t const* filename )
160 { uint8_t const* last_period,* iter;
163 for (iter = filename; *iter; iter++)
166 ntok = (last_period) ?
167 lexer_lexstring(filename, (int)(last_period - filename))
168 : lexer_lexstring(filename, (int)(iter - filename));
169 PUSHTOK(PATH,&filename);
174 /**************************/
175 /****Abandon All Hope******/
176 /**************************/
186 machine setdirection;
188 action ret_north {printf("Lexer_setdirection:: direction is north, returning 4\n"); return 4;; }
189 action ret_west { printf("Lexer_setdirection:: direction is west, returning 2\n");return 2;}
190 action ret_east { printf("Lexer_setdirection:: direction is east, returning 6\n");return 6;}
191 action ret_south { printf("Lexer_setdirection:: direction is south, returning 0\n");return 0;}
192 action ret_northeast { printf("Lexer_setdirection:: direction is northeast, returning 5\n");return 5 ;}
193 action ret_northwest { printf("Lexer_setdirection:: direction is northwest, returning 3\n");return 3;}
194 action ret_southeast { printf("Lexer_setdirection:: direction is southeast, returning 7\n");return 7;}
195 action ret_southwest { printf("Lexer_setdirection:: direction is southwest, returning 1\n");return 1;}
197 def = [_\0] %to(ret_south);
198 N = 'N'[_\0] %to(ret_north);
199 W = 'W' [_\0] %to(ret_west);
200 S = 'S' [_\0] %to(ret_south);
201 E = 'E' [_\0] %to(ret_east);
202 NW = 'NW' [_\0] %to(ret_northwest);
203 NE = 'NE' [_\0] %to(ret_northeast);
204 SW = 'SW' [_\0] %to(ret_southwest);
205 SE = 'SE' [_\0] %to(ret_southeast);
207 direction = (N | W | S | E | NW | NE | SW | SE | def);
211 write data nofinal noprefix noerror;
219 (uint8_t* str, int size)
220 { uint8_t *p, *pe, *eof;
227 printf("|--- Begin lexer_setdirection str = %s, p = %s, pe = %s ---|\n", str,p, pe);
232 printf("|--- Error in: lexer_setdirection ---|\n");
243 action lex_setvlink {printf("Lexer_setstr:: Returning setvlink filetype for %s\n", str); type = 5; newstrt = lexer_lexsetvlink(str); fbreak;}
244 action lex_elevlink {printf("Lexer_setstr:: Returning elevlink filetype for %s\n", str); type = 6; newstrt = lexer_lexelevlink(str); fbreak;}
245 action lex_setmodel {printf("Lexer_setstr:: Returning setmodel filetype\n"); newstrt = lexer_lexsetmodel(str); type = 1; fbreak;}
246 action lex_setmap {printf("Lexer_setstr:: Returning setmap filetype\n"); newstrt = lexer_lexsetmap(str); type = 2; fbreak;}
247 action lex_elemodel {printf("Lexer_setstr:: Returning elemodel filetype for %s\n", str); newstrt = lexer_lexelemodel(str); type = 3; fbreak;}
248 action lex_elemap {printf("Lexer_setstr:: Returning elemap filetype for %s\n", str); newstrt = lexer_lexelemap(str); type = 4; fbreak;}
249 action lex_setolink { printf("Lexer_setstr:: Returning setolink filetype\n"); type = 8; newstrt = lexer_lexsetolink(str); fbreak;}
250 action lex_eleolink { printf("Lexer_setstr:: Returning eleolink filetype\n"); type = 7; newstrt = lexer_lexeleolink(str); fbreak;}
251 action p {printf("p = %s \n",p);}
252 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);}
265 direction = (N | W | S | E | NW | NE | SW | SE) $p;
271 name = alpha+ $p - SSD $p;
273 ref = '0x' $p alnum+ $p;
276 set_label = name | (name '_' ref);
277 ele_label = name | (name '_' ref);
279 model_types = (name) | (name '_' num '_' num) | (name '_' num);
282 set_model = set_label '_' SS %to(lex_setmodel);
283 set_map = set_label '_' '~' %to(lex_setmap);
284 ele_model = set_label '_' ele_label '_' SS %to(lex_elemodel);
285 ele_map = set_label '_' ele_label '_' '~' %to(lex_elemap);
286 set_olink = ref %to(lex_setolink) [\0] ;
287 ele_olink = set_label '_' '~' '_' ref [\0] %to(lex_eleolink);
288 set_vlink = set_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_setvlink);
289 ele_vlink = set_label '_' ele_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_elevlink);
291 main := (ele_map | set_model | set_map |ele_model | ele_vlink | set_vlink | set_olink | ele_olink);
300 (uint8_t* str, int size)
301 { uint8_t *p, *pe, *eof;
302 int cs, type, newstrt;
309 printf("|--- Begin lexer_setstr with str = %s, p = %s, pe = %s ---|\n", str,p, pe);
314 printf("|--- End lexer_setstr. Incrementing str by %d, type is %d ---|\n", newstrt, type);
323 /* machine file_matcher; */
325 /* action call_ml { ts = p; fgoto set_hw ;} */
326 /* action call_tl { return 0;} */
327 /* action set_height {height = ttov(p, p-ts+1); ts = p;} */
328 /* action set_width { width = ttov(p, p-ts+1);} */
329 /* action call_lmf {lexer_lexmapfile(height, width); } */
330 /* action lex_error {printf("input error: character %c in filename %s is invalid\n = %s\n", fc, str, p);} */
332 /* #This machine determines the type of file we are lexing */
333 /* #and calls the appropriate machine to handle it. */
335 /* #TODO add mapping name */
336 /* width = digit+ %set_width; */
337 /* height = digit+ %set_height; */
339 /* set_hw := height . '_' . width [\0] %to(call_lmf); */
341 /* tok_segment = alnum; */
342 /* map_end = 'm' . '_' %to(call_ml); */
343 /* tok_end = alnum+ . [\0] %to(call_tl); */
345 /* file_matcher := (tok_segment+ . '_' )+ ( map_end | tok_end ); */
351 /* lexer_matchfile */
352 /* (char* str, int size) */
355 /* int cs, ntok, height, width; */
359 /* height = width = 0; */
361 /* printf("Checking if filename is a map file:: filename = %s, p = %c, pe = %c\n", str, *p, *pe); */
364 /* %%write exec noend; */
366 /* printf("Ending lexer_ismapfile on %s\n", str); */
372 /* machine vartype; */
374 /* action isele {return 0;} */
375 /* action ismodel {return 1;} */
377 /* set_name = alpha+; */
378 /* ele_name = alpha+; */
379 /* model_name = alpha+; */
381 /* ele = set_name '_' model_name '_' ele_name %isele; */
382 /* model = set_name '_' model_name [\0] %ismodel; */
385 /* ismodel := (ele | model); */
393 /* (uint8_t* str, int size) */
394 /* { uint8_t *p, *pe, *eof; */
398 /* pe = p + size + 1; */