1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU 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 "Pooling.hpp"
37 #include "Options.hpp"
45 virtual void Output(struct CYOutput &out) const = 0;
63 CYOutput(std::ostream &out, CYOptions &options) :
73 void Check(char value);
76 CYOutput &operator <<(char rhs);
77 CYOutput &operator <<(const char *rhs);
79 _finline CYOutput &operator <<(const CYThing *rhs) {
85 _finline CYOutput &operator <<(const CYThing &rhs) {
91 struct CYPropertyName {
92 virtual void PropertyName(CYOutput &out) const = 0;
94 virtual ~CYPropertyName() {
109 CYNoBrace = (1 << 0),
110 CYNoFunction = (1 << 1),
113 CYNoRightHand = (1 << 4),
114 CYNoDangle = (1 << 5),
115 CYNoInteger = (1 << 6),
116 CYNoBF = (CYNoBrace | CYNoFunction),
119 _finline CYFlags operator ~(CYFlags rhs) {
120 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
123 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
124 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
127 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
128 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
131 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
132 return lhs = lhs | rhs;
135 _finline CYFlags CYLeft(CYFlags flags) {
136 return flags & ~(CYNoDangle | CYNoInteger);
139 _finline CYFlags CYRight(CYFlags flags) {
140 return flags & ~CYNoBF;
143 _finline CYFlags CYCenter(CYFlags flags) {
144 return CYLeft(CYRight(flags));
150 virtual ~CYStatement() {
153 void Single(CYOutput &out, CYFlags flags) const;
154 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
156 virtual CYStatement *Replace(CYContext &context) = 0;
159 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
162 struct CYStatements {
172 operator CYStatement *() const {
176 CYStatements &operator ->*(CYStatement *next) {
178 if (first_ == NULL) {
181 } else for (;; last_ = last_->next_)
182 if (last_->next_ == NULL) {
192 virtual ~CYClassName() {
195 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
196 virtual void ClassName(CYOutput &out, bool object) const = 0;
206 CYWord(const char *word) :
211 void Set(const char *value) {
215 virtual const char *Word() const;
216 virtual void Output(CYOutput &out) const;
218 virtual CYExpression *ClassName(CYContext &context, bool object);
219 virtual void ClassName(CYOutput &out, bool object) const;
220 virtual void PropertyName(CYOutput &out) const;
223 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
225 return lhs << rhs.Word();
228 struct CYIdentifier :
229 CYNext<CYIdentifier>,
232 CYIdentifier *replace_;
236 CYIdentifier(const char *word) :
244 virtual const char *Word() const;
245 CYIdentifier *Replace(CYContext &context);
253 CYComment(const char *value) :
258 virtual CYStatement *Replace(CYContext &context);
259 virtual void Output(CYOutput &out, CYFlags flags) const;
266 CYStatement *statement_;
268 CYLabel(CYIdentifier *name, CYStatement *statement) :
270 statement_(statement)
274 virtual CYStatement *Replace(CYContext &context);
275 virtual void Output(CYOutput &out, CYFlags flags) const;
278 struct CYCStringLess :
279 std::binary_function<const char *, const char *, bool>
281 _finline bool operator ()(const char *lhs, const char *rhs) const {
282 return strcmp(lhs, rhs) < 0;
286 struct CYIdentifierValueLess :
287 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
289 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
290 return CYCStringLess()(lhs->Word(), rhs->Word());
294 enum CYIdentifierFlags {
295 CYIdentifierArgument,
296 CYIdentifierVariable,
302 typedef std::set<const char *, CYCStringLess> CYCStringSet;
303 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
304 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
306 struct CYIdentifierUsage {
307 CYIdentifier *identifier_;
311 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
317 CYStatement *&statements_;
321 CYIdentifierAddressFlagsMap internal_;
322 CYIdentifierValueSet identifiers_;
324 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
329 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
330 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
331 void Merge(CYContext &context, CYIdentifier *identifier);
332 void Scope(CYContext &context, CYStatement *&statements);
338 CYStatement *statements_;
340 CYProgram(CYStatement *statements) :
341 statements_(statements)
345 virtual void Replace(CYContext &context);
346 virtual void Output(CYOutput &out) const;
358 CYIdentifierUsageVector rename_;
360 CYNonLocal *nonlocal_;
361 CYNonLocal *nextlocal_;
364 CYContext(CYOptions &options) :
374 virtual ~CYContext() {
377 template <typename Type_>
378 void ReplaceAll(Type_ *&values) {
379 Type_ **last(&values);
380 CYForEach (next, values) {
381 Replace(*last = next);
383 last = &(*last)->next_;
387 template <typename Type_>
388 void Replace(Type_ *&value) {
389 for (;;) if (value == NULL)
392 Type_ *replace(value->Replace(*this));
393 if (replace != value)
399 void NonLocal(CYStatement *&statements);
400 CYIdentifier *Unique();
404 CYIdentifier *identifier_;
411 CYIdentifier *Target(CYContext &context) {
412 if (identifier_ == NULL)
413 identifier_ = context.Unique();
421 CYIdentifier *identifier_;
428 CYIdentifier *Identifier(CYContext &context) {
430 return next_->Identifier(context);
431 if (identifier_ == NULL)
432 identifier_ = context.Unique();
441 CYStatement *statements_;
443 CYBlock(CYStatement *statements) :
444 statements_(statements)
448 operator CYStatement *() const {
452 void AddPrev(CYStatement *statement) {
453 CYSetLast(statement) = statements_;
454 statements_ = statement;
457 virtual CYStatement *Replace(CYContext &context);
459 virtual void Output(CYOutput &out) const;
460 virtual void Output(CYOutput &out, CYFlags flags) const;
463 struct CYForInitialiser {
464 virtual ~CYForInitialiser() {
467 virtual CYExpression *Replace(CYContext &context) = 0;
468 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
471 struct CYForInInitialiser {
472 virtual ~CYForInInitialiser() {
475 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
476 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
478 virtual CYExpression *Replace(CYContext &context) = 0;
479 virtual CYAssignment *Assignment(CYContext &context) = 0;
481 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
487 struct CYExpression :
493 virtual int Precedence() const = 0;
495 virtual bool RightHand() const {
499 virtual void ForIn(CYOutput &out, CYFlags flags) const;
500 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
502 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
504 virtual void Output(CYOutput &out) const;
505 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
506 void Output(CYOutput &out, int precedence, CYFlags flags) const;
508 virtual CYExpression *ClassName(CYContext &context, bool object);
509 virtual void ClassName(CYOutput &out, bool object) const;
511 virtual CYExpression *Replace(CYContext &context) = 0;
512 virtual CYAssignment *Assignment(CYContext &context);
514 virtual CYExpression *Primitive(CYContext &context) {
518 virtual CYNumber *Number(CYContext &context) {
522 virtual CYString *String(CYContext &context) {
526 virtual const char *Word() const {
531 #define CYAlphabetic(value) \
532 virtual bool Alphabetic() const { \
536 #define CYPrecedence(value) \
537 static const int Precedence_ = value; \
538 virtual int Precedence() const { \
539 return Precedence_; \
542 #define CYRightHand(value) \
543 virtual bool RightHand() const { \
550 CYExpression *expression_;
553 CYCompound(CYExpression *expression, CYExpression *next = NULL) :
554 expression_(expression),
557 if (expression_ == NULL)
559 _assert(expression_ != NULL);
564 virtual CYExpression *Replace(CYContext &context);
565 void Output(CYOutput &out, CYFlags flags) const;
567 virtual CYExpression *Primitive(CYContext &context);
570 struct CYDeclaration;
572 struct CYFunctionParameter :
573 CYNext<CYFunctionParameter>,
576 CYForInInitialiser *initialiser_;
578 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
579 CYNext<CYFunctionParameter>(next),
580 initialiser_(initialiser)
584 void Replace(CYContext &context, CYBlock &code);
585 void Output(CYOutput &out) const;
588 struct CYComprehension :
589 CYNext<CYComprehension>,
592 CYComprehension(CYComprehension *next = NULL) :
593 CYNext<CYComprehension>(next)
597 CYComprehension *Modify(CYComprehension *next) {
602 virtual const char *Name() const = 0;
604 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
605 CYFunctionParameter *Parameters(CYContext &context) const;
606 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
607 virtual void Output(CYOutput &out) const = 0;
610 struct CYForInComprehension :
616 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
617 CYComprehension(next),
623 virtual const char *Name() const {
624 return name_->Word();
627 virtual CYFunctionParameter *Parameter(CYContext &context) const;
628 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
629 virtual void Output(CYOutput &out) const;
632 struct CYForOfComprehension :
638 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
639 CYComprehension(next),
645 virtual const char *Name() const {
646 return name_->Word();
649 virtual CYFunctionParameter *Parameter(CYContext &context) const;
650 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
651 virtual void Output(CYOutput &out) const;
654 struct CYIfComprehension :
659 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
660 CYComprehension(next),
665 virtual const char *Name() const {
669 virtual CYFunctionParameter *Parameter(CYContext &context) const;
670 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
671 virtual void Output(CYOutput &out) const;
674 struct CYArrayComprehension :
677 CYExpression *expression_;
678 CYComprehension *comprehensions_;
680 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
681 expression_(expression),
682 comprehensions_(comprehensions)
688 virtual CYExpression *Replace(CYContext &context);
689 virtual void Output(CYOutput &out, CYFlags flags) const;
698 virtual CYExpression *Primitive(CYContext &context) {
706 virtual CYExpression *Replace(CYContext &context);
720 CYRange(uint64_t lo, uint64_t hi) :
725 bool operator [](uint8_t value) const {
726 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
729 void operator()(uint8_t value) {
732 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
736 extern CYRange DigitRange_;
737 extern CYRange WordStartRange_;
738 extern CYRange WordEndRange_;
753 CYString(const char *value) :
759 CYString(const char *value, size_t size) :
765 CYString(const CYWord *word) :
766 value_(word->Word()),
767 size_(strlen(value_))
771 const char *Value() const {
775 virtual const char *Word() const;
777 virtual CYNumber *Number(CYContext &context);
778 virtual CYString *String(CYContext &context);
780 CYString *Concat(CYContext &out, CYString *rhs) const;
781 virtual void Output(CYOutput &out, CYFlags flags) const;
782 virtual void PropertyName(CYOutput &out) const;
791 CYNumber(double value) :
796 double Value() const {
800 virtual CYNumber *Number(CYContext &context);
801 virtual CYString *String(CYContext &context);
803 virtual void Output(CYOutput &out, CYFlags flags) const;
804 virtual void PropertyName(CYOutput &out) const;
812 CYRegEx(const char *value) :
817 const char *Value() const {
821 virtual void Output(CYOutput &out, CYFlags flags) const;
833 virtual CYNumber *Number(CYContext &context);
834 virtual CYString *String(CYContext &context);
836 virtual void Output(CYOutput &out, CYFlags flags) const;
848 virtual CYExpression *Replace(CYContext &context);
849 virtual void Output(CYOutput &out, CYFlags flags) const;
855 virtual bool Value() const = 0;
856 virtual void Output(CYOutput &out, CYFlags flags) const;
868 virtual bool Value() const {
872 virtual CYNumber *Number(CYContext &context);
873 virtual CYString *String(CYContext &context);
885 virtual bool Value() const {
889 virtual CYNumber *Number(CYContext &context);
890 virtual CYString *String(CYContext &context);
898 CYVariable(CYIdentifier *name) :
903 CYVariable(const char *name) :
904 name_(new($pool) CYIdentifier(name))
911 virtual CYExpression *Replace(CYContext &context);
912 virtual void Output(CYOutput &out, CYFlags flags) const;
920 CYPrefix(CYExpression *rhs) :
925 virtual bool Alphabetic() const = 0;
926 virtual const char *Operator() const = 0;
930 virtual CYExpression *Replace(CYContext &context);
931 virtual void Output(CYOutput &out, CYFlags flags) const;
940 CYInfix(CYExpression *lhs, CYExpression *rhs) :
946 void SetLeft(CYExpression *lhs) {
950 virtual bool Alphabetic() const = 0;
951 virtual const char *Operator() const = 0;
953 virtual CYExpression *Replace(CYContext &context);
954 virtual void Output(CYOutput &out, CYFlags flags) const;
962 CYPostfix(CYExpression *lhs) :
967 virtual const char *Operator() const = 0;
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
975 struct CYAssignment :
981 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
987 void SetLeft(CYExpression *lhs) {
991 virtual const char *Operator() const = 0;
995 virtual CYExpression *Replace(CYContext &context);
996 virtual void Output(CYOutput &out, CYFlags flags) const;
1004 CYExpression *value_;
1006 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1007 CYNext<CYArgument>(next),
1013 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1014 CYNext<CYArgument>(next),
1020 CYArgument *Replace(CYContext &context);
1021 void Output(CYOutput &out) const;
1037 CYExpression *case_;
1038 CYStatement *statements_;
1040 CYClause(CYExpression *_case, CYStatement *statements) :
1042 statements_(statements)
1046 void Replace(CYContext &context);
1047 virtual void Output(CYOutput &out) const;
1054 CYExpression *value_;
1056 CYElement(CYExpression *value, CYElement *next) :
1057 CYNext<CYElement>(next),
1062 void Replace(CYContext &context);
1063 void Output(CYOutput &out) const;
1069 CYElement *elements_;
1071 CYArray(CYElement *elements = NULL) :
1076 virtual CYExpression *Replace(CYContext &context);
1077 virtual void Output(CYOutput &out, CYFlags flags) const;
1084 CYPropertyName *name_;
1085 CYExpression *value_;
1087 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1088 CYNext<CYProperty>(next),
1094 void Replace(CYContext &context);
1095 virtual void Output(CYOutput &out) const;
1098 struct CYDeclaration :
1101 CYIdentifier *identifier_;
1102 CYExpression *initialiser_;
1104 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1105 identifier_(identifier),
1106 initialiser_(initialiser)
1110 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1111 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1113 virtual CYExpression *Replace(CYContext &context);
1115 virtual CYAssignment *Assignment(CYContext &context);
1116 CYVariable *Variable(CYContext &context);
1118 virtual void Output(CYOutput &out, CYFlags flags) const;
1121 struct CYDeclarations :
1122 CYNext<CYDeclarations>,
1125 CYDeclaration *declaration_;
1127 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1128 CYNext<CYDeclarations>(next),
1129 declaration_(declaration)
1133 void Replace(CYContext &context);
1135 CYCompound *Compound(CYContext &context);
1136 CYProperty *Property(CYContext &context);
1137 CYArgument *Argument(CYContext &context);
1138 CYFunctionParameter *Parameter(CYContext &context);
1140 virtual void Output(CYOutput &out) const;
1141 virtual void Output(CYOutput &out, CYFlags flags) const;
1144 struct CYForDeclarations :
1147 CYDeclarations *declarations_;
1149 CYForDeclarations(CYDeclarations *declarations) :
1150 declarations_(declarations)
1154 virtual CYCompound *Replace(CYContext &context);
1155 virtual void Output(CYOutput &out, CYFlags flags) const;
1161 CYDeclarations *declarations_;
1163 CYVar(CYDeclarations *declarations) :
1164 declarations_(declarations)
1168 virtual CYStatement *Replace(CYContext &context);
1169 virtual void Output(CYOutput &out, CYFlags flags) const;
1172 struct CYLetStatement :
1175 CYDeclarations *declarations_;
1178 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1179 declarations_(declarations),
1184 virtual CYStatement *Replace(CYContext &context);
1185 virtual void Output(CYOutput &out, CYFlags flags) const;
1191 CYForInitialiser *initialiser_;
1192 CYExpression *test_;
1193 CYExpression *increment_;
1196 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1197 initialiser_(initialiser),
1199 increment_(increment),
1204 virtual CYStatement *Replace(CYContext &context);
1205 virtual void Output(CYOutput &out, CYFlags flags) const;
1211 CYForInInitialiser *initialiser_;
1215 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1216 initialiser_(initialiser),
1222 virtual CYStatement *Replace(CYContext &context);
1223 virtual void Output(CYOutput &out, CYFlags flags) const;
1229 CYForInInitialiser *initialiser_;
1233 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1234 initialiser_(initialiser),
1240 virtual CYStatement *Replace(CYContext &context);
1241 virtual void Output(CYOutput &out, CYFlags flags) const;
1247 CYProperty *properties_;
1249 CYObject(CYProperty *properties = NULL) :
1250 properties_(properties)
1254 virtual CYExpression *Replace(CYContext &context);
1255 void Output(CYOutput &out, CYFlags flags) const;
1261 CYExpression *object_;
1262 CYExpression *property_;
1264 CYMember(CYExpression *object, CYExpression *property) :
1270 void SetLeft(CYExpression *object) {
1275 struct CYDirectMember :
1278 CYDirectMember(CYExpression *object, CYExpression *property) :
1279 CYMember(object, property)
1286 virtual CYExpression *Replace(CYContext &context);
1287 virtual void Output(CYOutput &out, CYFlags flags) const;
1290 struct CYIndirectMember :
1293 CYIndirectMember(CYExpression *object, CYExpression *property) :
1294 CYMember(object, property)
1301 virtual CYExpression *Replace(CYContext &context);
1302 virtual void Output(CYOutput &out, CYFlags flags) const;
1311 CYExpression *constructor_;
1312 CYArgument *arguments_;
1314 New(CYExpression *constructor, CYArgument *arguments) :
1315 constructor_(constructor),
1316 arguments_(arguments)
1320 virtual int Precedence() const {
1321 return arguments_ == NULL ? 2 : 1;
1326 virtual CYExpression *Replace(CYContext &context);
1327 virtual void Output(CYOutput &out, CYFlags flags) const;
1329 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1337 CYExpression *function_;
1338 CYArgument *arguments_;
1340 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1341 function_(function),
1342 arguments_(arguments)
1349 virtual CYExpression *Replace(CYContext &context);
1350 virtual void Output(CYOutput &out, CYFlags flags) const;
1352 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1357 struct CYRubyBlock :
1360 CYExpression *call_;
1363 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1372 virtual CYExpression *Replace(CYContext &context);
1373 virtual void Output(CYOutput &out, CYFlags flags) const;
1379 CYExpression *test_;
1381 CYStatement *false_;
1383 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1390 virtual CYStatement *Replace(CYContext &context);
1391 virtual void Output(CYOutput &out, CYFlags flags) const;
1397 CYExpression *test_;
1400 CYDoWhile(CYExpression *test, CYStatement *code) :
1406 virtual CYStatement *Replace(CYContext &context);
1407 virtual void Output(CYOutput &out, CYFlags flags) const;
1413 CYExpression *test_;
1416 CYWhile(CYExpression *test, CYStatement *code) :
1422 virtual CYStatement *Replace(CYContext &context);
1423 virtual void Output(CYOutput &out, CYFlags flags) const;
1426 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1428 CYIdentifier *name_;
1429 CYFunctionParameter *parameters_;
1432 CYNonLocal *nonlocal_;
1435 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1437 parameters_(parameters),
1443 virtual ~CYFunction() {
1446 void Inject(CYContext &context);
1447 virtual void Replace_(CYContext &context, bool outer);
1448 virtual void Output(CYOutput &out, CYFlags flags) const;
1451 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1452 struct CYFunctionExpression :
1456 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1457 CYFunction(name, parameters, statements)
1464 virtual CYExpression *Replace(CYContext &context);
1465 virtual void Output(CYOutput &out, CYFlags flags) const;
1468 // XXX: this should derive from CYAnonymousFunction
1473 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1474 CYFunction(NULL, parameters, statements)
1481 virtual CYExpression *Replace(CYContext &context);
1482 virtual void Output(CYOutput &out, CYFlags flags) const;
1485 // XXX: this should derive from CYAnonymousFunctionExpression
1487 CYFunctionExpression
1489 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1490 CYFunctionExpression(NULL, parameters, statements)
1494 virtual CYExpression *Replace(CYContext &context);
1495 virtual void Output(CYOutput &out, CYFlags flags) const;
1498 // XXX: this should derive from CYNamedFunction
1499 struct CYFunctionStatement :
1503 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1504 CYFunction(name, parameters, statements)
1508 virtual CYStatement *Replace(CYContext &context);
1509 virtual void Output(CYOutput &out, CYFlags flags) const;
1515 CYExpression *expression_;
1517 CYExpress(CYExpression *expression) :
1518 expression_(expression)
1520 if (expression_ == NULL)
1524 virtual CYStatement *Replace(CYContext &context);
1525 virtual void Output(CYOutput &out, CYFlags flags) const;
1531 CYIdentifier *label_;
1533 CYContinue(CYIdentifier *label) :
1538 virtual CYStatement *Replace(CYContext &context);
1539 virtual void Output(CYOutput &out, CYFlags flags) const;
1545 CYIdentifier *label_;
1547 CYBreak(CYIdentifier *label) :
1552 virtual CYStatement *Replace(CYContext &context);
1553 virtual void Output(CYOutput &out, CYFlags flags) const;
1559 CYExpression *value_;
1561 CYReturn(CYExpression *value) :
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1573 virtual CYStatement *Replace(CYContext &context);
1574 virtual void Output(CYOutput &out, CYFlags flags) const;
1582 CYFinally(CYStatement *statements) :
1587 void Replace(CYContext &context);
1588 virtual void Output(CYOutput &out) const;
1591 struct CYTypeSpecifier :
1594 virtual CYExpression *Replace(CYContext &context) = 0;
1597 struct CYTypeError :
1603 virtual CYExpression *Replace(CYContext &context);
1604 virtual void Output(CYOutput &out) const;
1613 virtual CYExpression *Replace(CYContext &context);
1614 virtual void Output(CYOutput &out) const;
1617 struct CYTypeVariable :
1620 CYIdentifier *name_;
1622 CYTypeVariable(CYIdentifier *name) :
1627 CYTypeVariable(const char *name) :
1628 name_(new($pool) CYIdentifier(name))
1632 virtual CYExpression *Replace(CYContext &context);
1633 virtual void Output(CYOutput &out) const;
1636 struct CYTypeUnsigned :
1639 CYTypeSpecifier *specifier_;
1641 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1642 specifier_(specifier)
1646 virtual CYExpression *Replace(CYContext &context);
1647 virtual void Output(CYOutput &out) const;
1650 struct CYTypeSigned :
1653 CYTypeSpecifier *specifier_;
1655 CYTypeSigned(CYTypeSpecifier *specifier) :
1656 specifier_(specifier)
1660 virtual CYExpression *Replace(CYContext &context);
1661 virtual void Output(CYOutput &out) const;
1667 CYTypeSpecifier *specifier_;
1669 CYTypeLong(CYTypeSpecifier *specifier) :
1670 specifier_(specifier)
1674 virtual CYExpression *Replace(CYContext &context);
1675 virtual void Output(CYOutput &out) const;
1678 struct CYTypeShort :
1681 CYTypeSpecifier *specifier_;
1683 CYTypeShort(CYTypeSpecifier *specifier) :
1684 specifier_(specifier)
1688 virtual CYExpression *Replace(CYContext &context);
1689 virtual void Output(CYOutput &out) const;
1692 struct CYTypeModifier :
1693 CYNext<CYTypeModifier>
1695 CYTypeModifier(CYTypeModifier *next) :
1696 CYNext<CYTypeModifier>(next)
1700 virtual int Precedence() const = 0;
1702 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1703 CYExpression *Replace(CYContext &context, CYExpression *type);
1705 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1706 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1709 struct CYTypeArrayOf :
1712 CYExpression *size_;
1714 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1715 CYTypeModifier(next),
1722 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1723 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1726 struct CYTypeConstant :
1729 CYTypeConstant(CYTypeModifier *next = NULL) :
1730 CYTypeModifier(next)
1736 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1737 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1740 struct CYTypePointerTo :
1743 CYTypePointerTo(CYTypeModifier *next = NULL) :
1744 CYTypeModifier(next)
1750 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1751 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1754 struct CYTypeVolatile :
1757 CYTypeVolatile(CYTypeModifier *next = NULL) :
1758 CYTypeModifier(next)
1764 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1765 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1768 struct CYTypedIdentifier :
1769 CYNext<CYTypedIdentifier>,
1772 CYIdentifier *identifier_;
1773 CYTypeSpecifier *specifier_;
1774 CYTypeModifier *modifier_;
1776 CYTypedIdentifier(CYIdentifier *identifier = NULL) :
1777 identifier_(identifier),
1783 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1785 specifier_(specifier),
1790 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1791 CYSetLast(modifier_) = modifier;
1795 virtual CYExpression *Replace(CYContext &context);
1796 virtual void Output(CYOutput &out) const;
1799 struct CYEncodedType :
1802 CYTypedIdentifier *typed_;
1804 CYEncodedType(CYTypedIdentifier *typed) :
1811 virtual CYExpression *Replace(CYContext &context);
1812 virtual void Output(CYOutput &out, CYFlags flags) const;
1815 struct CYTypedParameter :
1816 CYNext<CYTypedParameter>,
1819 CYTypedIdentifier *typed_;
1821 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1822 CYNext<CYTypedParameter>(next),
1827 CYArgument *Argument(CYContext &context);
1828 CYFunctionParameter *Parameters(CYContext &context);
1829 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1831 virtual void Output(CYOutput &out) const;
1837 CYTypedIdentifier *typed_;
1838 CYTypedParameter *parameters_;
1839 CYStatement *statements_;
1841 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1843 parameters_(parameters),
1844 statements_(statements)
1850 virtual CYExpression *Replace(CYContext &context);
1851 virtual void Output(CYOutput &out, CYFlags flags) const;
1860 CYModule(CYWord *part, CYModule *next = NULL) :
1861 CYNext<CYModule>(next),
1866 CYString *Replace(CYContext &context, const char *separator) const;
1867 void Output(CYOutput &out) const;
1875 CYImport(CYModule *module) :
1880 virtual CYStatement *Replace(CYContext &context);
1881 virtual void Output(CYOutput &out, CYFlags flags) const;
1884 struct CYTypeDefinition :
1887 CYTypedIdentifier *typed_;
1889 CYTypeDefinition(CYTypedIdentifier *typed) :
1894 virtual CYStatement *Replace(CYContext &context);
1895 virtual void Output(CYOutput &out, CYFlags flags) const;
1898 struct CYTypeBlockWith :
1901 CYTypedParameter *parameters_;
1903 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1904 CYTypeModifier(next),
1905 parameters_(parameters)
1911 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1912 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1915 struct CYTypeFunctionWith :
1918 CYTypedParameter *parameters_;
1920 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1921 CYTypeModifier(next),
1922 parameters_(parameters)
1928 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1929 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1938 CYIdentifier *name_;
1941 Catch(CYIdentifier *name, CYStatement *statements) :
1947 void Replace(CYContext &context);
1948 virtual void Output(CYOutput &out) const;
1956 CYFinally *finally_;
1958 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1965 virtual CYStatement *Replace(CYContext &context);
1966 virtual void Output(CYOutput &out, CYFlags flags) const;
1972 CYExpression *value_;
1974 Throw(CYExpression *value = NULL) :
1979 virtual CYStatement *Replace(CYContext &context);
1980 virtual void Output(CYOutput &out, CYFlags flags) const;
1988 CYExpression *scope_;
1991 CYWith(CYExpression *scope, CYStatement *code) :
1997 virtual CYStatement *Replace(CYContext &context);
1998 virtual void Output(CYOutput &out, CYFlags flags) const;
2004 CYExpression *value_;
2007 CYSwitch(CYExpression *value, CYClause *clauses) :
2013 virtual CYStatement *Replace(CYContext &context);
2014 virtual void Output(CYOutput &out, CYFlags flags) const;
2024 virtual CYStatement *Replace(CYContext &context);
2025 virtual void Output(CYOutput &out, CYFlags flags) const;
2028 struct CYCondition :
2031 CYExpression *test_;
2032 CYExpression *true_;
2033 CYExpression *false_;
2035 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2044 virtual CYExpression *Replace(CYContext &context);
2045 virtual void Output(CYOutput &out, CYFlags flags) const;
2048 struct CYAddressOf :
2051 CYAddressOf(CYExpression *rhs) :
2056 virtual const char *Operator() const {
2062 virtual CYExpression *Replace(CYContext &context);
2068 CYIndirect(CYExpression *rhs) :
2073 virtual const char *Operator() const {
2079 virtual CYExpression *Replace(CYContext &context);
2083 virtual CYExpression *Replace(CYContext &context);
2085 #define CYPostfix_(op, name, args...) \
2086 struct CY ## name : \
2089 CY ## name(CYExpression *lhs) : \
2094 virtual const char *Operator() const { \
2099 #define CYPrefix_(alphabetic, op, name, args...) \
2100 struct CY ## name : \
2103 CY ## name(CYExpression *rhs) : \
2108 CYAlphabetic(alphabetic) \
2110 virtual const char *Operator() const { \
2115 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2116 struct CY ## name : \
2119 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2124 CYAlphabetic(alphabetic) \
2125 CYPrecedence(precedence) \
2127 virtual const char *Operator() const { \
2132 #define CYAssignment_(op, name, args...) \
2133 struct CY ## name ## Assign : \
2136 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2137 CYAssignment(lhs, rhs) \
2141 virtual const char *Operator() const { \
2146 CYPostfix_("++", PostIncrement)
2147 CYPostfix_("--", PostDecrement)
2149 CYPrefix_(true, "delete", Delete)
2150 CYPrefix_(true, "void", Void)
2151 CYPrefix_(true, "typeof", TypeOf)
2152 CYPrefix_(false, "++", PreIncrement)
2153 CYPrefix_(false, "--", PreDecrement)
2154 CYPrefix_(false, "+", Affirm)
2155 CYPrefix_(false, "-", Negate)
2156 CYPrefix_(false, "~", BitwiseNot)
2157 CYPrefix_(false, "!", LogicalNot)
2159 CYInfix_(false, 5, "*", Multiply, CYReplace)
2160 CYInfix_(false, 5, "/", Divide)
2161 CYInfix_(false, 5, "%", Modulus)
2162 CYInfix_(false, 6, "+", Add, CYReplace)
2163 CYInfix_(false, 6, "-", Subtract)
2164 CYInfix_(false, 7, "<<", ShiftLeft)
2165 CYInfix_(false, 7, ">>", ShiftRightSigned)
2166 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2167 CYInfix_(false, 8, "<", Less)
2168 CYInfix_(false, 8, ">", Greater)
2169 CYInfix_(false, 8, "<=", LessOrEqual)
2170 CYInfix_(false, 8, ">=", GreaterOrEqual)
2171 CYInfix_(true, 8, "instanceof", InstanceOf)
2172 CYInfix_(true, 8, "in", In)
2173 CYInfix_(false, 9, "==", Equal)
2174 CYInfix_(false, 9, "!=", NotEqual)
2175 CYInfix_(false, 9, "===", Identical)
2176 CYInfix_(false, 9, "!==", NotIdentical)
2177 CYInfix_(false, 10, "&", BitwiseAnd)
2178 CYInfix_(false, 11, "^", BitwiseXOr)
2179 CYInfix_(false, 12, "|", BitwiseOr)
2180 CYInfix_(false, 13, "&&", LogicalAnd)
2181 CYInfix_(false, 14, "||", LogicalOr)
2183 CYAssignment_("=", )
2184 CYAssignment_("*=", Multiply)
2185 CYAssignment_("/=", Divide)
2186 CYAssignment_("%=", Modulus)
2187 CYAssignment_("+=", Add)
2188 CYAssignment_("-=", Subtract)
2189 CYAssignment_("<<=", ShiftLeft)
2190 CYAssignment_(">>=", ShiftRightSigned)
2191 CYAssignment_(">>>=", ShiftRightUnsigned)
2192 CYAssignment_("&=", BitwiseAnd)
2193 CYAssignment_("^=", BitwiseXOr)
2194 CYAssignment_("|=", BitwiseOr)
2196 #endif/*CYCRIPT_PARSER_HPP*/