Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moved asm comment processing into the lexer.
  • Loading branch information
icculus committed Feb 24, 2009
1 parent c110621 commit e456e91
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 51 deletions.
72 changes: 25 additions & 47 deletions mojoshader_assembler.c
Expand Up @@ -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


Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion mojoshader_internal.h
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion mojoshader_lexer.re
Expand Up @@ -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; }
Expand Down
7 changes: 5 additions & 2 deletions mojoshader_preprocessor.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e456e91

Please sign in to comment.