uint8_t for chars, lexfilename needs work
[henge/webcc.git] / src / apc / lexer.c
index 096016a..f11c6a2 100644 (file)
 /* Standard */
 #include <stdio.h>
 #include <string.h>
+#include <stdint.h>
 #include <errno.h>
 /* Posix */
 #include <unistd.h>
+#include <unitypes.h>
+#include <unistr.h>
+#include <uniconv.h>
+#include <uniname.h>
+#include <unistdio.h>
 #include <stdlib.h>
 #include <limits.h> //realpath, NAME_MAX, PATH_MAX
 #include <dirent.h>
+/* Redefinitions of NAME_MAX and PATH_MAX */
+//#define NAME_MAX NAME_MAX/4
+//#define PATH_MAX PATH_MAX/4
+
 /* Local */
 #include "parser.tab.h"
 #ifndef DE_STACKSIZE
 #ifndef TK_STACKSIZE
 #define TK_STACKSIZE 1024
 #endif
+#ifndef MAX_SETNAME_LEN //max setname length
+#define MAX_SETNAME_LEN 32
+#endif
+
 /* Public */
 int         lexer_init(void);
 int         lexer(void);
-int         lexer_lexfile(const char*);
+int         lexer_lexfile(const uint8_t*);
 void        lexer_pushtok(int, YYSTYPE);
-char const* lexer_get_current_filepath(void);
-struct dirent* lexer_direntpa[DE_STACKSIZE], **lexer_direntpp;
+uint8_t const* lexer_get_current_filepath(void);
+int         lexer_lexfilename(uint8_t*);
+struct dirent* lexer_direntpa[DE_STACKSIZE],** lexer_direntpp,** lexer_direntpb;
 /* Private */
-extern //lexer_lex.rl
-int         lexer_lex(const char*);
+extern //lexer_fsm.rl
+int         lexer_lexstring(uint8_t*, int);
 extern //scanner.c
 int         scanner_init(void);
 extern //scanner.c
@@ -47,7 +62,7 @@ int         dredge_current_depth(void);
 extern //bison
 YYSTYPE     yylval;
 static
