]> git.saurik.com Git - cycript.git/blame - Cycript.l
Change mind (again) and use tightly-coupled C++ API.
[cycript.git] / Cycript.l
CommitLineData
e5332278 1%{
63b4c5a8
JF
2#include "Cycript.tab.hh"
3typedef cy::parser::token tk;
693d501b 4
5999c315 5#define YY_EXTRA_TYPE CYDriver *
693d501b 6#define YYLTYPE cy::location
db5e2840
JF
7
8#define T yylval->newline_ = yyextra->state_ == CYNewLine;
9#define C T yyextra->state_ = CYClear;
10#define R T yyextra->state_ = CYRestricted;
5befe15e
JF
11
12#define N \
13 if (yyextra->state_ != CYNewLine) { \
14 bool restricted(yyextra->state_ == CYRestricted); \
15 if (restricted) { \
16 yyextra->state_ = CYClear; \
17 return tk::NewLine; \
18 } else \
19 yyextra->state_ = CYNewLine; \
20 }
21
22#define L { \
23 yylloc->step(); \
24 yylloc->columns(yyleng); \
25}
e7ed5354 26
931b816a
JF
27int H(char c) {
28 if (c >= '0' && c <= '9')
29 return c - '0';
30 if (c >= 'a' && c <= 'f')
31 return c - 'a' + 10;
32 if (c >= 'A' && c <= 'F')
33 return c - 'A' + 10;
34 return -1;
35}
36
e7ed5354
JF
37#define YY_INPUT(data, value, size) { \
38 if (yyextra->size_ == 0) \
39 value = YY_NULL; \
40 else { \
41 size_t copy(std::min(size, yyextra->size_)); \
42 memcpy(data, yyextra->data_, copy); \
43 yyextra->data_ += copy; \
44 yyextra->size_ -= copy; \
45 value = copy; \
46 } \
47}
48
e5332278
JF
49%}
50
51%option prefix="cy"
52%option bison-bridge
53%option bison-locations
54%option noyywrap
55%option yylineno
56%option nounput
57%option interactive
924f67b2 58%option reentrant
e5332278 59
2bf24581 60Exponent [eE][+-]?[0-9]+
931b816a 61Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}
e5332278
JF
62
63%%
64
c3c20102 65\/\/[^\n]* ;
db5e2840
JF
66\/\*(\n|[^\*]|\*[^/])\*\/ if (memchr(yytext, '\n', yyleng) != NULL) N // XXX: supposedly I will be screwed on very very long multi-line comments and need to replace this with a manual lexer. http://websrv.cs.fsu.edu/~engelen/courses/COP5621/Pr2.pdf ; XXX: this rule doesn't work anyway, fucking A :(
67
5befe15e
JF
68"&" L C return tk::Ampersand;
69"&&" L C return tk::AmpersandAmpersand;
70"&=" L C return tk::AmpersandEqual;
71"^" L C return tk::Carrot;
72"^=" L C return tk::CarrotEqual;
73"=" L C return tk::Equal;
74"==" L C return tk::EqualEqual;
75"===" L C return tk::EqualEqualEqual;
76"!" L C return tk::Exclamation;
77"!=" L C return tk::ExclamationEqual;
78"!==" L C return tk::ExclamationEqualEqual;
79"-" L C return tk::Hyphen;
80"-=" L C return tk::HyphenEqual;
81"--" L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
82"->" L C return tk::HyphenRight;
83"<" L C return tk::Left;
84"<=" L C return tk::LeftEqual;
85"<<" L C return tk::LeftLeft;
86"<<=" L C return tk::LeftLeftEqual;
87"%" L C return tk::Percent;
88"%=" L C return tk::PercentEqual;
89"." L C return tk::Period;
90"|" L C return tk::Pipe;
91"|=" L C return tk::PipeEqual;
92"||" L C return tk::PipePipe;
93"+" L C return tk::Plus;
94"+=" L C return tk::PlusEqual;
95"++" L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
96">" L C return tk::Right;
97">=" L C return tk::RightEqual;
98">>" L C return tk::RightRight;
99">>=" L C return tk::RightRightEqual;
100">>>" L C return tk::RightRightRight;
101">>>=" L C return tk::RightRightRightEqual;
102"/" L C return tk::Slash;
103"/=" L C return tk::SlashEqual;
104"*" L C return tk::Star;
105"*=" L C return tk::StarEqual;
106"~" L C return tk::Tilde;
107
108":" L C return tk::Colon;
109"," L C return tk::Comma;
110"?" L C return tk::Question;
111";" L C return tk::SemiColon;
112
113"(" L C return tk::OpenParen;
114")" L C return tk::CloseParen;
115
116"{" L C return tk::OpenBrace;
117"}" L C return tk::CloseBrace;
118
119"[" L C return tk::OpenBracket;
120"]" L C return tk::CloseBracket;
121
122"@selector" L C return tk::AtSelector;
123
124"break" L R yylval->word_ = new CYWord("break"); return tk::Break;
125"case" L C yylval->word_ = new CYWord("case"); return tk::Case;
126"catch" L C yylval->word_ = new CYWord("catch"); return tk::Catch;
127"continue" L R yylval->word_ = new CYWord("continue"); return tk::Continue;
128"default" L C yylval->word_ = new CYWord("default"); return tk::Default;
129"delete" L C yylval->word_ = new CYWord("delete"); return tk::Delete;
130"do" L C yylval->word_ = new CYWord("do"); return tk::Do;
131"else" L C yylval->word_ = new CYWord("else"); return tk::Else;
132"false" L C yylval->false_ = new CYFalse(); return tk::False;
133"finally" L C yylval->word_ = new CYWord("finally"); return tk::Finally;
134"for" L C yylval->word_ = new CYWord("for"); return tk::For;
135"function" L C yylval->word_ = new CYWord("function"); return tk::Function;
136"if" L C yylval->word_ = new CYWord("if"); return tk::If;
137"in" L C yylval->word_ = new CYWord("in"); return tk::In;
138"instanceof" L C yylval->word_ = new CYWord("instanceof"); return tk::InstanceOf;
139"new" L C yylval->word_ = new CYWord("new"); return tk::New;
140"null" L C yylval->null_ = new CYNull(); return tk::Null;
141"return" L R yylval->word_ = new CYWord("return"); return tk::Return;
142"switch" L C yylval->word_ = new CYWord("switch"); return tk::Switch;
143"this" L C yylval->this_ = new CYThis(); return tk::This;
144"throw" L R yylval->word_ = new CYWord("throw"); return tk::Throw;
145"true" L C yylval->true_ = new CYTrue(); return tk::True;
146"try" L C yylval->word_ = new CYWord("try"); return tk::Try;
147"typeof" L C yylval->word_ = new CYWord("typeof"); return tk::TypeOf;
148"var" L C yylval->word_ = new CYWord("var"); return tk::Var;
149"void" L C yylval->word_ = new CYWord("void"); return tk::Void;
150"while" L C yylval->word_ = new CYWord("while"); return tk::While;
151"with" L C yylval->word_ = new CYWord("with"); return tk::With;
152
153[a-zA-Z$_][a-zA-Z$_0-9]* yylval->identifier_ = new CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier;
154
155(\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? yylval->number_ = new CYNumber(strtod(yytext, NULL)); L C return tk::NumericLiteral;
156
1570[xX][0-9a-fA-F]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
158
1590[bB][0-1]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;
160
931b816a
JF
161\"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
162 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
163 char *local(value);
164
165 for (int i(1); i != yyleng - 1; ++i) {
166 char next(yytext[i]);
167
168 if (yytext[i] == '\\')
169 switch (next = yytext[++i]) {
170 case '\\': next = '\\'; break;
171 case '\'': next = '\''; break;
172 case '"': next = '"'; break;
173 case 'b': next = '\b'; break;
174 case 'f': next = '\f'; break;
175 case 'n': next = '\n'; break;
176 case 'r': next = '\r'; break;
177 case 't': next = '\t'; break;
178 case 'v': next = '\v'; break;
179 case '0': next = '\0'; break;
180
181 case 'x':
182 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
183 i += 2;
184 break;
185 }
186
187 *local++ = next;
188 }
189
190 *local = '\0';
191 yylval->string_ = new CYString(value, local - value);
192 return tk::StringLiteral;
193}
5befe15e
JF
194
195\n yylloc->end.lines(); yylloc->step(); N
196
197[ \t] L
198<<EOF>> L yyterminate();
94d55b5c
JF
199
200. {
201 CYDriver::Error error;
202 error.location_ = *yylloc;
203 error.message_ = "syntax error, unknown token";
204 yyextra->errors_.push_back(error);
205 yyterminate();
206}
924f67b2
JF
207
208%%
209
5999c315 210void CYDriver::ScannerInit() {
924f67b2
JF
211 cylex_init(&scanner_);
212 cyset_extra(this, scanner_);
213}
214
5999c315 215void CYDriver::ScannerDestroy() {
924f67b2
JF
216 cylex_destroy(scanner_);
217}