]> git.saurik.com Git - cycript.git/blob - Cycript.l
Not quite the original, but pretty damn near close.
[cycript.git] / Cycript.l
1 %{
2 #include "Cycript.tab.hh"
3 typedef cy::parser::token tk;
4
5 #define YY_EXTRA_TYPE CYDriver *
6 #define YYLTYPE cy::location
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 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 }
26
27 int 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
37 #define YY_INPUT(data, value, size) { \
38 if (yyextra->file_ != NULL) { \
39 size_t copy(fread(data, 1, size, yyextra->file_)); \
40 value = copy == 0 ? YY_NULL : copy; \
41 } else if (yyextra->size_ == 0) \
42 value = YY_NULL; \
43 else { \
44 size_t copy(std::min(size, yyextra->size_)); \
45 memcpy(data, yyextra->data_, copy); \
46 yyextra->data_ += copy; \
47 yyextra->size_ -= copy; \
48 value = copy; \
49 } \
50 }
51
52 %}
53
54 %option prefix="cy"
55 %option bison-bridge
56 %option bison-locations
57 %option noyywrap
58 %option yylineno
59 %option nounput
60 %option interactive
61 %option reentrant
62
63 Exponent [eE][+-]?[0-9]+
64 Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
65
66 %%
67
68 \/\/[^\n]* ;
69 \/\*(\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 :(
70
71 "&" L C return tk::Ampersand;
72 "&&" L C return tk::AmpersandAmpersand;
73 "&=" L C return tk::AmpersandEqual;
74 "^" L C return tk::Carrot;
75 "^=" L C return tk::CarrotEqual;
76 "=" L C return tk::Equal;
77 "==" L C return tk::EqualEqual;
78 "===" L C return tk::EqualEqualEqual;
79 "!" L C return tk::Exclamation;
80 "!=" L C return tk::ExclamationEqual;
81 "!==" L C return tk::ExclamationEqualEqual;
82 "-" L C return tk::Hyphen;
83 "-=" L C return tk::HyphenEqual;
84 "--" L C return yylval->newline_ ? tk::HyphenHyphen_ : tk::HyphenHyphen;
85 "->" L C return tk::HyphenRight;
86 "<" L C return tk::Left;
87 "<=" L C return tk::LeftEqual;
88 "<<" L C return tk::LeftLeft;
89 "<<=" L C return tk::LeftLeftEqual;
90 "%" L C return tk::Percent;
91 "%=" L C return tk::PercentEqual;
92 "." L C return tk::Period;
93 "|" L C return tk::Pipe;
94 "|=" L C return tk::PipeEqual;
95 "||" L C return tk::PipePipe;
96 "+" L C return tk::Plus;
97 "+=" L C return tk::PlusEqual;
98 "++" L C return yylval->newline_ ? tk::PlusPlus_ : tk::PlusPlus;
99 ">" L C return tk::Right;
100 ">=" L C return tk::RightEqual;
101 ">>" L C return tk::RightRight;
102 ">>=" L C return tk::RightRightEqual;
103 ">>>" L C return tk::RightRightRight;
104 ">>>=" L C return tk::RightRightRightEqual;
105 "/" L C return tk::Slash;
106 "/=" L C return tk::SlashEqual;
107 "*" L C return tk::Star;
108 "*=" L C return tk::StarEqual;
109 "~" L C return tk::Tilde;
110
111 ":" L C return tk::Colon;
112 "," L C return tk::Comma;
113 "?" L C return tk::Question;
114 ";" L C return tk::SemiColon;
115
116 "(" L C return tk::OpenParen;
117 ")" L C return tk::CloseParen;
118
119 "{" L C return tk::OpenBrace;
120 "}" L C return tk::CloseBrace;
121
122 "[" L C return tk::OpenBracket;
123 "]" L C return tk::CloseBracket;
124
125 "@class" L C return tk::AtClass;
126 "@end" L C return tk::AtEnd;
127 "@selector" L C return tk::AtSelector;
128
129 "break" L R yylval->word_ = new CYWord("break"); return tk::Break;
130 "case" L C yylval->word_ = new CYWord("case"); return tk::Case;
131 "catch" L C yylval->word_ = new CYWord("catch"); return tk::Catch;
132 "continue" L R yylval->word_ = new CYWord("continue"); return tk::Continue;
133 "default" L C yylval->word_ = new CYWord("default"); return tk::Default;
134 "delete" L C yylval->word_ = new CYWord("delete"); return tk::Delete;
135 "do" L C yylval->word_ = new CYWord("do"); return tk::Do;
136 "else" L C yylval->word_ = new CYWord("else"); return tk::Else;
137 "false" L C yylval->false_ = new CYFalse(); return tk::False;
138 "finally" L C yylval->word_ = new CYWord("finally"); return tk::Finally;
139 "for" L C yylval->word_ = new CYWord("for"); return tk::For;
140 "function" L C yylval->word_ = new CYWord("function"); return tk::Function;
141 "if" L C yylval->word_ = new CYWord("if"); return tk::If;
142 "in" L C yylval->word_ = new CYWord("in"); return tk::In;
143 "instanceof" L C yylval->word_ = new CYWord("instanceof"); return tk::InstanceOf;
144 "new" L C yylval->word_ = new CYWord("new"); return tk::New;
145 "null" L C yylval->null_ = new CYNull(); return tk::Null;
146 "return" L R yylval->word_ = new CYWord("return"); return tk::Return;
147 "switch" L C yylval->word_ = new CYWord("switch"); return tk::Switch;
148 "this" L C yylval->this_ = new CYThis(); return tk::This;
149 "throw" L R yylval->word_ = new CYWord("throw"); return tk::Throw;
150 "true" L C yylval->true_ = new CYTrue(); return tk::True;
151 "try" L C yylval->word_ = new CYWord("try"); return tk::Try;
152 "typeof" L C yylval->word_ = new CYWord("typeof"); return tk::TypeOf;
153 "var" L C yylval->word_ = new CYWord("var"); return tk::Var;
154 "void" L C yylval->word_ = new CYWord("void"); return tk::Void;
155 "while" L C yylval->word_ = new CYWord("while"); return tk::While;
156 "with" L C yylval->word_ = new CYWord("with"); return tk::With;
157
158 "abstract" L C yylval->word_ = new CYWord("abstract"); return tk::Abstract;
159 "boolean" L C yylval->word_ = new CYWord("boolean"); return tk::Boolean;
160 "byte" L C yylval->word_ = new CYWord("byte"); return tk::Byte;
161 "char" L C yylval->word_ = new CYWord("char"); return tk::Char;
162 "class" L C yylval->word_ = new CYWord("class"); return tk::Class;
163 "const" L C yylval->word_ = new CYWord("const"); return tk::Const;
164 "debugger" L C yylval->word_ = new CYWord("debugger"); return tk::Debugger;
165 "double" L C yylval->word_ = new CYWord("double"); return tk::Double;
166 "enum" L C yylval->word_ = new CYWord("enum"); return tk::Enum;
167 "export" L C yylval->word_ = new CYWord("export"); return tk::Export;
168 "extends" L C yylval->word_ = new CYWord("extends"); return tk::Extends;
169 "final" L C yylval->word_ = new CYWord("final"); return tk::Final;
170 "float" L C yylval->word_ = new CYWord("float"); return tk::Float;
171 "goto" L C yylval->word_ = new CYWord("goto"); return tk::Goto;
172 "implements" L C yylval->word_ = new CYWord("implements"); return tk::Implements;
173 "import" L C yylval->word_ = new CYWord("import"); return tk::Import;
174 "int" L C yylval->word_ = new CYWord("int"); return tk::Int;
175 "interface" L C yylval->word_ = new CYWord("interface"); return tk::Interface;
176 "long" L C yylval->word_ = new CYWord("long"); return tk::Long;
177 "native" L C yylval->word_ = new CYWord("native"); return tk::Native;
178 "package" L C yylval->word_ = new CYWord("package"); return tk::Package;
179 "private" L C yylval->word_ = new CYWord("private"); return tk::Private;
180 "protected" L C yylval->word_ = new CYWord("protected"); return tk::Protected;
181 "public" L C yylval->word_ = new CYWord("public"); return tk::Public;
182 "short" L C yylval->word_ = new CYWord("short"); return tk::Short;
183 "static" L C yylval->word_ = new CYWord("static"); return tk::Static;
184 "super" L C yylval->word_ = new CYWord("super"); return tk::Super;
185 "synchronized" L C yylval->word_ = new CYWord("synchronized"); return tk::Synchronized;
186 "throws" L C yylval->word_ = new CYWord("throws"); return tk::Throws;
187 "transient" L C yylval->word_ = new CYWord("transient"); return tk::Transient;
188 "volatile" L C yylval->word_ = new CYWord("volatile"); return tk::Volatile;
189
190 [a-zA-Z$_][a-zA-Z$_0-9]* yylval->identifier_ = new CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier;
191
192 (\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? yylval->number_ = new CYNumber(strtod(yytext, NULL)); L C return tk::NumericLiteral;
193
194 0[xX][0-9a-fA-F]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
195
196 0[bB][0-1]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;
197
198 \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
199 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
200 char *local(value);
201
202 for (int i(1); i != yyleng - 1; ++i) {
203 char next(yytext[i]);
204
205 if (yytext[i] == '\\')
206 switch (next = yytext[++i]) {
207 case '\n': continue;
208 case '\\': next = '\\'; break;
209 case '\'': next = '\''; break;
210 case '"': next = '"'; break;
211 case 'b': next = '\b'; break;
212 case 'f': next = '\f'; break;
213 case 'n': next = '\n'; break;
214 case 'r': next = '\r'; break;
215 case 't': next = '\t'; break;
216 case 'v': next = '\v'; break;
217 case '0': next = '\0'; break;
218
219 case 'x':
220 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
221 i += 2;
222 break;
223 }
224
225 *local++ = next;
226 }
227
228 *local = '\0';
229 yylval->string_ = new CYString(value, local - value);
230 return tk::StringLiteral;
231 }
232
233 \n yylloc->end.lines(); yylloc->step(); N
234
235 [ \t] L
236 <<EOF>> L yyterminate();
237
238 . L {
239 CYDriver::Error error;
240 error.location_ = *yylloc;
241 error.message_ = "syntax error, unknown token";
242 yyextra->errors_.push_back(error);
243 yyterminate();
244 }
245
246 %%
247
248 void CYDriver::ScannerInit() {
249 cylex_init(&scanner_);
250 cyset_extra(this, scanner_);
251 }
252
253 void CYDriver::ScannerDestroy() {
254 cylex_destroy(scanner_);
255 }