]>
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 | "@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 | "abstract" L C yylval->word_ = new CYWord("abstract"); return tk::Abstract; | |
154 | "boolean" L C yylval->word_ = new CYWord("boolean"); return tk::Boolean; | |
155 | "byte" L C yylval->word_ = new CYWord("byte"); return tk::Byte; | |
156 | "char" L C yylval->word_ = new CYWord("char"); return tk::Char; | |
157 | "class" L C yylval->word_ = new CYWord("class"); return tk::Class; | |
158 | "const" L C yylval->word_ = new CYWord("const"); return tk::Const; | |
159 | "debugger" L C yylval->word_ = new CYWord("debugger"); return tk::Debugger; | |
160 | "double" L C yylval->word_ = new CYWord("double"); return tk::Double; | |
161 | "enum" L C yylval->word_ = new CYWord("enum"); return tk::Enum; | |
162 | "export" L C yylval->word_ = new CYWord("export"); return tk::Export; | |
163 | "extends" L C yylval->word_ = new CYWord("extends"); return tk::Extends; | |
164 | "final" L C yylval->word_ = new CYWord("final"); return tk::Final; | |
165 | "float" L C yylval->word_ = new CYWord("float"); return tk::Float; | |
166 | "goto" L C yylval->word_ = new CYWord("goto"); return tk::Goto; | |
167 | "implements" L C yylval->word_ = new CYWord("implements"); return tk::Implements; | |
168 | "import" L C yylval->word_ = new CYWord("import"); return tk::Import; | |
169 | "int" L C yylval->word_ = new CYWord("int"); return tk::Int; | |
170 | "interface" L C yylval->word_ = new CYWord("interface"); return tk::Interface; | |
171 | "long" L C yylval->word_ = new CYWord("long"); return tk::Long; | |
172 | "native" L C yylval->word_ = new CYWord("native"); return tk::Native; | |
173 | "package" L C yylval->word_ = new CYWord("package"); return tk::Package; | |
174 | "private" L C yylval->word_ = new CYWord("private"); return tk::Private; | |
175 | "protected" L C yylval->word_ = new CYWord("protected"); return tk::Protected; | |
176 | "public" L C yylval->word_ = new CYWord("public"); return tk::Public; | |
177 | "short" L C yylval->word_ = new CYWord("short"); return tk::Short; | |
178 | "static" L C yylval->word_ = new CYWord("static"); return tk::Static; | |
179 | "super" L C yylval->word_ = new CYWord("super"); return tk::Super; | |
180 | "synchronized" L C yylval->word_ = new CYWord("synchronized"); return tk::Synchronized; | |
181 | "throws" L C yylval->word_ = new CYWord("throws"); return tk::Throws; | |
182 | "transient" L C yylval->word_ = new CYWord("transient"); return tk::Transient; | |
183 | "volatile" L C yylval->word_ = new CYWord("volatile"); return tk::Volatile; | |
184 | ||
185 | [a-zA-Z$_][a-zA-Z$_0-9]* yylval->identifier_ = new CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier; | |
186 | ||
187 | (\.[0-9]+|(0|[1-9][0-9]*)(\.[0-9]*)?){Exponent}? yylval->number_ = new CYNumber(strtod(yytext, NULL)); L C return tk::NumericLiteral; | |
188 | ||
189 | 0[xX][0-9a-fA-F]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral; | |
190 | ||
191 | 0[bB][0-1]+ L C yylval->number_ = new CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral; | |
192 | ||
193 | \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C { | |
194 | char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng))); | |
195 | char *local(value); | |
196 | ||
197 | for (int i(1); i != yyleng - 1; ++i) { | |
198 | char next(yytext[i]); | |
199 | ||
200 | if (yytext[i] == '\\') | |
201 | switch (next = yytext[++i]) { | |
202 | case '\\': next = '\\'; break; | |
203 | case '\'': next = '\''; break; | |
204 | case '"': next = '"'; break; | |
205 | case 'b': next = '\b'; break; | |
206 | case 'f': next = '\f'; break; | |
207 | case 'n': next = '\n'; break; | |
208 | case 'r': next = '\r'; break; | |
209 | case 't': next = '\t'; break; | |
210 | case 'v': next = '\v'; break; | |
211 | case '0': next = '\0'; break; | |
212 | ||
213 | case 'x': | |
214 | next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]); | |
215 | i += 2; | |
216 | break; | |
217 | } | |
218 | ||
219 | *local++ = next; | |
220 | } | |
221 | ||
222 | *local = '\0'; | |
223 | yylval->string_ = new CYString(value, local - value); | |
224 | return tk::StringLiteral; | |
225 | } | |
226 | ||
227 | \n yylloc->end.lines(); yylloc->step(); N | |
228 | ||
229 | [ \t] L | |
230 | <<EOF>> L yyterminate(); | |
231 | ||
232 | . { | |
233 | CYDriver::Error error; | |
234 | error.location_ = *yylloc; | |
235 | error.message_ = "syntax error, unknown token"; | |
236 | yyextra->errors_.push_back(error); | |
237 | yyterminate(); | |
238 | } | |
239 | ||
240 | %% | |
241 | ||
242 | void CYDriver::ScannerInit() { | |
243 | cylex_init(&scanner_); | |
244 | cyset_extra(this, scanner_); | |
245 | } | |
246 | ||
247 | void CYDriver::ScannerDestroy() { | |
248 | cylex_destroy(scanner_); | |
249 | } |