fixes
[henge/apc.git] / src / lexer.rl
index 553ff3d..5e2fb77 100644 (file)
@@ -10,24 +10,31 @@ extern //lexer.c
 void lexer_pushtok(int, YYSTYPE);
 /* Public */
 int lexer_setdirection(uint8_t*, int);
 void lexer_pushtok(int, YYSTYPE);
 /* Public */
 int lexer_setdirection(uint8_t*, int);
-int lexer_lexfile(const uint8_t*);
-int lexer_lexdir(const uint8_t*);
-int lexer_lexstring(const uint8_t*, int);
+int lexer_lexfile(uint8_t*);
+int lexer_lexdir(uint8_t*);
+int lexer_lexstring(uint8_t*, int);
 int lexer_setstr(uint8_t*, int);
 //apc.c
 extern
 yypstate* apc_pstate;
 extern
 yycstate* apc_cstate;
 int lexer_setstr(uint8_t*, int);
 //apc.c
 extern
 yypstate* apc_pstate;
 extern
 yycstate* apc_cstate;
-#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (YYSTYPE*)(L), apc_cstate)
-#define LEXTOK(T,L) do {                       \
-    PUSHTOK(T,L);                              \
+static
+YYSTYPE   lval_stack[1024], * lvalsp;
+#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (L), apc_cstate)
+#define LEXTOK(T,Y,L) do {                     \
+    lvalsp->Y = L;                             \
+    PUSHTOK(T,lvalsp);                         \
+    lvalsp++;                                  \
     ntok++;                                    \
   } while (0);
     ntok++;                                    \
   } while (0);
-#define LEXFACE(F) do {                                \
-    lval.face = F;                             \
-    LEXTOK(FACING, &lval.face);                        \
-  } while (0);
+#define PUSHFACE(F) LEXTOK(FACING, face, F)
+#define PUSHREF(R)  LEXTOK(REF, ref, R)
+#define PUSHLINK()  LEXTOK(LINK, val, 0)
+#define PUSHNUM(N)  LEXTOK(NUM, val, N)
+#define PUSHNAME(N) LEXTOK(NAME, str, N)
+#define PUSHOP(O)   LEXTOK(O, val, 0)
+#define PUSHPATH(P) LEXTOK(PATH, str, P)
 
 /* Lexstring is the main lexer for APC and is generated by ragel. It lexes file names of files 
    that have been scanned and pushes their types and values into the tok_stack, which yyparse 
 
 /* Lexstring is the main lexer for APC and is generated by ragel. It lexes file names of files 
    that have been scanned and pushes their types and values into the tok_stack, which yyparse 
@@ -37,62 +44,56 @@ yycstate* apc_cstate;
   machine lexstring;
 
   # set up yylval and tok_t to be pushed to stack
   machine lexstring;
 
   # set up yylval and tok_t to be pushed to stack
-  action push_ref       { te = NULL; errno = 0;
-                          lval.ref = strtoll((char*)ts,(char**)&te,16);
-                         if (errno | (te != NULL))
+  action push_ref       { errno = 0;
+                          lval.ref = strtoll((char*)ts,NULL,16);
+                         if (errno)
                            { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
                            { fprintf(stderr, "Invalid hex number in file %s\n",(char*)str);
-                             if (te != NULL)
-                               { while (str++ < te)
-                                   fputc(' ', stderr);
-                                 fputc('^', stderr);
-                               }
                              exit(1);
                            }
                              exit(1);
                            }
-                         LEXTOK(REF, &lval.ref);
+                         PUSHREF(lval.ref);
                        }
                        }
-  action push_link      { lval.val = 0;
-                          PUSHTOK(LINK, &lval.val); }
-  action push_val       { te = NULL; errno = 0;
-                          lval.val = strtoll((char*)ts,(char**)&te,10);
+  action push_link      { PUSHLINK(); }
+  action push_val       { errno = 0;
+                          lval.val = strtoll((char*)ts,NULL,10);
                          if (errno)
                            { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
                              exit(1);
                            }
                          if (errno)
                            { fprintf(stderr, "strtoll could not parse %s\n", (char*)str);
                              exit(1);
                            }
-                         LEXTOK(NUM, &lval.val);
+                         PUSHNUM(lval.val);
                         }
   action push_name      { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
                         }
   action push_name      { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
-                         LEXTOK(NAME, ts);
+                         PUSHNAME(ts);
                         }
   action push_map       { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
                         }
   action push_map       { printf("Lexer_lexstring:: action:push_map: pushing map token\n");
-                          LEXTOK(MAP, "~");
+                         PUSHOP(MAP);
                        }
   action set_ts         { printf("Lexer_lexstring:: action:set_ts. ts = %s\n", p); ts = p; }
   action push_SS        { printf("Lexer_lexstring:: action:push_SS. p = %s\n",p);
                        }
   action set_ts         { printf("Lexer_lexstring:: action:set_ts. ts = %s\n", p); ts = p; }
   action push_SS        { printf("Lexer_lexstring:: action:push_SS. p = %s\n",p);
-                          LEXTOK(SS, "SS");
+                          PUSHOP(SS);
                         }
   action push_S         { printf("Lexer_lexstring:: action:push_S. p = %s\n", p);
                         }
   action push_S         { printf("Lexer_lexstring:: action:push_S. p = %s\n", p);
-                         LEXFACE(SFACE);
+                         PUSHFACE(SFACE);
                         }
   action push_SW        { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p);
                         }
   action push_SW        { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p);
-                          LEXFACE(SWFACE);
+                          PUSHFACE(SWFACE);
                        }
   action push_W         { printf("Lexer_lexstring:: action:push_W. p = %s\n", p);
                        }
   action push_W         { printf("Lexer_lexstring:: action:push_W. p = %s\n", p);
-                          LEXFACE(WFACE);
+                          PUSHFACE(WFACE);
                         }
   action push_NW        { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p);
                         }
   action push_NW        { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p);
-                         LEXFACE(NWFACE);
+                         PUSHFACE(NWFACE);
                         }
   action push_N         { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
                         }
   action push_N         { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
-                          LEXFACE(NFACE);
+                          PUSHFACE(NFACE);
                         }
   action push_NE        { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p);
                         }
   action push_NE        { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p);
-                         LEXFACE(NEFACE);
+                         PUSHFACE(NEFACE);
                         }
   action push_E         { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
                         }
   action push_E         { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
-                         LEXFACE(EFACE);
+                         PUSHFACE(EFACE);
                         }
   action push_SE        { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
                         }
   action push_SE        { printf("Lexer_lexstring:: action:push_N. p = %s\n", p);
-                         LEXFACE(SEFACE);
+                         PUSHFACE(SEFACE);
                         }
   action ref_error      { printf("ref from %s to %s has an inappropriate amount of hex digits, it must have eight.\n", ts, p);
                           exit(1);
                         }
   action ref_error      { printf("ref from %s to %s has an inappropriate amount of hex digits, it must have eight.\n", ts, p);
                           exit(1);
@@ -128,18 +129,18 @@ yycstate* apc_cstate;
 }%%
 
 int lexer_lexstring
 }%%
 
 int lexer_lexstring
-( const uint8_t* str, 
+( uint8_t* str, 
   int size
 )
   int size
 )
-{ const uint8_t *p; 
-  const uint8_t *ts, *pe, *te, *eof;
+{ uint8_t *p; 
+  uint8_t *ts, *pe, *eof;
   int cs, ntok;
   YYSTYPE lval;
 
   int cs, ntok;
   YYSTYPE lval;
 
+  lvalsp = lval_stack;
   ntok = 0;
   p = ts = str;
   pe = eof =  p + size + 1;
   ntok = 0;
   p = ts = str;
   pe = eof =  p + size + 1;
-  
 
   printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
 
 
   printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe);
 
@@ -157,26 +158,50 @@ int lexer_lexstring
    Returns the number of tokens pushed to the parser.
 */
 int lexer_lexfile
    Returns the number of tokens pushed to the parser.
 */
 int lexer_lexfile
