From 78ec1b2aecefbd7f6839f54c52b2dd6e3f80bf1a Mon Sep 17 00:00:00 2001 From: ken Date: Sat, 14 Jan 2017 16:25:15 -0800 Subject: [PATCH] fixes --- src/ir.c | 37 ++++++++++++--- src/lexer.rl | 121 ++++++++++++++++++++++++++++++-------------------- src/scanner.c | 12 ++--- 3 files changed, 112 insertions(+), 58 deletions(-) diff --git a/src/ir.c b/src/ir.c index b68142e..f39f85b 100644 --- a/src/ir.c +++ b/src/ir.c @@ -215,9 +215,14 @@ struct ir_class_t* ir_class_addchild const uint8_t* name ) { struct ir_class_t* iter; + printf("Class %s, addchild %s\n", class->name, name); if (class->nextchild == NULL) - return class->nextchild = struct_alloc(ir_class_t); + goto alloc; iter = class->nextchild; + if (iter->name == NULL) + eprintf("Null name pointer in class %p\n", iter); + if (name == NULL) + eprintf("Null child added to class %s\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -225,6 +230,7 @@ struct ir_class_t* ir_class_addchild { iter = iter->nextsib; goto check; } + alloc: iter = struct_alloc(ir_class_t); iter->nextsib = class->nextchild; iter->name = name_alloc(name); @@ -240,9 +246,14 @@ struct ir_set_t* ir_class_addset const uint8_t* name ) { struct ir_set_t* iter; + printf("Class %s, addset %s\n", class->name, name); if (class->root_set == NULL) - return class->root_set = struct_alloc(ir_set_t); + goto alloc; iter = class->root_set; + if (iter->name == NULL) + eprintf("Null name pointer in class %p\n", iter); + if (name == NULL) + eprintf("Null set added to class %s\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -250,6 +261,7 @@ struct ir_set_t* ir_class_addset { iter = iter->nextsib; goto check; } + alloc: iter = struct_alloc(ir_set_t); iter->nextsib = class->root_set; iter->name = name_alloc(name); @@ -265,9 +277,14 @@ struct ir_set_t* ir_set_addchild const uint8_t* name ) { struct ir_set_t* iter; + printf("Set %s, addchild %s\n", set->name, name); if (set->nextchild == NULL) - return set->nextchild = struct_alloc(ir_set_t); + goto alloc; iter = set->nextchild; + if (iter->name == NULL) + eprintf("Null name pointer in set %p\n", iter); + if (name == NULL) + eprintf("Null child added to set %s\n", iter->name); check: if (bytes_identical(iter->name, name)) return iter; @@ -275,6 +292,7 @@ struct ir_set_t* ir_set_addchild { iter = iter->nextsib; goto check; } + alloc: iter = struct_alloc(ir_set_t); iter->nextsib = set->nextchild; iter->name = name_alloc(name); @@ -292,7 +310,7 @@ struct ir_framebox_t* ir_set_add_framebox ) { struct ir_framebox_t* iter; if (set->frameboxes == NULL) - return set->frameboxes = struct_alloc(ir_framebox_t); + goto alloc; iter = set->frameboxes; check: if (bytes_identical(iter->header.data_name, name)) @@ -301,6 +319,7 @@ struct ir_framebox_t* ir_set_add_framebox { iter = (struct ir_framebox_t*) iter->header.nextsib; goto check; } + alloc: iter = struct_alloc(ir_framebox_t); iter->header.nextsib = (union ir_setdata_t*) set->frameboxes; iter->header.data_name = name_alloc(name); @@ -390,7 +409,9 @@ void ir_data_assign_path ( union ir_setdata_t* setdata, const uint8_t* path ) -{ if (setdata->header.src_filename != NULL) +{ if (path == NULL) + eprintf("Null path in data %s\n", setdata->header.data_name); + if (setdata->header.src_filename != NULL) wprintf("Path override: %s -> %s for setdata %s\n", setdata->header.src_filename, path, setdata->header.data_name); setdata->header.src_filename = name_alloc(path); @@ -421,6 +442,8 @@ union ir_setdata_t* ir_framedata int height ) { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t); + if (name == NULL) + eprint("Null name in set allocation\n"); framedata->header.type = type; framedata->header.data_name = name_alloc(name); framedata->frameinfo.facing = d; @@ -432,6 +455,8 @@ union ir_setdata_t* ir_framedata union ir_setdata_t* ir_audio ( const uint8_t* name ) { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t); + if (name == NULL) + eprint("Null audio\n"); audio->header.type = ADAT; audio->header.data_name = name_alloc(name); return (union ir_setdata_t*) audio; @@ -442,6 +467,8 @@ union ir_setdata_t* ir_audio struct ir_classld_t* ir_classld_from_class ( struct ir_class_t* class ) { struct ir_classld_t* classld; + if (class == NULL) + eprint("Null class in classld\n"); classld = struct_alloc(ir_classld_t); classld->root_class = class; return classld; diff --git a/src/lexer.rl b/src/lexer.rl index 553ff3d..5e2fb77 100644 --- a/src/lexer.rl +++ b/src/lexer.rl @@ -10,24 +10,31 @@ extern //lexer.c void lexer_pushtok(int, YYSTYPE); /* Public */ int lexer_setdirection(uint8_t*, int); -int lexer_lexfile(const uint8_t*); -int lexer_lexdir(const uint8_t*); -int lexer_lexstring(const uint8_t*, int); +int lexer_lexfile(uint8_t*); +int lexer_lexdir(uint8_t*); +int lexer_lexstring(uint8_t*, int); int lexer_setstr(uint8_t*, int); //apc.c extern yypstate* apc_pstate; extern yycstate* apc_cstate; -#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (YYSTYPE*)(L), apc_cstate) -#define LEXTOK(T,L) do { \ - PUSHTOK(T,L); \ +static +YYSTYPE lval_stack[1024], * lvalsp; +#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, (L), apc_cstate) +#define LEXTOK(T,Y,L) do { \ + lvalsp->Y = L; \ + PUSHTOK(T,lvalsp); \ + lvalsp++; \ ntok++; \ } while (0); -#define LEXFACE(F) do { \ - lval.face = F; \ - LEXTOK(FACING, &lval.face); \ - } while (0); +#define PUSHFACE(F) LEXTOK(FACING, face, F) +#define PUSHREF(R) LEXTOK(REF, ref, R) +#define PUSHLINK() LEXTOK(LINK, val, 0) +#define PUSHNUM(N) LEXTOK(NUM, val, N) +#define PUSHNAME(N) LEXTOK(NAME, str, N) +#define PUSHOP(O) LEXTOK(O, val, 0) +#define PUSHPATH(P) LEXTOK(PATH, str, P) /* 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 @@ -37,62 +44,56 @@ yycstate* apc_cstate; machine lexstring; # set up yylval and tok_t to be pushed to stack - action push_ref { te = NULL; errno = 0; - lval.ref = strtoll((char*)ts,(char**)&te,16); - if (errno | (te != NULL)) + 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); - if (te != NULL) - { while (str++ < te) - fputc(' ', stderr); - fputc('^', stderr); - } exit(1); } - LEXTOK(REF, &lval.ref); + PUSHREF(lval.ref); } - action push_link { lval.val = 0; - PUSHTOK(LINK, &lval.val); } - action push_val { te = NULL; errno = 0; - lval.val = strtoll((char*)ts,(char**)&te,10); + action push_link { PUSHLINK(); } + action push_val { errno = 0; + lval.val = strtoll((char*)ts,NULL,10); if (errno) { fprintf(stderr, "strtoll could not parse %s\n", (char*)str); exit(1); } - LEXTOK(NUM, &lval.val); + PUSHNUM(lval.val); } action push_name { printf("Lexer_lexstring:: action:push_name: from %s to %s\n", ts, p); - LEXTOK(NAME, ts); + PUSHNAME(ts); } action push_map { printf("Lexer_lexstring:: action:push_map: pushing map token\n"); - LEXTOK(MAP, "~"); + 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); - LEXTOK(SS, "SS"); + PUSHOP(SS); } action push_S { printf("Lexer_lexstring:: action:push_S. p = %s\n", p); - LEXFACE(SFACE); + PUSHFACE(SFACE); } action push_SW { printf("Lexer_lexstring:: action:push_SW. p = %s\n", p); - LEXFACE(SWFACE); + PUSHFACE(SWFACE); } action push_W { printf("Lexer_lexstring:: action:push_W. p = %s\n", p); - LEXFACE(WFACE); + PUSHFACE(WFACE); } action push_NW { printf("Lexer_lexstring:: action:push_NW. p = %s\n", p); - LEXFACE(NWFACE); + PUSHFACE(NWFACE); } action push_N { printf("Lexer_lexstring:: action:push_N. p = %s\n", p); - LEXFACE(NFACE); + PUSHFACE(NFACE); } action push_NE { printf("Lexer_lexstring:: action:push_NE. p = %s\n", p); - LEXFACE(NEFACE); + PUSHFACE(NEFACE); } action push_E { printf("Lexer_lexstring:: action:push_N. p = %s\n", p); - LEXFACE(EFACE); + PUSHFACE(EFACE); } action push_SE { printf("Lexer_lexstring:: action:push_N. p = %s\n", p); - LEXFACE(SEFACE); + 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); exit(1); @@ -128,18 +129,18 @@ yycstate* apc_cstate; }%% int lexer_lexstring -( const uint8_t* str, +( uint8_t* str, int size ) -{ const uint8_t *p; - const uint8_t *ts, *pe, *te, *eof; +{ uint8_t *p; + uint8_t *ts, *pe, *eof; int cs, ntok; YYSTYPE lval; + lvalsp = lval_stack; ntok = 0; p = ts = str; pe = eof = p + size + 1; - printf("|---Begin lexstring on p = %s, pe = %s.\n",p, pe); @@ -157,26 +158,50 @@ int lexer_lexstring Returns the number of tokens pushed to the parser. */ int lexer_lexfile -( uint8_t const* filename ) -{ uint8_t const* last_period,* iter; +( uint8_t* filename ) +{ uint8_t* last_period,* iter,* filename_end; int ntok; last_period = NULL; for (iter = filename; *iter; iter++) - if (*iter == '.') - last_period = iter; + switch (*iter) + { // Keep track of the last 'dot' in the name + case '.' : last_period = iter; continue; + // replace '_' with '\0' so bison can use strlen on them as tokens. + case '_' : *iter = '\0'; + default: continue; + } + // Mark the end of the filename + filename_end = iter; + // Lex from either the last period, if present, or filename end ntok = (last_period) ? lexer_lexstring(filename, (int)(last_period - filename)) : lexer_lexstring(filename, (int)(iter - filename)); - PUSHTOK(PATH,&filename); + // Replace nulls with their original '_' + for (iter = filename; iter < filename_end; iter++) + if (*iter == '\0') + *iter = '_'; + PUSHPATH(filename); return ntok + 1; return en_main == 1; } int lexer_lexdir -( uint8_t const* dirname ) -{ uint8_t const* de = dirname; - while (*de++); - return lexer_lexstring(dirname, (int)(de - dirname)); +( uint8_t* dirname ) +{ uint8_t* de = dirname; + int ntok; + ntok = 0; + de = dirname; + if (*de) while (*++de); + ntok = lexer_lexstring(dirname, (int)(de - dirname)); + PUSHOP(CLOPEN); + return ntok; +} + +int lexer_closedir +( void ) +{ int ntok = 0; + PUSHOP(CLCLOSE); + return ntok; } /**************************/ diff --git a/src/scanner.c b/src/scanner.c index 96b66b6..abf472e 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -32,8 +32,11 @@ yypstate* apc_pstate; yycstate* apc_cstate; /* Private */ extern //lexer.rl -int lexer_lexfile(uint8_t const*); -#define PUSHTOK(T,L) yypush_parse(apc_pstate, T, L, apc_cstate) +int lexer_lexfile(uint8_t*); +extern //lexer.rl +int lexer_lexdir(uint8_t*); +extern //lexer.rl +void lexer_closedir(void); /* Init Establishes yy states @@ -97,20 +100,19 @@ int scanner_scandir lexer_lexfile((uint8_t*)direntp->d_name); goto scan_next_dirent; case DT_DIR: - lexer_lexfile((uint8_t*)direntp->d_name); //lex the dirname + lexer_lexdir((uint8_t*)direntp->d_name); //lex the dirname printf("lexdir %s\n",direntp->d_name); if (chdir(direntp->d_name)) //change to the specified dir goto libfail; errno = 0; if ((cdirp = opendir(".")) == NULL || errno) //open it goto libfail; - PUSHTOK(CLOPEN, NULL); //push "Open Directory" token printf("Scanner entered [%s]\n",direntp->d_name); if(scanner_scandir(cdirp)) //scan the directory goto libfail; if (chdir("..")) //return to the parent dir goto libfail; - PUSHTOK(CLCLOSE, NULL); //push "Close Directory" token + lexer_closedir(); //push "Close Directory" token printf("Scanner returned\n"); goto scan_next_dirent; //continue scan case DT_UNKNOWN: -- 2.18.0