#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
+#include <unistr.h>
#include "parser.tab.h"
#include "apc.h"
-#include <unistdio.h>
-#include <unistr.h>
+#include "print.h"
/* Public */
int lexer_init(void);
void lexer_quit(void);
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 { \
- lval_stack[lval_offs].Y = L; \
- PUSHTOK(T,lval_stack + lval_offs); \
- lval_offs++; \
- ntok++; \
+#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++; \
+ ntok++; \
} while (0)
#define PUSHFACE(F) LEXTOK(FACING, face, F)
#define PUSHREF(R) LEXTOK(REF, ref, R)
#define PUSHOP(O) LEXTOK(O, val, 0)
#define PUSHPATH(P) LEXTOK(PATH, str, P)
-#define DEBUG 1
-#define dprintf(_B, ...) do { \
- if(_B) printf(__VA_ARGS__); \
- } while (0)
-
-
/* 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
eventually calls during parsing. */
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);
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 { dprintf(DEBUG, "Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p);
- PUSHNAME(ts);
- }
- action push_map { dprintf(DEBUG, "Lexer_lexstring:: action:push_map: pushing map token\n");
- PUSHOP(MAP);
- }
- action set_ts { dprintf(DEBUG, "Lexer_lexstring:: action:set_ts. ts = %s\n", p);
- ts = p; }
- action push_SS { dprintf(DEBUG, "Lexer_lexstring:: action:push_SS. p = %s\n",p);
- PUSHOP(SS);
- }
- action push_S { dprintf(DEBUG, "Lexer_lexstring:: action:push_S. p = %s\n", p);
- PUSHFACE(SFACE);
- }
- action push_SW { dprintf(DEBUG, "Lexer_lexstring:: action:push_SW. p = %s\n", p);
- PUSHFACE(SWFACE);
- }
- action push_W { dprintf(DEBUG, "Lexer_lexstring:: action:push_W. p = %s\n", p);
- PUSHFACE(WFACE);
- }
- action push_NW { dprintf(DEBUG, "Lexer_lexstring:: action:push_NW. p = %s\n", p);
- PUSHFACE(NWFACE);
- }
- action push_N { dprintf(DEBUG, "Lexer_lexstring:: action:push_N. p = %s\n", p);
- PUSHFACE(NFACE);
- }
- action push_NE { dprintf(DEBUG, "Lexer_lexstring:: action:push_NE. p = %s\n", p);
- PUSHFACE(NEFACE);
- }
- action push_E { dprintf(DEBUG, "Lexer_lexstring:: action:push_N. p = %s\n", p);
- PUSHFACE(EFACE);
- }
- action push_SE { dprintf(DEBUG, "Lexer_lexstring:: action:push_N. p = %s\n", p);
- PUSHFACE(SEFACE);
- }
- action ref_error { dprintf(DEBUG, "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 { dprintf("Lexer_lexstring:: p = %s\n", p);
- }
+ action p { dprintf("Lexer_lexstring:: p = %U\n", p); }
N = 'N' %push_N;
W = 'W' %push_W;
tok = (name | val | ref | dimensions | map | link | SS | direction);
- main := (tok tok_delimiter)* tok [\0];
+ main := (tok tok_delimiter)* tok ;
write data nofinal noerror noprefix;
cstate = yycstate_new();
lval_offs = 0;
return !pstate || !cstate;
+ return en_main == 1;
}
void lexer_quit
{ uint8_t* p, * ts, * pe, * eof;
int cs, ntok;
YYSTYPE lval;
-
ntok = 0;
p = ts = str;
- pe = eof = p + size + 1;
-
- dprintf("|---Begin lexstring on p = %s, pe = %s.\n", (char*)p, pe);
-
+ pe = eof = p + size;
%%write init;
%%write exec;
-
- dprintf("Ending lexstring of file %s, pushed %d tokens.\n", (char*)str, ntok);
-
return ntok;
}
// 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));
+
// Replace nulls with their original '_'
for (iter = filename; iter < filename_end; iter++)
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;
}