mojoshader_preprocessor.c
changeset 1120 f655ea9c8ecd
parent 1060 4cdf5a3ceb03
child 1121 4142681f9fda
equal deleted inserted replaced
1119:4363cfdb85b9 1120:f655ea9c8ecd
    57     MOJOSHADER_includeClose close_callback;
    57     MOJOSHADER_includeClose close_callback;
    58     MOJOSHADER_malloc malloc;
    58     MOJOSHADER_malloc malloc;
    59     MOJOSHADER_free free;
    59     MOJOSHADER_free free;
    60     void *malloc_data;
    60     void *malloc_data;
    61 } Context;
    61 } Context;
       
    62 
       
    63 // Microsoft's preprocessor has some quirks. In some ways, it doesn't work
       
    64 //  like you'd expect a C preprocessor to function.
       
    65 #ifndef MATCH_MICROSOFT_PREPROCESSOR
       
    66 #define MATCH_MICROSOFT_PREPROCESSOR 1
       
    67 #endif
    62 
    68 
    63 
    69 
    64 // Convenience functions for allocators...
    70 // Convenience functions for allocators...
    65 
    71 
    66 static inline void out_of_memory(Context *ctx)
    72 static inline void out_of_memory(Context *ctx)
   718 } // pushback
   724 } // pushback
   719 
   725 
   720 
   726 
   721 static Token lexer(IncludeState *state)
   727 static Token lexer(IncludeState *state)
   722 {
   728 {
       
   729     #if !MATCH_MICROSOFT_PREPROCESSOR
       
   730     state->report_whitespace = 1;
       
   731     #endif
       
   732 
   723     if (!state->pushedback)
   733     if (!state->pushedback)
   724         return preprocessor_lexer(state);
   734         return preprocessor_lexer(state);
   725     state->pushedback = 0;
   735     state->pushedback = 0;
   726     return state->tokenval;
   736     return state->tokenval;
   727 } // lexer
   737 } // lexer
  2116             print_debug_lexing_position(state);
  2126             print_debug_lexing_position(state);
  2117             if (ctx->parsing_pragma)  // let this one through.
  2127             if (ctx->parsing_pragma)  // let this one through.
  2118                 ctx->parsing_pragma = 0;
  2128                 ctx->parsing_pragma = 0;
  2119             else
  2129             else
  2120             {
  2130             {
       
  2131                 #if MATCH_MICROSOFT_PREPROCESSOR
  2121                 // preprocessor is line-oriented, nothing else gets newlines.
  2132                 // preprocessor is line-oriented, nothing else gets newlines.
  2122                 continue;  // get the next thing.
  2133                 continue;  // get the next thing.
       
  2134                 #endif
  2123             } // else
  2135             } // else
  2124         } // else if
  2136         } // else if
  2125 
  2137 
  2126         assert(!skipping);
  2138         assert(!skipping);
  2127         *_token = token;
  2139         *_token = token;
  2159 } // preprocessor_sourcepos
  2171 } // preprocessor_sourcepos
  2160 
  2172 
  2161 
  2173 
  2162 static void indent_buffer(Buffer *buffer, int n, const int newline)
  2174 static void indent_buffer(Buffer *buffer, int n, const int newline)
  2163 {
  2175 {
       
  2176 #if MATCH_MICROSOFT_PREPROCESSOR
  2164     static char spaces[4] = { ' ', ' ', ' ', ' ' };
  2177     static char spaces[4] = { ' ', ' ', ' ', ' ' };
  2165     if (newline)
  2178     if (newline)
  2166     {
  2179     {
  2167         while (n--)
  2180         while (n--)
  2168         {
  2181         {
  2173     else
  2186     else
  2174     {
  2187     {
  2175         if (!buffer_append(buffer, spaces, 1))
  2188         if (!buffer_append(buffer, spaces, 1))
  2176             return;
  2189             return;
  2177     } // else
  2190     } // else
       
  2191 #endif
  2178 } // indent_buffer
  2192 } // indent_buffer
  2179 
  2193 
  2180 
  2194 
  2181 static const MOJOSHADER_preprocessData out_of_mem_data_preprocessor = {
  2195 static const MOJOSHADER_preprocessData out_of_mem_data_preprocessor = {
  2182     1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0
  2196     1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0
  2239         assert(token != TOKEN_EOI);
  2253         assert(token != TOKEN_EOI);
  2240 
  2254 
  2241         if (preprocessor_outofmemory(pp))
  2255         if (preprocessor_outofmemory(pp))
  2242             goto preprocess_out_of_mem;
  2256             goto preprocess_out_of_mem;
  2243 
  2257 
       
  2258         if (token == ((Token) '\n'))
       
  2259         {
       
  2260             buffer_append(buffer, endline, sizeof (endline));
       
  2261             isnewline = 1;
       
  2262         } // else if
       
  2263 
       
  2264         #if MATCH_MICROSOFT_PREPROCESSOR
  2244         // Microsoft's preprocessor is weird.
  2265         // Microsoft's preprocessor is weird.
  2245         // It ignores newlines, and then inserts its own around certain
  2266         // It ignores newlines, and then inserts its own around certain
  2246         //  tokens. For example, after a semicolon. This allows HLSL code to
  2267         //  tokens. For example, after a semicolon. This allows HLSL code to
  2247         //  be mostly readable, instead of a stream of tokens.
  2268         //  be mostly readable, instead of a stream of tokens.
  2248         if ( (token == ((Token) '}')) || (token == ((Token) ';')) )
  2269         else if ( (token == ((Token) '}')) || (token == ((Token) ';')) )
  2249         {
  2270         {
  2250             if ( (token == ((Token) '}')) && (indent > 0) )
  2271             if ( (token == ((Token) '}')) && (indent > 0) )
  2251                 indent--;
  2272                 indent--;
  2252 
  2273 
  2253             indent_buffer(buffer, indent, nl);
  2274             indent_buffer(buffer, indent, nl);
  2254             buffer_append(buffer, tokstr, len);
  2275             buffer_append(buffer, tokstr, len);
  2255             buffer_append(buffer, endline, sizeof (endline));
  2276             buffer_append(buffer, endline, sizeof (endline));
  2256 
  2277 
  2257             isnewline = 1;
  2278             isnewline = 1;
  2258         } // if
  2279         } // if
  2259 
       
  2260         else if (token == ((Token) '\n'))
       
  2261         {
       
  2262             buffer_append(buffer, endline, sizeof (endline));
       
  2263             isnewline = 1;
       
  2264         } // else if
       
  2265 
  2280 
  2266         else if (token == ((Token) '{'))
  2281         else if (token == ((Token) '{'))
  2267         {
  2282         {
  2268             buffer_append(buffer, endline, sizeof (endline));
  2283             buffer_append(buffer, endline, sizeof (endline));
  2269             indent_buffer(buffer, indent, 1);
  2284             indent_buffer(buffer, indent, 1);
  2270             buffer_append(buffer, "{", 1);
  2285             buffer_append(buffer, "{", 1);
  2271             buffer_append(buffer, endline, sizeof (endline));
  2286             buffer_append(buffer, endline, sizeof (endline));
  2272             indent++;
  2287             indent++;
  2273             isnewline = 1;
  2288             isnewline = 1;
  2274         } // else if
  2289         } // else if
       
  2290         #endif
  2275 
  2291 
  2276         else if (token == TOKEN_PREPROCESSING_ERROR)
  2292         else if (token == TOKEN_PREPROCESSING_ERROR)
  2277         {
  2293         {
  2278             unsigned int pos = 0;
  2294             unsigned int pos = 0;
  2279             const char *fname = preprocessor_sourcepos(pp, &pos);
  2295             const char *fname = preprocessor_sourcepos(pp, &pos);