]> git.saurik.com Git - cycript.git/blame - Cycript.l.in
Completed massive refactoring operation to completely isolate Objective-C.
[cycript.git] / Cycript.l.in
CommitLineData
e5332278 1%{
cb02f8ae
JF
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
cac61857 4#define YYLTYPE cy::location
63b4c5a8
JF
5#include "Cycript.tab.hh"
6typedef cy::parser::token tk;
693d501b 7
5999c315 8#define YY_EXTRA_TYPE CYDriver *
db5e2840 9
697d6fd2 10#define T yylval->newline_ = yyextra->state_ == CYNewLine; BEGIN(Div);
db5e2840
JF
11#define C T yyextra->state_ = CYClear;
12#define R T yyextra->state_ = CYRestricted;
5befe15e 13
697d6fd2 14#define E(prefix) L C { \
63cd45c9
JF
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'; \
5d646fb5 19 yylval->literal_ = new(yyextra->pool_) CYRegEx(value); \
63cd45c9
JF
20 return tk::RegularExpressionLiteral; \
21}
22
5befe15e
JF
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
691e4717 33#define V(more) { \
cb02f8ae
JF
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(); \
691e4717 45 more \
cb02f8ae
JF
46 } else L \
47}
48
5befe15e
JF
49#define L { \
50 yylloc->step(); \
51 yylloc->columns(yyleng); \
52}
e7ed5354 53
931b816a
JF
54int 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
e7ed5354 64#define YY_INPUT(data, value, size) { \
48e3be8a
JF
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) \
e7ed5354
JF
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
e5332278
JF
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
924f67b2 88%option reentrant
691e4717 89%option stack
e5332278 90
2bf24581 91Exponent [eE][+-]?[0-9]+
367eebb1 92Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
e5332278 93
63cd45c9
JF
94IdentifierStart [a-zA-Z$_]
95IdentifierPart [a-zA-Z$_0-9]
96
97NonTerminator [^\n]
98BackslashSequence \\{NonTerminator}
99RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
100RegularExpressionChar [^\n\\/]|{BackslashSequence}
101RegularExpressionFlags {IdentifierPart}*
102RegularExpressionChars {RegularExpressionChar}*
697d6fd2 103RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionChars}
63cd45c9 104
691e4717
JF
105@begin E4X
106XMLNameStart [a-zA-Z_:]
107XMLNamePart [a-zA-Z0-9.-_:]
108XMLName {XMLNameStart}{XMLNamePart}*
109@end
110
697d6fd2
JF
111%s Div
112%s RegExp
63cd45c9 113
691e4717
JF
114@begin E4X
115%x XMLContent
116%x XMLTag
117@end
118
e5332278
JF
119%%
120
697d6fd2 121<RegExp>\/{RegularExpressionBody}\/{RegularExpressionFlags} E("")
63cd45c9 122
66fb559f 123\/\/[^\n]* L
691e4717 124\/\*(\n|[^\*]|\*[^/])*\*\/ V(N)
66fb559f 125
cb02f8ae 126@begin E4X
691e4717
JF
127<RegExp>"<>" L return tk::LeftRight;
128<XMLContent>"</>" L return tk::LeftSlashRight;
129
130<RegExp,XMLContent>\<!\[CDATA\[(\n|[^[]|\[[^[]|\[\[[^>])*]]> V() return tk::XMLCDATA;
131<RegExp,XMLContent>\<!--(\n|[^-]|-[^-])*--> V() return tk::XMLComment;
132<RegExp,XMLContent>\<?(\n|[^?]|\?[^>])*?> V() return tk::XMLPI;
133
134<XMLTag>"=" L return tk::Equal;
135<XMLTag>">" L return tk::Right;
136<XMLTag>"/>" L return tk::SlashRight;
137<XMLTag>"{" L return tk::OpenBrace;
138
139<XMLTag>\"(\n|[^"])*\"|'(\n|[^'])*' V() {
140 return tk::XMLAttributeValue;
141}
142
143<XMLTag>{XMLName} L return tk::XMLName;
144<XMLTag>[ \t\r\n] V() return tk::XMLWhitespace;
db5e2840 145
691e4717
JF
146<XMLContent>"{" L return tk::OpenBrace;
147<XMLContent>"<" L return tk::Left;
148<XMLContent>"</" L return tk::LeftSlash;
149@end
150
151@begin E4X
ac9a5ce1 152"@" L C return tk::At;
cb02f8ae 153"::" L C return tk::ColonColon;
cb02f8ae 154".." L C return tk::PeriodPeriod;
cb02f8ae 155@end
ac9a5ce1 156
5befe15e
JF
157"&" L C return tk::Ampersand;
158"&&" L C return tk::AmpersandAmpersand;
159"&=" L C return tk::AmpersandEqual;
160"^" L C return tk::Carrot;
161"^=" L C return tk::CarrotEqual;
162"=" L C return tk::Equal;
163"==" L C return tk::EqualEqual;
164"===" L C return tk::EqualEqualEqual;
165"!" L C return tk::Exclamation;
166"!=" L C return tk::ExclamationEqual;
167"!==" L C return tk::ExclamationEqualEqual;
168"-" L C return tk::Hyphen;
169"-=" L C return tk::HyphenEqual;
170"--" L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
171"->" L C return tk::HyphenRight;
172"<" L C return tk::Left;
173"<=" L C return tk::LeftEqual;
174"<<" L C return tk::LeftLeft;
175"<<=" L C return tk::LeftLeftEqual;
176"%" L C return tk::Percent;
177"%=" L C return tk::PercentEqual;
178"." L C return tk::Period;
179"|" L C return tk::Pipe;
180"|=" L C return tk::PipeEqual;
181"||" L C return tk::PipePipe;
182"+" L C return tk::Plus;
183"+=" L C return tk::PlusEqual;
184"++" L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
185">" L C return tk::Right;
186">=" L C return tk::RightEqual;
187">>" L C return tk::RightRight;
188">>=" L C return tk::RightRightEqual;
189">>>" L C return tk::RightRightRight;
190">>>=" L C return tk::RightRightRightEqual;
5befe15e
JF
191"*" L C return tk::Star;
192"*=" L C return tk::StarEqual;
193"~" L C return tk::Tilde;
194
697d6fd2
JF
195<Div>"/" L C return tk::Slash;
196<Div>"/=" L C return tk::SlashEqual;
197
5befe15e
JF
198":" L C return tk::Colon;
199"," L C return tk::Comma;
200"?" L C return tk::Question;
201";" L C return tk::SemiColon;
202
203"(" L C return tk::OpenParen;
204")" L C return tk::CloseParen;
205
206"{" L C return tk::OpenBrace;
207"}" L C return tk::CloseBrace;
208
209"[" L C return tk::OpenBracket;
210"]" L C return tk::CloseBracket;
211
cb02f8ae 212@begin ObjectiveC
b09da87b
JF
213"@class" L C return tk::AtClass;
214"@end" L C return tk::AtEnd;
d35a3b07 215"@selector" L C return tk::AtSelector;
cb02f8ae 216@end
d35a3b07 217
5d646fb5
JF
218"false" L C yylval->false_ = new(yyextra->pool_) CYFalse(); return tk::False;
219"null" L C yylval->null_ = new(yyextra->pool_) CYNull(); return tk::Null;
220"true" L C yylval->true_ = new(yyextra->pool_) CYTrue(); return tk::True;
221
222"break" L R yylval->word_ = new(yyextra->pool_) CYWord("break"); return tk::Break;
223"case" L C yylval->word_ = new(yyextra->pool_) CYWord("case"); return tk::Case;
224"catch" L C yylval->word_ = new(yyextra->pool_) CYWord("catch"); return tk::Catch;
225"continue" L R yylval->word_ = new(yyextra->pool_) CYWord("continue"); return tk::Continue;
226"default" L C yylval->word_ = new(yyextra->pool_) CYWord("default"); return tk::Default;
227"delete" L C yylval->word_ = new(yyextra->pool_) CYWord("delete"); return tk::Delete;
228"do" L C yylval->word_ = new(yyextra->pool_) CYWord("do"); return tk::Do;
229"else" L C yylval->word_ = new(yyextra->pool_) CYWord("else"); return tk::Else;
230"finally" L C yylval->word_ = new(yyextra->pool_) CYWord("finally"); return tk::Finally;
231"for" L C yylval->word_ = new(yyextra->pool_) CYWord("for"); return tk::For;
232"function" L C yylval->word_ = new(yyextra->pool_) CYWord("function"); return tk::Function;
233"if" L C yylval->word_ = new(yyextra->pool_) CYWord("if"); return tk::If;
234"in" L C yylval->word_ = new(yyextra->pool_) CYWord("in"); return tk::In;
235"instanceof" L C yylval->word_ = new(yyextra->pool_) CYWord("instanceof"); return tk::InstanceOf;
236"new" L C yylval->word_ = new(yyextra->pool_) CYWord("new"); return tk::New;
237"return" L R yylval->word_ = new(yyextra->pool_) CYWord("return"); return tk::Return;
238"switch" L C yylval->word_ = new(yyextra->pool_) CYWord("switch"); return tk::Switch;
239"this" L C yylval->this_ = new(yyextra->pool_) CYThis(); return tk::This;
240"throw" L R yylval->word_ = new(yyextra->pool_) CYWord("throw"); return tk::Throw;
241"try" L C yylval->word_ = new(yyextra->pool_) CYWord("try"); return tk::Try;
242"typeof" L C yylval->word_ = new(yyextra->pool_) CYWord("typeof"); return tk::TypeOf;
243"var" L C yylval->word_ = new(yyextra->pool_) CYWord("var"); return tk::Var;
244"void" L C yylval->word_ = new(yyextra->pool_) CYWord("void"); return tk::Void;
245"while" L C yylval->word_ = new(yyextra->pool_) CYWord("while"); return tk::While;
246"with" L C yylval->word_ = new(yyextra->pool_) CYWord("with"); return tk::With;
247
248"debugger" L C yylval->word_ = new(yyextra->pool_) CYWord("debugger"); return tk::Debugger;
249
250"const" L C yylval->word_ = new(yyextra->pool_) CYWord("const"); return tk::Const;
251
252"class" L C yylval->word_ = new(yyextra->pool_) CYWord("class"); return tk::Class;
253"enum" L C yylval->word_ = new(yyextra->pool_) CYWord("enum"); return tk::Enum;
254"export" L C yylval->word_ = new(yyextra->pool_) CYWord("export"); return tk::Export;
255"extends" L C yylval->word_ = new(yyextra->pool_) CYWord("extends"); return tk::Extends;
256"import" L C yylval->word_ = new(yyextra->pool_) CYWord("import"); return tk::Import;
257"super" L C yylval->word_ = new(yyextra->pool_) CYWord("super"); return tk::Super;
258
259"implements" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("implements"); return tk::Implements;
260"interface" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("interface"); return tk::Interface;
261"package" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("package"); return tk::Package;
262"private" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("private"); return tk::Private;
263"protected" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("protected"); return tk::Protected;
264"public" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("public"); return tk::Public;
265"static" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("static"); return tk::Static;
266
267"abstract" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("abstract"); return tk::Abstract;
268"boolean" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("boolean"); return tk::Boolean;
269"byte" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("byte"); return tk::Byte;
270"char" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("char"); return tk::Char;
271"double" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("double"); return tk::Double;
272"final" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("final"); return tk::Final;
273"float" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("float"); return tk::Float;
274"goto" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("goto"); return tk::Goto;
275"int" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("int"); return tk::Int;
276"long" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("long"); return tk::Long;
277"native" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("native"); return tk::Native;
278"short" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("short"); return tk::Short;
279"synchronized" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("synchronized"); return tk::Synchronized;
280"throws" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("throws"); return tk::Throws;
281"transient" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("transient"); return tk::Transient;
282"volatile" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("volatile"); return tk::Volatile;
283
284"let" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("let"); return tk::Let;
285"yield" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("yield"); return tk::Yield;
286
287"each" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("each"); return tk::Each;
288
691e4717
JF
289@begin E4X
290"namespace" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("namespace"); return tk::Namespace;
291"xml" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("xml"); return tk::XML;
292@end
293
697d6fd2 294{IdentifierStart}{IdentifierPart}* L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); return tk::Identifier_;
5d646fb5 295
697d6fd2 296(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? L C yylval->number_ = new(yyextra->pool_) CYNumber(strtod(yytext, NULL)); return tk::NumericLiteral;
5d646fb5
JF
297
2980[xX][0-9a-fA-F]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
299
3000[bB][0-1]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;
5befe15e 301
931b816a
JF
302\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
303 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
304 char *local(value);
305
306 for (int i(1); i != yyleng - 1; ++i) {
307 char next(yytext[i]);
308
309 if (yytext[i] == '\\')
310 switch (next = yytext[++i]) {
367eebb1 311 case '\n': continue;
931b816a
JF
312 case '\\': next = '\\'; break;
313 case '\'': next = '\''; break;
314 case '"': next = '"'; break;
315 case 'b': next = '\b'; break;
316 case 'f': next = '\f'; break;
317 case 'n': next = '\n'; break;
318 case 'r': next = '\r'; break;
319 case 't': next = '\t'; break;
320 case 'v': next = '\v'; break;
321 case '0': next = '\0'; break;
322
323 case 'x':
324 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
325 i += 2;
326 break;
327 }
328
329 *local++ = next;
330 }
331
332 *local = '\0';
5d646fb5 333 yylval->string_ = new(yyextra->pool_) CYString(value, local - value);
931b816a
JF
334 return tk::StringLiteral;
335}
5befe15e 336
b10bd496 337\r?\n yylloc->end.lines(); yylloc->step(); N
5befe15e
JF
338
339[ \t] L
340<<EOF>> L yyterminate();
94d55b5c 341
48e3be8a 342. L {
94d55b5c
JF
343 CYDriver::Error error;
344 error.location_ = *yylloc;
345 error.message_ = "syntax error, unknown token";
346 yyextra->errors_.push_back(error);
347 yyterminate();
348}
924f67b2
JF
349
350%%
351
5999c315 352void CYDriver::ScannerInit() {
924f67b2
JF
353 cylex_init(&scanner_);
354 cyset_extra(this, scanner_);
355}
356
5999c315 357void CYDriver::ScannerDestroy() {
924f67b2
JF
358 cylex_destroy(scanner_);
359}
63cd45c9 360
691e4717
JF
361CYDriver::Condition CYDriver::GetCondition() {
362 switch (yy_top_state(scanner_)) {
363 case RegExp:
364 return RegExpCondition;
365@begin E4X
366 case XMLContent:
367 return XMLContentCondition;
368 case XMLTag:
369 return XMLTagCondition;
370@end
371 default:
372 _assert(false);
373 }
374}
375
376void CYDriver::SetCondition(Condition condition) {
63cd45c9
JF
377 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));
378
379 switch (condition) {
697d6fd2
JF
380 case RegExpCondition:
381 BEGIN(RegExp);
63cd45c9 382 break;
691e4717
JF
383@begin E4X
384 case XMLContentCondition:
385 BEGIN(XMLContent);
386 break;
387 case XMLTagCondition:
388 BEGIN(XMLTag);
389 break;
390@end
63cd45c9
JF
391 default:
392 _assert(false);
393 }
394}
691e4717
JF
395
396void CYDriver::PushCondition(Condition condition) {
397 switch (condition) {
398 case RegExpCondition:
399 yy_push_state(RegExp, scanner_);
400 break;
401@begin E4X
402 case XMLContentCondition:
403 yy_push_state(XMLContent, scanner_);
404 break;
405 case XMLTagCondition:
406 yy_push_state(XMLTag, scanner_);
407 break;
408@end
409 default:
410 _assert(false);
411 }
412}
413
414void CYDriver::PopCondition() {
415 yy_pop_state(scanner_);
416}