X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fwebcc.git;a=blobdiff_plain;f=src%2Fapc%2Flexer.c;h=6ef1b4c5ac85600365e3fa56fc70f049c9f5de26;hp=4d3ab0a32ee4dee025e5851c4f859ecd387de456;hb=03efe0c43855ff6af7eb2194f6d4a5f022bc415d;hpb=e0b51d0d5df867ac6a44d047342312d535620c01 diff --git a/src/apc/lexer.c b/src/apc/lexer.c index 4d3ab0a..6ef1b4c 100644 --- a/src/apc/lexer.c +++ b/src/apc/lexer.c @@ -31,9 +31,9 @@ int lexer_init(void); int lexer(void); void lexer_pushtok(int, YYSTYPE); -extern //ragel +extern //lexer_lex.rl int lexer_lex(const char*); -struct dirent* lexer_direntpa[DE_STACKSIZE]; +struct dirent* lexer_direntpa[DE_STACKSIZE], **lexer_direntpp; /* Private */ extern //scanner.c int scanner_init(void); @@ -45,17 +45,15 @@ extern //bison YYSTYPE yylval; static struct tok -{ union YYSTYPE val; //token val - int tt; //token type +{ YYSTYPE lval; //token val + int tok_t; //token type } token_stack[TK_STACKSIZE]; static union tokp { int* tpt; //token pointer type struct tok* tok; - union YYSTYPE* tvp; //token value pointer + YYSTYPE* tvp; //token value pointer } tks, tkx; -static -struct dirent** dps; /* Directory Entity Array/Stack Simple array for keeping track of dirents yet to be processed by the scanner. @@ -64,7 +62,7 @@ struct dirent** dps; lexer as a stack. */ #define DE_STACK (lexer_direntpa) -#define DE_STACKP (dps) +#define DE_STACKP (lexer_direntpp) #define DE_LEN() (DE_STACKP - DE_STACK) #define DE_INIT() (DE_STACKP = DE_STACK) #define DE_POP() (*--DE_STACKP) @@ -83,7 +81,7 @@ struct dirent** dps; #define TK_STACKPL (tks.tvp) #define TK_STACKX (tkx.tok) #define TK_STACKXI (tkx.tpt) -#define TK_LEN() (TK_STACKP - TK_STACKX) +#define TK_LEN() (TK_STACKX - TK_STACKP) #define TK_INIT() (TK_STACKP = TK_STACKX = TK_STACK) #define TK_POP() (*TK_STACKP++) #define TK_POPI() (*TK_STACKPI++); @@ -102,24 +100,41 @@ int lexer_init /* Lexer If the token buffer is empty, 'lexer' will initialize the token buffer and - call 'lexer_scandir'. If #SCANDIR_ERROR is returned, an error is printed + call 'lexer_scandir'. If SCAN_ERROR is returned, an error is printed before sending a null return to bison. If 0 tokens are generated, the error printing is skipped. In all other cases, 'yylval' is set, and the token's integer representation is returned. */ int lexer +#define $($)#$ #define SCAN_ERROR -1 #define TK_EMPTY (TK_STACKP == TK_STACKX) +#define FAIL(...) \ + do { \ + fprintf(stderr,__VA_ARGS__); \ + goto done; \ + } while (0) () -{ if (TK_EMPTY) - { TK_INIT(); - if (scanner() == 0) - { yylval.val = 0; - return 0; +{start: + while (DE_LEN() > 0) //lex any directory entries in our stack + if (lexer_lex(DE_POP()->d_name) == 0) //fail if it generates no tokens + FAIL("Lexer failed to tokenize [%s]\n",(*DE_STACKP)->d_name); + if (TK_EMPTY) //if there are no tokens, + { TK_INIT(); //initialize the token stack back to 0 + switch (scanner()) + { case SCAN_ERROR: //if an error occurred, + FAIL("Scanner error\n"); + case 0: //if the the scanner finds no dirents, + goto done; //then we are done + default: //if we found some elements to scan, + goto start; //start over and lex them } } yylval = TK_POPL(); return TK_POPI(); + done: + yylval.val = 0; + return 0; } @@ -127,27 +142,13 @@ int lexer This receiver takes a struct tok and pushes it to the FIFO stack. */ void lexer_pushtok -#define S(S)#S //stringifier -#define ERR_TK "Fatal: Generated over " S(TK_STACKSIZE) " tokens in one pass." +#define $($)#$ //stringifier +#define ERR_TK "Fatal: Generated over " $(TK_STACKSIZE) " tokens in one pass." ( int tok, YYSTYPE lval ) { if (TK_LEN() >= TK_STACKSIZE) { fprintf(stderr, ERR_TK); exit(EXIT_FAILURE); } TK_PUSH(tok, lval); + printf("Pushed Token %i | %i\n", TK_STACK[TK_LEN() - 1].tok_t, TK_STACK[TK_LEN() - 1].lval.val); } -/* init_file: - if (lsp != NULL) - while ((c = *lsp++) == *csp) - { switch (c) - { case DELIM: - delimeters_skipped++; - default: - csp++; //delayed to ensure csp is the start of scannable text - break; - } - } - last_string = string; - scan_text: - return scanner_tokenize(csp); -*/