-( uint8_t const* filename )
-{ uint8_t const* last_period,* iter;
+( uint8_t* filename )
+{ uint8_t* last_period,* iter,* filename_end;
   int            ntok;
   last_period = NULL;
   for (iter = filename; *iter; iter++)
   int            ntok;
   last_period = NULL;
   for (iter = filename; *iter; iter++)
-    if (*iter ==  '.')
-      last_period = iter;
+    switch (*iter)
+      { // Keep track of the last 'dot' in the name
+        case '.' : last_period = iter; continue;
+        // replace '_' with '\0' so bison can use strlen on them as tokens.
+        case '_' : *iter = '\0';
+        default: continue;
+      }
+  // Mark the end of the filename
+  filename_end = iter;
+  // Lex from either the last period, if present, or filename end
   ntok = (last_period) ?
     lexer_lexstring(filename, (int)(last_period - filename))
   : lexer_lexstring(filename, (int)(iter - filename));
   ntok = (last_period) ?
     lexer_lexstring(filename, (int)(last_period - filename))
   : lexer_lexstring(filename, (int)(iter - filename));
-  PUSHTOK(PATH,&filename);
+  // Replace nulls with their original '_'
+  for (iter = filename; iter < filename_end; iter++)
+    if (*iter == '\0')
+      *iter = '_';
+  PUSHPATH(filename);
   return ntok + 1;
   return en_main == 1;
 }
 
 int lexer_lexdir
   return ntok + 1;
   return en_main == 1;
 }
 
 int lexer_lexdir
-( uint8_t const* dirname )
-{ uint8_t const* de = dirname;
-  while (*de++);
-  return lexer_lexstring(dirname, (int)(de - dirname));
+( uint8_t* dirname )
+{ uint8_t* de = dirname;
+  int ntok;
+  ntok = 0;
+  de = dirname;
+  if (*de) while (*++de);
+  ntok = lexer_lexstring(dirname, (int)(de - dirname));
+  PUSHOP(CLOPEN);
+  return ntok;
+}
+
+int lexer_closedir
+( void )
+{ int ntok = 0;
+  PUSHOP(CLCLOSE);
+  return ntok;
 }
 
 /**************************/
 }
 
 /**************************/