2 #define YYLTYPE cy::location
3 #include "Cycript.tab.hh"
4 typedef cy::parser::token tk;
6 #define YY_EXTRA_TYPE CYDriver *
8 #define T yylval->newline_ = yyextra->state_ == CYNewLine;
9 #define C T yyextra->state_ = CYClear;
10 #define R T yyextra->state_ = CYRestricted;
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; \
22 if (yyextra->state_ != CYNewLine) { \
23 bool restricted(yyextra->state_ == CYRestricted); \
25 yyextra->state_ = CYClear; \
28 yyextra->state_ = CYNewLine; \
33 yylloc->columns(yyleng); \
37 if (c >= '0' && c <= '9')
39 if (c >= 'a' && c <= 'f')
41 if (c >= 'A' && c <= 'F')
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) \
53 size_t copy(std::min(size, yyextra->size_)); \
54 memcpy(data, yyextra->data_, copy); \
55 yyextra->data_ += copy; \
56 yyextra->size_ -= copy; \
65 %option bison-locations
72 Exponent [eE][+-]?[0-9]+
73 Escape \\[\\'"bfnrtv]|\\0|\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}|\\\n
75 IdentifierStart [a-zA-Z$_]
76 IdentifierPart [a-zA-Z$_0-9]
79 BackslashSequence \\{NonTerminator}
80 RegularExpressionFirstChar [^\n*\\/]|{BackslashSequence}
81 RegularExpressionChar [^\n\\/]|{BackslashSequence}
82 RegularExpressionFlags {IdentifierPart}*
83 RegularExpressionChars {RegularExpressionChar}*
84 RegularExpressionBody_ {RegularExpressionChars}
85 RegularExpressionBody {RegularExpressionFirstChar}{RegularExpressionBody_}
87 RegularExpressionEnd_ \/{RegularExpressionFlags}
88 RegularExpressionRest_ {RegularExpressionBody_}{RegularExpressionEnd_}
89 RegularExpressionStart_ {RegularExpressionBody}{RegularExpressionEnd_}
96 <res>{RegularExpressionStart_} E("/")
97 <rer>{RegularExpressionRest_} E("/=")
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
104 if (const char *nl = reinterpret_cast<const char *>(memchr(yytext, '\n', yyleng))) {
110 left = yyleng - (nl - yytext) - 1;
111 nl = reinterpret_cast<const char *>(memchr(nl + 1, '\n', left));
112 } while (nl != NULL);
114 yylloc->end.lines(lines);
115 yylloc->end.columns(left);
122 "@" L C return tk::At;
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;
164 ":" L C return tk::Colon;
165 "," L C return tk::Comma;
166 "?" L C return tk::Question;
167 ";" L C return tk::SemiColon;
169 "(" L C return tk::OpenParen;
170 ")" L C return tk::CloseParen;
172 "{" L C return tk::OpenBrace;
173 "}" L C return tk::CloseBrace;
175 "[" L C return tk::OpenBracket;
176 "]" L C return tk::CloseBracket;
178 "@class" L C return tk::AtClass;
179 "@end" L C return tk::AtEnd;
180 "@selector" L C return tk::AtSelector;
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;
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;
212 "debugger" L C yylval->word_ = new(yyextra->pool_) CYWord("debugger"); return tk::Debugger;
214 "const" L C yylval->word_ = new(yyextra->pool_) CYWord("const"); return tk::Const;
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;
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;
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;
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;
251 "each" L C yylval->identifier_ = new(yyextra->pool_) CYIdentifier("each"); return tk::Each;
253 {IdentifierStart}{IdentifierPart}* yylval->identifier_ = new(yyextra->pool_) CYIdentifier(apr_pstrmemdup(yyextra->pool_, yytext, yyleng)); L C return tk::Identifier_;
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;
257 0[xX][0-9a-fA-F]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 16)); return tk::NumericLiteral;
259 0[bB][0-1]+ L C yylval->number_ = new(yyextra->pool_) CYNumber(strtoull(yytext + 2, NULL, 2)); return tk::NumericLiteral;
261 \"([^"\\\n]|{Escape})*\"|'([^'\\\n]|{Escape})*' L C {
262 char *value(reinterpret_cast<char *>(apr_palloc(yyextra->pool_, yyleng)));
265 for (int i(1); i != yyleng - 1; ++i) {
266 char next(yytext[i]);
268 if (yytext[i] == '\\')
269 switch (next = yytext[++i]) {
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;
283 next = H(yytext[i + 1]) << 4 | H(yytext[i + 2]);
292 yylval->string_ = new(yyextra->pool_) CYString(value, local - value);
293 return tk::StringLiteral;
296 \r?\n yylloc->end.lines(); yylloc->step(); N
299 <<EOF>> L yyterminate();
302 CYDriver::Error error;
303 error.location_ = *yylloc;
304 error.message_ = "syntax error, unknown token";
305 yyextra->errors_.push_back(error);
311 void CYDriver::ScannerInit() {
312 cylex_init(&scanner_);
313 cyset_extra(this, scanner_);
316 void CYDriver::ScannerDestroy() {
317 cylex_destroy(scanner_);
320 void CYDriver::SetCondition(Condition condition) {
321 struct yyguts_t *yyg(reinterpret_cast<struct yyguts_t *>(scanner_));