5 #include "parser.tab.h"
10 void lexer_pushtok(int, YYSTYPE);
12 int lexer_setdirection(uint8_t*, int);
13 int lexer_lexfile(uint8_t*);
14 int lexer_lexdir(uint8_t*);
15 int lexer_lexstring(uint8_t*, int);
16 int lexer_setstr(uint8_t*, int);
23 YYSTYPE lval_stack[1024], * lvalsp;
24 #define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (L), apc_cstate)
25 #define LEXTOK(T,Y,L) do { \
31 #define PUSHFACE(F) LEXTOK(FACING, face, F)
32 #define PUSHREF(R) LEXTOK(REF, ref, R)
33 #define PUSHLINK() LEXTOK(LINK, val, 0)
34 #define PUSHNUM(N) LEXTOK(NUM, val, N)
35 #define PUSHNAME(N) LEXTOK(NAME, str, N)
36 #define PUSHOP(O) LEXTOK(O, val, 0)
37 #define PUSHPATH(P) LEXTOK(PATH, str, P)
39 /* Lexstring is the main lexer for APC and is generated by ragel. It lexes file names of files
40 that have been scanned and pushes their types and values into the tok_stack, which yyparse
41 eventually calls during parsing. */
46 # set up yylval and tok_t to be pushed to stack
47 action push_ref { errno = 0;
48 lval.ref = strtoll((char*)ts,NULL,16);
50 { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
55 action push_link { PUSHLINK(); }
56 action push_val { errno = 0;
57 lval.val = strtoll((char*)ts,NULL,10);
59 { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
64 action push_name { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
67 action push_map { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
70 action set_ts { printf("Lexer_lexstring:: action:set_ts. ts = %s\n", p); ts = p; }
71 action push_SS { printf("Lexer_lexstring:: action:push_SS. p = %s\n",p);
74 action push_S { printf("Lexer_lexstring:: action:push_S. p = %s\n", p);
77 action push_SW { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p);
80 action push_W { printf("Lexer_lexstring:: action:push_W. p = %s\n", p);
83 action push_NW { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p);
86 action push_N { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
89 action push_NE { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p);
92 action push_E { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
95 action push_SE { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
98 action ref_error { printf("ref from %s to %s has an inappropriate amount of hex digits, it must have eight.\n", ts, p);
101 action p { printf("Lexer_lexstring:: p = %s\n", p);}
114 direction = (N | W | S | E | NW | NE | SW | SE) ;
115 dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts %push_val;
116 link = '#' %push_link;
117 SS = ('+SS' %to(push_SS)) | ('+SS' %to(push_SS) link ) ;
118 ref = '0x' >set_ts alnum{8} $err(ref_error) %push_ref ;
119 val = digit+ >set_ts %push_val ;
120 name = lower >set_ts (lower | digit)* %push_name ;
121 map = '+MAP' %to(push_map);
122 tok = (name | val | ref | dimensions | map | link | SS | direction);
125 main := (tok tok_delimiter)* tok [\0];
127 write data nofinal noerror noprefix;
136 uint8_t *ts, *pe, *eof;
143 pe = eof = p + size + 1;
145 printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
150 printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
155 /* Lexical analysis of a file
156 Strips a filename to its base name, then sends it to lexer_lexstring before
157 pushing a PATH token with the filename
158 Returns the number of tokens pushed to the parser.
161 ( uint8_t* filename )
162 { uint8_t* last_period,* iter,* filename_end;
165 for (iter = filename; *iter; iter++)
167 { // Keep track of the last 'dot' in the name
168 case '.' : last_period = iter; continue;
169 // replace '_' with '\0' so bison can use strlen on them as tokens.
170 case '_' : *iter = '\0';
173 // Mark the end of the filename
175 // Lex from either the last period, if present, or filename end
176 ntok = (last_period) ?
177 lexer_lexstring(filename, (int)(last_period - filename))
178 : lexer_lexstring(filename, (int)(iter - filename));
179 // Replace nulls with their original '_'
180 for (iter = filename; iter < filename_end; iter++)
190 { uint8_t* de = dirname;
194 if (*de) while (*++de);
195 ntok = lexer_lexstring(dirname, (int)(de - dirname));
207 /**************************/
208 /****Abandon All Hope******/
209 /**************************/
219 machine setdirection;
221 action ret_north {printf("Lexer_setdirection:: direction is north, returning 4\n"); return 4;; }
222 action ret_west { printf("Lexer_setdirection:: direction is west, returning 2\n");return 2;}
223 action ret_east { printf("Lexer_setdirection:: direction is east, returning 6\n");return 6;}
224 action ret_south { printf("Lexer_setdirection:: direction is south, returning 0\n");return 0;}
225 action ret_northeast { printf("Lexer_setdirection:: direction is northeast, returning 5\n");return 5 ;}
226 action ret_northwest { printf("Lexer_setdirection:: direction is northwest, returning 3\n");return 3;}
227 action ret_southeast { printf("Lexer_setdirection:: direction is southeast, returning 7\n");return 7;}
228 action ret_southwest { printf("Lexer_setdirection:: direction is southwest, returning 1\n");return 1;}
230 def = [_\0] %to(ret_south);
231 N = 'N'[_\0] %to(ret_north);
232 W = 'W' [_\0] %to(ret_west);
233 S = 'S' [_\0] %to(ret_south);
234 E = 'E' [_\0] %to(ret_east);
235 NW = 'NW' [_\0] %to(ret_northwest);
236 NE = 'NE' [_\0] %to(ret_northeast);
237 SW = 'SW' [_\0] %to(ret_southwest);
238 SE = 'SE' [_\0] %to(ret_southeast);
240 direction = (N | W | S | E | NW | NE | SW | SE | def);
244 write data nofinal noprefix noerror;
252 (uint8_t* str, int size)
253 { uint8_t *p, *pe, *eof;
260 printf("|--- Begin lexer_setdirection str = %s, p = %s, pe = %s ---|\n", str,p, pe);
265 printf("|--- Error in: lexer_setdirection ---|\n");
276 action lex_setvlink {printf("Lexer_setstr:: Returning setvlink filetype for %s\n", str); type = 5; newstrt = lexer_lexsetvlink(str); fbreak;}
277 action lex_elevlink {printf("Lexer_setstr:: Returning elevlink filetype for %s\n", str); type = 6; newstrt = lexer_lexelevlink(str); fbreak;}
278 action lex_setmodel {printf("Lexer_setstr:: Returning setmodel filetype\n"); newstrt = lexer_lexsetmodel(str); type = 1; fbreak;}
279 action lex_setmap {printf("Lexer_setstr:: Returning setmap filetype\n"); newstrt = lexer_lexsetmap(str); type = 2; fbreak;}
280 action lex_elemodel {printf("Lexer_setstr:: Returning elemodel filetype for %s\n", str); newstrt = lexer_lexelemodel(str); type = 3; fbreak;}
281 action lex_elemap {printf("Lexer_setstr:: Returning elemap filetype for %s\n", str); newstrt = lexer_lexelemap(str); type = 4; fbreak;}
282 action lex_setolink { printf("Lexer_setstr:: Returning setolink filetype\n"); type = 8; newstrt = lexer_lexsetolink(str); fbreak;}
283 action lex_eleolink { printf("Lexer_setstr:: Returning eleolink filetype\n"); type = 7; newstrt = lexer_lexeleolink(str); fbreak;}
284 action p {printf("p = %s \n",p);}
285 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);}
298 direction = (N | W | S | E | NW | NE | SW | SE) $p;
304 name = alpha+ $p - SSD $p;
306 ref = '0x' $p alnum+ $p;
309 set_label = name | (name '_' ref);
310 ele_label = name | (name '_' ref);
312 model_types = (name) | (name '_' num '_' num) | (name '_' num);
315 set_model = set_label '_' SS %to(lex_setmodel);
316 set_map = set_label '_' '~' %to(lex_setmap);
317 ele_model = set_label '_' ele_label '_' SS %to(lex_elemodel);
318 ele_map = set_label '_' ele_label '_' '~' %to(lex_elemap);
319 set_olink = ref %to(lex_setolink) [\0] ;
320 ele_olink = set_label '_' '~' '_' ref [\0] %to(lex_eleolink);
321 set_vlink = set_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_setvlink);
322 ele_vlink = set_label '_' ele_label '_' '#' '_' (ref | ref '_' name) [\0] %to(lex_elevlink);
324 main := (ele_map | set_model | set_map |ele_model | ele_vlink | set_vlink | set_olink | ele_olink);
333 (uint8_t* str, int size)
334 { uint8_t *p, *pe, *eof;
335 int cs, type, newstrt;
342 printf("|--- Begin lexer_setstr with str = %s, p = %s, pe = %s ---|\n", str,p, pe);
347 printf("|--- End lexer_setstr. Incrementing str by %d, type is %d ---|\n", newstrt, type);
356 /* machine file_matcher; */
358 /* action call_ml { ts = p; fgoto set_hw ;} */
359 /* action call_tl { return 0;} */
360 /* action set_height {height = ttov(p, p-ts+1); ts = p;} */
361 /* action set_width { width = ttov(p, p-ts+1);} */
362 /* action call_lmf {lexer_lexmapfile(height, width); } */
363 /* action lex_error {printf("input error: character %c in filename %s is invalid\n = %s\n", fc, str, p);} */
365 /* #This machine determines the type of file we are lexing */
366 /* #and calls the appropriate machine to handle it. */
368 /* #TODO add mapping name */
369 /* width = digit+ %set_width; */
370 /* height = digit+ %set_height; */
372 /* set_hw := height . '_' . width [\0] %to(call_lmf); */
374 /* tok_segment = alnum; */
375 /* map_end = 'm' . '_' %to(call_ml); */
376 /* tok_end = alnum+ . [\0] %to(call_tl); */
378 /* file_matcher := (tok_segment+ . '_' )+ ( map_end | tok_end ); */
384 /* lexer_matchfile */
385 /* (char* str, int size) */
388 /* int cs, ntok, height, width; */
392 /* height = width = 0; */
394 /* printf("Checking if filename is a map file:: filename = %s, p = %c, pe = %c\n", str, *p, *pe); */
397 /* %%write exec noend; */
399 /* printf("Ending lexer_ismapfile on %s\n", str); */
405 /* machine vartype; */
407 /* action isele {return 0;} */
408 /* action ismodel {return 1;} */
410 /* set_name = alpha+; */
411 /* ele_name = alpha+; */
412 /* model_name = alpha+; */
414 /* ele = set_name '_' model_name '_' ele_name %isele; */
415 /* model = set_name '_' model_name [\0] %ismodel; */
418 /* ismodel := (ele | model); */
426 /* (uint8_t* str, int size) */
427 /* { uint8_t *p, *pe, *eof; */
431 /* pe = p + size + 1; */