]> git.saurik.com Git - cycript.git/blame - Scanner.lpp.in
We aren't allowed to treat Infinity as keyword ;P.
[cycript.git] / Scanner.lpp.in
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
d15b59f5
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
d15b59f5 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b3378a02 19**/
d15b59f5
JF
20/* }}} */
21
7c4c728d 22%top{
1771224f
JF
23#if defined(__clang__)
24#pragma clang diagnostic push
1771224f 25#pragma clang diagnostic ignored "-Wdeprecated-register"
b900e1a4
JF
26#pragma clang diagnostic ignored "-Wunused-function"
27#pragma clang diagnostic ignored "-Wunused-variable"
7c4c728d
JF
28#else
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wsign-compare"
31#pragma GCC diagnostic ignored "-Wunused-function"
32#pragma GCC diagnostic ignored "-Wunused-variable"
1771224f 33#endif
7c4c728d
JF
34}
35
36%{
1771224f 37
58afc6aa 38#define YYLTYPE CYLocation
20052ff7 39#include "Parser.hpp"
63b4c5a8 40typedef cy::parser::token tk;
693d501b 41
82a02ede
JF
42#include "Highlight.hpp"
43
cbd87cbe
JF
44#include "IdentifierStart.h"
45#include "IdentifierContinue.h"
46
5999c315 47#define YY_EXTRA_TYPE CYDriver *
db5e2840 48
82a02ede 49#define F(value, highlight) do { \
442609f7
JF
50 yyextra->newline_ = yyextra->last_; \
51 yyextra->last_ = false; \
52 yyextra->next_ = false; \
53 BEGIN(yyextra->template_.top() ? DivOrTemplateTail : Div); \
82a02ede 54 yylval->highlight_ = highlight; \
442609f7 55 return value; \
3ea7eed0
JF
56} while (false)
57
79eea88e
JF
58#define S(stack) do { \
59 if (yyextra->stack.size() == 1) \
60 E("invalid brace") \
61 yyextra->stack.pop(); \
62} while (false)
63
2c1d569a
JF
64#define P yyextra->pool_
65#define A new(P)
66#define Y P.strmemdup(yytext, yyleng)
2eb8215d 67
82a02ede 68#define I(type, Type, value, highlight) do { \
a5662a53 69 yylval->semantic_.type ## _ = A CY ## Type; \
7085e1ab 70 yylval->semantic_.type ## _->location_ = *yylloc; \
82a02ede 71 F(value, highlight); \
2eb8215d
JF
72} while (false)
73
5befe15e 74#define N \
442609f7 75 if (yyextra->last_ && yyextra->next_) { \
b900e1a4
JF
76 yyextra->last_ = false; \
77 F(tk::NewLine, hi::Nothing); \
78 }
5befe15e 79
691e4717 80#define V(more) { \
cb02f8ae
JF
81 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
82 unsigned lines(0); \
83 size_t left; \
84 do { \
85 ++lines; \
86 left = yyleng - (nl - yytext) - 1; \
87 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
88 } while (nl != NULL); \
f89aaa11 89 yylloc->step(); \
51b2dc6b
JF
90 yylloc->end.Lines(lines); \
91 yylloc->end.Columns(left); \
691e4717 92 more \
cb02f8ae
JF
93 } else L \
94}
95
51b2dc6b 96#define R yylloc->end.Columns(yyleng);
b900e1a4 97#define L yylloc->step(); R
e7ed5354 98
b900e1a4
JF
99#define H(value, highlight) do { \
100 if (yyextra->highlight_) \
101 F(value, highlight); \
102} while (false)
103
104#define M \
105 H(tk::Comment, hi::Comment);
abc79d6e 106
e31ea496
JF
107#define E(message) { \
108 CYDriver::Error error; \
109 error.location_ = *yylloc; \
110 error.message_ = "syntax error, " message; \
111 yyextra->errors_.push_back(error); \
112 yyterminate(); \
113}
114
b900e1a4 115int X(char c) {
931b816a
JF
116 if (c >= '0' && c <= '9')
117 return c - '0';
118 if (c >= 'a' && c <= 'f')
119 return c - 'a' + 10;
120 if (c >= 'A' && c <= 'F')
121 return c - 'A' + 10;
122 return -1;
123}
124
b900e1a4
JF
125template <typename Type_>
126static void U(Type_ &local, unsigned point) {
ee6c04ef
JF
127 if (false) {
128 } else if (point < 0x000080) {
129 *local++ = point;
130 } else if (point < 0x000800) {
131 *local++ = 0xc0 | point >> 0x06 & 0x1f;
132 goto one;
133 } else if (point < 0x010000) {
134 *local++ = 0xe0 | point >> 0x0c & 0x0f;
135 goto two;
136 } else if (point < 0x110000) {
137 *local++ = 0xf0 | point >> 0x12 & 0x07;
138 *local++ = 0x80 | point >> 0x0c & 0x3f;
139 two:
140 *local++ = 0x80 | point >> 0x06 & 0x3f;
141 one:
142 *local++ = 0x80 | point >> 0x00 & 0x3f;
143 } else _assert(false);
144}
145
cbd87cbe 146static unsigned U(char *&local, const char *text, yy_size_t &i) {
ee6c04ef
JF
147 unsigned point;
148
149 char next(text[++i]);
150 if (next != '{') {
b900e1a4 151 point = X(text[i + 0]) << 12 | X(text[i + 1]) << 8 | X(text[i + 2]) << 4 | X(text[i + 3]);
ee6c04ef
JF
152 i += 3;
153 } else {
154 point = 0;
155 for (;;) {
156 next = text[++i];
157 if (next == '}')
158 break;
b900e1a4 159 point = (point << 4) | X(next);
ee6c04ef
JF
160 }
161 }
162
163 U(local, point);
cbd87cbe 164 return point;
ee6c04ef
JF
165}
166
b900e1a4
JF
167#define CYLexBufferPoint(point) do { \
168 std::back_insert_iterator<std::vector<char> > inserter(yyextra->buffer_); \
169 U(inserter, point); \
170} while (false)
171
172#define CYLexBufferUnit(value) do { \
173 yyextra->buffer_.push_back(value); \
174} while (false)
175
176#define CYLexBufferUnits(data, size) do { \
177 yyextra->buffer_.insert(yyextra->buffer_.end(), data, data + size); \
178} while (false)
179
180#define CYLexBufferStart(condition) do { \
181 yyextra->buffer_.clear(); \
182 yy_push_state(condition, yyscanner); \
183} while (false)
184
185#define CYLexBufferEnd(type, Type, value, highlight) do { \
186 yy_pop_state(yyscanner); \
442609f7 187 I(type, Type(P.strmemdup(yyextra->buffer_.data(), yyextra->buffer_.size()), yyextra->buffer_.size()), value, highlight); \
b900e1a4
JF
188} while (false)
189
e7ed5354 190#define YY_INPUT(data, value, size) { \
d3b63265 191 if (yyextra->data_.eof()) \
e7ed5354
JF
192 value = YY_NULL; \
193 else { \
d3b63265
JF
194 yyextra->data_.read(data, size); \
195 size_t copy(yyextra->data_.gcount()); \
196 value = copy == 0 ? YY_NULL : copy; \
e7ed5354
JF
197 } \
198}
199
e5332278
JF
200%}
201
202%option prefix="cy"
203%option bison-bridge
204%option bison-locations
af340def 205%option nodefault
e5332278 206%option noyywrap
7b869615 207%option noyylineno
e5332278 208%option nounput
af340def
JF
209%option nounistd
210%option 8bit
211%option backup
6c962f8b
JF
212%option batch
213%option never-interactive
af340def 214%option pointer
924f67b2 215%option reentrant
691e4717 216%option stack
e5332278 217
ee6c04ef
JF
218U1 [\x00-\x7f]
219U0 [\x80-\xbf]
220U2 [\xc2-\xdf]
221U3 [\xe0-\xef]
222U4 [\xf0-\xf4]
b900e1a4 223UN [\xc0-\xc1\xf5-\xff]
1cff1a90 224UE {U1}|{U2}|{U3}|{U4}|{UN}
e5332278 225
ee6c04ef
JF
226HexDigit [0-9a-fA-F]
227LineTerminatorSequence \r?\n|\r|\xe2\x80[\xa8\xa9]
228WhiteSpace [\x09\x0b\x0c\x20]|\xc2\xa0|\xef\xbb\xbf
229UnicodeEscape \\u({HexDigit}{4}|\{{HexDigit}+\})
230
b900e1a4 231@include NotLineTerminator.l
c8a2a786
JF
232NoneTerminatorCharacter [^\r\n\x80-\xff]|{NotLineTerminator}
233RegExCharacter [^/[\\]{-}[\r\n\x80-\xff]|{NotLineTerminator}
234RegClsCharacter [^]\\]{-}[\r\n\x80-\xff]|{NotLineTerminator}
b900e1a4
JF
235CommentCharacter [^*/]{-}[\r\n\x80-\xff]|{NotLineTerminator}
236SingleCharacter [^'\\]{-}[\r\n\x80-\xff]|{NotLineTerminator}
237DoubleCharacter [^"\\]{-}[\r\n\x80-\xff]|{NotLineTerminator}
238PlateCharacter [^$`\\]{-}[\r\n\x80-\xff]|{NotLineTerminator}
ee6c04ef
JF
239
240@include UnicodeIDStart.l
241@include UnicodeIDContinue.l
ee6c04ef
JF
242IdentifierMore [$_]
243
244UnicodeStart {IdentifierMore}|{UnicodeIDStart}
245UnicodePart {IdentifierMore}|\xe2\x80[\x8c\x8d]|{UnicodeIDContinue}
1cff1a90
JF
246UnicodeScrap {U2}|{U3}{U0}{0,1}|{U4}{U0}{0,2}|{UN}|{U0}
247UnicodeError ({U2}|{U3}{U0}{0,1}|{U4}{U0}{0,2}){UE}|{UN}|{U0}
ee6c04ef
JF
248
249IdentifierStart {UnicodeStart}|{UnicodeEscape}
250IdentifierPart {UnicodePart}|{UnicodeEscape}
1cff1a90 251IdentifierFail {UnicodeError}|\\(u({HexDigit}{0,3}|\{{HexDigit}*))?
ee6c04ef 252IdentifierScrap {IdentifierPart}*{IdentifierFail}?
63cd45c9 253
c8a2a786
JF
254RegularExpressionBackslashSequence \\{NoneTerminatorCharacter}
255RegularExpressionClassChars ({RegClsCharacter}|{RegularExpressionBackslashSequence})*
63cd45c9 256
691e4717
JF
257@begin E4X
258XMLNameStart [a-zA-Z_:]
259XMLNamePart [a-zA-Z0-9.-_:]
260XMLName {XMLNameStart}{XMLNamePart}*
261@end
262
c8a2a786 263%x RegularExpression
b900e1a4
JF
264%x MultiLine
265
266%x LegacySingleString
267%x LegacyDoubleString
268
269%x StrictSingleString
270%x StrictDoubleString
271%x StrictAccentString
272
697d6fd2 273%s Div
b900e1a4 274%s DivOrTemplateTail
63cd45c9 275
691e4717
JF
276@begin E4X
277%x XMLContent
278%x XMLTag
279@end
280
e5332278
JF
281%%
282
aca28f96 283 /* RegEx {{{ */
c8a2a786
JF
284<RegularExpression>{
285 \/{UnicodePart}* R CYLexBufferUnits(yytext, yyleng); CYLexBufferEnd(literal, RegEx, tk::RegularExpressionLiteral, hi::Constant);
1cff1a90 286 \/{UnicodePart}*{UnicodeError} R E("invalid character");
c8a2a786
JF
287
288 {RegExCharacter}+ R CYLexBufferUnits(yytext, yyleng);
c8a2a786
JF
289
290 {RegularExpressionBackslashSequence} R CYLexBufferUnits(yytext, yyleng);
1cff1a90
JF
291 \\ R E("invalid escape")
292
293 (\\|{RegExCharacter}+)?{LineTerminatorSequence} R E("invalid newline");
294 (\\|{RegExCharacter}+)?{UnicodeScrap} R E("invalid character");
c8a2a786
JF
295
296 "["{RegularExpressionClassChars}"]" R CYLexBufferUnits(yytext, yyleng);
297 "["{RegularExpressionClassChars}\\? R E("invalid class");
1cff1a90 298
c8a2a786 299 "["{RegularExpressionClassChars}\\?{LineTerminatorSequence} R E("invalid newline");
1cff1a90 300 "["{RegularExpressionClassChars}\\?{UnicodeScrap} R E("invalid character");
c8a2a786 301
c8a2a786 302 <<EOF>> R E("unterminated regex")
b900e1a4 303}
aca28f96
JF
304 /* }}} */
305 /* Comment {{{ */
032131d5
JF
306 /* XXX: maybe fold LineTerminatorSequence into these definitions */
307#!{NoneTerminatorCharacter}* L M
308\/\/{NoneTerminatorCharacter}* L M
309(#!|\/\/){NoneTerminatorCharacter}*{UnicodeError} L E("invalid character");
fe123f47 310
b900e1a4
JF
311\/\* L yy_push_state(MultiLine, yyscanner);
312
313<MultiLine>{
314 \**\*\/ R yy_pop_state(yyscanner); M N
51b2dc6b 315 \**{LineTerminatorSequence} yylloc->end.Lines(); yyextra->last_ = true;
b900e1a4 316 \**{CommentCharacter}|\/ R
1cff1a90
JF
317
318 \**{UnicodeScrap} R E("invalid character");
319 \**\* R E("invalid comment");
320
b900e1a4
JF
321 <<EOF>> R E("invalid comment")
322}
aca28f96
JF
323 /* }}} */
324 /* Element {{{ */
cb02f8ae 325@begin E4X
82a02ede
JF
326<RegExp>"<>" L F(tk::LeftRight, hi::Structure);
327<XMLContent>"</>" L F(tk::LeftSlashRight, hi::Structure);
691e4717 328
82a02ede
JF
329<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() F(tk::XMLCDATA, hi::Constant);
330<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() F(tk::XMLComment, hi::Comment);
331<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() F(tk::XMLPI, hi::Meta);
691e4717 332
82a02ede
JF
333<XMLTag>"=" L F(tk::Equal, hi::Structure);
334<XMLTag>">" L F(tk::Right, hi::Structure);
335<XMLTag>"/>" L F(tk::SlashRight, hi::Structure);
336<XMLTag>"{" L F(tk::OpenBrace, hi::Structure);
691e4717 337
82a02ede
JF
338<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() F(tk::XMLAttributeValue, hi::Constant);
339<XMLTag>{XMLName} L F(tk::XMLName, hi::Identifier);
340<XMLTag>[ \t\r\n] V() F(tk::XMLWhitespace, hi::Nothing);
db5e2840 341
82a02ede
JF
342<XMLContent>"{" L F(tk::OpenBrace, hi::Structure);
343<XMLContent>"<" L F(tk::Left, hi::Structure);
344<XMLContent>"</" L F(tk::LeftSlash, hi::Structure);
691e4717 345@end
aca28f96
JF
346 /* }}} */
347 /* Operator {{{ */
442609f7 348"..." L F(tk::PeriodPeriodPeriod, hi::Meta);
e31ea496 349".." L E("invalid operator")
c8a0500b 350
691e4717 351@begin E4X
442609f7
JF
352"::" L F(tk::ColonColon, hi::Operator);
353".." L F(tk::PeriodPeriod, hi::Operator);
cb02f8ae 354@end
ac9a5ce1 355
313708a9 356@begin E4X ObjectiveC
442609f7
JF
357"@" L F(tk::At, hi::Operator);
358"#" L F(tk::Pound, hi::Operator);
313708a9
JF
359@end
360
442609f7
JF
361"&" L F(tk::Ampersand, hi::Operator);
362"&&" L F(tk::AmpersandAmpersand, hi::Operator);
363"&=" L F(tk::AmpersandEqual, hi::Operator);
364"^" L F(tk::Carrot, hi::Operator);
365"^=" L F(tk::CarrotEqual, hi::Operator);
366"=" L F(tk::Equal, hi::Operator);
367"==" L F(tk::EqualEqual, hi::Operator);
368"===" L F(tk::EqualEqualEqual, hi::Operator);
369"=>" L F(yyextra->newline_ ? tk::EqualRight_ : tk::EqualRight, hi::Operator);
370"!" L F(tk::Exclamation, hi::Operator);
371"!=" L F(tk::ExclamationEqual, hi::Operator);
372"!==" L F(tk::ExclamationEqualEqual, hi::Operator);
373"-" L F(tk::Hyphen, hi::Operator);
374"-=" L F(tk::HyphenEqual, hi::Operator);
375"--" L F(yyextra->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen, hi::Operator);
376"->" L F(tk::HyphenRight, hi::Operator);
377"<" L F(tk::Left, hi::Operator);
378"<=" L F(tk::LeftEqual, hi::Operator);
379"<<" L F(tk::LeftLeft, hi::Operator);
380"<<=" L F(tk::LeftLeftEqual, hi::Operator);
381"%" L F(tk::Percent, hi::Operator);
382"%=" L F(tk::PercentEqual, hi::Operator);
383"." L F(tk::Period, hi::Operator);
384"|" L F(tk::Pipe, hi::Operator);
385"|=" L F(tk::PipeEqual, hi::Operator);
386"||" L F(tk::PipePipe, hi::Operator);
387"+" L F(tk::Plus, hi::Operator);
388"+=" L F(tk::PlusEqual, hi::Operator);
389"++" L F(yyextra->newline_ ? tk::PlusPlus_ : tk::PlusPlus, hi::Operator);
390">" L F(tk::Right, hi::Operator);
391">=" L F(tk::RightEqual, hi::Operator);
392">>" L F(tk::RightRight, hi::Operator);
393">>=" L F(tk::RightRightEqual, hi::Operator);
394">>>" L F(tk::RightRightRight, hi::Operator);
395">>>=" L F(tk::RightRightRightEqual, hi::Operator);
396"*" L F(tk::Star, hi::Operator);
397"*=" L F(tk::StarEqual, hi::Operator);
398"~" L F(tk::Tilde, hi::Operator);
399
98711170
JF
400"/" L F(tk::Slash, hi::Operator);
401"/=" L F(tk::SlashEqual, hi::Operator);
442609f7
JF
402
403":" L F(tk::Colon, hi::Structure);
404"," L F(tk::Comma, hi::Structure);
405"?" L F(tk::Question, hi::Structure);
406";" L F(tk::SemiColon, hi::Structure);
407
408"(" L F(tk::OpenParen, hi::Structure);
409")" L F(tk::CloseParen, hi::Structure);
410
411"{" L yyextra->template_.push(false); F(yyextra->newline_ ? tk::OpenBrace_ : tk::OpenBrace, hi::Structure);
98711170 412<Div>"}" L S(template_); F(tk::CloseBrace, hi::Structure);
442609f7
JF
413
414"[" L F(tk::OpenBracket, hi::Structure);
415"]" L F(tk::CloseBracket, hi::Structure);
aca28f96
JF
416 /* }}} */
417 /* Keyword {{{ */
442609f7 418"@error" L F(tk::At_error_, hi::Error);
dc5d7cf4 419
1ba6903e 420@begin Java
442609f7 421"@class" L F(tk::At_class_, hi::Meta);
1ba6903e
JF
422@end
423
8a2eb1be 424@begin C
442609f7 425"@encode" L F(tk::At_encode_, hi::Meta);
8a2eb1be
JF
426@end
427
428@begin ObjectiveC
442609f7
JF
429"@end" L F(tk::At_end_, hi::Meta);
430"@false" L F(tk::At_false_, hi::Constant);
431"@implementation" L F(tk::At_implementation_, hi::Meta);
432"@import" L F(tk::At_import_, hi::Special);
433"@NO" L F(tk::At_NO_, hi::Constant);
434"@null" L F(tk::At_null_, hi::Constant);
435"@selector" L F(tk::At_selector_, hi::Meta);
436"@true" L F(tk::At_true_, hi::Constant);
437"@YES" L F(tk::At_YES_, hi::Constant);
aca28f96 438@end
4ea461c0 439
1cff1a90 440@({UnicodeStart}{UnicodePart}*{UnicodeError}?|{UnicodeError}) L E("invalid keyword")
aca28f96
JF
441 /* }}} */
442 /* Highlight {{{ */
442609f7 443"undefined" L F(tk::_undefined_, hi::Operator);
8b820c00 444
aca28f96 445@begin ObjectiveC
442609f7
JF
446"bool" L F(tk::_bool_, hi::Type);
447"BOOL" L F(tk::_BOOL_, hi::Type);
448"id" L F(tk::_id_, hi::Type);
449"nil" L F(tk::_nil_, hi::Constant);
450"NULL" L F(tk::_NULL_, hi::Constant);
451"SEL" L F(tk::_SEL_, hi::Type);
cb02f8ae 452@end
aca28f96
JF
453 /* }}} */
454 /* Reserved {{{ */
442609f7
JF
455"abstract" L /*FII*/ F(tk::_abstract_, hi::Meta);
456"await" L /*II?*/ F(tk::_await_, hi::Meta);
457"boolean" L /*FII*/ F(tk::_boolean_, hi::Type);
458"break" L /*KKK*/ F(tk::_break_, hi::Control);
459"byte" L /*FII*/ F(tk::_byte_, hi::Type);
460"case" L /*KKK*/ F(tk::_case_, hi::Control);
461"catch" L /*KKK*/ F(tk::_catch_, hi::Control);
462"char" L /*FII*/ F(tk::_char_, hi::Type);
463"class" L /*FFK*/ F(tk::_class_, hi::Meta);
464"const" L /*FFK*/ F(tk::_const_, hi::Meta);
c5b15840 465"constructor" L /*III*/ F(tk::_constructor_, hi::Special);
442609f7
JF
466"continue" L /*KKK*/ F(tk::_continue_, hi::Control);
467"debugger" L /*FKK*/ F(tk::_debugger_, hi::Meta);
468"default" L /*KKK*/ F(tk::_default_, hi::Control);
469"delete" L /*KKK*/ F(tk::_delete_, hi::Operator);
470"do" L /*KKK*/ F(tk::_do_, hi::Control);
471"double" L /*FII*/ F(tk::_double_, hi::Type);
472"else" L /*KKK*/ F(tk::_else_, hi::Control);
473"enum" L /*FFF*/ F(tk::_enum_, hi::Meta);
474"export" L /*FFK*/ F(tk::_export_, hi::Meta);
475"extends" L /*FFK*/ F(tk::_extends_, hi::Meta);
7085e1ab 476"eval" L /*III*/ F(tk::_eval_, hi::Special);
442609f7
JF
477"false" L /*LLL*/ F(tk::_false_, hi::Constant);
478"final" L /*FII*/ F(tk::_final_, hi::Meta);
479"finally" L /*KKK*/ F(tk::_finally_, hi::Control);
480"float" L /*FII*/ F(tk::_float_, hi::Type);
481"for" L /*KKK*/ F(tk::_for_, hi::Control);
9d2b125d 482"from" L /*III*/ F(tk::_from_, hi::Meta);
442609f7
JF
483"function" L /*KKK*/ F(tk::_function_, hi::Meta);
484"goto" L /*FII*/ F(tk::_goto_, hi::Control);
9d2b125d 485"get" L /*III*/ F(tk::_get_, hi::Meta);
442609f7
JF
486"if" L /*KKK*/ F(tk::_if_, hi::Control);
487"implements" L /*FSS*/ F(tk::_implements_, hi::Meta);
488"import" L /*FFK*/ F(tk::_import_, hi::Meta);
489"in" L /*KKK*/ F(yyextra->in_.top() ? tk::_in__ : tk::_in_, hi::Operator);
5f6902c2 490"Infinity" L /*III*/ F(tk::_Infinity_, hi::Constant);
442609f7
JF
491"instanceof" L /*KKK*/ F(tk::_instanceof_, hi::Operator);
492"int" L /*FII*/ F(tk::_int_, hi::Type);
493"interface" L /*FSS*/ F(tk::_interface_, hi::Meta);
494"let" L /*IS?*/ F(tk::_let_, hi::Meta);
495"long" L /*FII*/ F(tk::_long_, hi::Type);
496"native" L /*FII*/ F(tk::_native_, hi::Meta);
497"new" L /*KKK*/ F(tk::_new_, hi::Operator);
498"null" L /*LLL*/ F(tk::_null_, hi::Constant);
499"package" L /*FSS*/ F(tk::_package_, hi::Meta);
500"private" L /*FSS*/ F(tk::_private_, hi::Meta);
501"protected" L /*FSS*/ F(tk::_protected_, hi::Meta);
c5b15840 502"prototype" L /*III*/ F(tk::_prototype_, hi::Special);
442609f7 503"public" L /*FSS*/ F(tk::_public_, hi::Meta);
9d2b125d
JF
504"return" L /*KKK*/ F(yyextra->return_.top() ? tk::_return__ : tk::_return_, hi::Control);
505"set" L /*III*/ F(tk::_set_, hi::Meta);
442609f7
JF
506"short" L /*FII*/ F(tk::_short_, hi::Type);
507"static" L /*FS?*/ F(tk::_static_, hi::Meta);
d3941d9f 508"super" L /*FFK*/ F(tk::_super_, hi::Constant);
442609f7
JF
509"switch" L /*KKK*/ F(tk::_switch_, hi::Control);
510"synchronized" L /*FII*/ F(tk::_synchronized_, hi::Meta);
322286dd 511"target" L /*III*/ F(tk::_target_, hi::Identifier);
442609f7
JF
512"this" L /*KKK*/ F(tk::_this_, hi::Constant);
513"throw" L /*KKK*/ F(tk::_throw_, hi::Control);
514"throws" L /*FII*/ F(tk::_throws_, hi::Meta);
515"transient" L /*FII*/ F(tk::_transient_, hi::Meta);
516"true" L /*LLL*/ F(tk::_true_, hi::Constant);
517"try" L /*KKK*/ F(tk::_try_, hi::Control);
518"typeof" L /*KKK*/ F(tk::_typeof_, hi::Operator);
519"var" L /*KKK*/ F(tk::_var_, hi::Meta);
520"void" L /*KKK*/ F(tk::_void_, hi::Operator);
521"volatile" L /*FII*/ F(tk::_volatile_, hi::Meta);
522"while" L /*KKK*/ F(tk::_while_, hi::Control);
523"with" L /*KKK*/ F(tk::_with_, hi::Control);
9d2b125d 524"yield" L /*IS?*/ F(yyextra->yield_.top() ? tk::_yield__ : tk::_yield_, hi::Control);
442609f7
JF
525
526"auto" L F(tk::_auto_, hi::Meta);
527"each" L F(tk::_each_, hi::Control);
528"of" L F(tk::_of_, hi::Operator);
d35a3b07 529
aca28f96 530@begin C
442609f7
JF
531"extern" L F(tk::_extern_, hi::Type);
532"signed" L F(tk::_signed_, hi::Type);
533"typedef" L F(tk::_typedef_, hi::Meta);
534"unsigned" L F(tk::_unsigned_, hi::Type);
aca28f96 535@end
5d646fb5 536
aca28f96 537@begin ObjectiveC
442609f7
JF
538"NO" L F(tk::_NO_, hi::Constant);
539"YES" L F(tk::_YES_, hi::Constant);
691e4717
JF
540@end
541
aca28f96 542@begin E4X
442609f7
JF
543"namespace" L F(tk::_namespace_, hi::Meta);
544"xml" L F(tk::_xml_, hi::Meta);
aca28f96
JF
545@end
546 /* }}} */
547 /* Identifier {{{ */
442609f7 548{UnicodeStart}{UnicodePart}* L I(identifier, Identifier(Y), tk::Identifier_, hi::Identifier);
ee6c04ef 549
442609f7 550{IdentifierStart}{IdentifierPart}* L {
ee6c04ef
JF
551 char *value(A char[yyleng + 1]);
552 char *local(value);
553
554 for (yy_size_t i(0), e(yyleng); i != e; ++i) {
555 char next(yytext[i]);
556 if (next != '\\')
557 *local++ = next;
cbd87cbe
JF
558 else {
559 bool (*is)(unsigned) = (i == 0 ? &IsIdentifierStart : &IsIdentifierContinue);
560 unsigned point(U(local, yytext, ++i));
561 if (!is(point))
562 E("invalid character");
563 }
ee6c04ef
JF
564 }
565
566 *local = '\0';
567 I(identifier, Identifier(value), tk::Identifier_, hi::Identifier);
568}
569
570({IdentifierStart}{IdentifierPart}*)?{IdentifierFail} L E("invalid identifier")
aca28f96
JF
571 /* }}} */
572 /* Number {{{ */
442609f7
JF
5730[0-7]+ L I(number, Number(strtoull(yytext + 1, NULL, 8)), tk::NumericLiteral, hi::Constant);
5740[0-9]+ L I(number, Number(strtoull(yytext + 1, NULL, 10)), tk::NumericLiteral, hi::Constant);
5d646fb5 575
442609f7
JF
5760[xX][0-9a-fA-F]+ L I(number, Number(strtoull(yytext + 2, NULL, 16)), tk::NumericLiteral, hi::Constant);
5770[oO][0-7]+ L I(number, Number(strtoull(yytext + 2, NULL, 8)), tk::NumericLiteral, hi::Constant);
5780[bB][0-1]+ L I(number, Number(strtoull(yytext + 2, NULL, 2)), tk::NumericLiteral, hi::Constant);
5befe15e 579
442609f7 580(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?)([eE][+-]?[0-9]+)? L I(number, Number(strtod(yytext, NULL)), tk::NumericLiteral, hi::Constant);
ee6c04ef
JF
581(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?)[eE][+-]?{IdentifierScrap} L E("invalid exponent")
582(\.?[0-9]|(0|[1-9][0-9]*)\.){IdentifierScrap} L E("invalid number")
aca28f96
JF
583 /* }}} */
584 /* String {{{ */
b900e1a4
JF
585\' L CYLexBufferStart(LegacySingleString);
586<LegacySingleString,StrictSingleString>{
587 \' R CYLexBufferEnd(string, String, tk::StringLiteral, hi::Constant);
588 {SingleCharacter}+ R CYLexBufferUnits(yytext, yyleng);
1cff1a90
JF
589 {SingleCharacter}*{LineTerminatorSequence} R E("invalid newline");
590 {SingleCharacter}*{UnicodeScrap} R E("invalid character");
b900e1a4 591}
931b816a 592
b900e1a4
JF
593\" L CYLexBufferStart(LegacyDoubleString);
594<LegacyDoubleString,StrictDoubleString>{
595 \" R CYLexBufferEnd(string, String, tk::StringLiteral, hi::Constant);
596 {DoubleCharacter}+ R CYLexBufferUnits(yytext, yyleng);
1cff1a90
JF
597 {DoubleCharacter}*{LineTerminatorSequence} R E("invalid newline");
598 {DoubleCharacter}*{UnicodeScrap} R E("invalid character");
b900e1a4
JF
599}
600 /* }}} */
601 /* Template {{{ */
602"`" L yyextra->tail_ = false; CYLexBufferStart(StrictAccentString);
98711170 603<DivOrTemplateTail>"}" L yyextra->tail_ = true; S(template_); CYLexBufferStart(StrictAccentString);
931b816a 604
b900e1a4
JF
605<StrictAccentString>{
606 "`" R CYLexBufferEnd(string, String, yyextra->tail_ ? tk::TemplateTail : tk::NoSubstitutionTemplate, hi::Constant);
607 "${" R yyextra->template_.push(true); CYLexBufferEnd(string, String, yyextra->tail_ ? tk::TemplateMiddle : tk::TemplateHead, hi::Constant);
a703494a 608
b900e1a4
JF
609 "$" R CYLexBufferUnit('$');
610
611 {PlateCharacter}+ R CYLexBufferUnits(yytext, yyleng);
1cff1a90
JF
612 {PlateCharacter}*{LineTerminatorSequence} R E("invalid newline");
613 {PlateCharacter}*{UnicodeScrap} R E("invalid character");
b900e1a4
JF
614}
615 /* }}} */
616 /* Escapes {{{ */
617<LegacySingleString,LegacyDoubleString>{
618 \\[0-3][0-7][0-7] R CYLexBufferPoint(X(yytext[1]) << 6 | X(yytext[2]) << 3 | X(yytext[3]));
619 \\[0-7][0-7] R CYLexBufferUnit(X(yytext[1]) << 3 | X(yytext[2]));
620 \\[0-7] R CYLexBufferUnit(X(yytext[1]));
621}
931b816a 622
b900e1a4
JF
623<StrictSingleString,StrictDoubleString,StrictAccentString>{
624 \\0[0-7] R E("legacy escape");
625 \\0 R CYLexBufferUnit('\0');
626}
ee6c04ef 627
b900e1a4
JF
628<LegacySingleString,LegacyDoubleString,StrictSingleString,StrictDoubleString,StrictAccentString>{
629 \\b R CYLexBufferUnit('\b');
630 \\f R CYLexBufferUnit('\f');
631 \\n R CYLexBufferUnit('\n');
632 \\r R CYLexBufferUnit('\r');
633 \\t R CYLexBufferUnit('\t');
634 \\v R CYLexBufferUnit('\v');
931b816a 635
b900e1a4
JF
636 \\x{HexDigit}{2} R CYLexBufferPoint(X(yytext[2]) << 4 | X(yytext[3]));
637
638 \\u{HexDigit}{4} R CYLexBufferPoint(X(yytext[2]) << 12 | X(yytext[3]) << 8 | X(yytext[4]) << 4 | X(yytext[5]));
639
640 \\u\{{HexDigit}+\} R {
641 unsigned point(0);
642 for (yy_size_t i(3); i != yyleng - 1; ++i)
643 point = point << 4 | X(yytext[i]);
644 CYLexBufferPoint(point);
931b816a
JF
645 }
646
51b2dc6b 647 \\{LineTerminatorSequence} yylloc->end.Lines();
1cff1a90
JF
648 \\{NoneTerminatorCharacter} R CYLexBufferUnits(yytext + 1, yyleng - 1);
649 \\{UnicodeScrap} R E("invalid character");
5befe15e 650
1cff1a90 651 \\(x{HexDigit}{0,1}|u({HexDigit}{0,3}|\{{HexDigit}*))? R E("invalid escape");
b900e1a4
JF
652 <<EOF>> R E("invalid string");
653}
aca28f96 654 /* }}} */
e31ea496 655
51b2dc6b 656{LineTerminatorSequence} yylloc->step(); yylloc->end.Lines(); yyextra->last_ = true; N
ee6c04ef 657{WhiteSpace} L
1cff1a90 658{U1}|{UnicodeScrap} L E("invalid character");
7e5391fd 659
82a02ede 660<<EOF>> if (yyextra->auto_) { yyextra->auto_ = false; F(tk::AutoComplete, hi::Nothing); } L yyterminate();
94d55b5c 661
924f67b2
JF
662%%
663
98711170
JF
664#undef yyextra
665#define yyextra this
666#define yyscanner scanner_
667
5999c315 668void CYDriver::ScannerInit() {
924f67b2
JF
669 cylex_init(&scanner_);
670 cyset_extra(this, scanner_);
671}
672
5999c315 673void CYDriver::ScannerDestroy() {
924f67b2
JF
674 cylex_destroy(scanner_);
675}
63cd45c9 676
98711170
JF
677void CYDriver::SetRegEx(bool equal) {
678 CYLexBufferStart(RegularExpression);
679 CYLexBufferUnit('/');
680 if (equal)
681 CYLexBufferUnit('=');
682}
683
691e4717 684void CYDriver::SetCondition(Condition condition) {
63cd45c9
JF
685 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
686
687 switch (condition) {
691e4717
JF
688@begin E4X
689 case XMLContentCondition:
690 BEGIN(XMLContent);
691 break;
692 case XMLTagCondition:
693 BEGIN(XMLTag);
694 break;
695@end
63cd45c9
JF
696 default:
697 _assert(false);
698 }
699}
691e4717
JF
700
701void CYDriver::PushCondition(Condition condition) {
702 switch (condition) {
691e4717
JF
703@begin E4X
704 case XMLContentCondition:
705 yy_push_state(XMLContent, scanner_);
706 break;
707 case XMLTagCondition:
708 yy_push_state(XMLTag, scanner_);
709 break;
710@end
711 default:
712 _assert(false);
713 }
714}
715
716void CYDriver::PopCondition() {
717 yy_pop_state(scanner_);
718}
1771224f 719
8a392978
JF
720bool CYLexerHighlight(hi::Value &highlight, CYLocation &location, void *scanner) {
721 YYSTYPE value;
722 if (cylex(&value, &location, scanner) == 0)
723 return false;
724 highlight = value.highlight_;
725 return true;
726}
727
1771224f
JF
728#if defined(__clang__)
729#pragma clang diagnostic pop
7c4c728d
JF
730#else
731// must not pop -Wunused-function
732//#pragma GCC diagnostic pop
1771224f 733#endif