]> git.saurik.com Git - cycript.git/blob - Cycript.l.in
Began work on implementing E4X.
[cycript.git] / Cycript.l.in
1 %{
2 // 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
3
4 #define YYLTYPE cy::location
5 #include "Cycript.tab.hh"
6 typedef cy::parser::token tk;
7
8 #define YY_EXTRA_TYPE CYDriver *
9
10 #define T yylval->newline_ = yyextra->state_ == CYNewLine;
11 #define C T yyextra->state_ = CYClear;
12 #define R T yyextra->state_ = CYRestricted;
13
14 #define E(prefix) L C BEGIN(INITIAL); { \
15 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng + sizeof(prefix)))); \
16 memcpy(value, prefix, sizeof(prefix) - 1); \
17 memcpy(value + sizeof(prefix) - 1, yytext, yyleng); \
18 value[yyleng + sizeof(prefix) - 1] = '\0'; \
19 yylval->literal_ = new(yyextra->pool_) CYRegEx(value); \
20 return tk::RegularExpressionLiteral; \
21 }
22
23 #define N \
24 if (yyextra->state_ != CYNewLine) { \
25 bool restricted(yyextra->state_ == CYRestricted); \
26 if (restricted) { \
27 yyextra->state_ = CYClear; \
28 return tk::NewLine; \
29 } else \
30 yyextra->state_ = CYNewLine; \
31 }
32
33 #define M { \
34 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) { \
35 unsigned lines(0); \
36 size_t left; \
37 do { \
38 ++lines; \
39 left = yyleng - (nl - yytext) - 1; \
40 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left)); \
41 } while (nl != NULL); \
42 yylloc->end.lines(lines); \
43 yylloc->end.columns(left); \
44 yylloc->step(); \
45 N \
46 } else L \
47 }
48
49 #define L { \
50 yylloc->step(); \
51 yylloc->columns(yyleng); \
52 }
53
54 int H(char c) {
55 if (c >= '0' && c <= '9')
56 return c - '0';
57 if (c >= 'a' && c <= 'f')
58 return c - 'a' + 10;
59 if (c >= 'A' && c <= 'F')
60 return c - 'A' + 10;
61 return -1;
62 }
63
64 #define YY_INPUT(data, value, size) { \
65 if (yyextra->file_ != NULL) { \
66 size_t copy(fread(data, 1, size, yyextra->file_)); \
67 value = copy == 0 ? YY_NULL : copy; \
68 } else if (yyextra->size_ == 0) \
69 value = YY_NULL; \
70 else { \
71 size_t copy(std::min(size, yyextra->size_)); \
72 memcpy(data, yyextra->data_, copy); \
73 yyextra->data_ += copy; \
74 yyextra->size_ -= copy; \
75 value = copy; \
76 } \
77 }
78
79 %}
80
81 %option prefix="cy"
82 %option bison-bridge
83 %option bison-locations
84 %option noyywrap
85 %option yylineno
86 %option nounput
87 %option interactive
88 %option reentrant
89
90 Exponent [eE][+-]?[0-9]+
91 Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
92
93 IdentifierStart [a-zA-Z$_]
94 IdentifierPart [a-zA-Z$_0-9]
95
96 NonTerminator [^\n]
97 BackslashSequence \\{NonTerminator}
98 RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
99 RegularExpressionChar [^\n\\/]|{BackslashSequence}
100 RegularExpressionFlags {IdentifierPart}*
101 RegularExpressionChars {RegularExpressionChar}*
102 RegularExpressionBody_ {RegularExpressionChars}
103 RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionBody_}
104
105 RegularExpressionEnd_ \/{RegularExpressionFlags}
106 RegularExpressionRest_ {RegularExpressionBody_}{RegularExpressionEnd_}
107 RegularExpressionStart_ {RegularExpressionBody}{RegularExpressionEnd_}
108
109 %x RegExp
110 %x RegExpSlash
111 %x RegExpSlashEqual
112 %x RegExpSlashRight
113
114 %%
115
116 <RegExpSlash>{RegularExpressionStart_} E("/")
117 <RegExpSlashEqual>{RegularExpressionRest_} E("/=")
118 <RegExpSlashRight>{RegularExpressionRest_} E("/>")
119
120 \/\/[^\n]* L
121 \/\*(\n|[^\*]|\*[^/])*\*\/ M
122
123 @begin E4X
124 <RegExp>\<!--(\n|[^-]|-[^-])*-->
125 <RegExp>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]>
126 <RegExp>\<?(\n|[^?]|\?[^>])*?>
127
128 "@" L C return tk::At;
129 "::" L C return tk::ColonColon;
130 "<>" L C return tk::LeftRight;
131 "</>" L C return tk::LeftSlashRight;
132 ".." L C return tk::PeriodPeriod;
133 "/>" L C return tk::SlashRight;
134 @end
135
136 "&" L C return tk::Ampersand;
137 "&&" L C return tk::AmpersandAmpersand;
138 "&=" L C return tk::AmpersandEqual;
139 "^" L C return tk::Carrot;
140 "^=" L C return tk::CarrotEqual;
141 "=" L C return tk::Equal;
142 "==" L C return tk::EqualEqual;
143 "===" L C return tk::EqualEqualEqual;
144 "!" L C return tk::Exclamation;
145 "!=" L C return tk::ExclamationEqual;
146 "!==" L C return tk::ExclamationEqualEqual;
147 "-" L C return tk::Hyphen;
148 "-=" L C return tk::HyphenEqual;
149 "--" L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
150 "->" L C return tk::HyphenRight;
151 "<" L C return tk::Left;
152 "<=" L C return tk::LeftEqual;
153 "<<" L C return tk::LeftLeft;
154 "<<=" L C return tk::LeftLeftEqual;
155 "%" L C return tk::Percent;
156 "%=" L C return tk::PercentEqual;
157 "." L C return tk::Period;
158 "|" L C return tk::Pipe;
159 "|=" L C return tk::PipeEqual;
160 "||" L C return tk::PipePipe;
161 "+" L C return tk::Plus;
162 "+=" L C return tk::PlusEqual;
163 "++" L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
164 ">" L C return tk::Right;
165 ">=" L C return tk::RightEqual;
166 ">>" L C return tk::RightRight;
167 ">>=" L C return tk::RightRightEqual;
168 ">>>" L C return tk::RightRightRight;
169 ">>>=" L C return tk::RightRightRightEqual;
170 "/" L C return tk::Slash;
171 "/=" L C return tk::SlashEqual;
172 "*" L C return tk::Star;
173 "*=" L C return tk::StarEqual;
174 "~" L C return tk::Tilde;
175
176 ":" L C return tk::Colon;
177 "," L C return tk::Comma;
178 "?" L C return tk::Question;
179 ";" L C return tk::SemiColon;
180
181 "(" L C return tk::OpenParen;
182 ")" L C return tk::CloseParen;
183
184 "{" L C return tk::OpenBrace;
185 "}" L C return tk::CloseBrace;
186
187 "[" L C return tk::OpenBracket;
188 "]" L C return tk::CloseBracket;
189
190 @begin ObjectiveC
191 "@class" L C return tk::AtClass;
192 "@end" L C return tk::AtEnd;
193 "@selector" L C return tk::AtSelector;
194 @end
195
196 "false" L C yylval->false_ = new(yyextra->pool_) CYFalse(); return tk::False;
197 "null" L C yylval->null_ = new(yyextra->pool_) CYNull(); return tk::Null;
198 "true" L C yylval->true_ = new(yyextra->pool_) CYTrue(); return tk::True;
199
200 "break" L R yylval->word_ = new(yyextra->pool_) CYWord("break"); return tk::Break;
201 "case" L C yylval->word_ = new(yyextra->pool_) CYWord("case"); return tk::Case;
202 "catch" L C yylval->word_ = new(yyextra->pool_) CYWord("catch"); return tk::Catch;
203 "continue" L R yylval->word_ = new(yyextra->pool_) CYWord("continue"); return tk::Continue;
204 "default" L C yylval->word_ = new(yyextra->pool_) CYWord("default"); return tk::Default;
205 "delete" L C yylval->word_ = new(yyextra->pool_) CYWord("delete"); return tk::Delete;
206 "do" L C yylval->word_ = new(yyextra->pool_) CYWord("do"); return tk::Do;
207 "else" L C yylval->word_ = new(yyextra->pool_) CYWord("else"); return tk::Else;
208 "finally" L C yylval->word_ = new(yyextra->pool_) CYWord("finally"); return tk::Finally;
209 "for" L C yylval->word_ = new(yyextra->pool_) CYWord("for"); return tk::For;
210 "function" L C yylval->word_ = new(yyextra->pool_) CYWord("function"); return tk::Function;
211 "if" L C yylval->word_ = new(yyextra->pool_) CYWord("if"); return tk::If;
212 "in" L C yylval->word_ = new(yyextra->pool_) CYWord("in"); return tk::In;
213 "instanceof" L C yylval->word_ = new(yyextra->pool_) CYWord("instanceof"); return tk::InstanceOf;
214 "new" L C yylval->word_ = new(yyextra->pool_) CYWord("new"); return tk::New;
215 "return" L R yylval->word_ = new(yyextra->pool_) CYWord("return"); return tk::Return;
216 "switch" L C yylval->word_ = new(yyextra->pool_) CYWord("switch"); return tk::Switch;
217 "this" L C yylval->this_ = new(yyextra->pool_) CYThis(); return tk::This;
218 "throw" L R yylval->word_ = new(yyextra->pool_) CYWord("throw"); return tk::Throw;
219 "try" L C yylval->word_ = new(yyextra->pool_) CYWord("try"); return tk::Try;
220 "typeof" L C yylval->word_ = new(yyextra->pool_) CYWord("typeof"); return tk::TypeOf;
221 "var" L C yylval->word_ = new(yyextra->pool_) CYWord("var"); return tk::Var;
222 "void" L C yylval->word_ = new(yyextra->pool_) CYWord("void"); return tk::Void;
223 "while" L C yylval->word_ = new(yyextra->pool_) CYWord("while"); return tk::While;
224 "with" L C yylval->word_ = new(yyextra->pool_) CYWord("with"); return tk::With;
225
226 "debugger" L C yylval->word_ = new(yyextra->pool_) CYWord("debugger"); return tk::Debugger;
227
228 "const" L C yylval->word_ = new(yyextra->pool_) CYWord("const"); return tk::Const;
229
230 "class" L C yylval->word_ = new(yyextra->pool_) CYWord("class"); return tk::Class;
231 "enum" L C yylval->word_ = new(yyextra->pool_) CYWord("enum"); return tk::Enum;
232 "export" L C yylval->word_ = new(yyextra->pool_) CYWord("export"); return tk::Export;
233 "extends" L C yylval->word_ = new(yyextra->pool_) CYWord("extends"); return tk::Extends;
234 "import" L C yylval->word_ = new(yyextra->pool_) CYWord("import"); return tk::Import;
235 "super" L C yylval->word_ = new(yyextra->pool_) CYWord("super"); return tk::Super;
236
237 "implements" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("implements"); return tk::Implements;
238 "interface" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("interface"); return tk::Interface;
239 "package" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("package"); return tk::Package;
240 "private" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("private"); return tk::Private;
241 "protected" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("protected"); return tk::Protected;
242 "public" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("public"); return tk::Public;
243 "static" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("static"); return tk::Static;
244
245 "abstract" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("abstract"); return tk::Abstract;
246 "boolean" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("boolean"); return tk::Boolean;
247 "byte" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("byte"); return tk::Byte;
248 "char" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("char"); return tk::Char;
249 "double" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("double"); return tk::Double;
250 "final" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("final"); return tk::Final;
251 "float" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("float"); return tk::Float;
252 "goto" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("goto"); return tk::Goto;
253 "int" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("int"); return tk::Int;
254 "long" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("long"); return tk::Long;
255 "native" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("native"); return tk::Native;
256 "short" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("short"); return tk::Short;
257 "synchronized" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("synchronized"); return tk::Synchronized;
258 "throws" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("throws"); return tk::Throws;
259 "transient" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("transient"); return tk::Transient;
260 "volatile" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("volatile"); return tk::Volatile;
261
262 "let" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("let"); return tk::Let;
263 "yield" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("yield"); return tk::Yield;
264
265 "each" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("each"); return tk::Each;
266
267 {IdentifierStart}{IdentifierPart}* yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier_;
268
269 (\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? yylval->number_ = new(yyextra->pool_) CYNumber(strtod(yytext, NULL)); L C return tk::NumericLiteral;
270
271 0[xX][0-9a-fA-F]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
272
273 0[bB][0-1]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;
274
275 \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
276 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
277 char *local(value);
278
279 for (int i(1); i != yyleng - 1; ++i) {
280 char next(yytext[i]);
281
282 if (yytext[i] == '\\')
283 switch (next = yytext[++i]) {
284 case '\n': continue;
285 case '\\': next = '\\'; break;
286 case '\'': next = '\''; break;
287 case '"': next = '"'; break;
288 case 'b': next = '\b'; break;
289 case 'f': next = '\f'; break;
290 case 'n': next = '\n'; break;
291 case 'r': next = '\r'; break;
292 case 't': next = '\t'; break;
293 case 'v': next = '\v'; break;
294 case '0': next = '\0'; break;
295
296 case 'x':
297 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
298 i += 2;
299 break;
300 }
301
302 *local++ = next;
303 }
304
305 *local = '\0';
306 yylval->string_ = new(yyextra->pool_) CYString(value, local - value);
307 return tk::StringLiteral;
308 }
309
310 \r?\n yylloc->end.lines(); yylloc->step(); N
311
312 [ \t] L
313 <<EOF>> L yyterminate();
314
315 . L {
316 CYDriver::Error error;
317 error.location_ = *yylloc;
318 error.message_ = "syntax error, unknown token";
319 yyextra->errors_.push_back(error);
320 yyterminate();
321 }
322
323 %%
324
325 void CYDriver::ScannerInit() {
326 cylex_init(&scanner_);
327 cyset_extra(this, scanner_);
328 }
329
330 void CYDriver::ScannerDestroy() {
331 cylex_destroy(scanner_);
332 }
333
334 void CYDriver::SetCondition(Condition condition) {
335 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
336
337 switch (condition) {
338 case RegExpSlash:
339 BEGIN(RegExpSlash);
340 break;
341 case RegExpSlashEqual:
342 BEGIN(RegExpSlashEqual);
343 break;
344 case RegExpSlashRight:
345 BEGIN(RegExpSlashRight);
346 break;
347 default:
348 _assert(false);
349 }
350 }