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;
454 std::stack<bool> in_;
468 std::string filename_;
472 cy::location location_;
473 std::string message_;
476 typedef std::vector<Error> Errors;
484 CYExpression *context_;
486 Context(CYExpression *context) :
491 typedef std::vector<CYWord *> Words;
495 typedef std::vector<Context> Contexts;
498 CYExpression *context_;
510 void ScannerDestroy();
513 CYDriver(const std::string &filename = "");
516 Condition GetCondition();
517 void SetCondition(Condition condition);
519 void PushCondition(Condition condition);
522 void Warning(const cy::location &location, const char *message);
525 struct CYForInitialiser {
526 virtual ~CYForInitialiser() {
529 virtual CYExpression *Replace(CYContext &context) = 0;
530 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
533 struct CYForInInitialiser {
534 virtual ~CYForInInitialiser() {
537 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
538 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
540 virtual CYExpression *Replace(CYContext &context) = 0;
541 virtual CYAssignment *Assignment(CYContext &context) = 0;
543 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
549 struct CYExpression :
550 CYNext<CYExpression>,
556 virtual unsigned Precedence() const = 0;
558 virtual bool RightHand() const {
562 virtual void ForIn(CYOutput &out, CYFlags flags) const;
563 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
565 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
567 virtual void Output(CYOutput &out) const;
568 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
569 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
571 virtual CYExpression *ClassName(CYContext &context, bool object);
572 virtual void ClassName(CYOutput &out, bool object) const;
574 virtual CYExpression *Replace(CYContext &context) = 0;
575 virtual CYAssignment *Assignment(CYContext &context);
577 virtual CYExpression *Primitive(CYContext &context) {
581 virtual CYNumber *Number(CYContext &context) {
585 virtual CYString *String(CYContext &context) {
589 virtual const char *Word() const {
594 #define CYAlphabetic(value) \
595 virtual bool Alphabetic() const { \
599 #define CYPrecedence(value) \
600 static const unsigned Precedence_ = value; \
601 virtual unsigned Precedence() const { \
602 return Precedence_; \
605 #define CYRightHand(value) \
606 virtual bool RightHand() const { \
613 CYExpression *expressions_;
615 CYCompound(CYExpression *expressions = NULL) :
616 expressions_(expressions)
620 void AddPrev(CYExpression *expression) {
621 CYSetLast(expression, expressions_);
622 expressions_ = expression;
627 virtual CYExpression *Replace(CYContext &context);
628 void Output(CYOutput &out, CYFlags flags) const;
631 struct CYDeclaration;
633 struct CYFunctionParameter :
634 CYNext<CYFunctionParameter>,
637 CYForInInitialiser *initialiser_;
639 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
640 CYNext<CYFunctionParameter>(next),
641 initialiser_(initialiser)
645 void Replace(CYContext &context, CYBlock &code);
646 void Output(CYOutput &out) const;
649 struct CYComprehension :
650 CYNext<CYComprehension>,
653 virtual const char *Name() const = 0;
655 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
656 CYFunctionParameter *Parameters(CYContext &context) const;
657 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
658 virtual void Output(CYOutput &out) const = 0;
661 struct CYForInComprehension :
667 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
673 virtual const char *Name() const {
674 return name_->Word();
677 virtual CYFunctionParameter *Parameter(CYContext &context) const;
678 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
679 virtual void Output(CYOutput &out) const;
682 struct CYForEachInComprehension :
688 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
694 virtual const char *Name() const {
695 return name_->Word();
698 virtual CYFunctionParameter *Parameter(CYContext &context) const;
699 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
700 virtual void Output(CYOutput &out) const;
703 struct CYIfComprehension :
708 CYIfComprehension(CYExpression *test) :
713 virtual const char *Name() const {
717 virtual CYFunctionParameter *Parameter(CYContext &context) const;
718 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
719 virtual void Output(CYOutput &out) const;
722 struct CYArrayComprehension :
725 CYExpression *expression_;
726 CYComprehension *comprehensions_;
728 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
729 expression_(expression),
730 comprehensions_(comprehensions)
736 virtual CYExpression *Replace(CYContext &context);
737 virtual void Output(CYOutput &out, CYFlags flags) const;
750 virtual CYExpression *Replace(CYContext &context);
764 CYRange(uint64_t lo, uint64_t hi) :
769 bool operator [](uint8_t value) const {
770 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
773 void operator()(uint8_t value) {
776 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
780 extern CYRange DigitRange_;
781 extern CYRange WordStartRange_;
782 extern CYRange WordEndRange_;
797 CYString(const char *value) :
803 CYString(const char *value, size_t size) :
809 CYString(const CYWord *word) :
810 value_(word->Word()),
811 size_(strlen(value_))
815 const char *Value() const {
819 virtual const char *Word() const;
821 virtual CYNumber *Number(CYContext &context);
822 virtual CYString *String(CYContext &context);
824 CYString *Concat(CYContext &out, CYString *rhs) const;
825 virtual void Output(CYOutput &out, CYFlags flags) const;
826 virtual void PropertyName(CYOutput &out) const;
835 CYNumber(double value) :
840 double Value() const {
844 virtual CYNumber *Number(CYContext &context);
845 virtual CYString *String(CYContext &context);
847 virtual void Output(CYOutput &out, CYFlags flags) const;
848 virtual void PropertyName(CYOutput &out) const;
856 CYRegEx(const char *value) :
861 const char *Value() const {
865 virtual void Output(CYOutput &out, CYFlags flags) const;
877 virtual CYNumber *Number(CYContext &context);
878 virtual CYString *String(CYContext &context);
880 virtual void Output(CYOutput &out, CYFlags flags) const;
892 virtual CYExpression *Replace(CYContext &context);
893 virtual void Output(CYOutput &out, CYFlags flags) const;
899 virtual bool Value() const = 0;
900 virtual void Output(CYOutput &out, CYFlags flags) const;
912 virtual bool Value() const {
916 virtual CYNumber *Number(CYContext &context);
917 virtual CYString *String(CYContext &context);
929 virtual bool Value() const {
933 virtual CYNumber *Number(CYContext &context);
934 virtual CYString *String(CYContext &context);
942 CYVariable(CYIdentifier *name) :
947 CYVariable(const char *name) :
948 name_(new($pool) CYIdentifier(name))
955 virtual CYExpression *Replace(CYContext &context);
956 virtual void Output(CYOutput &out, CYFlags flags) const;
964 CYPrefix(CYExpression *rhs) :
969 virtual bool Alphabetic() const = 0;
970 virtual const char *Operator() const = 0;
974 virtual CYExpression *Replace(CYContext &context);
975 virtual void Output(CYOutput &out, CYFlags flags) const;
984 CYInfix(CYExpression *lhs, CYExpression *rhs) :
990 void SetLeft(CYExpression *lhs) {
994 virtual bool Alphabetic() const = 0;
995 virtual const char *Operator() const = 0;
997 virtual CYExpression *Replace(CYContext &context);
998 virtual void Output(CYOutput &out, CYFlags flags) const;
1006 CYPostfix(CYExpression *lhs) :
1011 virtual const char *Operator() const = 0;
1015 virtual CYExpression *Replace(CYContext &context);
1016 virtual void Output(CYOutput &out, CYFlags flags) const;
1019 struct CYAssignment :
1025 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1031 void SetLeft(CYExpression *lhs) {
1035 virtual const char *Operator() const = 0;
1039 virtual CYExpression *Replace(CYContext &context);
1040 virtual void Output(CYOutput &out, CYFlags flags) const;
1048 CYExpression *value_;
1050 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1051 CYNext<CYArgument>(next),
1057 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1058 CYNext<CYArgument>(next),
1064 CYArgument *Replace(CYContext &context);
1065 void Output(CYOutput &out) const;
1081 CYExpression *case_;
1082 CYStatement *statements_;
1084 CYClause(CYExpression *_case, CYStatement *statements) :
1086 statements_(statements)
1090 void Replace(CYContext &context);
1091 virtual void Output(CYOutput &out) const;
1098 CYExpression *value_;
1100 CYElement(CYExpression *value, CYElement *next) :
1101 CYNext<CYElement>(next),
1106 void Replace(CYContext &context);
1107 void Output(CYOutput &out) const;
1113 CYElement *elements_;
1115 CYArray(CYElement *elements = NULL) :
1120 virtual CYExpression *Replace(CYContext &context);
1121 virtual void Output(CYOutput &out, CYFlags flags) const;
1128 CYPropertyName *name_;
1129 CYExpression *value_;
1131 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1132 CYNext<CYProperty>(next),
1138 void Replace(CYContext &context);
1139 virtual void Output(CYOutput &out) const;
1142 struct CYDeclaration :
1145 CYIdentifier *identifier_;
1146 CYExpression *initialiser_;
1148 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1149 identifier_(identifier),
1150 initialiser_(initialiser)
1154 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1155 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1157 virtual CYExpression *Replace(CYContext &context);
1159 virtual CYAssignment *Assignment(CYContext &context);
1160 CYVariable *Variable(CYContext &context);
1162 virtual void Output(CYOutput &out, CYFlags flags) const;
1165 struct CYDeclarations :
1166 CYNext<CYDeclarations>,
1169 CYDeclaration *declaration_;
1171 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1172 CYNext<CYDeclarations>(next),
1173 declaration_(declaration)
1177 void Replace(CYContext &context);
1179 CYCompound *Compound(CYContext &context);
1180 CYProperty *Property(CYContext &context);
1181 CYArgument *Argument(CYContext &context);
1182 CYFunctionParameter *Parameter(CYContext &context);
1184 virtual void Output(CYOutput &out) const;
1185 virtual void Output(CYOutput &out, CYFlags flags) const;
1188 struct CYForDeclarations :
1191 CYDeclarations *declarations_;
1193 CYForDeclarations(CYDeclarations *declarations) :
1194 declarations_(declarations)
1198 virtual CYCompound *Replace(CYContext &context);
1199 virtual void Output(CYOutput &out, CYFlags flags) const;
1205 CYDeclarations *declarations_;
1207 CYVar(CYDeclarations *declarations) :
1208 declarations_(declarations)
1212 virtual CYStatement *Replace(CYContext &context);
1213 virtual void Output(CYOutput &out, CYFlags flags) const;
1216 struct CYLetStatement :
1219 CYDeclarations *declarations_;
1222 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1223 declarations_(declarations),
1228 virtual CYStatement *Replace(CYContext &context);
1229 virtual void Output(CYOutput &out, CYFlags flags) const;
1235 CYForInitialiser *initialiser_;
1236 CYExpression *test_;
1237 CYExpression *increment_;
1240 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1241 initialiser_(initialiser),
1243 increment_(increment),
1248 virtual CYStatement *Replace(CYContext &context);
1249 virtual void Output(CYOutput &out, CYFlags flags) const;
1255 CYForInInitialiser *initialiser_;
1259 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1260 initialiser_(initialiser),
1266 virtual CYStatement *Replace(CYContext &context);
1267 virtual void Output(CYOutput &out, CYFlags flags) const;
1270 struct CYForEachIn :
1273 CYForInInitialiser *initialiser_;
1277 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1278 initialiser_(initialiser),
1284 virtual CYStatement *Replace(CYContext &context);
1285 virtual void Output(CYOutput &out, CYFlags flags) const;
1291 CYProperty *properties_;
1293 CYObject(CYProperty *properties = NULL) :
1294 properties_(properties)
1298 virtual CYExpression *Replace(CYContext &context);
1299 void Output(CYOutput &out, CYFlags flags) const;
1305 CYExpression *object_;
1306 CYExpression *property_;
1308 CYMember(CYExpression *object, CYExpression *property) :
1314 void SetLeft(CYExpression *object) {
1319 struct CYDirectMember :
1322 CYDirectMember(CYExpression *object, CYExpression *property) :
1323 CYMember(object, property)
1330 virtual CYExpression *Replace(CYContext &context);
1331 virtual void Output(CYOutput &out, CYFlags flags) const;
1334 struct CYIndirectMember :
1337 CYIndirectMember(CYExpression *object, CYExpression *property) :
1338 CYMember(object, property)
1345 virtual CYExpression *Replace(CYContext &context);
1346 virtual void Output(CYOutput &out, CYFlags flags) const;
1355 CYExpression *constructor_;
1356 CYArgument *arguments_;
1358 New(CYExpression *constructor, CYArgument *arguments) :
1359 constructor_(constructor),
1360 arguments_(arguments)
1364 virtual unsigned Precedence() const {
1365 return arguments_ == NULL ? 2 : 1;
1370 virtual CYExpression *Replace(CYContext &context);
1371 virtual void Output(CYOutput &out, CYFlags flags) const;
1373 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1381 CYExpression *function_;
1382 CYArgument *arguments_;
1384 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1385 function_(function),
1386 arguments_(arguments)
1393 virtual CYExpression *Replace(CYContext &context);
1394 virtual void Output(CYOutput &out, CYFlags flags) const;
1396 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1401 struct CYRubyBlock :
1404 CYExpression *call_;
1407 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1416 virtual CYExpression *Replace(CYContext &context);
1417 virtual void Output(CYOutput &out, CYFlags flags) const;
1423 CYExpression *test_;
1425 CYStatement *false_;
1427 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1434 virtual CYStatement *Replace(CYContext &context);
1435 virtual void Output(CYOutput &out, CYFlags flags) const;
1441 CYExpression *test_;
1444 CYDoWhile(CYExpression *test, CYStatement *code) :
1450 virtual CYStatement *Replace(CYContext &context);
1451 virtual void Output(CYOutput &out, CYFlags flags) const;
1457 CYExpression *test_;
1460 CYWhile(CYExpression *test, CYStatement *code) :
1466 virtual CYStatement *Replace(CYContext &context);
1467 virtual void Output(CYOutput &out, CYFlags flags) const;
1470 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1472 CYIdentifier *name_;
1473 CYFunctionParameter *parameters_;
1475 CYNonLocal *nonlocal_;
1477 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1479 parameters_(parameters),
1485 virtual ~CYFunction() {
1488 void Inject(CYContext &context);
1489 virtual void Replace_(CYContext &context, bool outer);
1490 virtual void Output(CYOutput &out, CYFlags flags) const;
1493 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1494 struct CYFunctionExpression :
1498 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1499 CYFunction(name, parameters, statements)
1506 virtual CYExpression *Replace(CYContext &context);
1507 virtual void Output(CYOutput &out, CYFlags flags) const;
1510 // XXX: this should derive from CYAnonymousFunctionExpression
1512 CYFunctionExpression
1514 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1515 CYFunctionExpression(NULL, parameters, statements)
1519 virtual CYExpression *Replace(CYContext &context);
1520 virtual void Output(CYOutput &out, CYFlags flags) const;
1523 // XXX: this should derive from CYNamedFunction
1524 struct CYFunctionStatement :
1528 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1529 CYFunction(name, parameters, statements)
1533 virtual CYStatement *Replace(CYContext &context);
1534 virtual void Output(CYOutput &out, CYFlags flags) const;
1540 CYExpression *expression_;
1542 CYExpress(CYExpression *expression) :
1543 expression_(expression)
1545 if (expression == NULL)
1549 virtual CYStatement *Replace(CYContext &context);
1550 virtual void Output(CYOutput &out, CYFlags flags) const;
1556 CYIdentifier *label_;
1558 CYContinue(CYIdentifier *label) :
1563 virtual CYStatement *Replace(CYContext &context);
1564 virtual void Output(CYOutput &out, CYFlags flags) const;
1570 CYIdentifier *label_;
1572 CYBreak(CYIdentifier *label) :
1577 virtual CYStatement *Replace(CYContext &context);
1578 virtual void Output(CYOutput &out, CYFlags flags) const;
1584 CYExpression *value_;
1586 CYReturn(CYExpression *value) :
1591 virtual CYStatement *Replace(CYContext &context);
1592 virtual void Output(CYOutput &out, CYFlags flags) const;
1598 virtual CYStatement *Replace(CYContext &context);
1599 virtual void Output(CYOutput &out, CYFlags flags) const;
1607 CYFinally(CYStatement *statements) :
1612 void Replace(CYContext &context);
1613 virtual void Output(CYOutput &out) const;
1622 CYIdentifier *name_;
1625 Catch(CYIdentifier *name, CYStatement *statements) :
1631 void Replace(CYContext &context);
1632 virtual void Output(CYOutput &out) const;
1640 CYFinally *finally_;
1642 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1649 virtual CYStatement *Replace(CYContext &context);
1650 virtual void Output(CYOutput &out, CYFlags flags) const;
1656 CYExpression *value_;
1658 Throw(CYExpression *value = NULL) :
1663 virtual CYStatement *Replace(CYContext &context);
1664 virtual void Output(CYOutput &out, CYFlags flags) const;
1672 CYExpression *scope_;
1675 CYWith(CYExpression *scope, CYStatement *code) :
1681 virtual CYStatement *Replace(CYContext &context);
1682 virtual void Output(CYOutput &out, CYFlags flags) const;
1688 CYExpression *value_;
1691 CYSwitch(CYExpression *value, CYClause *clauses) :
1697 virtual CYStatement *Replace(CYContext &context);
1698 virtual void Output(CYOutput &out, CYFlags flags) const;
1708 virtual CYStatement *Replace(CYContext &context);
1709 virtual void Output(CYOutput &out, CYFlags flags) const;
1712 struct CYCondition :
1715 CYExpression *test_;
1716 CYExpression *true_;
1717 CYExpression *false_;
1719 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1728 virtual CYExpression *Replace(CYContext &context);
1729 virtual void Output(CYOutput &out, CYFlags flags) const;
1732 struct CYAddressOf :
1735 CYAddressOf(CYExpression *rhs) :
1740 virtual const char *Operator() const {
1746 virtual CYExpression *Replace(CYContext &context);
1752 CYIndirect(CYExpression *rhs) :
1757 virtual const char *Operator() const {
1763 virtual CYExpression *Replace(CYContext &context);
1767 virtual CYExpression *Replace(CYContext &context);
1769 #define CYPostfix_(op, name, args...) \
1770 struct CY ## name : \
1773 CY ## name(CYExpression *lhs) : \
1778 virtual const char *Operator() const { \
1783 #define CYPrefix_(alphabetic, op, name, args...) \
1784 struct CY ## name : \
1787 CY ## name(CYExpression *rhs) : \
1792 CYAlphabetic(alphabetic) \
1794 virtual const char *Operator() const { \
1799 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1800 struct CY ## name : \
1803 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1808 CYAlphabetic(alphabetic) \
1809 CYPrecedence(precedence) \
1811 virtual const char *Operator() const { \
1816 #define CYAssignment_(op, name, args...) \
1817 struct CY ## name ## Assign : \
1820 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1821 CYAssignment(lhs, rhs) \
1825 virtual const char *Operator() const { \
1830 CYPostfix_("++", PostIncrement)
1831 CYPostfix_("--", PostDecrement)
1833 CYPrefix_(true, "delete", Delete)
1834 CYPrefix_(true, "void", Void)
1835 CYPrefix_(true, "typeof", TypeOf)
1836 CYPrefix_(false, "++", PreIncrement)
1837 CYPrefix_(false, "--", PreDecrement)
1838 CYPrefix_(false, "+", Affirm)
1839 CYPrefix_(false, "-", Negate)
1840 CYPrefix_(false, "~", BitwiseNot)
1841 CYPrefix_(false, "!", LogicalNot)
1843 CYInfix_(false, 5, "*", Multiply)
1844 CYInfix_(false, 5, "/", Divide)
1845 CYInfix_(false, 5, "%", Modulus)
1846 CYInfix_(false, 6, "+", Add, CYReplace)
1847 CYInfix_(false, 6, "-", Subtract)
1848 CYInfix_(false, 7, "<<", ShiftLeft)
1849 CYInfix_(false, 7, ">>", ShiftRightSigned)
1850 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1851 CYInfix_(false, 8, "<", Less)
1852 CYInfix_(false, 8, ">", Greater)
1853 CYInfix_(false, 8, "<=", LessOrEqual)
1854 CYInfix_(false, 8, ">=", GreaterOrEqual)
1855 CYInfix_(true, 8, "instanceof", InstanceOf)
1856 CYInfix_(true, 8, "in", In)
1857 CYInfix_(false, 9, "==", Equal)
1858 CYInfix_(false, 9, "!=", NotEqual)
1859 CYInfix_(false, 9, "===", Identical)
1860 CYInfix_(false, 9, "!==", NotIdentical)
1861 CYInfix_(false, 10, "&", BitwiseAnd)
1862 CYInfix_(false, 11, "^", BitwiseXOr)
1863 CYInfix_(false, 12, "|", BitwiseOr)
1864 CYInfix_(false, 13, "&&", LogicalAnd)
1865 CYInfix_(false, 14, "||", LogicalOr)
1867 CYAssignment_("=", )
1868 CYAssignment_("*=", Multiply)
1869 CYAssignment_("/=", Divide)
1870 CYAssignment_("%=", Modulus)
1871 CYAssignment_("+=", Add)
1872 CYAssignment_("-=", Subtract)
1873 CYAssignment_("<<=", ShiftLeft)
1874 CYAssignment_(">>=", ShiftRightSigned)
1875 CYAssignment_(">>>=", ShiftRightUnsigned)
1876 CYAssignment_("&=", BitwiseAnd)
1877 CYAssignment_("^=", BitwiseXOr)
1878 CYAssignment_("|=", BitwiseOr)
1880 #endif/*CYCRIPT_PARSER_HPP*/