1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
36 #include "location.hh"
39 #include "Pooling.hpp"
40 #include "Options.hpp"
48 virtual void Output(struct CYOutput &out) const = 0;
66 CYOutput(std::ostream &out, CYOptions &options) :
76 void Check(char value);
79 CYOutput &operator <<(char rhs);
80 CYOutput &operator <<(const char *rhs);
82 _finline CYOutput &operator <<(const CYThing *rhs) {
88 _finline CYOutput &operator <<(const CYThing &rhs) {
94 struct CYPropertyName {
95 virtual void PropertyName(CYOutput &out) const = 0;
97 virtual ~CYPropertyName() {
112 CYNoBrace = (1 << 0),
113 CYNoFunction = (1 << 1),
116 CYNoRightHand = (1 << 4),
117 CYNoDangle = (1 << 5),
118 CYNoInteger = (1 << 6),
119 CYNoBF = (CYNoBrace | CYNoFunction),
122 _finline CYFlags operator ~(CYFlags rhs) {
123 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
126 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
127 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
130 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
131 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
134 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
135 return lhs = lhs | rhs;
138 _finline CYFlags CYLeft(CYFlags flags) {
139 return flags & ~(CYNoDangle | CYNoInteger);
142 _finline CYFlags CYRight(CYFlags flags) {
143 return flags & ~CYNoBF;
146 _finline CYFlags CYCenter(CYFlags flags) {
147 return CYLeft(CYRight(flags));
153 virtual ~CYStatement() {
156 void Single(CYOutput &out, CYFlags flags) const;
157 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
159 virtual CYStatement *Replace(CYContext &context) = 0;
162 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
165 struct CYStatements {
175 operator CYStatement *() const {
179 CYStatements &operator ->*(CYStatement *next) {
181 if (first_ == NULL) {
184 } else for (;; last_ = last_->next_)
185 if (last_->next_ == NULL) {
195 virtual ~CYClassName() {
198 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
199 virtual void ClassName(CYOutput &out, bool object) const = 0;
209 CYWord(const char *word) :
214 void Set(const char *value) {
218 virtual const char *Word() const;
219 virtual void Output(CYOutput &out) const;
221 virtual CYExpression *ClassName(CYContext &context, bool object);
222 virtual void ClassName(CYOutput &out, bool object) const;
223 virtual void PropertyName(CYOutput &out) const;
226 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
228 return lhs << rhs.Word();
231 struct CYIdentifier :
232 CYNext<CYIdentifier>,
235 CYIdentifier *replace_;
239 CYIdentifier(const char *word) :
247 virtual const char *Word() const;
248 CYIdentifier *Replace(CYContext &context);
256 CYComment(const char *value) :
261 virtual CYStatement *Replace(CYContext &context);
262 virtual void Output(CYOutput &out, CYFlags flags) const;
269 CYStatement *statement_;
271 CYLabel(CYIdentifier *name, CYStatement *statement) :
273 statement_(statement)
277 virtual CYStatement *Replace(CYContext &context);
278 virtual void Output(CYOutput &out, CYFlags flags) const;
281 struct CYCStringLess :
282 std::binary_function<const char *, const char *, bool>
284 _finline bool operator ()(const char *lhs, const char *rhs) const {
285 return strcmp(lhs, rhs) < 0;
289 struct CYIdentifierValueLess :
290 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
292 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
293 return CYCStringLess()(lhs->Word(), rhs->Word());
297 enum CYIdentifierFlags {
298 CYIdentifierArgument,
299 CYIdentifierVariable,
305 typedef std::set<const char *, CYCStringLess> CYCStringSet;
306 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
307 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
309 struct CYIdentifierUsage {
310 CYIdentifier *identifier_;
314 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
320 CYStatement *&statements_;
324 CYIdentifierAddressFlagsMap internal_;
325 CYIdentifierValueSet identifiers_;
327 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
332 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
333 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
334 void Merge(CYContext &context, CYIdentifier *identifier);
335 void Scope(CYContext &context, CYStatement *&statements);
341 CYStatement *statements_;
343 CYProgram(CYStatement *statements) :
344 statements_(statements)
348 virtual void Replace(CYContext &context);
349 virtual void Output(CYOutput &out) const;
358 CYIdentifierUsageVector rename_;
360 CYNonLocal *nonlocal_;
361 CYNonLocal *nextlocal_;
364 CYContext(CYOptions &options) :
373 virtual ~CYContext() {
376 template <typename Type_>
377 void ReplaceAll(Type_ *&values) {
378 Type_ **last(&values);
379 CYForEach (next, values) {
380 Replace(*last = next);
382 last = &(*last)->next_;
386 template <typename Type_>
387 void Replace(Type_ *&value) {
388 for (;;) if (value == NULL)
391 Type_ *replace(value->Replace(*this));
392 if (replace != value)
398 void NonLocal(CYStatement *&statements);
399 CYIdentifier *Unique();
403 CYIdentifier *identifier_;
410 CYIdentifier *Target(CYContext &context) {
411 if (identifier_ == NULL)
412 identifier_ = context.Unique();
421 CYStatement *statements_;
423 CYBlock(CYStatement *statements) :
424 statements_(statements)
428 operator CYStatement *() const {
432 void AddPrev(CYStatement *statement) {
433 CYSetLast(statement, statements_);
434 statements_ = statement;
437 virtual CYStatement *Replace(CYContext &context);
439 virtual void Output(CYOutput &out) const;
440 virtual void Output(CYOutput &out, CYFlags flags) const;
455 std::stack<bool> in_;
469 std::string filename_;
473 cy::location location_;
474 std::string message_;
477 typedef std::vector<Error> Errors;
485 CYExpression *context_;
487 Context(CYExpression *context) :
492 typedef std::vector<CYWord *> Words;
496 typedef std::vector<Context> Contexts;
499 CYExpression *context_;
511 void ScannerDestroy();
514 CYDriver(const std::string &filename = "");
517 Condition GetCondition();
518 void SetCondition(Condition condition);
520 void PushCondition(Condition condition);
523 void Warning(const cy::location &location, const char *message);
526 struct CYForInitialiser {
527 virtual ~CYForInitialiser() {
530 virtual CYExpression *Replace(CYContext &context) = 0;
531 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
534 struct CYForInInitialiser {
535 virtual ~CYForInInitialiser() {
538 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
539 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
541 virtual CYExpression *Replace(CYContext &context) = 0;
542 virtual CYAssignment *Assignment(CYContext &context) = 0;
544 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
550 struct CYExpression :
551 CYNext<CYExpression>,
557 virtual unsigned Precedence() const = 0;
559 virtual bool RightHand() const {
563 virtual void ForIn(CYOutput &out, CYFlags flags) const;
564 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
566 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
568 virtual void Output(CYOutput &out) const;
569 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
570 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
572 virtual CYExpression *ClassName(CYContext &context, bool object);
573 virtual void ClassName(CYOutput &out, bool object) const;
575 virtual CYExpression *Replace(CYContext &context) = 0;
576 virtual CYAssignment *Assignment(CYContext &context);
578 virtual CYExpression *Primitive(CYContext &context) {
582 virtual CYNumber *Number(CYContext &context) {
586 virtual CYString *String(CYContext &context) {
590 virtual const char *Word() const {
595 #define CYAlphabetic(value) \
596 virtual bool Alphabetic() const { \
600 #define CYPrecedence(value) \
601 static const unsigned Precedence_ = value; \
602 virtual unsigned Precedence() const { \
603 return Precedence_; \
606 #define CYRightHand(value) \
607 virtual bool RightHand() const { \
614 CYExpression *expressions_;
616 CYCompound(CYExpression *expressions = NULL) :
617 expressions_(expressions)
621 void AddPrev(CYExpression *expression) {
622 CYSetLast(expression, expressions_);
623 expressions_ = expression;
628 virtual CYExpression *Replace(CYContext &context);
629 void Output(CYOutput &out, CYFlags flags) const;
632 struct CYDeclaration;
634 struct CYFunctionParameter :
635 CYNext<CYFunctionParameter>,
638 CYForInInitialiser *initialiser_;
640 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
641 CYNext<CYFunctionParameter>(next),
642 initialiser_(initialiser)
646 void Replace(CYContext &context, CYBlock &code);
647 void Output(CYOutput &out) const;
650 struct CYComprehension :
651 CYNext<CYComprehension>,
654 virtual const char *Name() const = 0;
656 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
657 CYFunctionParameter *Parameters(CYContext &context) const;
658 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
659 virtual void Output(CYOutput &out) const = 0;
662 struct CYForInComprehension :
668 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
674 virtual const char *Name() const {
675 return name_->Word();
678 virtual CYFunctionParameter *Parameter(CYContext &context) const;
679 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
680 virtual void Output(CYOutput &out) const;
683 struct CYForOfComprehension :
689 CYForOfComprehension(CYIdentifier *name, CYExpression *set) :
695 virtual const char *Name() const {
696 return name_->Word();
699 virtual CYFunctionParameter *Parameter(CYContext &context) const;
700 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
701 virtual void Output(CYOutput &out) const;
704 struct CYIfComprehension :
709 CYIfComprehension(CYExpression *test) :
714 virtual const char *Name() const {
718 virtual CYFunctionParameter *Parameter(CYContext &context) const;
719 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
720 virtual void Output(CYOutput &out) const;
723 struct CYArrayComprehension :
726 CYExpression *expression_;
727 CYComprehension *comprehensions_;
729 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
730 expression_(expression),
731 comprehensions_(comprehensions)
737 virtual CYExpression *Replace(CYContext &context);
738 virtual void Output(CYOutput &out, CYFlags flags) const;
751 virtual CYExpression *Replace(CYContext &context);
765 CYRange(uint64_t lo, uint64_t hi) :
770 bool operator [](uint8_t value) const {
771 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
774 void operator()(uint8_t value) {
777 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
781 extern CYRange DigitRange_;
782 extern CYRange WordStartRange_;
783 extern CYRange WordEndRange_;
798 CYString(const char *value) :
804 CYString(const char *value, size_t size) :
810 CYString(const CYWord *word) :
811 value_(word->Word()),
812 size_(strlen(value_))
816 const char *Value() const {
820 virtual const char *Word() const;
822 virtual CYNumber *Number(CYContext &context);
823 virtual CYString *String(CYContext &context);
825 CYString *Concat(CYContext &out, CYString *rhs) const;
826 virtual void Output(CYOutput &out, CYFlags flags) const;
827 virtual void PropertyName(CYOutput &out) const;
836 CYNumber(double value) :
841 double Value() const {
845 virtual CYNumber *Number(CYContext &context);
846 virtual CYString *String(CYContext &context);
848 virtual void Output(CYOutput &out, CYFlags flags) const;
849 virtual void PropertyName(CYOutput &out) const;
857 CYRegEx(const char *value) :
862 const char *Value() const {
866 virtual void Output(CYOutput &out, CYFlags flags) const;
878 virtual CYNumber *Number(CYContext &context);
879 virtual CYString *String(CYContext &context);
881 virtual void Output(CYOutput &out, CYFlags flags) const;
893 virtual CYExpression *Replace(CYContext &context);
894 virtual void Output(CYOutput &out, CYFlags flags) const;
900 virtual bool Value() const = 0;
901 virtual void Output(CYOutput &out, CYFlags flags) const;
913 virtual bool Value() const {
917 virtual CYNumber *Number(CYContext &context);
918 virtual CYString *String(CYContext &context);
930 virtual bool Value() const {
934 virtual CYNumber *Number(CYContext &context);
935 virtual CYString *String(CYContext &context);
943 CYVariable(CYIdentifier *name) :
948 CYVariable(const char *name) :
949 name_(new($pool) CYIdentifier(name))
956 virtual CYExpression *Replace(CYContext &context);
957 virtual void Output(CYOutput &out, CYFlags flags) const;
965 CYPrefix(CYExpression *rhs) :
970 virtual bool Alphabetic() const = 0;
971 virtual const char *Operator() const = 0;
975 virtual CYExpression *Replace(CYContext &context);
976 virtual void Output(CYOutput &out, CYFlags flags) const;
985 CYInfix(CYExpression *lhs, CYExpression *rhs) :
991 void SetLeft(CYExpression *lhs) {
995 virtual bool Alphabetic() const = 0;
996 virtual const char *Operator() const = 0;
998 virtual CYExpression *Replace(CYContext &context);
999 virtual void Output(CYOutput &out, CYFlags flags) const;
1007 CYPostfix(CYExpression *lhs) :
1012 virtual const char *Operator() const = 0;
1016 virtual CYExpression *Replace(CYContext &context);
1017 virtual void Output(CYOutput &out, CYFlags flags) const;
1020 struct CYAssignment :
1026 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1032 void SetLeft(CYExpression *lhs) {
1036 virtual const char *Operator() const = 0;
1040 virtual CYExpression *Replace(CYContext &context);
1041 virtual void Output(CYOutput &out, CYFlags flags) const;
1049 CYExpression *value_;
1051 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1052 CYNext<CYArgument>(next),
1058 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1059 CYNext<CYArgument>(next),
1065 CYArgument *Replace(CYContext &context);
1066 void Output(CYOutput &out) const;
1082 CYExpression *case_;
1083 CYStatement *statements_;
1085 CYClause(CYExpression *_case, CYStatement *statements) :
1087 statements_(statements)
1091 void Replace(CYContext &context);
1092 virtual void Output(CYOutput &out) const;
1099 CYExpression *value_;
1101 CYElement(CYExpression *value, CYElement *next) :
1102 CYNext<CYElement>(next),
1107 void Replace(CYContext &context);
1108 void Output(CYOutput &out) const;
1114 CYElement *elements_;
1116 CYArray(CYElement *elements = NULL) :
1121 virtual CYExpression *Replace(CYContext &context);
1122 virtual void Output(CYOutput &out, CYFlags flags) const;
1129 CYPropertyName *name_;
1130 CYExpression *value_;
1132 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1133 CYNext<CYProperty>(next),
1139 void Replace(CYContext &context);
1140 virtual void Output(CYOutput &out) const;
1143 struct CYDeclaration :
1146 CYIdentifier *identifier_;
1147 CYExpression *initialiser_;
1149 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1150 identifier_(identifier),
1151 initialiser_(initialiser)
1155 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1156 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1158 virtual CYExpression *Replace(CYContext &context);
1160 virtual CYAssignment *Assignment(CYContext &context);
1161 CYVariable *Variable(CYContext &context);
1163 virtual void Output(CYOutput &out, CYFlags flags) const;
1166 struct CYDeclarations :
1167 CYNext<CYDeclarations>,
1170 CYDeclaration *declaration_;
1172 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1173 CYNext<CYDeclarations>(next),
1174 declaration_(declaration)
1178 void Replace(CYContext &context);
1180 CYCompound *Compound(CYContext &context);
1181 CYProperty *Property(CYContext &context);
1182 CYArgument *Argument(CYContext &context);
1183 CYFunctionParameter *Parameter(CYContext &context);
1185 virtual void Output(CYOutput &out) const;
1186 virtual void Output(CYOutput &out, CYFlags flags) const;
1189 struct CYForDeclarations :
1192 CYDeclarations *declarations_;
1194 CYForDeclarations(CYDeclarations *declarations) :
1195 declarations_(declarations)
1199 virtual CYCompound *Replace(CYContext &context);
1200 virtual void Output(CYOutput &out, CYFlags flags) const;
1206 CYDeclarations *declarations_;
1208 CYVar(CYDeclarations *declarations) :
1209 declarations_(declarations)
1213 virtual CYStatement *Replace(CYContext &context);
1214 virtual void Output(CYOutput &out, CYFlags flags) const;
1217 struct CYLetStatement :
1220 CYDeclarations *declarations_;
1223 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1224 declarations_(declarations),
1229 virtual CYStatement *Replace(CYContext &context);
1230 virtual void Output(CYOutput &out, CYFlags flags) const;
1236 CYForInitialiser *initialiser_;
1237 CYExpression *test_;
1238 CYExpression *increment_;
1241 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1242 initialiser_(initialiser),
1244 increment_(increment),
1249 virtual CYStatement *Replace(CYContext &context);
1250 virtual void Output(CYOutput &out, CYFlags flags) const;
1256 CYForInInitialiser *initialiser_;
1260 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1261 initialiser_(initialiser),
1267 virtual CYStatement *Replace(CYContext &context);
1268 virtual void Output(CYOutput &out, CYFlags flags) const;
1274 CYForInInitialiser *initialiser_;
1278 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1279 initialiser_(initialiser),
1285 virtual CYStatement *Replace(CYContext &context);
1286 virtual void Output(CYOutput &out, CYFlags flags) const;
1292 CYProperty *properties_;
1294 CYObject(CYProperty *properties = NULL) :
1295 properties_(properties)
1299 virtual CYExpression *Replace(CYContext &context);
1300 void Output(CYOutput &out, CYFlags flags) const;
1306 CYExpression *object_;
1307 CYExpression *property_;
1309 CYMember(CYExpression *object, CYExpression *property) :
1315 void SetLeft(CYExpression *object) {
1320 struct CYDirectMember :
1323 CYDirectMember(CYExpression *object, CYExpression *property) :
1324 CYMember(object, property)
1331 virtual CYExpression *Replace(CYContext &context);
1332 virtual void Output(CYOutput &out, CYFlags flags) const;
1335 struct CYIndirectMember :
1338 CYIndirectMember(CYExpression *object, CYExpression *property) :
1339 CYMember(object, property)
1346 virtual CYExpression *Replace(CYContext &context);
1347 virtual void Output(CYOutput &out, CYFlags flags) const;
1356 CYExpression *constructor_;
1357 CYArgument *arguments_;
1359 New(CYExpression *constructor, CYArgument *arguments) :
1360 constructor_(constructor),
1361 arguments_(arguments)
1365 virtual unsigned Precedence() const {
1366 return arguments_ == NULL ? 2 : 1;
1371 virtual CYExpression *Replace(CYContext &context);
1372 virtual void Output(CYOutput &out, CYFlags flags) const;
1374 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1382 CYExpression *function_;
1383 CYArgument *arguments_;
1385 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1386 function_(function),
1387 arguments_(arguments)
1394 virtual CYExpression *Replace(CYContext &context);
1395 virtual void Output(CYOutput &out, CYFlags flags) const;
1397 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1402 struct CYRubyBlock :
1405 CYExpression *call_;
1408 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1417 virtual CYExpression *Replace(CYContext &context);
1418 virtual void Output(CYOutput &out, CYFlags flags) const;
1424 CYExpression *test_;
1426 CYStatement *false_;
1428 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1435 virtual CYStatement *Replace(CYContext &context);
1436 virtual void Output(CYOutput &out, CYFlags flags) const;
1442 CYExpression *test_;
1445 CYDoWhile(CYExpression *test, CYStatement *code) :
1451 virtual CYStatement *Replace(CYContext &context);
1452 virtual void Output(CYOutput &out, CYFlags flags) const;
1458 CYExpression *test_;
1461 CYWhile(CYExpression *test, CYStatement *code) :
1467 virtual CYStatement *Replace(CYContext &context);
1468 virtual void Output(CYOutput &out, CYFlags flags) const;
1471 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1473 CYIdentifier *name_;
1474 CYFunctionParameter *parameters_;
1476 CYNonLocal *nonlocal_;
1478 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1480 parameters_(parameters),
1486 virtual ~CYFunction() {
1489 void Inject(CYContext &context);
1490 virtual void Replace_(CYContext &context, bool outer);
1491 virtual void Output(CYOutput &out, CYFlags flags) const;
1494 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1495 struct CYFunctionExpression :
1499 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1500 CYFunction(name, parameters, statements)
1507 virtual CYExpression *Replace(CYContext &context);
1508 virtual void Output(CYOutput &out, CYFlags flags) const;
1511 // XXX: this should derive from CYAnonymousFunctionExpression
1513 CYFunctionExpression
1515 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1516 CYFunctionExpression(NULL, parameters, statements)
1520 virtual CYExpression *Replace(CYContext &context);
1521 virtual void Output(CYOutput &out, CYFlags flags) const;
1524 // XXX: this should derive from CYNamedFunction
1525 struct CYFunctionStatement :
1529 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1530 CYFunction(name, parameters, statements)
1534 virtual CYStatement *Replace(CYContext &context);
1535 virtual void Output(CYOutput &out, CYFlags flags) const;
1541 CYExpression *expression_;
1543 CYExpress(CYExpression *expression) :
1544 expression_(expression)
1546 if (expression == NULL)
1550 virtual CYStatement *Replace(CYContext &context);
1551 virtual void Output(CYOutput &out, CYFlags flags) const;
1557 CYIdentifier *label_;
1559 CYContinue(CYIdentifier *label) :
1564 virtual CYStatement *Replace(CYContext &context);
1565 virtual void Output(CYOutput &out, CYFlags flags) const;
1571 CYIdentifier *label_;
1573 CYBreak(CYIdentifier *label) :
1578 virtual CYStatement *Replace(CYContext &context);
1579 virtual void Output(CYOutput &out, CYFlags flags) const;
1585 CYExpression *value_;
1587 CYReturn(CYExpression *value) :
1592 virtual CYStatement *Replace(CYContext &context);
1593 virtual void Output(CYOutput &out, CYFlags flags) const;
1599 virtual CYStatement *Replace(CYContext &context);
1600 virtual void Output(CYOutput &out, CYFlags flags) const;
1608 CYFinally(CYStatement *statements) :
1613 void Replace(CYContext &context);
1614 virtual void Output(CYOutput &out) const;
1623 CYIdentifier *name_;
1626 Catch(CYIdentifier *name, CYStatement *statements) :
1632 void Replace(CYContext &context);
1633 virtual void Output(CYOutput &out) const;
1641 CYFinally *finally_;
1643 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1650 virtual CYStatement *Replace(CYContext &context);
1651 virtual void Output(CYOutput &out, CYFlags flags) const;
1657 CYExpression *value_;
1659 Throw(CYExpression *value = NULL) :
1664 virtual CYStatement *Replace(CYContext &context);
1665 virtual void Output(CYOutput &out, CYFlags flags) const;
1673 CYExpression *scope_;
1676 CYWith(CYExpression *scope, CYStatement *code) :
1682 virtual CYStatement *Replace(CYContext &context);
1683 virtual void Output(CYOutput &out, CYFlags flags) const;
1689 CYExpression *value_;
1692 CYSwitch(CYExpression *value, CYClause *clauses) :
1698 virtual CYStatement *Replace(CYContext &context);
1699 virtual void Output(CYOutput &out, CYFlags flags) const;
1709 virtual CYStatement *Replace(CYContext &context);
1710 virtual void Output(CYOutput &out, CYFlags flags) const;
1713 struct CYCondition :
1716 CYExpression *test_;
1717 CYExpression *true_;
1718 CYExpression *false_;
1720 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1729 virtual CYExpression *Replace(CYContext &context);
1730 virtual void Output(CYOutput &out, CYFlags flags) const;
1733 struct CYAddressOf :
1736 CYAddressOf(CYExpression *rhs) :
1741 virtual const char *Operator() const {
1747 virtual CYExpression *Replace(CYContext &context);
1753 CYIndirect(CYExpression *rhs) :
1758 virtual const char *Operator() const {
1764 virtual CYExpression *Replace(CYContext &context);
1768 virtual CYExpression *Replace(CYContext &context);
1770 #define CYPostfix_(op, name, args...) \
1771 struct CY ## name : \
1774 CY ## name(CYExpression *lhs) : \
1779 virtual const char *Operator() const { \
1784 #define CYPrefix_(alphabetic, op, name, args...) \
1785 struct CY ## name : \
1788 CY ## name(CYExpression *rhs) : \
1793 CYAlphabetic(alphabetic) \
1795 virtual const char *Operator() const { \
1800 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1801 struct CY ## name : \
1804 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1809 CYAlphabetic(alphabetic) \
1810 CYPrecedence(precedence) \
1812 virtual const char *Operator() const { \
1817 #define CYAssignment_(op, name, args...) \
1818 struct CY ## name ## Assign : \
1821 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1822 CYAssignment(lhs, rhs) \
1826 virtual const char *Operator() const { \
1831 CYPostfix_("++", PostIncrement)
1832 CYPostfix_("--", PostDecrement)
1834 CYPrefix_(true, "delete", Delete)
1835 CYPrefix_(true, "void", Void)
1836 CYPrefix_(true, "typeof", TypeOf)
1837 CYPrefix_(false, "++", PreIncrement)
1838 CYPrefix_(false, "--", PreDecrement)
1839 CYPrefix_(false, "+", Affirm)
1840 CYPrefix_(false, "-", Negate)
1841 CYPrefix_(false, "~", BitwiseNot)
1842 CYPrefix_(false, "!", LogicalNot)
1844 CYInfix_(false, 5, "*", Multiply)
1845 CYInfix_(false, 5, "/", Divide)
1846 CYInfix_(false, 5, "%", Modulus)
1847 CYInfix_(false, 6, "+", Add, CYReplace)
1848 CYInfix_(false, 6, "-", Subtract)
1849 CYInfix_(false, 7, "<<", ShiftLeft)
1850 CYInfix_(false, 7, ">>", ShiftRightSigned)
1851 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1852 CYInfix_(false, 8, "<", Less)
1853 CYInfix_(false, 8, ">", Greater)
1854 CYInfix_(false, 8, "<=", LessOrEqual)
1855 CYInfix_(false, 8, ">=", GreaterOrEqual)
1856 CYInfix_(true, 8, "instanceof", InstanceOf)
1857 CYInfix_(true, 8, "in", In)
1858 CYInfix_(false, 9, "==", Equal)
1859 CYInfix_(false, 9, "!=", NotEqual)
1860 CYInfix_(false, 9, "===", Identical)
1861 CYInfix_(false, 9, "!==", NotIdentical)
1862 CYInfix_(false, 10, "&", BitwiseAnd)
1863 CYInfix_(false, 11, "^", BitwiseXOr)
1864 CYInfix_(false, 12, "|", BitwiseOr)
1865 CYInfix_(false, 13, "&&", LogicalAnd)
1866 CYInfix_(false, 14, "||", LogicalOr)
1868 CYAssignment_("=", )
1869 CYAssignment_("*=", Multiply)
1870 CYAssignment_("/=", Divide)
1871 CYAssignment_("%=", Modulus)
1872 CYAssignment_("+=", Add)
1873 CYAssignment_("-=", Subtract)
1874 CYAssignment_("<<=", ShiftLeft)
1875 CYAssignment_(">>=", ShiftRightSigned)
1876 CYAssignment_(">>>=", ShiftRightUnsigned)
1877 CYAssignment_("&=", BitwiseAnd)
1878 CYAssignment_("^=", BitwiseXOr)
1879 CYAssignment_("|=", BitwiseOr)
1881 #endif/*CYCRIPT_PARSER_HPP*/