-char const* current_filename;
+uint8_t const* current_filename;
 static
 struct tok
 { YYSTYPE lval;  //token val
@@ -58,13 +73,14 @@ struct tok
    Simple array for keeping track of dirents yet to be processed by the scanner.
    If this list is empty and there are no tokens, the lexer is done.
    This array is populated by the scanner as an array, and popped locally by the
-   lexer as a stack.
+   lexer as a stack, and is popped as a FIFO stack.
 */
 #define DE_STACK    (lexer_direntpa)
 #define DE_STACKP   (lexer_direntpp)
-#define DE_LEN()    (DE_STACKP - DE_STACK)
-#define DE_INIT()   (DE_STACKP = DE_STACK)
-#define DE_POP()    (*--DE_STACKP)
+#define DE_STACKB   (lexer_direntpb)
+#define DE_LEN()    (DE_STACKP - DE_STACKB)
+#define DE_INIT()   (DE_STACKP = DE_STACKB = DE_STACK)
+#define DE_POP()    (*DE_STACKB++)
 
 /* Token Stack
    This is a FIFO stack whose pointers are a union of either a pointer to an
@@ -111,10 +127,12 @@ int lexer
   } while (0)
 ()
 { struct tok token;
- start:
-  while (DE_LEN() > 0)    //lex any directory entries in our stack
-    if (lexer_lexfile(DE_POP()->d_name) == 0)
-      FAIL("Lexer failed to tokenize [%s]\n",(*DE_STACKP)->d_name);
+   start:
+  while (DE_LEN() > 0)//lex any directory entries in our stack
+    {
+      if (lexer_lexfile(DE_POP()->d_name) == 0)
+       FAIL("Lexer failed to tokenize [%s]\n",(*DE_STACKB)->d_name);
+    }
   if (TK_EMPTY)           //if there are no tokens,
     { TK_INIT();            //initialize the token stack back to 0
       switch (scanner())
@@ -154,37 +172,133 @@ void lexer_pushtok
 */
 int lexer_lexfile
 #define HIDDEN_WARNING "%s is hidden and will not be parsed!\n", filename
-( const char  *filename
+( const uint8_t  *filename
 )
-{ static char fname[NAME_MAX];
-  char        *last_period = NULL, *iter;
+{ static uint8_t fname[NAME_MAX];
+  uint8_t        *last_period = NULL, *iter;
 
-  if (*filename == '.')
+  if (*filename ==  '.')
     { fprintf (stderr, HIDDEN_WARNING);
       return 0;
     }
   /* Copy the filename and remove its suffix */
-  strncpy(fname,filename,NAME_MAX);
+  u8_strncpy(fname,filename,NAME_MAX);
   last_period = NULL;
   for (iter = fname; *iter; iter++)  //find the last '.' char
-    if (*iter == '.')
+    if (*iter ==  '.')
       last_period = iter;
   if (last_period)                   //if we found one,
-    *last_period = '\0';             //truncate the string there
+    *last_period =  0;             //truncate the string there
   /* Register the current_filename */
   current_filename = filename;
   
-  return lexer_lex(fname);
+  return lexer_lexfilename(fname);
 }
 
-char const* lexer_get_current_filepath
+uint8_t const* lexer_get_current_filepath
 ()
-{ static char        current_path[PATH_MAX];
-  static char const* last_filename;
+{ static uint8_t        current_path[PATH_MAX];
+  static uint8_t const* last_filename;
   if ((!last_filename || last_filename != current_filename) &&
-      (realpath(current_filename, current_path) != current_path))
+      (realpath(current_filename, current_path) != (char*) current_path))
     { perror("realpath: ");
       return NULL;
     }
   return (const char*)current_path;
 }
+
+/* Scan filename and push the its tokens
+   onto the stack */
+int lexer_lexfilename
+(uint8_t* str)
+{
+  int ntok, i, cmp, len, set_len, height, width;
+  char map_key[] = "_m_";
+  static uint8_t set_name[MAX_SETNAME_LEN] = {0};
+  uint8_t *first, *map_begin;
+
+  printf("Starting lexerfilename on %s\n", str);
+  
+
+  if(*str == 0)
+    printf("Lexfilename:: str is NULL so fail\n");
+  printf("setname is %s\n", set_name);
+  
+  /* If last file was a mapfile, then its 5th to last token should
+  be a MOPEN. If this is the case, then we only pass MOPEN, height,
+  weight and name of the current file. */
+  if( (TK_STACKX - 5)->tok_t == MOPEN )
+    { printf("The last file was a mapfile\n");
+      if( (map_begin = strstr(map_key, str)) ) //if the current file is a mapfile
+       { printf("The current file is a variant of the last mapfile\n");
+         printf("Start lexing mapfile %s\n", str);
+         ntok += lexer_lexstring(map_begin, strlen(map_begin));
+       }
+      printf("Current file is not a variant of the last mapfile\n");      
+    }
+  else //last file was not a mapfile
+    { printf("Last file was not a mapfile\n");
+
+      first = (uint8_t*) u8_strchr(str,  '_'); //find the first '_' to find where str set_name ends    
+      
+      if(set_name[0] != 0) //if there is a set_name from last str     
+       { printf("There is a set_name (%s) present\n", set_name);
+          set_len = first - str;
+           
+         if(u8_strncmp(str, set_name, set_len) == 0) //check if it matches the current set_name
+           { str = str + set_len + 1;                    //if so, remove it from str
+             printf("str set_name matched last set_name, set str to %s\n", str);
+           }
+         else //update set_name to be str set_name
+           { u8_cpy(set_name, str, set_len);
+             set_name[set_len] = 0;
+             
+           }
+        }
+      else //set set_name
+       { u8_cpy(set_name, str, first-str);
+       } 
+      /* Call lexer_lexstring to tokenize the string */
+      printf("calling lexstring to tokenize str (%s) of len %d\n", str, u8_strlen(str));
+      ntok += lexer_lexstring(str, u8_strlen(str));
+    }
+    
+  /*TODO: if regfile, store full path for later */
+
+  printf("Ending lexer_lex on %s, %d tokens were lexed\n", str, ntok);
+  return ntok;
+}
+
+/* int lexer_lexmapfile */
+/* #define INC_X() */
+/* (int height, int width) */
+/* { */
+/*   int x, y; */
+
+/*   /\* Give scanner_scanpixels a buffer and a len. Iterate through */
+/*      buf with buf[n]. If n == 0, do nothing. if n has a value, push x, */
+/*      push y, push (z = n << 24), push (ref_id = n >> 8) *\/ */
+/*   //scanner_scanpixels() */
+
+/*   for(i = 0; i < len; i++) */
+/*     if(buf[i] == 0) */
+/*       if(x == width) */
+/*     x = 0; */
+/*       else */
+       
+         
+      
+
+/* } */
+/* fname_bytes = (uint8_t*)(DE_POP()->d_name); */
+      /* printf("d_name is %s\n", fname_bytes); */
+      /* for (fnp = filename, i = 0; i < NAME_MAX; i += unit_size, fnp++) */
+      /*       { unit_size = u8_mblen(fname_bytes + i, min(4, NAME_MAX - i)); */
+      /*         if (u8_mbtouc(fnp, fname_bytes + i, unit_size) == -1) //add ucs4 char to the filename */
+      /*           FAIL("Lexer failed to convert ^%s to unicode\n", (fname_bytes + i)); */
+      /*         if (*fnp == 0) //added a terminating char */
+      /*           break; */
+      /*       } */
+      /* if(u8_mbtouc(filename, DE_POP()->d_name, NAME_MAX) == -1) */
+      /*       FAIL("Lexer failed to convert d_name into uint8_t\n"); */
+      /* ulc_fprintf(stdout, "filename is %11U\n c", filename); */