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