From e456e9175b187fcbf2900cadf919d2dd454fabe1 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 23 Feb 2009 23:07:06 -0500 Subject: [PATCH] Moved asm comment processing into the lexer. --- mojoshader_assembler.c | 72 ++++++++++++++------------------------- mojoshader_internal.h | 3 +- mojoshader_lexer.re | 3 +- mojoshader_preprocessor.c | 7 ++-- 4 files changed, 34 insertions(+), 51 deletions(-) diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index 30d7e904..a2a6636e 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -197,62 +197,40 @@ static inline void pushback(Context *ctx) ctx->pushedback = 1; } // pushback -static Token _nexttoken(Context *ctx) -{ - while (1) - { - ctx->token = preprocessor_nexttoken(ctx->preprocessor, &ctx->tokenlen, - &ctx->tokenval); - - if (preprocessor_outofmemory(ctx->preprocessor)) - { - out_of_memory(ctx); - ctx->tokenval = TOKEN_EOI; - ctx->token = NULL; - ctx->tokenlen = 0; - break; - } // if - - if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR) - { - fail(ctx, ctx->token); - continue; - } // else - - break; - } // while - - return ctx->tokenval; -} // _nexttoken - static Token nexttoken(Context *ctx) { if (ctx->pushedback) - { - print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval); ctx->pushedback = 0; - return ctx->tokenval; - } // if - - Token token = _nexttoken(ctx); - - while (token == ((Token) '\n')) - token = _nexttoken(ctx); // skip endlines. - - if (token == ((Token) ';')) // single line comment in assembler. + else { - do + while (1) { - token = _nexttoken(ctx); - } while ((token != ((Token) '\n')) && (token != TOKEN_EOI)); + ctx->token = preprocessor_nexttoken(ctx->preprocessor, + &ctx->tokenlen, + &ctx->tokenval); - while (token == ((Token) '\n')) - token = _nexttoken(ctx); // skip endlines. - } // if + if (preprocessor_outofmemory(ctx->preprocessor)) + { + out_of_memory(ctx); + ctx->tokenval = TOKEN_EOI; + ctx->token = NULL; + ctx->tokenlen = 0; + break; + } // if + + else if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR) + { + fail(ctx, ctx->token); + continue; + } // else if + + break; + } // while + } // else print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval); - return token; + return ctx->tokenval; } // nexttoken @@ -1427,7 +1405,7 @@ static Context *build_context(const char *filename, ctx->parse_phase = MOJOSHADER_PARSEPHASE_NOTSTARTED; ctx->preprocessor = preprocessor_start(filename, source, sourcelen, include_open, include_close, - defines, define_count, m, f, d); + defines, define_count, 1, m, f, d); if (ctx->preprocessor == NULL) { diff --git a/mojoshader_internal.h b/mojoshader_internal.h index 1dfac764..8ae7a07a 100644 --- a/mojoshader_internal.h +++ b/mojoshader_internal.h @@ -410,6 +410,7 @@ typedef struct IncludeState int pushedback; const unsigned char *lexer_marker; int report_whitespace; + int asm_comments; unsigned int orig_length; unsigned int bytes_left; unsigned int line; @@ -428,7 +429,7 @@ Preprocessor *preprocessor_start(const char *fname, const char *source, MOJOSHADER_includeOpen open_callback, MOJOSHADER_includeClose close_callback, const MOJOSHADER_preprocessorDefine *defines, - unsigned int define_count, + unsigned int define_count, int asm_comments, MOJOSHADER_malloc m, MOJOSHADER_free f, void *d); void preprocessor_end(Preprocessor *pp); diff --git a/mojoshader_lexer.re b/mojoshader_lexer.re index 4c0f272e..73b1c6eb 100644 --- a/mojoshader_lexer.re +++ b/mojoshader_lexer.re @@ -148,12 +148,13 @@ scanner_loop: "^" { RET('^'); } "|" { RET('|'); } ":" { RET(':'); } - ";" { RET(';'); } "{" { RET('{'); } "}" { RET('}'); } "=" { RET('='); } "?" { RET('?'); } + ";" { if (s->asm_comments) goto singlelinecomment; RET(';'); } + "\000" { if (eoi) { RET(TOKEN_EOI); } goto bad_chars; } WHITESPACE { if (s->report_whitespace) RET(' '); goto scanner_loop; } diff --git a/mojoshader_preprocessor.c b/mojoshader_preprocessor.c index e0d3a9dc..0c7d225f 100644 --- a/mojoshader_preprocessor.c +++ b/mojoshader_preprocessor.c @@ -42,6 +42,7 @@ typedef struct Context int out_of_memory; char failstr[256]; int recursion_count; + int asm_comments; Conditional *conditional_pool; IncludeState *include_stack; IncludeState *include_pool; @@ -552,6 +553,7 @@ static int push_source(Context *ctx, const char *fname, const char *source, state->line = linenum; state->defines = defs; state->next = ctx->include_stack; + state->asm_comments = ctx->asm_comments; ctx->include_stack = state; @@ -609,7 +611,7 @@ Preprocessor *preprocessor_start(const char *fname, const char *source, MOJOSHADER_includeOpen open_callback, MOJOSHADER_includeClose close_callback, const MOJOSHADER_preprocessorDefine *defines, - unsigned int define_count, + unsigned int define_count, int asm_comments, MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) { int okay = 1; @@ -631,6 +633,7 @@ Preprocessor *preprocessor_start(const char *fname, const char *source, ctx->malloc_data = d; ctx->open_callback = open_callback; ctx->close_callback = close_callback; + ctx->asm_comments = asm_comments; // let the usual preprocessor parser sort these out. char *define_include = NULL; @@ -1919,7 +1922,7 @@ const MOJOSHADER_preprocessData *MOJOSHADER_preprocess(const char *filename, Preprocessor *pp = preprocessor_start(filename, source, sourcelen, include_open, include_close, - defines, define_count, m, f, d); + defines, define_count, 0, m, f, d); if (pp == NULL) return &out_of_mem_data_preprocessor;