optimization pass thru parser
[henge/apc.git] / src / lexer_fsm.rl
index 344ffdb..d8db2b4 100644 (file)
@@ -1,40 +1,49 @@
 #include <stdio.h>
 #include <stdint.h>
-#include <apc/parser.tab.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "parser.tab.h"
 #include "apc.h"
 #include <unistdio.h>
 #include <unistr.h>
-
-extern void lexer_pushtok(int, YYSTYPE);
-extern int lexer_lexmapfile(int, int);
-extern int ipower(int, int);
-extern int ttov(uint8_t*, int);
-extern struct name* tton(const uint8_t*, int); 
-
+extern //lexer.c
+void lexer_pushtok(int, YYSTYPE);
+/* Public */
 int lexer_setdirection(uint8_t*, int);
 int lexer_lexstring(uint8_t*, int);
 int lexer_setstr(uint8_t*, int);
 
-#define $($)#$
-#define PUSHTOK(TOK,LFUNC,UTYPE)                       \
-  do {                                                 \
-    printf("Lexer_lexstring:: PUSHTOK(" $(TOK) $(LFUNC) $(UTYPE) ")\n");               \
-    tok_t = TOK;                                       \
-    yylval.UTYPE = LFUNC(ts, p-ts);                    \
-    lexer_pushtok(tok_t, yylval);                      \
-    ++ntok;                                            \
-    } while (0);
-
 %%{    
   machine lexstring;
 
   # set up yylval and tok_t to be pushed to stack
-  action push_ref       { PUSHTOK(REF, ttov, ref); }
-  action push_val       { PUSHTOK(NUM, ttov, val); }
+  action push_ref       { te = NULL; errno = 0;
+                          yylval.ref = strtoll((char*)ts,(char**)&te,16);
+                         if (errno | (te != NULL))
+                           { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
+                             if (te != NULL)
+                               { while (str++ < te)
+                                   fputc(' ', stderr);
+                                 fputc('^', stderr);
+                               }
+                             exit(1);
+                           }
+                          lexer_pushtok(REF, yylval); ntok++;
+                       }
+  action push_link      { lexer_pushtok(LINK,(YYSTYPE)0); ntok++; }
+  action push_val       { te = NULL; errno = 0;
+                          yylval.val = strtoll((char*)ts,(char**)&te,10);
+                         if (errno)
+                           { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
+                             exit(1);
+                           }
+                         lexer_pushtok(NUM, yylval);
+                        }
   action push_name      { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
-                          PUSHTOK(NAME, tton, str);
+                          lexer_pushtok(NAME, yylval);
+                         ntok++;
                         }
-  action push_map       { printf("Lexer_lexstring:: action:push_map: pushing '~'\n");
+  action push_map       { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
                           yylval.str = (uint8_t*) '~';
                           lexer_pushtok(MOPEN, yylval);
                          ntok++;
@@ -78,17 +87,12 @@ int lexer_setstr(uint8_t*, int);
                          lexer_pushtok(D, yylval);
                         }
   action push_height    { printf("Lexer_lexstring:: action:push_height p = %s\n", p);
-                          PUSHTOK(HEIGHT, ttov, val);
                         }
   action push_width     { printf("Lexer_lexstring:: action:push_height p = %s\n", p);
-                          PUSHTOK(WIDTH, ttov, val);
                         }
-  action push_link      {yylval.str = (uint8_t*) '#'; lexer_pushtok(LINK),yylval); }
-  action lex_error      { printf("input error: character %c in filename %s is invalid\n p = %s\n", fc, str, p);}
+  #action lex_error      { printf("input error: character %c in filename %s is invalid\n p = %s\n", fc, str, p);}
   action p              { printf("Lexer_lexstring:: p = %s\n", p);}
     
-  # Parses a regular filename (not a mapping) and pushes
-  # tokens to the parser
   N = 'N' %push_N;
   W = 'W' %push_W;
   S = 'S' %push_S;
@@ -98,40 +102,44 @@ int lexer_setstr(uint8_t*, int);
   SW = 'SW' %push_SW;
   SE = 'SE' %push_SE;
 
+  #what goes in between tokens in a filename
   tok_delimiter = [_];
+
+  #types of tokes a filename can contain
   direction = (N | W | S | E | NW | NE | SW | SE) ;
   #make sure 0x123123 doesnt get mistaken for a ref
-  dimensions = (digit+ - '0') >set_ts %push_height 'x' (digit+ - '0') >set_ts  %push_width;
+  dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts  %push_val;
   link = '#'  %push_link;
   SS = ('+SS' %to(push_SS)) | ('+SS' %to(push_SS) link ) ;
   ref = '0x' >set_ts alnum+ %push_ref;
   val = digit+ >set_ts %push_val ;
   name = (lower+ >set_ts) %push_name ;
   map = '+MAP' %to(push_map);
-  tok = name %to(push_name) ;
+  tok = (name | val | ref | dimensions | map | link | SS | direction);
   
 
-tok_lexer := ((name | val | ref | dimensions | map) tok_delimiter)+ tok [\0];
+  main := (tok tok_delimiter)+ tok [\0];
 
-  write data;
+  write data nofinal noerror noprefix;
   
 }%%
 
-int
-lexer_lexstring
-(uint8_t* str, int size)
+int lexer_lexstring
+( uint8_t* str, 
+  int size
+)
 { uint8_t *p; 
-  uint8_t *ts, *pe, *eof;
-  int cs, ntok, tok_t, direction;
+  uint8_t *ts, *pe, *te;
+  int cs, ntok;//, tok_t;
 
-  ntok = direction = 0;
+  ntok = 0;
   p = ts = str;
-  pe = p + size +1;
+  pe = p + size + 1;
 
   printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
 
   %%write init;
-  %%write exec ;
+  %%write exec;
 
    printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok);
 
@@ -139,6 +147,17 @@ lexer_lexstring
 }
 
 
+/**************************/
+/****Abandon All Hope******/
+/**************************/
+/***                    ***/
+/***                    ***/       
+/***                    ***/
+/***                    ***/
+
+
+#if 0 
+
 %%{
   machine setdirection;
 
@@ -165,10 +184,12 @@ lexer_lexstring
 
   main := direction; 
 
-  write data;
-  
+  write data nofinal noprefix noerror;
+
 }%%
 
+
 int
 lexer_setdirection
 (uint8_t* str, int size)
@@ -191,16 +212,6 @@ lexer_setdirection
 
 
 
-/**************************/
-/****Abandon All Hope******/
-/**************************/
-/***                    ***/
-/***                    ***/       
-/***                    ***/
-/***                    ***/
-
-
-#if 0 
 %%{
   machine setstr;