mojoshader_preprocessor.c
changeset 649 527cfe6f2d45
parent 648 243e35876142
child 650 9c91067da49d
equal deleted inserted replaced
648:243e35876142 649:527cfe6f2d45
    16 #else
    16 #else
    17     #define print_debug_token(token, len, val)
    17     #define print_debug_token(token, len, val)
    18 #endif
    18 #endif
    19 
    19 
    20 #if DEBUG_LEXER
    20 #if DEBUG_LEXER
    21 static Token debug_preprocessor_internal_lexer(IncludeState *s)
    21 static Token debug_preprocessor_lexer(IncludeState *s)
    22 {
    22 {
    23     const Token retval = preprocessor_internal_lexer(s);
    23     const Token retval = preprocessor_lexer(s);
    24     MOJOSHADER_print_debug_token("LEXER", s->token, s->tokenlen, retval);
    24     MOJOSHADER_print_debug_token("LEXER", s->token, s->tokenlen, retval);
    25     return retval;
    25     return retval;
    26 } // debug_preprocessor_internal_lexer
    26 } // debug_preprocessor_lexer
    27 #define preprocessor_internal_lexer(s) debug_preprocessor_internal_lexer(s)
    27 #define preprocessor_lexer(s) debug_preprocessor_lexer(s)
    28 #endif
    28 #endif
    29 
    29 
    30 typedef struct DefineHash
    30 typedef struct DefineHash
    31 {
    31 {
    32     MOJOSHADER_preprocessorDefine define;
    32     MOJOSHADER_preprocessorDefine define;
   674 static int require_newline(IncludeState *state)
   674 static int require_newline(IncludeState *state)
   675 {
   675 {
   676     const char *source = state->source;
   676     const char *source = state->source;
   677     const unsigned int bytes_left = state->bytes_left;
   677     const unsigned int bytes_left = state->bytes_left;
   678     const unsigned int linenum = state->line;
   678     const unsigned int linenum = state->line;
   679     const Token token = preprocessor_internal_lexer(state);
   679     const Token token = preprocessor_lexer(state);
   680     state->source = source;  // rewind no matter what.
   680     state->source = source;  // rewind no matter what.
   681     state->bytes_left = bytes_left;
   681     state->bytes_left = bytes_left;
   682     state->line = linenum;
   682     state->line = linenum;
   683     if (token == TOKEN_INCOMPLETE_COMMENT)
   683     if (token == TOKEN_INCOMPLETE_COMMENT)
   684         return 1; // call it an eol.
   684         return 1; // call it an eol.
   687 
   687 
   688 
   688 
   689 static void handle_pp_include(Context *ctx)
   689 static void handle_pp_include(Context *ctx)
   690 {
   690 {
   691     IncludeState *state = ctx->include_stack;
   691     IncludeState *state = ctx->include_stack;
   692     Token token = preprocessor_internal_lexer(state);
   692     Token token = preprocessor_lexer(state);
   693     MOJOSHADER_includeType incltype;
   693     MOJOSHADER_includeType incltype;
   694     char *filename = NULL;
   694     char *filename = NULL;
   695     int bogus = 0;
   695     int bogus = 0;
   696 
   696 
   697     if (token == TOKEN_STRING_LITERAL)
   697     if (token == TOKEN_STRING_LITERAL)
   761     IncludeState *state = ctx->include_stack;
   761     IncludeState *state = ctx->include_stack;
   762     char *filename = NULL;
   762     char *filename = NULL;
   763     int linenum = 0;
   763     int linenum = 0;
   764     int bogus = 0;
   764     int bogus = 0;
   765 
   765 
   766     if (preprocessor_internal_lexer(state) != TOKEN_INT_LITERAL)
   766     if (preprocessor_lexer(state) != TOKEN_INT_LITERAL)
   767         bogus = 1;
   767         bogus = 1;
   768     else
   768     else
   769     {
   769     {
   770         char *buf = (char *) alloca(state->tokenlen+1);
   770         char *buf = (char *) alloca(state->tokenlen+1);
   771         memcpy(buf, state->token, state->tokenlen);
   771         memcpy(buf, state->token, state->tokenlen);
   772         buf[state->tokenlen] = '\0';
   772         buf[state->tokenlen] = '\0';
   773         linenum = atoi(buf);
   773         linenum = atoi(buf);
   774     } // else
   774     } // else
   775 
   775 
   776     if (!bogus)
   776     if (!bogus)
   777         bogus = (preprocessor_internal_lexer(state) != TOKEN_STRING_LITERAL);
   777         bogus = (preprocessor_lexer(state) != TOKEN_STRING_LITERAL);
   778 
   778 
   779     if (!bogus)
   779     if (!bogus)
   780     {
   780     {
   781         state->token++;  // skip '\"'...
   781         state->token++;  // skip '\"'...
   782         filename = (char *) alloca(state->tokenlen);
   782         filename = (char *) alloca(state->tokenlen);
   817     const char *source = NULL;
   817     const char *source = NULL;
   818     while (!done)
   818     while (!done)
   819     {
   819     {
   820         bytes_left = state->bytes_left;
   820         bytes_left = state->bytes_left;
   821         source = state->source;
   821         source = state->source;
   822         const Token token = preprocessor_internal_lexer(state);
   822         const Token token = preprocessor_lexer(state);
   823         switch (token)
   823         switch (token)
   824         {
   824         {
   825             case ((Token) '\n'):
   825             case ((Token) '\n'):
   826                 state->line--;  // make sure error is on the right line.
   826                 state->line--;  // make sure error is on the right line.
   827                 // fall through!
   827                 // fall through!
   857 
   857 
   858 static void handle_pp_define(Context *ctx)
   858 static void handle_pp_define(Context *ctx)
   859 {
   859 {
   860     IncludeState *state = ctx->include_stack;
   860     IncludeState *state = ctx->include_stack;
   861 
   861 
   862     if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
   862     if (preprocessor_lexer(state) != TOKEN_IDENTIFIER)
   863     {
   863     {
   864         fail(ctx, "Macro names must be indentifiers");
   864         fail(ctx, "Macro names must be indentifiers");
   865         return;
   865         return;
   866     } // if
   866     } // if
   867 
   867 
   881     state->report_whitespace = 1;
   881     state->report_whitespace = 1;
   882     while ((!done) && (!ctx->out_of_memory))
   882     while ((!done) && (!ctx->out_of_memory))
   883     {
   883     {
   884         bytes_left = state->bytes_left;
   884         bytes_left = state->bytes_left;
   885         source = state->source;
   885         source = state->source;
   886         const Token token = preprocessor_internal_lexer(state);
   886         const Token token = preprocessor_lexer(state);
   887         switch (token)
   887         switch (token)
   888         {
   888         {
   889             case TOKEN_INCOMPLETE_COMMENT:
   889             case TOKEN_INCOMPLETE_COMMENT:
   890             case TOKEN_EOI:
   890             case TOKEN_EOI:
   891                 state->bytes_left = bytes_left;
   891                 state->bytes_left = bytes_left;
   930 
   930 
   931 static void handle_pp_undef(Context *ctx)
   931 static void handle_pp_undef(Context *ctx)
   932 {
   932 {
   933     IncludeState *state = ctx->include_stack;
   933     IncludeState *state = ctx->include_stack;
   934 
   934 
   935     if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
   935     if (preprocessor_lexer(state) != TOKEN_IDENTIFIER)
   936     {
   936     {
   937         fail(ctx, "Macro names must be indentifiers");
   937         fail(ctx, "Macro names must be indentifiers");
   938         return;
   938         return;
   939     } // if
   939     } // if
   940 
   940 
   956 {
   956 {
   957     IncludeState *state = ctx->include_stack;
   957     IncludeState *state = ctx->include_stack;
   958 
   958 
   959     assert((type == TOKEN_PP_IFDEF) || (type == TOKEN_PP_IFNDEF));
   959     assert((type == TOKEN_PP_IFDEF) || (type == TOKEN_PP_IFNDEF));
   960 
   960 
   961     if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
   961     if (preprocessor_lexer(state) != TOKEN_IDENTIFIER)
   962     {
   962     {
   963         fail(ctx, "Macro names must be indentifiers");
   963         fail(ctx, "Macro names must be indentifiers");
   964         return NULL;
   964         return NULL;
   965     } // if
   965     } // if
   966 
   966 
  1125         // TOKEN_PP_ELIF,
  1125         // TOKEN_PP_ELIF,
  1126 
  1126 
  1127         const Conditional *cond = state->conditional_stack;
  1127         const Conditional *cond = state->conditional_stack;
  1128         const int skipping = ((cond != NULL) && (cond->skipping));
  1128         const int skipping = ((cond != NULL) && (cond->skipping));
  1129 
  1129 
  1130         Token token = preprocessor_internal_lexer(state);
  1130         Token token = preprocessor_lexer(state);
  1131         if (token == TOKEN_EOI)
  1131         if (token == TOKEN_EOI)
  1132         {
  1132         {
  1133             assert(state->bytes_left == 0);
  1133             assert(state->bytes_left == 0);
  1134             if (state->conditional_stack != NULL)
  1134             if (state->conditional_stack != NULL)
  1135             {
  1135             {