/
mojoshader_lexer.re
195 lines (171 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
// This was originally based on examples/pp-c.re from re2c: http://re2c.org/
// re2c is public domain code.
//
// You build mojoshader_lexer_preprocessor.c from the .re file with re2c...
// re2c -is -o mojoshader_lexer_preprocessor.c mojoshader_lexer_preprocessor.re
//
// Changes to the lexer are done to the .re file, not the C code!
//
// Please note that this isn't a perfect C lexer, since it is used for both
// HLSL and shader assembly language, and follows the quirks of Microsoft's
// tools.
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
typedef unsigned char uchar;
27
#define RET(t) do { update_state(s, cursor, token); return t; } while (0)
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define YYCTYPE uchar
#define YYCURSOR cursor
#define YYLIMIT limit
#define YYMARKER s->lexer_marker
#define YYFILL(n) { if ((n) == 1) { RET(TOKEN_EOI); } }
static void update_state(IncludeState *s, const uchar *cur, const uchar *tok)
{
s->bytes_left -= (unsigned int) (cur - ((const uchar *) s->source));
s->source = (const char *) cur;
s->token = (const char *) tok;
} // update_state
Token preprocessor_internal_lexer(IncludeState *s)
{
const uchar *cursor = (const uchar *) s->source;
const uchar *token;
45
const uchar *matchptr;
46
const uchar *limit = cursor + s->bytes_left;
47
int saw_newline = 0;
48
49
50
51
52
53
54
55
scanner_loop:
token = cursor;
if (YYLIMIT == YYCURSOR)
RET(TOKEN_EOI);
/*!re2c
56
ANY = [\000-\377];
57
ANYLEGAL = [a-zA-Z0-9_/'*=+%^&|!#<>()[{}.,~^:;? \t\v\f\r\n\-\]\\];
58
59
60
61
62
63
64
65
66
O = [0-7];
D = [0-9];
L = [a-zA-Z_];
H = [a-fA-F0-9];
E = [Ee] [+-]? D+;
FS = [fFlL];
IS = [uUlL]*;
ESC = [\\] ([abfnrtv?'"\\] | "x" H+ | O+);
PP = "#" [ \t]*;
67
NEWLINE = ("\r\n" | "\r" | "\n");
68
69
70
71
72
73
74
75
76
77
WHITESPACE = [ \t\v\f]+;
*/
/*!re2c
"/*" { goto multilinecomment; }
"//" { goto singlelinecomment; }
L (L|D)* { RET(TOKEN_IDENTIFIER); }
("0" [xX] H+ IS?) | ("0" D+ IS?) | (D+ IS?) |
78
(['] (ESC|ANY\[\r\n\\'])* ['])
79
80
81
82
83
{ RET(TOKEN_INT_LITERAL); }
(D+ E FS?) | (D* "." D+ E? FS?) | (D+ "." D* E? FS?)
{ RET(TOKEN_FLOAT_LITERAL); }
84
(["] (ESC|ANY\[\r\n\\"])* ["])
85
86
{ RET(TOKEN_STRING_LITERAL); }
87
88
89
90
91
92
93
94
95
96
97
98
">>=" { RET(TOKEN_RSHIFTASSIGN); }
"<<=" { RET(TOKEN_LSHIFTASSIGN); }
"+=" { RET(TOKEN_ADDASSIGN); }
"-=" { RET(TOKEN_SUBASSIGN); }
"*=" { RET(TOKEN_MULTASSIGN); }
"/=" { RET(TOKEN_DIVASSIGN); }
"%=" { RET(TOKEN_MODASSIGN); }
"^=" { RET(TOKEN_XORASSIGN); }
"&=" { RET(TOKEN_ANDASSIGN); }
"|=" { RET(TOKEN_ORASSIGN); }
"++" { RET(TOKEN_INCREMENT); }
"--" { RET(TOKEN_DECREMENT); }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
">>" { RET(TOKEN_RSHIFT); }
"<<" { RET(TOKEN_LSHIFT); }
"&&" { RET(TOKEN_ANDAND); }
"||" { RET(TOKEN_OROR); }
"<=" { RET(TOKEN_LEQ); }
">=" { RET(TOKEN_GEQ); }
"==" { RET(TOKEN_EQL); }
"!=" { RET(TOKEN_NEQ); }
"##" { RET(TOKEN_HASHHASH); }
"(" { RET('('); }
")" { RET(')'); }
"[" { RET('['); }
"]" { RET(']'); }
"." { RET('.'); }
"," { RET(','); }
"&" { RET('&'); }
"!" { RET('!'); }
"~" { RET('~'); }
"-" { RET('-'); }
"+" { RET('+'); }
"*" { RET('*'); }
"/" { RET('/'); }
"%" { RET('%'); }
"<" { RET('<'); }
">" { RET('>'); }
"^" { RET('^'); }
"|" { RET('|'); }
":" { RET(':'); }
";" { RET(';'); }
"{" { RET('{'); }
"}" { RET('}'); }
"=" { RET('='); }
"?" { RET('?'); }
"\\" { RET('\\'); }
"#" { RET('#'); }
PP "include" { RET(TOKEN_PP_INCLUDE); }
PP "line" { RET(TOKEN_PP_LINE); }
PP "define" { RET(TOKEN_PP_DEFINE); }
PP "undef" { RET(TOKEN_PP_UNDEF); }
PP "if" { RET(TOKEN_PP_IF); }
PP "ifdef" { RET(TOKEN_PP_IFDEF); }
PP "ifndef" { RET(TOKEN_PP_IFNDEF); }
PP "else" { RET(TOKEN_PP_ELSE); }
PP "elif" { RET(TOKEN_PP_ELIF); }
PP "endif" { RET(TOKEN_PP_ENDIF); }
PP "error" { RET(TOKEN_PP_ERROR); }
WHITESPACE { goto scanner_loop; }
148
NEWLINE { s->line++; RET('\n'); }
149
ANY { goto bad_chars; }
150
151
152
153
*/
multilinecomment:
if (YYLIMIT == YYCURSOR)
154
RET(TOKEN_INCOMPLETE_COMMENT);
155
matchptr = cursor;
156
157
// The "*\/" is just to avoid screwing up text editor syntax highlighting.
/*!re2c
158
159
160
161
162
163
164
"*\/" {
if (saw_newline)
RET('\n');
goto scanner_loop;
}
NEWLINE {
s->line++;
165
token = matchptr;
166
167
168
saw_newline = 1;
goto multilinecomment;
}
169
ANY { goto multilinecomment; }
170
171
172
173
174
*/
singlelinecomment:
if (YYLIMIT == YYCURSOR)
RET(TOKEN_EOI);
175
matchptr = cursor;
176
/*!re2c
177
NEWLINE { s->line++; token = matchptr; RET('\n'); }
178
ANY { goto singlelinecomment; }
179
180
181
182
*/
bad_chars:
if (YYLIMIT == YYCURSOR)
183
RET(TOKEN_BAD_CHARS);
184
185
/*!re2c
186
ANYLEGAL { cursor--; RET(TOKEN_BAD_CHARS); }
187
ANY { goto bad_chars; }
188
189
190
191
192
193
194
*/
assert(0 && "Shouldn't hit this code");
RET(TOKEN_UNKNOWN);
} // preprocessor_internal_lexer
// end of mojoshader_lexer_preprocessor.re (or .c) ...