X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fapc.git;a=blobdiff_plain;f=src%2Flexer.rl;h=920d62dafc5048b7b2ea2c2987c3adee8a7c66c0;hp=6bb1d1bb7460781ee2629d62d160c4a90dec5f7c;hb=7dae7dc73dfbabdc895a78738f550f56561da644;hpb=25a23e5134b1d9649f0279a4028b887c9deb9987 diff --git a/src/lexer.rl b/src/lexer.rl index 6bb1d1b..920d62d 100644 --- a/src/lexer.rl +++ b/src/lexer.rl @@ -2,10 +2,10 @@ #include #include #include +#include #include "parser.tab.h" #include "apc.h" -#include -#include +#include "print.h" /* Public */ int lexer_init(void); void lexer_quit(void); @@ -22,14 +22,25 @@ static YYSTYPE lval_stack[0xFF + 1]; static uint8_t lval_offs; +#define $($)#$ #define PUSHTOK(T,L) yypush_parse(pstate, T, (L), cstate) -#define LEXTOK(T,Y,L) do { \ - printf("lval[%i]\n",lval_offs);\ +#define LEXTOK(T,Y,L) do { \ + if (DEBUG) { \ + ulc_fprintf(stdout, "["$(T)); \ + switch (T) { \ + case NAME: case PATH: \ + ulc_fprintf(stdout, "->%U", L); break; \ + case REF: \ + ulc_fprintf(stdout, "->%X", L); break; \ + default: break; \ + } \ + ulc_fprintf(stdout, "]"); \ + } \ lval_stack[lval_offs].Y = L; \ PUSHTOK(T,lval_stack + lval_offs); \ - lval_offs = (lval_offs + 1); \ - ntok++; \ - } while (0); + lval_offs++; \ + ntok++; \ + } while (0) #define PUSHFACE(F) LEXTOK(FACING, face, F) #define PUSHREF(R) LEXTOK(REF, ref, R) #define PUSHLINK() LEXTOK(LINK, val, 0) @@ -49,7 +60,7 @@ uint8_t lval_offs; 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); + { ulc_fprintf(stderr, "Invalid hex number in file %U\n",str); exit(1); } PUSHREF(lval.ref); @@ -58,49 +69,27 @@ uint8_t lval_offs; action push_val { errno = 0; lval.val = strtoll((char*)ts,NULL,10); if (errno) - { fprintf(stderr, "strtoll could not parse %s\n", (char*)str); + { ulc_fprintf(stderr, "strtoll could not parse %U\n",str); exit(1); } PUSHNUM(lval.val); } - action push_name { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p); - PUSHNAME(ts); - } - action push_map { printf("Lexer_lexstring:: action:push_map: pushing map token\n"); - 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); - PUSHOP(SS); - } - action push_S { printf("Lexer_lexstring:: action:push_S. p = %s\n", p); - PUSHFACE(SFACE); - } - action push_SW { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p); - PUSHFACE(SWFACE); - } - action push_W { printf("Lexer_lexstring:: action:push_W. p = %s\n", p); - PUSHFACE(WFACE); - } - action push_NW { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p); - PUSHFACE(NWFACE); - } - action push_N { printf("Lexer_lexstring:: action:push_N. p = %s\n", p); - PUSHFACE(NFACE); - } - action push_NE { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p); - PUSHFACE(NEFACE); - } - action push_E { printf("Lexer_lexstring:: action:push_N. p = %s\n", p); - PUSHFACE(EFACE); - } - action push_SE { printf("Lexer_lexstring:: action:push_N. p = %s\n", p); - 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); + action push_name { PUSHNAME(ts); } + action push_map { PUSHOP(MAP); } + action set_ts { ts = p; } + action push_SS { PUSHOP(SS); } + action push_S { PUSHFACE(SFACE); } + action push_SW { PUSHFACE(SWFACE); } + action push_W { PUSHFACE(WFACE); } + action push_NW { PUSHFACE(NWFACE); } + action push_N { PUSHFACE(NFACE); } + action push_NE { PUSHFACE(NEFACE); } + action push_E { PUSHFACE(EFACE); } + action push_SE { PUSHFACE(SEFACE); } + action ref_error { ulc_fprintf(stderr, "ref from %U to %U has an inappropriate amount of hex digits, it must have eight.\n", ts, p); exit(1); } - action p { printf("Lexer_lexstring:: p = %s\n", p);} + action p { dprintf("Lexer_lexstring:: p = %U\n", p); } N = 'N' %push_N; W = 'W' %push_W; @@ -111,7 +100,7 @@ uint8_t lval_offs; SW = 'SW' %push_SW; SE = 'SE' %push_SE; - tok_delimiter = [_]; + tok_delimiter = [_\0]; direction = (N | W | S | E | NW | NE | SW | SE) ; dimensions = (digit+ - '0') >set_ts %push_val 'x' (digit+ - '0') >set_ts %push_val; @@ -136,6 +125,7 @@ int lexer_init cstate = yycstate_new(); lval_offs = 0; return !pstate || !cstate; + return en_main == 1; } void lexer_quit @@ -148,22 +138,14 @@ int lexer_lexstring ( uint8_t* str, int size ) -{ uint8_t *p; - uint8_t *ts, *pe, *eof; +{ uint8_t* p, * ts, * pe, * eof; int cs, ntok; YYSTYPE lval; - ntok = 0; p = ts = str; pe = eof = p + size + 1; - - printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe); - %%write init; %%write exec; - - printf("Ending lexstring of file %s, pushed %d tokens.\n",str, ntok); - return ntok; } @@ -188,6 +170,7 @@ int lexer_lexfile // Mark the end of the filename filename_end = iter; // Lex from either the last period, if present, or filename end + dprintf("%U\n\t",filename); ntok = (last_period) ? lexer_lexstring(filename, (int)(last_period - filename)) : lexer_lexstring(filename, (int)(iter - filename)); @@ -196,25 +179,26 @@ int lexer_lexfile if (*iter == '\0') *iter = '_'; PUSHPATH(filename); + dprintf("\n\t[%i Token%s]\n", ntok, (ntok > 1) ? "s" : ""); return ntok + 1; - return en_main == 1; } int lexer_lexdir ( uint8_t* dirname ) -{ uint8_t* de = dirname; - int ntok; +{ int ntok; ntok = 0; - de = dirname; - if (*de) while (*++de); - ntok = lexer_lexstring(dirname, (int)(de - dirname)); + if (DEBUG) putchar('\t'); + PUSHNAME(dirname); PUSHOP(CLOPEN); + if (DEBUG) putchar('\n'); return ntok; } int lexer_closedir ( void ) { int ntok = 0; + if (DEBUG) putchar('\t'); PUSHOP(CLCLOSE); + if (DEBUG) putchar('\n'); return ntok; }