]> git.saurik.com Git - cycript.git/blob - Cycript.l.in
54724177d4d42759f8723ec1e899fe7d15ba65dd
[cycript.git] / Cycript.l.in
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 /* XXX: supposedly I will be screwed on very very long multi-line comments and need to replace these with a manual lexer. http://websrv.cs.fsu.edu/~engelen/courses/COP5621/Pr2.pdf */
23
24 %{
25 #define YYLTYPE cy::location
26 #include "Cycript.tab.hh"
27 typedef cy::parser::token tk;
28
29 #include "Highlight.hpp"
30
31 #define YY_EXTRA_TYPE CYDriver *
32
33 // do /not/ fold token to the return: this is a macro and the ordering is dependent
34 #define F(value, highlight) do { \
35 int token(value); \
36 yyextra->no_.AtImplementation = false; \
37 yyextra->no_.Function = false; \
38 yyextra->no_.OpenBrace = false; \
39 yylval->highlight_ = highlight; \
40 return token; \
41 } while (false)
42
43 #define A new($pool)
44 #define Y apr_pstrmemdup($pool, yytext, yyleng)
45
46 #define I(type, Type, value, highlight) do { \
47 yylval->type ## _ = A CY ## Type; \
48 F(value, highlight); \
49 } while (false)
50
51 #define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
52 #define C T yyextra->state_ = CYClear;
53 #define R T yyextra->state_ = CYRestricted;
54
55 #define N \
56 if (yyextra->state_ != CYNewLine) { \
57 if (yyextra->state_ != CYRestricted) \
58 yyextra->state_ = CYNewLine; \
59 else { \
60 yyextra->state_ = CYClear; \
61 F(tk::NewLine, hi::Nothing); \
62 } \
63 }
64
65 #define V(more) { \
66 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
67 unsigned lines(0); \
68 size_t left; \
69 do { \
70 ++lines; \
71 left = yyleng - (nl - yytext) - 1; \
72 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
73 } while (nl != NULL); \
74 yylloc->end.lines(lines); \
75 yylloc->end.columns(left); \
76 yylloc->step(); \
77 more \
78 } else L \
79 }
80
81 #define L { \
82 yylloc->step(); \
83 yylloc->columns(yyleng); \
84 }
85
86 int H(char c) {
87 if (c >= '0' && c <= '9')
88 return c - '0';
89 if (c >= 'a' && c <= 'f')
90 return c - 'a' + 10;
91 if (c >= 'A' && c <= 'F')
92 return c - 'A' + 10;
93 return -1;
94 }
95
96 #define YY_INPUT(data, value, size) { \
97 if (yyextra->data_.eof()) \
98 value = YY_NULL; \
99 else { \
100 yyextra->data_.read(data, size); \
101 size_t copy(yyextra->data_.gcount()); \
102 value = copy == 0 ? YY_NULL : copy; \
103 } \
104 }
105
106 %}
107
108 %option prefix="cy"
109 %option bison-bridge
110 %option bison-locations
111 %option noyywrap
112 %option yylineno
113 %option nounput
114 %option interactive
115 %option reentrant
116 %option stack
117
118 Exponent [eE][+-]?[0-9]+
119 Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
120
121 IdentifierStart [a-zA-Z$_]
122 IdentifierPart [a-zA-Z$_0-9]
123
124 NonTerminator [^\n]
125 BackslashSequence \\{NonTerminator}
126 RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
127 RegularExpressionChar [^\n\\/]|{BackslashSequence}
128 RegularExpressionFlags {IdentifierPart}*
129 RegularExpressionChars {RegularExpressionChar}*
130 RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
131
132 @begin E4X
133 XMLNameStart [a-zA-Z_:]
134 XMLNamePart [a-zA-Z0-9.-_:]
135 XMLName {XMLNameStart}{XMLNamePart}*
136 @end
137
138 %s Div
139 %s RegExp
140
141 @begin E4X
142 %x XMLContent
143 %x XMLTag
144 @end
145
146 %%
147
148 <RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} L C I(literal, RegEx(Y), tk::RegularExpressionLiteral, hi::Constant);
149
150 \/\/[^\n]* L
151
152 /* http://ostermiller.org/findcomment.html */
153 /* XXX: unify these two rules using !? */
154 \/\*!([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V() C I(comment, Comment(Y), tk::Comment, hi::Comment);
155 \/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/ V(N)
156
157 @begin E4X
158 <RegExp>"<>" L F(tk::LeftRight, hi::Structure);
159 <XMLContent>"</>" L F(tk::LeftSlashRight, hi::Structure);
160
161 <RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() F(tk::XMLCDATA, hi::Constant);
162 <RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() F(tk::XMLComment, hi::Comment);
163 <RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() F(tk::XMLPI, hi::Meta);
164
165 <XMLTag>"=" L F(tk::Equal, hi::Structure);
166 <XMLTag>">" L F(tk::Right, hi::Structure);
167 <XMLTag>"/>" L F(tk::SlashRight, hi::Structure);
168 <XMLTag>"{" L F(tk::OpenBrace, hi::Structure);
169
170 <XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() F(tk::XMLAttributeValue, hi::Constant);
171 <XMLTag>{XMLName} L F(tk::XMLName, hi::Identifier);
172 <XMLTag>[ \t\r\n] V() F(tk::XMLWhitespace, hi::Nothing);
173
174 <XMLContent>"{" L F(tk::OpenBrace, hi::Structure);
175 <XMLContent>"<" L F(tk::Left, hi::Structure);
176 <XMLContent>"</" L F(tk::LeftSlash, hi::Structure);
177 @end
178
179 "..." L C F(tk::PeriodPeriodPeriod, hi::Meta);
180
181 @begin E4X
182 "::" L C F(tk::ColonColon, hi::Operator);
183 ".." L C F(tk::PeriodPeriod, hi::Operator);
184 @end
185
186 @begin E4X ObjectiveC
187 "@" L C F(tk::At, hi::Operator);
188 @end
189
190 "&" L C F(tk::Ampersand, hi::Operator);
191 "&&" L C F(tk::AmpersandAmpersand, hi::Operator);
192 "&=" L C F(tk::AmpersandEqual, hi::Operator);
193 "^" L C F(tk::Carrot, hi::Operator);
194 "^=" L C F(tk::CarrotEqual, hi::Operator);
195 "=" L C F(tk::Equal, hi::Operator);
196 "==" L C F(tk::EqualEqual, hi::Operator);
197 "===" L C F(tk::EqualEqualEqual, hi::Operator);
198 "=>" L C F(tk::EqualRight, hi::Operator);
199 "!" L C F(tk::Exclamation, hi::Operator);
200 "!=" L C F(tk::ExclamationEqual, hi::Operator);
201 "!==" L C F(tk::ExclamationEqualEqual, hi::Operator);
202 "-" L C F(tk::Hyphen, hi::Operator);
203 "-=" L C F(tk::HyphenEqual, hi::Operator);
204 "--" L C F(yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen, hi::Operator);
205 "->" L C F(tk::HyphenRight, hi::Operator);
206 "<" L C F(tk::Left, hi::Operator);
207 "<=" L C F(tk::LeftEqual, hi::Operator);
208 "<<" L C F(tk::LeftLeft, hi::Operator);
209 "<<=" L C F(tk::LeftLeftEqual, hi::Operator);
210 "%" L C F(tk::Percent, hi::Operator);
211 "%=" L C F(tk::PercentEqual, hi::Operator);
212 "." L C F(tk::Period, hi::Operator);
213 "|" L C F(tk::Pipe, hi::Operator);
214 "|=" L C F(tk::PipeEqual, hi::Operator);
215 "||" L C F(tk::PipePipe, hi::Operator);
216 "+" L C F(tk::Plus, hi::Operator);
217 "+=" L C F(tk::PlusEqual, hi::Operator);
218 "++" L C F(yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus, hi::Operator);
219 ">" L C F(tk::Right, hi::Operator);
220 ">=" L C F(tk::RightEqual, hi::Operator);
221 ">>" L C F(tk::RightRight, hi::Operator);
222 ">>=" L C F(tk::RightRightEqual, hi::Operator);
223 ">>>" L C F(tk::RightRightRight, hi::Operator);
224 ">>>=" L C F(tk::RightRightRightEqual, hi::Operator);
225 "*" L C F(tk::Star, hi::Operator);
226 "*=" L C F(tk::StarEqual, hi::Operator);
227 "~" L C F(tk::Tilde, hi::Operator);
228
229 <Div>"/" L C F(tk::Slash, hi::Operator);
230 <Div>"/=" L C F(tk::SlashEqual, hi::Operator);
231
232 ":" L C F(tk::Colon, hi::Structure);
233 "," L C F(tk::Comma, hi::Structure);
234 "?" L C F(tk::Question, hi::Structure);
235 ";" L C F(tk::SemiColon, hi::Structure);
236
237 "(" L C F(tk::OpenParen, hi::Structure);
238 ")" L C F(tk::CloseParen, hi::Structure);
239
240 "{" L C F(yyextra->no_.OpenBrace ? tk::OpenBrace__ : yylval->newline_ ? tk::OpenBrace_ : tk::OpenBrace, hi::Structure);
241 "}" L C F(tk::CloseBrace, hi::Structure);
242
243 "[" L C F(tk::OpenBracket, hi::Structure);
244 "]" L C F(tk::CloseBracket, hi::Structure);
245
246 @begin Java
247 "@class" L C F(tk::AtClass, hi::Meta);
248 @end
249
250 @begin ObjectiveC
251 "@end" L C F(tk::AtEnd, hi::Meta);
252 "@implementation" L C F(yyextra->no_.AtImplementation ? tk::AtImplementation_ : tk::AtImplementation, hi::Meta);
253 "@import" L C F(tk::AtImport, hi::Meta);
254 "@selector" L C F(tk::AtSelector, hi::Meta);
255 "nil" L C I(identifier, Identifier("nil"), tk::Identifier_, hi::Constant);
256 @end
257
258 "false" L C I(false, False(), tk::False, hi::Constant);
259 "null" L C I(null, Null(), tk::Null, hi::Constant);
260 "true" L C I(true, True(), tk::True, hi::Constant);
261
262 "auto" L C I(word, Word("auto"), tk::Auto, hi::Meta);
263 "break" L R I(word, Word("break"), tk::Break, hi::Control);
264 "case" L C I(word, Word("case"), tk::Case, hi::Control);
265 "catch" L C I(word, Word("catch"), tk::Catch, hi::Control);
266 "continue" L R I(word, Word("continue"), tk::Continue, hi::Control);
267 "default" L C I(word, Word("default"), tk::Default, hi::Control);
268 "delete" L C I(word, Word("delete"), tk::Delete, hi::Operator);
269 "do" L C I(word, Word("do"), tk::Do, hi::Control);
270 "else" L C I(word, Word("else"), tk::Else, hi::Control);
271 "finally" L C I(word, Word("finally"), tk::Finally, hi::Control);
272 "for" L C I(word, Word("for"), tk::For, hi::Control);
273 "function" L C I(word, Word("function"), yyextra->no_.Function ? tk::Function_ : tk::Function, hi::Meta);
274 "if" L C I(word, Word("if"), tk::If, hi::Control);
275 "in" L C I(word, Word("in"), yyextra->in_.top() ? tk::In_ : tk::In, hi::Operator);
276 "instanceof" L C I(word, Word("instanceof"), tk::InstanceOf, hi::Operator);
277 "new" L C I(word, Word("new"), tk::New, hi::Operator);
278 "return" L R I(word, Word("return"), tk::Return, hi::Control);
279 "switch" L C I(word, Word("switch"), tk::Switch, hi::Control);
280 "this" L C I(this, This(), tk::This, hi::Constant);
281 "throw" L R I(word, Word("throw"), tk::Throw, hi::Control);
282 "try" L C I(word, Word("try"), tk::Try, hi::Control);
283 "typeof" L C I(word, Word("typeof"), tk::TypeOf, hi::Operator);
284 "var" L C I(word, Word("var"), tk::Var, hi::Meta);
285 "void" L C I(word, Word("void"), tk::Void, hi::Operator);
286 "while" L C I(word, Word("while"), tk::While, hi::Control);
287 "with" L C I(word, Word("with"), tk::With, hi::Control);
288
289 "debugger" L C I(word, Word("debugger"), tk::Debugger, hi::Meta);
290
291 "const" L C I(word, Word("const"), tk::Const, hi::Meta);
292
293 "class" L C I(word, Word("class"), tk::Class, hi::Meta);
294 "enum" L C I(word, Word("enum"), tk::Enum, hi::Meta);
295 "export" L C I(word, Word("export"), tk::Export, hi::Meta);
296 "extends" L C I(word, Word("extends"), tk::Extends, hi::Meta);
297 "import" L C I(word, Word("import"), tk::Import, hi::Meta);
298 "super" L C I(word, Word("super"), tk::Super, hi::Constant);
299
300 "implements" L C I(identifier, Identifier("implements"), tk::Implements, hi::Meta);
301 "interface" L C I(identifier, Identifier("interface"), tk::Interface, hi::Meta);
302 "package" L C I(identifier, Identifier("package"), tk::Package, hi::Meta);
303 "private" L C I(identifier, Identifier("private"), tk::Private, hi::Meta);
304 "protected" L C I(identifier, Identifier("protected"), tk::Protected, hi::Meta);
305 "public" L C I(identifier, Identifier("public"), tk::Public, hi::Meta);
306 "static" L C I(identifier, Identifier("static"), tk::Static, hi::Meta);
307
308 "abstract" L C I(identifier, Identifier("abstract"), tk::Abstract, hi::Meta);
309 "boolean" L C I(identifier, Identifier("boolean"), tk::Boolean, hi::Type);
310 "byte" L C I(identifier, Identifier("byte"), tk::Byte, hi::Type);
311 "char" L C I(identifier, Identifier("char"), tk::Char, hi::Type);
312 "double" L C I(identifier, Identifier("double"), tk::Double, hi::Type);
313 "final" L C I(identifier, Identifier("final"), tk::Final, hi::Meta);
314 "float" L C I(identifier, Identifier("float"), tk::Float, hi::Type);
315 "goto" L C I(identifier, Identifier("goto"), tk::Goto, hi::Control);
316 "int" L C I(identifier, Identifier("int"), tk::Int, hi::Type);
317 "long" L C I(identifier, Identifier("long"), tk::Long, hi::Type);
318 "native" L C I(identifier, Identifier("native"), tk::Native, hi::Meta);
319 "short" L C I(identifier, Identifier("short"), tk::Short, hi::Type);
320 "synchronized" L C I(identifier, Identifier("synchronized"), tk::Synchronized, hi::Meta);
321 "throws" L C I(identifier, Identifier("throws"), tk::Throws, hi::Meta);
322 "transient" L C I(identifier, Identifier("transient"), tk::Transient, hi::Meta);
323 "volatile" L C I(identifier, Identifier("volatile"), tk::Volatile, hi::Meta);
324
325 "let" L C I(identifier, Identifier("let"), tk::Let, hi::Meta);
326 "yield" L C I(identifier, Identifier("yield"), tk::Yield, hi::Control);
327
328 "each" L C I(identifier, Identifier("each"), tk::Each, hi::Control);
329 "of" L C I(identifier, Identifier("of"), tk::Of, hi::Operator);
330
331 @begin E4X
332 "namespace" L C I(identifier, Identifier("namespace"), tk::Namespace, hi::Meta);
333 "xml" L C I(identifier, Identifier("xml"), tk::XML, hi::Meta);
334 @end
335
336 {IdentifierStart}{IdentifierPart}* L C I(identifier, Identifier(Y), tk::Identifier_, hi::Identifier);
337
338 (\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C I(number, Number(strtod(yytext, NULL)), tk::NumericLiteral, hi::Constant);
339
340 0[xX][0-9a-fA-F]+ L C I(number, Number(strtoull(yytext + 2, NULL, 16)), tk::NumericLiteral, hi::Constant);
341 0[0-7]+ L C I(number, Number(strtoull(yytext + 1, NULL, 8)), tk::NumericLiteral, hi::Constant);
342 0[bB][0-1]+ L C I(number, Number(strtoull(yytext + 2, NULL, 2)), tk::NumericLiteral, hi::Constant);
343
344 \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
345 char *value(A char[yyleng]);
346 char *local(value);
347
348 for (yy_size_t i(1), e(yyleng - 1); i != e; ++i) {
349 char next(yytext[i]);
350
351 if (yytext[i] == '\\')
352 switch (next = yytext[++i]) {
353 case '\n': continue;
354 case '\\': next = '\\'; break;
355 case '\'': next = '\''; break;
356 case '"': next = '"'; break;
357 case 'b': next = '\b'; break;
358 case 'f': next = '\f'; break;
359 case 'n': next = '\n'; break;
360 case 'r': next = '\r'; break;
361 case 't': next = '\t'; break;
362 case 'v': next = '\v'; break;
363 case '0': next = '\0'; break;
364
365 case 'x':
366 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
367 i += 2;
368 break;
369 }
370
371 *local++ = next;
372 }
373
374 *local = '\0';
375 I(string, String(value, local - value), tk::StringLiteral, hi::Constant);
376 }
377
378 \r?\n|\r|\xe2\x80[\xa8\xa9] yylloc->end.lines(); yylloc->step(); N
379
380 [ \t] L
381
382 <<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; F(tk::AutoComplete, hi::Nothing); } L yyterminate();
383
384 . L {
385 CYDriver::Error error;
386 error.location_ = *yylloc;
387 error.message_ = "syntax error, unknown token";
388 yyextra->errors_.push_back(error);
389 yyterminate();
390 }
391
392 %%
393
394 void CYDriver::ScannerInit() {
395 cylex_init(&scanner_);
396 cyset_extra(this, scanner_);
397 }
398
399 void CYDriver::ScannerDestroy() {
400 cylex_destroy(scanner_);
401 }
402
403 CYDriver::Condition CYDriver::GetCondition() {
404 switch (yy_top_state(scanner_)) {
405 case RegExp:
406 return RegExpCondition;
407 @begin E4X
408 case XMLContent:
409 return XMLContentCondition;
410 case XMLTag:
411 return XMLTagCondition;
412 @end
413 default:
414 _assert(false);
415 }
416 }
417
418 void CYDriver::SetCondition(Condition condition) {
419 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
420
421 switch (condition) {
422 case RegExpCondition:
423 BEGIN(RegExp);
424 break;
425 @begin E4X
426 case XMLContentCondition:
427 BEGIN(XMLContent);
428 break;
429 case XMLTagCondition:
430 BEGIN(XMLTag);
431 break;
432 @end
433 default:
434 _assert(false);
435 }
436 }
437
438 void CYDriver::PushCondition(Condition condition) {
439 switch (condition) {
440 case RegExpCondition:
441 yy_push_state(RegExp, scanner_);
442 break;
443 @begin E4X
444 case XMLContentCondition:
445 yy_push_state(XMLContent, scanner_);
446 break;
447 case XMLTagCondition:
448 yy_push_state(XMLTag, scanner_);
449 break;
450 @end
451 default:
452 _assert(false);
453 }
454 }
455
456 void CYDriver::PopCondition() {
457 yy_pop_state(scanner_);
458 }