APC Scanner 1.0
authorksg <ken@mihrtec.com>
Tue, 30 Aug 2016 00:19:34 +0000 (17:19 -0700)
committerksg <ken@mihrtec.com>
Tue, 30 Aug 2016 00:19:34 +0000 (17:19 -0700)
src/apc/lexer.c
src/apc/scanner.c

index 4e0a611..dd1eb81 100644 (file)
 #include <dirent.h>
 /* Local */
 //#include "parser.tab.h"
-#ifndef DP_STACKSIZE
-#define DP_STACKSIZE 1024
+#ifndef DE_STACKSIZE
+#define DE_STACKSIZE 1024
 #endif
 #ifndef TK_STACKSIZE
 #define TK_STACKSIZE 1024
 #endif
 /* Public */
-struct tok
-{ int lval;
-  int tok;
-};
-int            lexer_init(void);
-int            lexer(void);
-inline
-void           lexer_pushtok(int int);
-struct dirent* lexer_direntpa[DP_STACKSIZE];
+int  lexer_init(void);
+int  lexer(void);
+int  lexer_lex(const char*);
+void lexer_pushtok(int, int);
+struct dirent* lexer_direntpa[DE_STACKSIZE];
 /* Private */
-static inline
-int scan(void);
+extern //scanner.c
+int scanner_init(void);
+extern //scanner.c
+int scanner(void);
 static inline
 int dredge_current_depth(void);
+extern //bison
+int yylval;
 static
-struct dirent** dps;
-static
-struct tok token_stack[TK_STACKSIZE];
+struct tok
+{ int lval;
+  int tok;
+} token_stack[TK_STACKSIZE];
 static
 union tokp
 { int*        i;
   struct tok* t;
 } tks, tkx;
+static
+struct dirent** dps;
 
 /* Directory Entity Array/Stack
    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.
 */
-#define DP_STACK    (lexer_direntpa)
-#define DP_STACKP   (dps)
-#define DP_LEN()    (DP_STACKP - DP_STACK)
-#define DP_INIT()   (DP_STACKP = DP_STACK)
-#define DP_POP()    (*--DP_STACKP)
+#define DE_STACK    (lexer_direntpa)
+#define DE_STACKP   (dps)
+#define DE_LEN()    (DE_STACKP - DE_STACK)
+#define DE_INIT()   (DE_STACKP = DE_STACK)
+#define DE_POP()    (*--DE_STACKP)
 
 /* Token Stack
    This is a FIFO stack whose pointers are a union of either a pointer to an
@@ -70,25 +75,16 @@ union tokp
    An alignment error will occur if IPOP or IPUSH are used a non-even number of
    times in a sequence!
 */
-#define TK_STACK          (token_stack)
-#define TK_STACKP   (tks.t)
-#define TK_STACKPI  (tks.i)
-#define TK_STACKX   (tkx.t)
-#define TK_STACKXI  (tkx.i)
-#define TK_LEN()    (TK_STACKP - TK_STACKX)
-#define TK_INIT()   (TK_STACKP = TK_STACKX = TK_STACK)
-#define TK_POP()    (*TK_STACKP++)
-#define TK_POPI()   (*TK_STACKPI++);
-#define TK_PUSH(T)  (*TKSTACKX++ = T)
-#define TK_PUSHI(I) (*TKSTACKXI++ = (I))
-
-extern //main.c
-const char* cargs['Z'];
-
-extern //scanner.c
-int scanner_init(void);
-extern //scanner.c
-int scanner(struct dirent**);
+#define TK_STACK     (token_stack)
+#define TK_STACKP    (tks.t)
+#define TK_STACKPI   (tks.i)
+#define TK_STACKX    (tkx.t)
+#define TK_STACKXI   (tkx.i)
+#define TK_LEN()     (TK_STACKP - TK_STACKX)
+#define TK_INIT()    (TK_STACKP = TK_STACKX = TK_STACK)
+#define TK_POP()     (*TK_STACKP++)
+#define TK_POPI()    (*TK_STACKPI++);
+#define TK_PUSH(T,L) (*TK_STACKX++ = (struct tok){L,T})
 
 /* Initializer
    The initializer returns boolean true if an error occurs, which may be handled   with standard errno.
@@ -96,7 +92,7 @@ int scanner(struct dirent**);
 int lexer_init
 ()
 { TK_INIT();
-  DP_INIT();
+  DE_INIT();
   return scanner_init();
 }
 
@@ -112,46 +108,40 @@ int lexer
 #define TK_EMPTY   (TK_STACKP == TK_STACKX)
 ()
 { if (TK_EMPTY)
-    { switch (parsedir())
-        { case SCAN_ERROR:
-            perror("lexer_scan");
-          case 0:
-            yylval = 0;
-            return 0;
-          default:
-            break;
+    { TK_INIT();
+      if (scanner() == 0)
+        { yylval = 0;
+          return 0;
         }
     }
-  yylval = TK_IPOP();
-  return TK_IPOP();
+  yylval = TK_POPI();
+  return TK_POPI();
+}
+
+/* Lexical Analysis
+   Ragel state machine for tokenizing text.
+*/
+int lexer_lex
+(const char* str)
+{ lexer_pushtok(1, 2);
+  printf (str);
+  return 1;
 }
 
+
 /* Token Receiver
    This receiver takes a struct tok and pushes it to the FIFO stack.
 */
-inline
 void lexer_pushtok
+#define S(S)#S //stringifier
 #define ERR_TK "Fatal: Generated over " S(TK_STACKSIZE) " tokens in one pass."
-( struct tok token )
-{ if (TK_LEN >= TK_STACKSIZE)
+( int tok, int lval )
+{ if (TK_LEN() >= TK_STACKSIZE)
     { fprintf(stderr, ERR_TK);
       exit(EXIT_FAILURE);
     }
-  TK_PUSH(token);
-}
-
-/* Lexical Analysis
-   Ragel state machine for tokenizing text.
- */
-void lexer_lex
-(const char* str)
-{ struct tok token;
-  token.TOK = 1;
-  token.LVAL = 2;
-  lexer_pushtok(token);
-  printf (str);
+  TK_PUSH(tok, lval);
 }
-
 /*  init_file:
     if (lsp != NULL)
     while ((c = *lsp++) == *csp)
index b039cda..089d0f3 100644 (file)
@@ -20,7 +20,7 @@
 #include <unistd.h> //chdir
 #include <dirent.h> //opendir
 /* Public */
-int scanner_init();
+int scanner_init(void);
 int scanner(void);
 /* Private */
 #ifndef DL_STACKSIZE