1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 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;
361 CYIdentifierUsageVector rename_;
363 CYNonLocal *nonlocal_;
364 CYNonLocal *nextlocal_;
367 CYContext(CYOptions &options) :
377 virtual ~CYContext() {
380 template <typename Type_>
381 void ReplaceAll(Type_ *&values) {
382 Type_ **last(&values);
383 CYForEach (next, values) {
384 Replace(*last = next);
386 last = &(*last)->next_;
390 template <typename Type_>
391 void Replace(Type_ *&value) {
392 for (;;) if (value == NULL)
395 Type_ *replace(value->Replace(*this));
396 if (replace != value)
402 void NonLocal(CYStatement *&statements);
403 CYIdentifier *Unique();
407 CYIdentifier *identifier_;
414 CYIdentifier *Target(CYContext &context) {
415 if (identifier_ == NULL)
416 identifier_ = context.Unique();
424 CYIdentifier *identifier_;
431 CYIdentifier *Identifier(CYContext &context) {
433 return next_->Identifier(context);
434 if (identifier_ == NULL)
435 identifier_ = context.Unique();
444 CYStatement *statements_;
446 CYBlock(CYStatement *statements) :
447 statements_(statements)
451 operator CYStatement *() const {
455 void AddPrev(CYStatement *statement) {
456 CYSetLast(statement) = statements_;
457 statements_ = statement;
460 virtual CYStatement *Replace(CYContext &context);
462 virtual void Output(CYOutput &out) const;
463 virtual void Output(CYOutput &out, CYFlags flags) const;
477 std::stack<bool> in_;
480 bool AtImplementation;
497 std::string filename_;
501 cy::location location_;
502 std::string message_;
505 typedef std::vector<Error> Errors;
513 CYExpression *context_;
515 Context(CYExpression *context) :
520 typedef std::vector<CYWord *> Words;
524 typedef std::vector<Context> Contexts;
527 CYExpression *context_;
539 void ScannerDestroy();
542 CYDriver(const std::string &filename = "");
545 Condition GetCondition();
546 void SetCondition(Condition condition);
548 void PushCondition(Condition condition);
551 void Warning(const cy::location &location, const char *message);
554 struct CYForInitialiser {
555 virtual ~CYForInitialiser() {
558 virtual CYExpression *Replace(CYContext &context) = 0;
559 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
562 struct CYForInInitialiser {
563 virtual ~CYForInInitialiser() {
566 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
567 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
569 virtual CYExpression *Replace(CYContext &context) = 0;
570 virtual CYAssignment *Assignment(CYContext &context) = 0;
572 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
578 struct CYExpression :
579 CYNext<CYExpression>,
585 virtual unsigned Precedence() const = 0;
587 virtual bool RightHand() const {
591 virtual void ForIn(CYOutput &out, CYFlags flags) const;
592 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
594 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
596 virtual void Output(CYOutput &out) const;
597 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
598 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
600 virtual CYExpression *ClassName(CYContext &context, bool object);
601 virtual void ClassName(CYOutput &out, bool object) const;
603 virtual CYExpression *Replace(CYContext &context) = 0;
604 virtual CYAssignment *Assignment(CYContext &context);
606 virtual CYExpression *Primitive(CYContext &context) {
610 virtual CYNumber *Number(CYContext &context) {
614 virtual CYString *String(CYContext &context) {
618 virtual const char *Word() const {
623 #define CYAlphabetic(value) \
624 virtual bool Alphabetic() const { \
628 #define CYPrecedence(value) \
629 static const unsigned Precedence_ = value; \
630 virtual unsigned Precedence() const { \
631 return Precedence_; \
634 #define CYRightHand(value) \
635 virtual bool RightHand() const { \
642 CYExpression *expressions_;
644 CYCompound(CYExpression *expressions = NULL) :
645 expressions_(expressions)
649 void AddPrev(CYExpression *expression) {
650 CYSetLast(expression) = expressions_;
651 expressions_ = expression;
656 virtual CYExpression *Replace(CYContext &context);
657 void Output(CYOutput &out, CYFlags flags) const;
659 virtual CYExpression *Primitive(CYContext &context);
662 struct CYDeclaration;
664 struct CYFunctionParameter :
665 CYNext<CYFunctionParameter>,
668 CYForInInitialiser *initialiser_;
670 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
671 CYNext<CYFunctionParameter>(next),
672 initialiser_(initialiser)
676 void Replace(CYContext &context, CYBlock &code);
677 void Output(CYOutput &out) const;
680 struct CYComprehension :
681 CYNext<CYComprehension>,
684 CYComprehension(CYComprehension *next = NULL) :
685 CYNext<CYComprehension>(next)
689 virtual const char *Name() const = 0;
691 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
692 CYFunctionParameter *Parameters(CYContext &context) const;
693 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
694 virtual void Output(CYOutput &out) const = 0;
697 struct CYForInComprehension :
703 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
704 CYComprehension(next),
710 virtual const char *Name() const {
711 return name_->Word();
714 virtual CYFunctionParameter *Parameter(CYContext &context) const;
715 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
716 virtual void Output(CYOutput &out) const;
719 struct CYForOfComprehension :
725 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
726 CYComprehension(next),
732 virtual const char *Name() const {
733 return name_->Word();
736 virtual CYFunctionParameter *Parameter(CYContext &context) const;
737 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
738 virtual void Output(CYOutput &out) const;
741 struct CYIfComprehension :
746 CYIfComprehension(CYExpression *test) :
751 virtual const char *Name() const {
755 virtual CYFunctionParameter *Parameter(CYContext &context) const;
756 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
757 virtual void Output(CYOutput &out) const;
760 struct CYArrayComprehension :
763 CYExpression *expression_;
764 CYComprehension *comprehensions_;
766 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
767 expression_(expression),
768 comprehensions_(comprehensions)
774 virtual CYExpression *Replace(CYContext &context);
775 virtual void Output(CYOutput &out, CYFlags flags) const;
788 virtual CYExpression *Replace(CYContext &context);
802 CYRange(uint64_t lo, uint64_t hi) :
807 bool operator [](uint8_t value) const {
808 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
811 void operator()(uint8_t value) {
814 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
818 extern CYRange DigitRange_;
819 extern CYRange WordStartRange_;
820 extern CYRange WordEndRange_;
835 CYString(const char *value) :
841 CYString(const char *value, size_t size) :
847 CYString(const CYWord *word) :
848 value_(word->Word()),
849 size_(strlen(value_))
853 const char *Value() const {
857 virtual const char *Word() const;
859 virtual CYNumber *Number(CYContext &context);
860 virtual CYString *String(CYContext &context);
862 CYString *Concat(CYContext &out, CYString *rhs) const;
863 virtual void Output(CYOutput &out, CYFlags flags) const;
864 virtual void PropertyName(CYOutput &out) const;
873 CYNumber(double value) :
878 double Value() const {
882 virtual CYNumber *Number(CYContext &context);
883 virtual CYString *String(CYContext &context);
885 virtual void Output(CYOutput &out, CYFlags flags) const;
886 virtual void PropertyName(CYOutput &out) const;
894 CYRegEx(const char *value) :
899 const char *Value() const {
903 virtual void Output(CYOutput &out, CYFlags flags) const;
915 virtual CYNumber *Number(CYContext &context);
916 virtual CYString *String(CYContext &context);
918 virtual void Output(CYOutput &out, CYFlags flags) const;
930 virtual CYExpression *Replace(CYContext &context);
931 virtual void Output(CYOutput &out, CYFlags flags) const;
937 virtual bool Value() const = 0;
938 virtual void Output(CYOutput &out, CYFlags flags) const;
950 virtual bool Value() const {
954 virtual CYNumber *Number(CYContext &context);
955 virtual CYString *String(CYContext &context);
967 virtual bool Value() const {
971 virtual CYNumber *Number(CYContext &context);
972 virtual CYString *String(CYContext &context);
980 CYVariable(CYIdentifier *name) :
985 CYVariable(const char *name) :
986 name_(new($pool) CYIdentifier(name))
993 virtual CYExpression *Replace(CYContext &context);
994 virtual void Output(CYOutput &out, CYFlags flags) const;
1002 CYPrefix(CYExpression *rhs) :
1007 virtual bool Alphabetic() const = 0;
1008 virtual const char *Operator() const = 0;
1012 virtual CYExpression *Replace(CYContext &context);
1013 virtual void Output(CYOutput &out, CYFlags flags) const;
1022 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1028 void SetLeft(CYExpression *lhs) {
1032 virtual bool Alphabetic() const = 0;
1033 virtual const char *Operator() const = 0;
1035 virtual CYExpression *Replace(CYContext &context);
1036 virtual void Output(CYOutput &out, CYFlags flags) const;
1044 CYPostfix(CYExpression *lhs) :
1049 virtual const char *Operator() const = 0;
1053 virtual CYExpression *Replace(CYContext &context);
1054 virtual void Output(CYOutput &out, CYFlags flags) const;
1057 struct CYAssignment :
1063 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1069 void SetLeft(CYExpression *lhs) {
1073 virtual const char *Operator() const = 0;
1077 virtual CYExpression *Replace(CYContext &context);
1078 virtual void Output(CYOutput &out, CYFlags flags) const;
1086 CYExpression *value_;
1088 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1089 CYNext<CYArgument>(next),
1095 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1096 CYNext<CYArgument>(next),
1102 CYArgument *Replace(CYContext &context);
1103 void Output(CYOutput &out) const;
1119 CYExpression *case_;
1120 CYStatement *statements_;
1122 CYClause(CYExpression *_case, CYStatement *statements) :
1124 statements_(statements)
1128 void Replace(CYContext &context);
1129 virtual void Output(CYOutput &out) const;
1136 CYExpression *value_;
1138 CYElement(CYExpression *value, CYElement *next) :
1139 CYNext<CYElement>(next),
1144 void Replace(CYContext &context);
1145 void Output(CYOutput &out) const;
1151 CYElement *elements_;
1153 CYArray(CYElement *elements = NULL) :
1158 virtual CYExpression *Replace(CYContext &context);
1159 virtual void Output(CYOutput &out, CYFlags flags) const;
1166 CYPropertyName *name_;
1167 CYExpression *value_;
1169 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1170 CYNext<CYProperty>(next),
1176 void Replace(CYContext &context);
1177 virtual void Output(CYOutput &out) const;
1180 struct CYDeclaration :
1183 CYIdentifier *identifier_;
1184 CYExpression *initialiser_;
1186 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1187 identifier_(identifier),
1188 initialiser_(initialiser)
1192 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1193 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1195 virtual CYExpression *Replace(CYContext &context);
1197 virtual CYAssignment *Assignment(CYContext &context);
1198 CYVariable *Variable(CYContext &context);
1200 virtual void Output(CYOutput &out, CYFlags flags) const;
1203 struct CYDeclarations :
1204 CYNext<CYDeclarations>,
1207 CYDeclaration *declaration_;
1209 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1210 CYNext<CYDeclarations>(next),
1211 declaration_(declaration)
1215 void Replace(CYContext &context);
1217 CYCompound *Compound(CYContext &context);
1218 CYProperty *Property(CYContext &context);
1219 CYArgument *Argument(CYContext &context);
1220 CYFunctionParameter *Parameter(CYContext &context);
1222 virtual void Output(CYOutput &out) const;
1223 virtual void Output(CYOutput &out, CYFlags flags) const;
1226 struct CYForDeclarations :
1229 CYDeclarations *declarations_;
1231 CYForDeclarations(CYDeclarations *declarations) :
1232 declarations_(declarations)
1236 virtual CYCompound *Replace(CYContext &context);
1237 virtual void Output(CYOutput &out, CYFlags flags) const;
1243 CYDeclarations *declarations_;
1245 CYVar(CYDeclarations *declarations) :
1246 declarations_(declarations)
1250 virtual CYStatement *Replace(CYContext &context);
1251 virtual void Output(CYOutput &out, CYFlags flags) const;
1254 struct CYLetStatement :
1257 CYDeclarations *declarations_;
1260 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1261 declarations_(declarations),
1266 virtual CYStatement *Replace(CYContext &context);
1267 virtual void Output(CYOutput &out, CYFlags flags) const;
1273 CYForInitialiser *initialiser_;
1274 CYExpression *test_;
1275 CYExpression *increment_;
1278 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1279 initialiser_(initialiser),
1281 increment_(increment),
1286 virtual CYStatement *Replace(CYContext &context);
1287 virtual void Output(CYOutput &out, CYFlags flags) const;
1293 CYForInInitialiser *initialiser_;
1297 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1298 initialiser_(initialiser),
1304 virtual CYStatement *Replace(CYContext &context);
1305 virtual void Output(CYOutput &out, CYFlags flags) const;
1311 CYForInInitialiser *initialiser_;
1315 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1316 initialiser_(initialiser),
1322 virtual CYStatement *Replace(CYContext &context);
1323 virtual void Output(CYOutput &out, CYFlags flags) const;
1329 CYProperty *properties_;
1331 CYObject(CYProperty *properties = NULL) :
1332 properties_(properties)
1336 virtual CYExpression *Replace(CYContext &context);
1337 void Output(CYOutput &out, CYFlags flags) const;
1343 CYExpression *object_;
1344 CYExpression *property_;
1346 CYMember(CYExpression *object, CYExpression *property) :
1352 void SetLeft(CYExpression *object) {
1357 struct CYDirectMember :
1360 CYDirectMember(CYExpression *object, CYExpression *property) :
1361 CYMember(object, property)
1368 virtual CYExpression *Replace(CYContext &context);
1369 virtual void Output(CYOutput &out, CYFlags flags) const;
1372 struct CYIndirectMember :
1375 CYIndirectMember(CYExpression *object, CYExpression *property) :
1376 CYMember(object, property)
1383 virtual CYExpression *Replace(CYContext &context);
1384 virtual void Output(CYOutput &out, CYFlags flags) const;
1393 CYExpression *constructor_;
1394 CYArgument *arguments_;
1396 New(CYExpression *constructor, CYArgument *arguments) :
1397 constructor_(constructor),
1398 arguments_(arguments)
1402 virtual unsigned Precedence() const {
1403 return arguments_ == NULL ? 2 : 1;
1408 virtual CYExpression *Replace(CYContext &context);
1409 virtual void Output(CYOutput &out, CYFlags flags) const;
1411 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1419 CYExpression *function_;
1420 CYArgument *arguments_;
1422 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1423 function_(function),
1424 arguments_(arguments)
1431 virtual CYExpression *Replace(CYContext &context);
1432 virtual void Output(CYOutput &out, CYFlags flags) const;
1434 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1439 struct CYRubyBlock :
1442 CYExpression *call_;
1445 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1454 virtual CYExpression *Replace(CYContext &context);
1455 virtual void Output(CYOutput &out, CYFlags flags) const;
1461 CYExpression *test_;
1463 CYStatement *false_;
1465 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1472 virtual CYStatement *Replace(CYContext &context);
1473 virtual void Output(CYOutput &out, CYFlags flags) const;
1479 CYExpression *test_;
1482 CYDoWhile(CYExpression *test, CYStatement *code) :
1488 virtual CYStatement *Replace(CYContext &context);
1489 virtual void Output(CYOutput &out, CYFlags flags) const;
1495 CYExpression *test_;
1498 CYWhile(CYExpression *test, CYStatement *code) :
1504 virtual CYStatement *Replace(CYContext &context);
1505 virtual void Output(CYOutput &out, CYFlags flags) const;
1508 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1510 CYIdentifier *name_;
1511 CYFunctionParameter *parameters_;
1514 CYNonLocal *nonlocal_;
1517 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1519 parameters_(parameters),
1525 virtual ~CYFunction() {
1528 void Inject(CYContext &context);
1529 virtual void Replace_(CYContext &context, bool outer);
1530 virtual void Output(CYOutput &out, CYFlags flags) const;
1533 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1534 struct CYFunctionExpression :
1538 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1539 CYFunction(name, parameters, statements)
1546 virtual CYExpression *Replace(CYContext &context);
1547 virtual void Output(CYOutput &out, CYFlags flags) const;
1550 // XXX: this should derive from CYAnonymousFunction
1555 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1556 CYFunction(NULL, parameters, statements)
1563 virtual CYExpression *Replace(CYContext &context);
1564 virtual void Output(CYOutput &out, CYFlags flags) const;
1567 // XXX: this should derive from CYAnonymousFunctionExpression
1569 CYFunctionExpression
1571 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1572 CYFunctionExpression(NULL, parameters, statements)
1576 virtual CYExpression *Replace(CYContext &context);
1577 virtual void Output(CYOutput &out, CYFlags flags) const;
1580 // XXX: this should derive from CYNamedFunction
1581 struct CYFunctionStatement :
1585 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1586 CYFunction(name, parameters, statements)
1590 virtual CYStatement *Replace(CYContext &context);
1591 virtual void Output(CYOutput &out, CYFlags flags) const;
1597 CYExpression *expression_;
1599 CYExpress(CYExpression *expression) :
1600 expression_(expression)
1602 if (expression == NULL)
1606 virtual CYStatement *Replace(CYContext &context);
1607 virtual void Output(CYOutput &out, CYFlags flags) const;
1613 CYIdentifier *label_;
1615 CYContinue(CYIdentifier *label) :
1620 virtual CYStatement *Replace(CYContext &context);
1621 virtual void Output(CYOutput &out, CYFlags flags) const;
1627 CYIdentifier *label_;
1629 CYBreak(CYIdentifier *label) :
1634 virtual CYStatement *Replace(CYContext &context);
1635 virtual void Output(CYOutput &out, CYFlags flags) const;
1641 CYExpression *value_;
1643 CYReturn(CYExpression *value) :
1648 virtual CYStatement *Replace(CYContext &context);
1649 virtual void Output(CYOutput &out, CYFlags flags) const;
1655 virtual CYStatement *Replace(CYContext &context);
1656 virtual void Output(CYOutput &out, CYFlags flags) const;
1664 CYFinally(CYStatement *statements) :
1669 void Replace(CYContext &context);
1670 virtual void Output(CYOutput &out) const;
1679 CYIdentifier *name_;
1682 Catch(CYIdentifier *name, CYStatement *statements) :
1688 void Replace(CYContext &context);
1689 virtual void Output(CYOutput &out) const;
1697 CYFinally *finally_;
1699 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1706 virtual CYStatement *Replace(CYContext &context);
1707 virtual void Output(CYOutput &out, CYFlags flags) const;
1713 CYExpression *value_;
1715 Throw(CYExpression *value = NULL) :
1720 virtual CYStatement *Replace(CYContext &context);
1721 virtual void Output(CYOutput &out, CYFlags flags) const;
1729 CYExpression *scope_;
1732 CYWith(CYExpression *scope, CYStatement *code) :
1738 virtual CYStatement *Replace(CYContext &context);
1739 virtual void Output(CYOutput &out, CYFlags flags) const;
1745 CYExpression *value_;
1748 CYSwitch(CYExpression *value, CYClause *clauses) :
1754 virtual CYStatement *Replace(CYContext &context);
1755 virtual void Output(CYOutput &out, CYFlags flags) const;
1765 virtual CYStatement *Replace(CYContext &context);
1766 virtual void Output(CYOutput &out, CYFlags flags) const;
1769 struct CYCondition :
1772 CYExpression *test_;
1773 CYExpression *true_;
1774 CYExpression *false_;
1776 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1785 virtual CYExpression *Replace(CYContext &context);
1786 virtual void Output(CYOutput &out, CYFlags flags) const;
1789 struct CYAddressOf :
1792 CYAddressOf(CYExpression *rhs) :
1797 virtual const char *Operator() const {
1803 virtual CYExpression *Replace(CYContext &context);
1809 CYIndirect(CYExpression *rhs) :
1814 virtual const char *Operator() const {
1820 virtual CYExpression *Replace(CYContext &context);
1824 virtual CYExpression *Replace(CYContext &context);
1826 #define CYPostfix_(op, name, args...) \
1827 struct CY ## name : \
1830 CY ## name(CYExpression *lhs) : \
1835 virtual const char *Operator() const { \
1840 #define CYPrefix_(alphabetic, op, name, args...) \
1841 struct CY ## name : \
1844 CY ## name(CYExpression *rhs) : \
1849 CYAlphabetic(alphabetic) \
1851 virtual const char *Operator() const { \
1856 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1857 struct CY ## name : \
1860 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1865 CYAlphabetic(alphabetic) \
1866 CYPrecedence(precedence) \
1868 virtual const char *Operator() const { \
1873 #define CYAssignment_(op, name, args...) \
1874 struct CY ## name ## Assign : \
1877 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1878 CYAssignment(lhs, rhs) \
1882 virtual const char *Operator() const { \
1887 CYPostfix_("++", PostIncrement)
1888 CYPostfix_("--", PostDecrement)
1890 CYPrefix_(true, "delete", Delete)
1891 CYPrefix_(true, "void", Void)
1892 CYPrefix_(true, "typeof", TypeOf)
1893 CYPrefix_(false, "++", PreIncrement)
1894 CYPrefix_(false, "--", PreDecrement)
1895 CYPrefix_(false, "+", Affirm)
1896 CYPrefix_(false, "-", Negate)
1897 CYPrefix_(false, "~", BitwiseNot)
1898 CYPrefix_(false, "!", LogicalNot)
1900 CYInfix_(false, 5, "*", Multiply)
1901 CYInfix_(false, 5, "/", Divide)
1902 CYInfix_(false, 5, "%", Modulus)
1903 CYInfix_(false, 6, "+", Add, CYReplace)
1904 CYInfix_(false, 6, "-", Subtract)
1905 CYInfix_(false, 7, "<<", ShiftLeft)
1906 CYInfix_(false, 7, ">>", ShiftRightSigned)
1907 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1908 CYInfix_(false, 8, "<", Less)
1909 CYInfix_(false, 8, ">", Greater)
1910 CYInfix_(false, 8, "<=", LessOrEqual)
1911 CYInfix_(false, 8, ">=", GreaterOrEqual)
1912 CYInfix_(true, 8, "instanceof", InstanceOf)
1913 CYInfix_(true, 8, "in", In)
1914 CYInfix_(false, 9, "==", Equal)
1915 CYInfix_(false, 9, "!=", NotEqual)
1916 CYInfix_(false, 9, "===", Identical)
1917 CYInfix_(false, 9, "!==", NotIdentical)
1918 CYInfix_(false, 10, "&", BitwiseAnd)
1919 CYInfix_(false, 11, "^", BitwiseXOr)
1920 CYInfix_(false, 12, "|", BitwiseOr)
1921 CYInfix_(false, 13, "&&", LogicalAnd)
1922 CYInfix_(false, 14, "||", LogicalOr)
1924 CYAssignment_("=", )
1925 CYAssignment_("*=", Multiply)
1926 CYAssignment_("/=", Divide)
1927 CYAssignment_("%=", Modulus)
1928 CYAssignment_("+=", Add)
1929 CYAssignment_("-=", Subtract)
1930 CYAssignment_("<<=", ShiftLeft)
1931 CYAssignment_(">>=", ShiftRightSigned)
1932 CYAssignment_(">>>=", ShiftRightUnsigned)
1933 CYAssignment_("&=", BitwiseAnd)
1934 CYAssignment_("^=", BitwiseXOr)
1935 CYAssignment_("|=", BitwiseOr)
1937 #endif/*CYCRIPT_PARSER_HPP*/