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