]>
Commit | Line | Data |
---|---|---|
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->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 | ||
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 | |
58 | %option reentrant | |
59 | ||
60 | Exponent [eE][+-]?[0-9]+ | |
61 | Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4} | |
62 | ||
63 | %% | |
64 | ||
65 | \/\/[^\n]* ; | |
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 | ||
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 | "@class" L C return tk::AtClass; | |
123 | "@end" L C return tk::AtEnd; | |
124 | "@selector" L C return tk::AtSelector; | |
125 | ||
126 | "break" L R yylval->word_ = new CYWord("break"); return tk::Break; | |
127 | "case" L C yylval->word_ = new CYWord("case"); return tk::Case; | |
128 | "catch" L C yylval->word_ = new CYWord("catch"); return tk::Catch; | |
129 | "continue" L R yylval->word_ = new CYWord("continue"); return tk::Continue; | |
130 | "default" L C yylval->word_ = new CYWord("default"); return tk::Default; | |
131 | "delete" L C yylval->word_ = new CYWord("delete"); return tk::Delete; | |
132 | "do" L C yylval->word_ = new CYWord("do"); return tk::Do; | |
133 | "else" L C yylval->word_ = new CYWord("else"); return tk::Else; | |
134 | "false" L C yylval->false_ = new CYFalse(); return tk::False; | |
135 | "finally" L C yylval->word_ = new CYWord("finally"); return tk::Finally; | |
136 | "for" L C yylval->word_ = new CYWord("for"); return tk::For; | |
137 | "function" L C yylval->word_ = new CYWord("function"); return tk::Function; | |
138 | "if" L C yylval->word_ = new CYWord("if"); return tk::If; | |
139 | "in" L C yylval->word_ = new CYWord("in"); return tk::In; | |
140 | "instanceof" L C yylval->word_ = new CYWord("instanceof"); return tk::InstanceOf; | |
141 | "new" L C yylval->word_ = new CYWord("new"); return tk::New; | |
142 | "null" L C yylval->null_ = new CYNull(); return tk::Null; | |
143 | "return" L R yylval->word_ = new CYWord("return"); return tk::Return; | |
144 | "switch" L C yylval->word_ = new CYWord("switch"); return tk::Switch; | |
145 | "this" L C yylval->this_ = new CYThis(); return tk::This; | |
146 | "throw" L R yylval->word_ = new CYWord("throw"); return tk::Throw; | |
147 | "true" L C yylval->true_ = new CYTrue(); return tk::True; | |
148 | "try" L C yylval->word_ = new CYWord("try"); return tk::Try; | |
149 | "typeof" L C yylval->word_ = new CYWord("typeof"); return tk::TypeOf; | |
150 | "var" L C yylval->word_ = new CYWord("var"); return tk::Var; | |
151 | "void" L C yylval->word_ = new CYWord("void"); return tk::Void; | |
152 | "while" L C yylval->word_ = new CYWord("while"); return tk::While; | |
153 | "with" L C yylval->word_ = new CYWord("with"); return tk::With; | |
154 | ||
155 | "abstract" L C yylval->word_ = new CYWord("abstract"); return tk::Abstract; | |
156 | "boolean" L C yylval->word_ = new CYWord("boolean"); return tk::Boolean; | |
157 | "byte" L C yylval->word_ = new CYWord("byte"); return tk::Byte; | |
158 | "char" L C yylval->word_ = new CYWord("char"); return tk::Char; | |
159 | "class" L C yylval->word_ = new CYWord("class"); return tk::Class; | |
160 | "const" L C yylval->word_ = new CYWord("const"); return tk::Const; | |
161 | "debugger" L C yylval->word_ = new CYWord("debugger"); return tk::Debugger; | |
162 | "double" L C yylval->word_ = new CYWord("double"); return tk::Double; | |
163 | "enum" L C yylval->word_ = new CYWord("enum"); return tk::Enum; | |
164 | "export" L C yylval->word_ = new CYWord("export"); return tk::Export; | |
165 | "extends" L C yylval->word_ = new CYWord("extends"); return tk::Extends; | |
166 | "final" L C yylval->word_ = new CYWord("final"); return tk::Final; | |
167 | "float" L C yylval->word_ = new CYWord("float"); return tk::Float; | |
168 | "goto" L C yylval->word_ = new CYWord("goto"); return tk::Goto; | |
169 | "implements" L C yylval->word_ = new CYWord("implements"); return tk::Implements; | |
170 | "import" L C yylval->word_ = new CYWord("import"); return tk::Import; | |
171 | "int" L C yylval->word_ = new CYWord("int"); return tk::Int; | |
172 | "interface" L C yylval->word_ = new CYWord("interface"); return tk::Interface; | |
173 | "long" L C yylval->word_ = new CYWord("long"); return tk::Long; | |
174 | "native" L C yylval->word_ = new CYWord("native"); return tk::Native; | |
175 | "package" L C yylval->word_ = new CYWord("package"); return tk::Package; | |
176 | "private" L C yylval->word_ = new CYWord("private"); return tk::Private; | |
177 | "protected" L C yylval->word_ = new CYWord("protected"); return tk::Protected; | |
178 | "public" L C yylval->word_ = new CYWord("public"); return tk::Public; | |
179 | "short" L C yylval->word_ = new CYWord("short"); return tk::Short; | |
180 | "static" L C yylval->word_ = new CYWord("static"); return tk::Static; | |
181 | "super" L C yylval->word_ = new CYWord("super"); return tk::Super; | |
182 | "synchronized" L C yylval->word_ = new CYWord("synchronized"); return tk::Synchronized; | |
183 | "throws" L C yylval->word_ = new CYWord("throws"); return tk::Throws; | |
184 | "transient" L C yylval->word_ = new CYWord("transient"); return tk::Transient; | |
185 | "volatile" L C yylval->word_ = new CYWord("volatile"); return tk::Volatile; | |
186 | ||
187 | [a-zA-Z$_][a-zA-Z$_0-9]* yylval->identifier_ = new CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier; | |
188 | ||
189 | (\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? yylval->number_ = new CYNumber(strtod(yytext, NULL)); L C return tk::NumericLiteral; | |
190 | ||
191 | 0[xX][0-9a-fA-F]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral; | |
192 | ||
193 | 0[bB][0-1]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral; | |
194 | ||
195 | \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C { | |
196 | char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng))); | |
197 | char *local(value); | |
198 | ||
199 | for (int i(1); i != yyleng - 1; ++i) { | |
200 | char next(yytext[i]); | |
201 | ||
202 | if (yytext[i] == '\\') | |
203 | switch (next = yytext[++i]) { | |
204 | case '\\': next = '\\'; break; | |
205 | case '\'': next = '\''; break; | |
206 | case '"': next = '"'; break; | |
207 | case 'b': next = '\b'; break; | |
208 | case 'f': next = '\f'; break; | |
209 | case 'n': next = '\n'; break; | |
210 | case 'r': next = '\r'; break; | |
211 | case 't': next = '\t'; break; | |
212 | case 'v': next = '\v'; break; | |
213 | case '0': next = '\0'; break; | |
214 | ||
215 | case 'x': | |
216 | next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]); | |
217 | i += 2; | |
218 | break; | |
219 | } | |
220 | ||
221 | *local++ = next; | |
222 | } | |
223 | ||
224 | *local = '\0'; | |
225 | yylval->string_ = new CYString(value, local - value); | |
226 | return tk::StringLiteral; | |
227 | } | |
228 | ||
229 | \n yylloc->end.lines(); yylloc->step(); N | |
230 | ||
231 | [ \t] L | |
232 | <<EOF>> L yyterminate(); | |
233 | ||
234 | . { | |
235 | CYDriver::Error error; | |
236 | error.location_ = *yylloc; | |
237 | error.message_ = "syntax error, unknown token"; | |
238 | yyextra->errors_.push_back(error); | |
239 | yyterminate(); | |
240 | } | |
241 | ||
242 | %% | |
243 | ||
244 | void CYDriver::ScannerInit() { | |
245 | cylex_init(&scanner_); | |
246 | cyset_extra(this, scanner_); | |
247 | } | |
248 | ||
249 | void CYDriver::ScannerDestroy() { | |
250 | cylex_destroy(scanner_); | |
251 | } |