Skip to content

Latest commit

 

History

History
229 lines (203 loc) · 7.3 KB

mojoshader_lexer.re

File metadata and controls

229 lines (203 loc) · 7.3 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;
Feb 17, 2009
Feb 17, 2009
27
28
/*!max:re2c */
#define RET(t) do { update_state(s, eoi, cursor, token); return t; } while (0)
29
30
31
32
#define YYCTYPE uchar
#define YYCURSOR cursor
#define YYLIMIT limit
#define YYMARKER s->lexer_marker
Feb 17, 2009
Feb 17, 2009
33
#define YYFILL(n) { if ((n) == 1) { cursor = sentinel; limit = cursor + YYMAXFILL; eoi = 1; } }
Feb 17, 2009
Feb 17, 2009
35
36
37
38
static uchar sentinel[YYMAXFILL];
static void update_state(IncludeState *s, int eoi,
const uchar *cur, const uchar *tok)
Feb 17, 2009
Feb 17, 2009
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
if (eoi)
{
s->bytes_left = 0;
s->source = (const char *) s->source_base + s->orig_length;
if ( (tok >= sentinel) && (tok < (sentinel+YYMAXFILL)) )
s->token = s->source;
else
s->token = (const char *) tok;
} // if
else
{
s->bytes_left -= (unsigned int) (cur - ((const uchar *) s->source));
s->source = (const char *) cur;
s->token = (const char *) tok;
} // else
Feb 18, 2009
Feb 18, 2009
55
s->tokenlen = (unsigned int) (s->source - s->token);
56
57
} // update_state
Feb 18, 2009
Feb 18, 2009
58
Token preprocessor_lexer(IncludeState *s)
59
60
61
{
const uchar *cursor = (const uchar *) s->source;
const uchar *token;
Feb 13, 2009
Feb 13, 2009
62
const uchar *matchptr;
63
const uchar *limit = cursor + s->bytes_left;
Feb 17, 2009
Feb 17, 2009
64
int eoi = 0;
Feb 12, 2009
Feb 12, 2009
65
int saw_newline = 0;
66
67
scanner_loop:
Feb 17, 2009
Feb 17, 2009
68
if (YYLIMIT == YYCURSOR) YYFILL(1);
69
70
71
token = cursor;
/*!re2c
Feb 13, 2009
Feb 13, 2009
72
ANY = [\000-\377];
Feb 13, 2009
Feb 13, 2009
73
ANYLEGAL = [a-zA-Z0-9_/'*=+%^&|!#<>()[{}.,~^:;? \t\v\f\r\n\-\]\\];
74
75
76
77
78
79
80
81
82
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]*;
Feb 13, 2009
Feb 13, 2009
83
NEWLINE = ("\r\n" | "\r" | "\n");
84
85
86
87
WHITESPACE = [ \t\v\f]+;
*/
/*!re2c
Feb 15, 2009
Feb 15, 2009
88
89
"\\" [ \t\v\f]* NEWLINE { s->line++; goto scanner_loop; }
90
91
92
93
94
95
"/*" { goto multilinecomment; }
"//" { goto singlelinecomment; }
L (L|D)* { RET(TOKEN_IDENTIFIER); }
("0" [xX] H+ IS?) | ("0" D+ IS?) | (D+ IS?) |
Feb 13, 2009
Feb 13, 2009
96
(['] (ESC|ANY\[\r\n\\'])* ['])
97
98
99
100
101
{ RET(TOKEN_INT_LITERAL); }
(D+ E FS?) | (D* "." D+ E? FS?) | (D+ "." D* E? FS?)
{ RET(TOKEN_FLOAT_LITERAL); }
Feb 13, 2009
Feb 13, 2009
102
(["] (ESC|ANY\[\r\n\\"])* ["])
103
{ RET(TOKEN_STRING_LITERAL); }
Feb 15, 2009
Feb 15, 2009
104
Feb 12, 2009
Feb 12, 2009
105
106
107
108
109
110
111
112
113
114
115
116
">>=" { 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); }
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
148
149
150
">>" { 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('?'); }
Feb 17, 2009
Feb 17, 2009
151
152
"\000" { if (eoi) { RET(TOKEN_EOI); } goto bad_chars; }
153
154
155
156
157
158
159
160
161
162
163
164
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); }
Feb 17, 2009
Feb 17, 2009
165
WHITESPACE { if (s->report_whitespace) RET(' '); goto scanner_loop; }
Feb 12, 2009
Feb 12, 2009
166
NEWLINE { s->line++; RET('\n'); }
Feb 13, 2009
Feb 13, 2009
167
ANY { goto bad_chars; }
168
169
170
*/
multilinecomment:
Feb 17, 2009
Feb 17, 2009
171
if (YYLIMIT == YYCURSOR) YYFILL(1);
Feb 13, 2009
Feb 13, 2009
172
matchptr = cursor;
173
174
// The "*\/" is just to avoid screwing up text editor syntax highlighting.
/*!re2c
Feb 12, 2009
Feb 12, 2009
175
176
177
"*\/" {
if (saw_newline)
RET('\n');
Feb 17, 2009
Feb 17, 2009
178
179
else if (s->report_whitespace)
RET(' ');
Feb 12, 2009
Feb 12, 2009
180
181
182
183
goto scanner_loop;
}
NEWLINE {
s->line++;
Feb 13, 2009
Feb 13, 2009
184
token = matchptr;
Feb 12, 2009
Feb 12, 2009
185
186
187
saw_newline = 1;
goto multilinecomment;
}
Feb 17, 2009
Feb 17, 2009
188
189
190
191
192
"\000" {
if (eoi)
RET(TOKEN_INCOMPLETE_COMMENT);
goto multilinecomment;
}
Feb 13, 2009
Feb 13, 2009
193
ANY { goto multilinecomment; }
194
195
196
*/
singlelinecomment:
Feb 17, 2009
Feb 17, 2009
197
if (YYLIMIT == YYCURSOR) YYFILL(1);
Feb 13, 2009
Feb 13, 2009
198
matchptr = cursor;
199
/*!re2c
Feb 13, 2009
Feb 13, 2009
200
NEWLINE { s->line++; token = matchptr; RET('\n'); }
Feb 17, 2009
Feb 17, 2009
201
"\000" { if (eoi) { RET(TOKEN_EOI); } goto singlelinecomment; }
Feb 13, 2009
Feb 13, 2009
202
ANY { goto singlelinecomment; }
203
204
205
*/
bad_chars:
Feb 17, 2009
Feb 17, 2009
206
if (YYLIMIT == YYCURSOR) YYFILL(1);
207
/*!re2c
Feb 13, 2009
Feb 13, 2009
208
ANYLEGAL { cursor--; RET(TOKEN_BAD_CHARS); }
Feb 17, 2009
Feb 17, 2009
209
210
211
212
213
214
215
216
217
218
219
220
"\000" {
if (eoi)
{
assert( !((token >= sentinel) &&
(token < sentinel+YYMAXFILL)) );
eoi = 0;
cursor = (uchar *) s->source_base + s->orig_length;
RET(TOKEN_BAD_CHARS); // next call will be EOI.
}
goto bad_chars;
}
Feb 13, 2009
Feb 13, 2009
221
ANY { goto bad_chars; }
222
223
224
225
*/
assert(0 && "Shouldn't hit this code");
RET(TOKEN_UNKNOWN);
Feb 18, 2009
Feb 18, 2009
226
} // preprocessor_lexer
227
228
// end of mojoshader_lexer_preprocessor.re (or .c) ...