| 1 |
/** |
| 2 |
* MojoShader; generate shader programs from bytecode of compiled |
| 3 |
* Direct3D shaders. |
| 4 |
* |
| 5 |
* Please see the file LICENSE.txt in the source's root directory. |
| 6 |
* |
| 7 |
* This file written by Ryan C. Gordon. |
| 8 |
*/ |
| 9 |
|
| 10 |
// This was originally based on examples/pp-c.re from re2c: http://re2c.org/ |
| 11 |
// re2c is public domain code. |
| 12 |
// |
| 13 |
// You build mojoshader_lexer.c from the .re file with re2c... |
| 14 |
// re2c -is -o mojoshader_lexer.c mojoshader_lexer.re |
| 15 |
// |
| 16 |
// Changes to the lexer are done to the .re file, not the C code! |
| 17 |
// |
| 18 |
// Please note that this isn't a perfect C lexer, since it is used for both |
| 19 |
// HLSL and shader assembly language, and follows the quirks of Microsoft's |
| 20 |
// tools. |
| 21 |
|
| 22 |
#define __MOJOSHADER_INTERNAL__ 1 |
| 23 |
#include "mojoshader_internal.h" |
| 24 |
|
| 25 |
typedef unsigned char uchar; |
| 26 |
|
| 27 |
/*!max:re2c */ |
| 28 |
#define RET(t) return update_state(s, eoi, cursor, token, (Token) t) |
| 29 |
#define YYCTYPE uchar |
| 30 |
#define YYCURSOR cursor |
| 31 |
#define YYLIMIT limit |
| 32 |
#define YYMARKER s->lexer_marker |
| 33 |
#define YYFILL(n) { if ((n) == 1) { cursor = sentinel; limit = cursor + YYMAXFILL; eoi = 1; } } |
| 34 |
|
| 35 |
static uchar sentinel[YYMAXFILL]; |
| 36 |
|
| 37 |
static Token update_state(IncludeState *s, int eoi, const uchar *cur, |
| 38 |
const uchar *tok, const Token val) |
| 39 |
{ |
| 40 |
if (eoi) |
| 41 |
{ |
| 42 |
s->bytes_left = 0; |
| 43 |
s->source = (const char *) s->source_base + s->orig_length; |
| 44 |
if ( (tok >= sentinel) && (tok < (sentinel+YYMAXFILL)) ) |
| 45 |
s->token = s->source; |
| 46 |
else |
| 47 |
s->token = (const char *) tok; |
| 48 |
} // if |
| 49 |
else |
| 50 |
{ |
| 51 |
s->bytes_left -= (unsigned int) (cur - ((const uchar *) s->source)); |
| 52 |
s->source = (const char *) cur; |
| 53 |
s->token = (const char *) tok; |
| 54 |
} // else |
| 55 |
s->tokenlen = (unsigned int) (s->source - s->token); |
| 56 |
s->tokenval = val; |
| 57 |
return val; |
| 58 |
} // update_state |
| 59 |
|
| 60 |
Token preprocessor_lexer(IncludeState *s) |
| 61 |
{ |
| 62 |
const uchar *cursor = (const uchar *) s->source; |
| 63 |
const uchar *token = cursor; |
| 64 |
const uchar *matchptr; |
| 65 |
const uchar *limit = cursor + s->bytes_left; |
| 66 |
int eoi = 0; |
| 67 |
|
| 68 |
/*!re2c |
| 69 |
ANY = [\000-\377]; |
| 70 |
ANYLEGAL = [a-zA-Z0-9_/'*=+%^&|!#<>()[{}.,~^:;? \t\v\f\r\n\-\]\\]; |
| 71 |
O = [0-7]; |
| 72 |
D = [0-9]; |
| 73 |
L = [a-zA-Z_]; |
| 74 |
H = [a-fA-F0-9]; |
| 75 |
E = [Ee] [+-]? D+; |
| 76 |
FS = [fFhH]; |
| 77 |
IS = [uUlL]*; |
| 78 |
ESC = [\\] ([abfnrtv?'"\\] | "x" H+ | O+); |
| 79 |
PP = "#" [ \t]*; |
| 80 |
NEWLINE = ("\r\n" | "\r" | "\n"); |
| 81 |
WHITESPACE = [ \t\v\f]+; |
| 82 |
*/ |
| 83 |
|
| 84 |
// preprocessor directives are only valid at start of line. |
| 85 |
if (s->tokenval == ((Token) '\n')) |
| 86 |
goto ppdirective; // may jump back to scanner_loop. |
| 87 |
|
| 88 |
scanner_loop: |
| 89 |
if (YYLIMIT == YYCURSOR) YYFILL(1); |
| 90 |
token = cursor; |
| 91 |
|
| 92 |
/*!re2c |
| 93 |
"\\" [ \t\v\f]* NEWLINE { s->line++; goto scanner_loop; } |
| 94 |
|
| 95 |
"/*" { goto multilinecomment; } |
| 96 |
"//" { goto singlelinecomment; } |
| 97 |
|
| 98 |
L (L|D)* { RET(TOKEN_IDENTIFIER); } |
| 99 |
|
| 100 |
("0" [xX] H+ IS?) | ("0" D+ IS?) | (D+ IS?) | |
| 101 |
(['] (ESC|ANY\[\r\n\\'])* [']) |
| 102 |
{ RET(TOKEN_INT_LITERAL); } |
| 103 |
|
| 104 |
(D+ E FS?) | (D* "." D+ E? FS?) | (D+ "." D* E? FS?) |
| 105 |
{ RET(TOKEN_FLOAT_LITERAL); } |
| 106 |
|
| 107 |
(["] (ESC|ANY\[\r\n\\"])* ["]) |
| 108 |
{ RET(TOKEN_STRING_LITERAL); } |
| 109 |
|
| 110 |
">>=" { RET(TOKEN_RSHIFTASSIGN); } |
| 111 |
"<<=" { RET(TOKEN_LSHIFTASSIGN); } |
| 112 |
"+=" { RET(TOKEN_ADDASSIGN); } |
| 113 |
"-=" { RET(TOKEN_SUBASSIGN); } |
| 114 |
"*=" { RET(TOKEN_MULTASSIGN); } |
| 115 |
"/=" { RET(TOKEN_DIVASSIGN); } |
| 116 |
"%=" { RET(TOKEN_MODASSIGN); } |
| 117 |
"^=" { RET(TOKEN_XORASSIGN); } |
| 118 |
"&=" { RET(TOKEN_ANDASSIGN); } |
| 119 |
"|=" { RET(TOKEN_ORASSIGN); } |
| 120 |
"++" { RET(TOKEN_INCREMENT); } |
| 121 |
"--" { RET(TOKEN_DECREMENT); } |
| 122 |
">>" { RET(TOKEN_RSHIFT); } |
| 123 |
"<<" { RET(TOKEN_LSHIFT); } |
| 124 |
"&&" { RET(TOKEN_ANDAND); } |
| 125 |
"||" { RET(TOKEN_OROR); } |
| 126 |
"<=" { RET(TOKEN_LEQ); } |
| 127 |
">=" { RET(TOKEN_GEQ); } |
| 128 |
"==" { RET(TOKEN_EQL); } |
| 129 |
"!=" { RET(TOKEN_NEQ); } |
| 130 |
"#" { RET(TOKEN_HASH); } |
| 131 |
"##" { RET(TOKEN_HASHHASH); } |
| 132 |
"(" { RET('('); } |
| 133 |
")" { RET(')'); } |
| 134 |
"[" { RET('['); } |
| 135 |
"]" { RET(']'); } |
| 136 |
"." { RET('.'); } |
| 137 |
"," { RET(','); } |
| 138 |
"&" { RET('&'); } |
| 139 |
"!" { RET('!'); } |
| 140 |
"~" { RET('~'); } |
| 141 |
"-" { RET('-'); } |
| 142 |
"+" { RET('+'); } |
| 143 |
"*" { RET('*'); } |
| 144 |
"/" { RET('/'); } |
| 145 |
"%" { RET('%'); } |
| 146 |
"<" { RET('<'); } |
| 147 |
">" { RET('>'); } |
| 148 |
"^" { RET('^'); } |
| 149 |
"|" { RET('|'); } |
| 150 |
":" { RET(':'); } |
| 151 |
"{" { RET('{'); } |
| 152 |
"}" { RET('}'); } |
| 153 |
"=" { RET('='); } |
| 154 |
"?" { RET('?'); } |
| 155 |
|
| 156 |
";" { if (s->asm_comments) goto singlelinecomment; RET(';'); } |
| 157 |
|
| 158 |
"\000" { if (eoi) { RET(TOKEN_EOI); } goto bad_chars; } |
| 159 |
|
| 160 |
WHITESPACE { if (s->report_whitespace) RET(' '); goto scanner_loop; } |
| 161 |
NEWLINE { s->line++; RET('\n'); } |
| 162 |
ANY { goto bad_chars; } |
| 163 |
*/ |
| 164 |
|
| 165 |
multilinecomment: |
| 166 |
if (YYLIMIT == YYCURSOR) YYFILL(1); |
| 167 |
matchptr = cursor; |
| 168 |
// The "*\/" is just to avoid screwing up text editor syntax highlighting. |
| 169 |
/*!re2c |
| 170 |
"*\/" { |
| 171 |
if (s->report_comments) |
| 172 |
RET(TOKEN_MULTI_COMMENT); |
| 173 |
else if (s->report_whitespace) |
| 174 |
RET(' '); |
| 175 |
|
| 176 |
// Microsoft's preprocessor allows multiline comments |
| 177 |
// before a preprocessor directive, even though C/C++ |
| 178 |
// doesn't. See if we've hit this case. |
| 179 |
#if MATCH_MICROSOFT_PREPROCESSOR |
| 180 |
if (s->tokenval == ((Token) '\n')) // was start of line? |
| 181 |
{ |
| 182 |
update_state(s, eoi, cursor, token, (Token) '\n'); |
| 183 |
goto ppdirective; // may jump back to scanner_loop. |
| 184 |
} |
| 185 |
#endif |
| 186 |
|
| 187 |
goto scanner_loop; |
| 188 |
} |
| 189 |
NEWLINE { |
| 190 |
s->line++; |
| 191 |
goto multilinecomment; |
| 192 |
} |
| 193 |
"\000" { |
| 194 |
if (eoi) |
| 195 |
RET(TOKEN_INCOMPLETE_COMMENT); |
| 196 |
goto multilinecomment; |
| 197 |
} |
| 198 |
ANY { goto multilinecomment; } |
| 199 |
*/ |
| 200 |
|
| 201 |
singlelinecomment: |
| 202 |
if (YYLIMIT == YYCURSOR) YYFILL(1); |
| 203 |
matchptr = cursor; |
| 204 |
/*!re2c |
| 205 |
NEWLINE { |
| 206 |
if (s->report_comments) |
| 207 |
{ |
| 208 |
cursor = matchptr; // so we RET('\n') next. |
| 209 |
RET(TOKEN_SINGLE_COMMENT); |
| 210 |
} |
| 211 |
s->line++; |
| 212 |
token = matchptr; |
| 213 |
RET('\n'); |
| 214 |
} |
| 215 |
"\000" { |
| 216 |
if (eoi) |
| 217 |
{ |
| 218 |
if (s->report_comments) |
| 219 |
RET(TOKEN_SINGLE_COMMENT); |
| 220 |
else |
| 221 |
RET(TOKEN_EOI); |
| 222 |
} |
| 223 |
goto singlelinecomment; |
| 224 |
} |
| 225 |
ANY { goto singlelinecomment; } |
| 226 |
*/ |
| 227 |
|
| 228 |
ppdirective: |
| 229 |
if (YYLIMIT == YYCURSOR) YYFILL(1); |
| 230 |
/*!re2c |
| 231 |
PP "include" { RET(TOKEN_PP_INCLUDE); } |
| 232 |
PP "line" { RET(TOKEN_PP_LINE); } |
| 233 |
PP "define" { RET(TOKEN_PP_DEFINE); } |
| 234 |
PP "undef" { RET(TOKEN_PP_UNDEF); } |
| 235 |
PP "if" { RET(TOKEN_PP_IF); } |
| 236 |
PP "ifdef" { RET(TOKEN_PP_IFDEF); } |
| 237 |
PP "ifndef" { RET(TOKEN_PP_IFNDEF); } |
| 238 |
PP "else" { RET(TOKEN_PP_ELSE); } |
| 239 |
PP "elif" { RET(TOKEN_PP_ELIF); } |
| 240 |
PP "endif" { RET(TOKEN_PP_ENDIF); } |
| 241 |
PP "error" { RET(TOKEN_PP_ERROR); } |
| 242 |
PP "pragma" { RET(TOKEN_PP_PRAGMA); } |
| 243 |
WHITESPACE { goto ppdirective; } |
| 244 |
|
| 245 |
ANY { |
| 246 |
token = cursor = (const uchar *) s->source; |
| 247 |
limit = cursor + s->bytes_left; |
| 248 |
goto scanner_loop; |
| 249 |
} |
| 250 |
*/ |
| 251 |
|
| 252 |
bad_chars: |
| 253 |
if (YYLIMIT == YYCURSOR) YYFILL(1); |
| 254 |
/*!re2c |
| 255 |
ANYLEGAL { cursor--; RET(TOKEN_BAD_CHARS); } |
| 256 |
"\000" { |
| 257 |
if (eoi) |
| 258 |
{ |
| 259 |
assert( !((token >= sentinel) && |
| 260 |
(token < sentinel+YYMAXFILL)) ); |
| 261 |
eoi = 0; |
| 262 |
cursor = (uchar *) s->source_base + s->orig_length; |
| 263 |
RET(TOKEN_BAD_CHARS); // next call will be EOI. |
| 264 |
} |
| 265 |
goto bad_chars; |
| 266 |
} |
| 267 |
|
| 268 |
ANY { goto bad_chars; } |
| 269 |
*/ |
| 270 |
|
| 271 |
assert(0 && "Shouldn't hit this code"); |
| 272 |
RET(TOKEN_UNKNOWN); |
| 273 |
} // preprocessor_lexer |
| 274 |
|
| 275 |
// end of mojoshader_lexer.re (or .c) ... |
| 276 |
|