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;
468 public std::streambuf
471 CYBuffer(const char *start, const char *end) {
472 setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
477 CYStream(const char *start, const char *end) :
478 std::istream(&buffer_),
484 struct CYForInitialiser {
485 virtual ~CYForInitialiser() {
488 virtual CYExpression *Replace(CYContext &context) = 0;
489 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
492 struct CYForInInitialiser {
493 virtual ~CYForInInitialiser() {
496 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
497 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
499 virtual CYExpression *Replace(CYContext &context) = 0;
500 virtual CYAssignment *Assignment(CYContext &context) = 0;
502 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
508 struct CYExpression :
509 CYNext<CYExpression>,
515 virtual int Precedence() const = 0;
517 virtual bool RightHand() const {
521 virtual void ForIn(CYOutput &out, CYFlags flags) const;
522 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
524 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
526 virtual void Output(CYOutput &out) const;
527 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
528 void Output(CYOutput &out, int precedence, CYFlags flags) const;
530 virtual CYExpression *ClassName(CYContext &context, bool object);
531 virtual void ClassName(CYOutput &out, bool object) const;
533 virtual CYExpression *Replace(CYContext &context) = 0;
534 virtual CYAssignment *Assignment(CYContext &context);
536 virtual CYExpression *Primitive(CYContext &context) {
540 virtual CYNumber *Number(CYContext &context) {
544 virtual CYString *String(CYContext &context) {
548 virtual const char *Word() const {
553 #define CYAlphabetic(value) \
554 virtual bool Alphabetic() const { \
558 #define CYPrecedence(value) \
559 static const int Precedence_ = value; \
560 virtual int Precedence() const { \
561 return Precedence_; \
564 #define CYRightHand(value) \
565 virtual bool RightHand() const { \
572 CYExpression *expressions_;
574 CYCompound(CYExpression *expressions = NULL) :
575 expressions_(expressions)
579 void AddPrev(CYExpression *expression) {
580 CYSetLast(expression) = expressions_;
581 expressions_ = expression;
586 virtual CYExpression *Replace(CYContext &context);
587 void Output(CYOutput &out, CYFlags flags) const;
589 virtual CYExpression *Primitive(CYContext &context);
592 struct CYDeclaration;
594 struct CYFunctionParameter :
595 CYNext<CYFunctionParameter>,
598 CYForInInitialiser *initialiser_;
600 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
601 CYNext<CYFunctionParameter>(next),
602 initialiser_(initialiser)
606 void Replace(CYContext &context, CYBlock &code);
607 void Output(CYOutput &out) const;
610 struct CYComprehension :
611 CYNext<CYComprehension>,
614 CYComprehension(CYComprehension *next = NULL) :
615 CYNext<CYComprehension>(next)
619 virtual const char *Name() const = 0;
621 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
622 CYFunctionParameter *Parameters(CYContext &context) const;
623 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
624 virtual void Output(CYOutput &out) const = 0;
627 struct CYForInComprehension :
633 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
634 CYComprehension(next),
640 virtual const char *Name() const {
641 return name_->Word();
644 virtual CYFunctionParameter *Parameter(CYContext &context) const;
645 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
646 virtual void Output(CYOutput &out) const;
649 struct CYForOfComprehension :
655 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
656 CYComprehension(next),
662 virtual const char *Name() const {
663 return name_->Word();
666 virtual CYFunctionParameter *Parameter(CYContext &context) const;
667 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
668 virtual void Output(CYOutput &out) const;
671 struct CYIfComprehension :
676 CYIfComprehension(CYExpression *test) :
681 virtual const char *Name() const {
685 virtual CYFunctionParameter *Parameter(CYContext &context) const;
686 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
687 virtual void Output(CYOutput &out) const;
690 struct CYArrayComprehension :
693 CYExpression *expression_;
694 CYComprehension *comprehensions_;
696 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
697 expression_(expression),
698 comprehensions_(comprehensions)
704 virtual CYExpression *Replace(CYContext &context);
705 virtual void Output(CYOutput &out, CYFlags flags) const;
718 virtual CYExpression *Replace(CYContext &context);
732 CYRange(uint64_t lo, uint64_t hi) :
737 bool operator [](uint8_t value) const {
738 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
741 void operator()(uint8_t value) {
744 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
748 extern CYRange DigitRange_;
749 extern CYRange WordStartRange_;
750 extern CYRange WordEndRange_;
765 CYString(const char *value) :
771 CYString(const char *value, size_t size) :
777 CYString(const CYWord *word) :
778 value_(word->Word()),
779 size_(strlen(value_))
783 const char *Value() const {
787 virtual const char *Word() const;
789 virtual CYNumber *Number(CYContext &context);
790 virtual CYString *String(CYContext &context);
792 CYString *Concat(CYContext &out, CYString *rhs) const;
793 virtual void Output(CYOutput &out, CYFlags flags) const;
794 virtual void PropertyName(CYOutput &out) const;
803 CYNumber(double value) :
808 double Value() const {
812 virtual CYNumber *Number(CYContext &context);
813 virtual CYString *String(CYContext &context);
815 virtual void Output(CYOutput &out, CYFlags flags) const;
816 virtual void PropertyName(CYOutput &out) const;
824 CYRegEx(const char *value) :
829 const char *Value() const {
833 virtual void Output(CYOutput &out, CYFlags flags) const;
845 virtual CYNumber *Number(CYContext &context);
846 virtual CYString *String(CYContext &context);
848 virtual void Output(CYOutput &out, CYFlags flags) const;
860 virtual CYExpression *Replace(CYContext &context);
861 virtual void Output(CYOutput &out, CYFlags flags) const;
867 virtual bool Value() const = 0;
868 virtual void Output(CYOutput &out, CYFlags flags) const;
880 virtual bool Value() const {
884 virtual CYNumber *Number(CYContext &context);
885 virtual CYString *String(CYContext &context);
897 virtual bool Value() const {
901 virtual CYNumber *Number(CYContext &context);
902 virtual CYString *String(CYContext &context);
910 CYVariable(CYIdentifier *name) :
915 CYVariable(const char *name) :
916 name_(new($pool) CYIdentifier(name))
923 virtual CYExpression *Replace(CYContext &context);
924 virtual void Output(CYOutput &out, CYFlags flags) const;
932 CYPrefix(CYExpression *rhs) :
937 virtual bool Alphabetic() const = 0;
938 virtual const char *Operator() const = 0;
942 virtual CYExpression *Replace(CYContext &context);
943 virtual void Output(CYOutput &out, CYFlags flags) const;
952 CYInfix(CYExpression *lhs, CYExpression *rhs) :
958 void SetLeft(CYExpression *lhs) {
962 virtual bool Alphabetic() const = 0;
963 virtual const char *Operator() const = 0;
965 virtual CYExpression *Replace(CYContext &context);
966 virtual void Output(CYOutput &out, CYFlags flags) const;
974 CYPostfix(CYExpression *lhs) :
979 virtual const char *Operator() const = 0;
983 virtual CYExpression *Replace(CYContext &context);
984 virtual void Output(CYOutput &out, CYFlags flags) const;
987 struct CYAssignment :
993 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
999 void SetLeft(CYExpression *lhs) {
1003 virtual const char *Operator() const = 0;
1007 virtual CYExpression *Replace(CYContext &context);
1008 virtual void Output(CYOutput &out, CYFlags flags) const;
1016 CYExpression *value_;
1018 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1019 CYNext<CYArgument>(next),
1025 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1026 CYNext<CYArgument>(next),
1032 CYArgument *Replace(CYContext &context);
1033 void Output(CYOutput &out) const;
1049 CYExpression *case_;
1050 CYStatement *statements_;
1052 CYClause(CYExpression *_case, CYStatement *statements) :
1054 statements_(statements)
1058 void Replace(CYContext &context);
1059 virtual void Output(CYOutput &out) const;
1066 CYExpression *value_;
1068 CYElement(CYExpression *value, CYElement *next) :
1069 CYNext<CYElement>(next),
1074 void Replace(CYContext &context);
1075 void Output(CYOutput &out) const;
1081 CYElement *elements_;
1083 CYArray(CYElement *elements = NULL) :
1088 virtual CYExpression *Replace(CYContext &context);
1089 virtual void Output(CYOutput &out, CYFlags flags) const;
1096 CYPropertyName *name_;
1097 CYExpression *value_;
1099 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1100 CYNext<CYProperty>(next),
1106 void Replace(CYContext &context);
1107 virtual void Output(CYOutput &out) const;
1110 struct CYDeclaration :
1113 CYIdentifier *identifier_;
1114 CYExpression *initialiser_;
1116 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1117 identifier_(identifier),
1118 initialiser_(initialiser)
1122 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1123 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1125 virtual CYExpression *Replace(CYContext &context);
1127 virtual CYAssignment *Assignment(CYContext &context);
1128 CYVariable *Variable(CYContext &context);
1130 virtual void Output(CYOutput &out, CYFlags flags) const;
1133 struct CYDeclarations :
1134 CYNext<CYDeclarations>,
1137 CYDeclaration *declaration_;
1139 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1140 CYNext<CYDeclarations>(next),
1141 declaration_(declaration)
1145 void Replace(CYContext &context);
1147 CYCompound *Compound(CYContext &context);
1148 CYProperty *Property(CYContext &context);
1149 CYArgument *Argument(CYContext &context);
1150 CYFunctionParameter *Parameter(CYContext &context);
1152 virtual void Output(CYOutput &out) const;
1153 virtual void Output(CYOutput &out, CYFlags flags) const;
1156 struct CYForDeclarations :
1159 CYDeclarations *declarations_;
1161 CYForDeclarations(CYDeclarations *declarations) :
1162 declarations_(declarations)
1166 virtual CYCompound *Replace(CYContext &context);
1167 virtual void Output(CYOutput &out, CYFlags flags) const;
1173 CYDeclarations *declarations_;
1175 CYVar(CYDeclarations *declarations) :
1176 declarations_(declarations)
1180 virtual CYStatement *Replace(CYContext &context);
1181 virtual void Output(CYOutput &out, CYFlags flags) const;
1184 struct CYLetStatement :
1187 CYDeclarations *declarations_;
1190 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1191 declarations_(declarations),
1196 virtual CYStatement *Replace(CYContext &context);
1197 virtual void Output(CYOutput &out, CYFlags flags) const;
1203 CYForInitialiser *initialiser_;
1204 CYExpression *test_;
1205 CYExpression *increment_;
1208 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1209 initialiser_(initialiser),
1211 increment_(increment),
1216 virtual CYStatement *Replace(CYContext &context);
1217 virtual void Output(CYOutput &out, CYFlags flags) const;
1223 CYForInInitialiser *initialiser_;
1227 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1228 initialiser_(initialiser),
1234 virtual CYStatement *Replace(CYContext &context);
1235 virtual void Output(CYOutput &out, CYFlags flags) const;
1241 CYForInInitialiser *initialiser_;
1245 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1246 initialiser_(initialiser),
1252 virtual CYStatement *Replace(CYContext &context);
1253 virtual void Output(CYOutput &out, CYFlags flags) const;
1259 CYProperty *properties_;
1261 CYObject(CYProperty *properties = NULL) :
1262 properties_(properties)
1266 virtual CYExpression *Replace(CYContext &context);
1267 void Output(CYOutput &out, CYFlags flags) const;
1273 CYExpression *object_;
1274 CYExpression *property_;
1276 CYMember(CYExpression *object, CYExpression *property) :
1282 void SetLeft(CYExpression *object) {
1287 struct CYDirectMember :
1290 CYDirectMember(CYExpression *object, CYExpression *property) :
1291 CYMember(object, property)
1298 virtual CYExpression *Replace(CYContext &context);
1299 virtual void Output(CYOutput &out, CYFlags flags) const;
1302 struct CYIndirectMember :
1305 CYIndirectMember(CYExpression *object, CYExpression *property) :
1306 CYMember(object, property)
1313 virtual CYExpression *Replace(CYContext &context);
1314 virtual void Output(CYOutput &out, CYFlags flags) const;
1323 CYExpression *constructor_;
1324 CYArgument *arguments_;
1326 New(CYExpression *constructor, CYArgument *arguments) :
1327 constructor_(constructor),
1328 arguments_(arguments)
1332 virtual int Precedence() const {
1333 return arguments_ == NULL ? 2 : 1;
1338 virtual CYExpression *Replace(CYContext &context);
1339 virtual void Output(CYOutput &out, CYFlags flags) const;
1341 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1349 CYExpression *function_;
1350 CYArgument *arguments_;
1352 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1353 function_(function),
1354 arguments_(arguments)
1361 virtual CYExpression *Replace(CYContext &context);
1362 virtual void Output(CYOutput &out, CYFlags flags) const;
1364 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1369 struct CYRubyBlock :
1372 CYExpression *call_;
1375 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1384 virtual CYExpression *Replace(CYContext &context);
1385 virtual void Output(CYOutput &out, CYFlags flags) const;
1391 CYExpression *test_;
1393 CYStatement *false_;
1395 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1402 virtual CYStatement *Replace(CYContext &context);
1403 virtual void Output(CYOutput &out, CYFlags flags) const;
1409 CYExpression *test_;
1412 CYDoWhile(CYExpression *test, CYStatement *code) :
1418 virtual CYStatement *Replace(CYContext &context);
1419 virtual void Output(CYOutput &out, CYFlags flags) const;
1425 CYExpression *test_;
1428 CYWhile(CYExpression *test, CYStatement *code) :
1434 virtual CYStatement *Replace(CYContext &context);
1435 virtual void Output(CYOutput &out, CYFlags flags) const;
1438 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1440 CYIdentifier *name_;
1441 CYFunctionParameter *parameters_;
1444 CYNonLocal *nonlocal_;
1447 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1449 parameters_(parameters),
1455 virtual ~CYFunction() {
1458 void Inject(CYContext &context);
1459 virtual void Replace_(CYContext &context, bool outer);
1460 virtual void Output(CYOutput &out, CYFlags flags) const;
1463 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1464 struct CYFunctionExpression :
1468 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1469 CYFunction(name, parameters, statements)
1476 virtual CYExpression *Replace(CYContext &context);
1477 virtual void Output(CYOutput &out, CYFlags flags) const;
1480 // XXX: this should derive from CYAnonymousFunction
1485 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1486 CYFunction(NULL, parameters, statements)
1493 virtual CYExpression *Replace(CYContext &context);
1494 virtual void Output(CYOutput &out, CYFlags flags) const;
1497 // XXX: this should derive from CYAnonymousFunctionExpression
1499 CYFunctionExpression
1501 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1502 CYFunctionExpression(NULL, parameters, statements)
1506 virtual CYExpression *Replace(CYContext &context);
1507 virtual void Output(CYOutput &out, CYFlags flags) const;
1510 // XXX: this should derive from CYNamedFunction
1511 struct CYFunctionStatement :
1515 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1516 CYFunction(name, parameters, statements)
1520 virtual CYStatement *Replace(CYContext &context);
1521 virtual void Output(CYOutput &out, CYFlags flags) const;
1527 CYExpression *expression_;
1529 CYExpress(CYExpression *expression) :
1530 expression_(expression)
1532 if (expression == NULL)
1536 virtual CYStatement *Replace(CYContext &context);
1537 virtual void Output(CYOutput &out, CYFlags flags) const;
1543 CYIdentifier *label_;
1545 CYContinue(CYIdentifier *label) :
1550 virtual CYStatement *Replace(CYContext &context);
1551 virtual void Output(CYOutput &out, CYFlags flags) const;
1557 CYIdentifier *label_;
1559 CYBreak(CYIdentifier *label) :
1564 virtual CYStatement *Replace(CYContext &context);
1565 virtual void Output(CYOutput &out, CYFlags flags) const;
1571 CYExpression *value_;
1573 CYReturn(CYExpression *value) :
1578 virtual CYStatement *Replace(CYContext &context);
1579 virtual void Output(CYOutput &out, CYFlags flags) const;
1585 virtual CYStatement *Replace(CYContext &context);
1586 virtual void Output(CYOutput &out, CYFlags flags) const;
1594 CYFinally(CYStatement *statements) :
1599 void Replace(CYContext &context);
1600 virtual void Output(CYOutput &out) const;
1603 struct CYTypeModifier :
1604 CYNext<CYTypeModifier>
1606 CYTypeModifier(CYTypeModifier *next) :
1607 CYNext<CYTypeModifier>(next)
1611 virtual int Precedence() const = 0;
1613 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1614 CYExpression *Replace(CYContext &context, CYExpression *type);
1616 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1617 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1620 struct CYTypeArrayOf :
1623 CYExpression *size_;
1625 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1626 CYTypeModifier(next),
1633 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1634 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1637 struct CYTypeConstant :
1640 CYTypeConstant(CYTypeModifier *next = NULL) :
1641 CYTypeModifier(next)
1647 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1648 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1651 struct CYTypePointerTo :
1654 CYTypePointerTo(CYTypeModifier *next = NULL) :
1655 CYTypeModifier(next)
1661 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1662 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1665 struct CYTypeVolatile :
1668 CYTypeVolatile(CYTypeModifier *next = NULL) :
1669 CYTypeModifier(next)
1675 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1676 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1679 struct CYTypedIdentifier :
1680 CYNext<CYTypedIdentifier>,
1683 CYIdentifier *identifier_;
1684 CYExpression *type_;
1685 CYTypeModifier *modifier_;
1687 CYTypedIdentifier(CYIdentifier *identifier = NULL) :
1688 identifier_(identifier),
1694 CYTypedIdentifier(CYExpression *type, CYTypeModifier *modifier = NULL) :
1701 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1702 CYSetLast(modifier_) = modifier;
1706 virtual CYExpression *Replace(CYContext &context);
1707 virtual void Output(CYOutput &out) const;
1710 struct CYEncodedType :
1713 CYTypedIdentifier *typed_;
1715 CYEncodedType(CYTypedIdentifier *typed) :
1722 virtual CYExpression *Replace(CYContext &context);
1723 virtual void Output(CYOutput &out, CYFlags flags) const;
1726 struct CYTypedParameter :
1727 CYNext<CYTypedParameter>,
1730 CYTypedIdentifier *typed_;
1732 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1733 CYNext<CYTypedParameter>(next),
1738 CYArgument *Argument(CYContext &context);
1739 CYFunctionParameter *Parameters(CYContext &context);
1740 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1742 virtual void Output(CYOutput &out) const;
1748 CYTypedIdentifier *typed_;
1749 CYTypedParameter *parameters_;
1750 CYStatement *statements_;
1752 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1754 parameters_(parameters),
1755 statements_(statements)
1761 virtual CYExpression *Replace(CYContext &context);
1762 virtual void Output(CYOutput &out, CYFlags flags) const;
1765 struct CYTypeDefinition :
1768 CYTypedIdentifier *typed_;
1770 CYTypeDefinition(CYTypedIdentifier *typed) :
1775 virtual CYStatement *Replace(CYContext &context);
1776 virtual void Output(CYOutput &out, CYFlags flags) const;
1779 struct CYTypeFunctionWith :
1782 CYTypedParameter *parameters_;
1784 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1785 CYTypeModifier(next),
1786 parameters_(parameters)
1792 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1793 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1802 CYIdentifier *name_;
1805 Catch(CYIdentifier *name, CYStatement *statements) :
1811 void Replace(CYContext &context);
1812 virtual void Output(CYOutput &out) const;
1820 CYFinally *finally_;
1822 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1829 virtual CYStatement *Replace(CYContext &context);
1830 virtual void Output(CYOutput &out, CYFlags flags) const;
1836 CYExpression *value_;
1838 Throw(CYExpression *value = NULL) :
1843 virtual CYStatement *Replace(CYContext &context);
1844 virtual void Output(CYOutput &out, CYFlags flags) const;
1852 CYExpression *scope_;
1855 CYWith(CYExpression *scope, CYStatement *code) :
1861 virtual CYStatement *Replace(CYContext &context);
1862 virtual void Output(CYOutput &out, CYFlags flags) const;
1868 CYExpression *value_;
1871 CYSwitch(CYExpression *value, CYClause *clauses) :
1877 virtual CYStatement *Replace(CYContext &context);
1878 virtual void Output(CYOutput &out, CYFlags flags) const;
1888 virtual CYStatement *Replace(CYContext &context);
1889 virtual void Output(CYOutput &out, CYFlags flags) const;
1892 struct CYCondition :
1895 CYExpression *test_;
1896 CYExpression *true_;
1897 CYExpression *false_;
1899 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1908 virtual CYExpression *Replace(CYContext &context);
1909 virtual void Output(CYOutput &out, CYFlags flags) const;
1912 struct CYAddressOf :
1915 CYAddressOf(CYExpression *rhs) :
1920 virtual const char *Operator() const {
1926 virtual CYExpression *Replace(CYContext &context);
1932 CYIndirect(CYExpression *rhs) :
1937 virtual const char *Operator() const {
1943 virtual CYExpression *Replace(CYContext &context);
1947 virtual CYExpression *Replace(CYContext &context);
1949 #define CYPostfix_(op, name, args...) \
1950 struct CY ## name : \
1953 CY ## name(CYExpression *lhs) : \
1958 virtual const char *Operator() const { \
1963 #define CYPrefix_(alphabetic, op, name, args...) \
1964 struct CY ## name : \
1967 CY ## name(CYExpression *rhs) : \
1972 CYAlphabetic(alphabetic) \
1974 virtual const char *Operator() const { \
1979 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1980 struct CY ## name : \
1983 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1988 CYAlphabetic(alphabetic) \
1989 CYPrecedence(precedence) \
1991 virtual const char *Operator() const { \
1996 #define CYAssignment_(op, name, args...) \
1997 struct CY ## name ## Assign : \
2000 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2001 CYAssignment(lhs, rhs) \
2005 virtual const char *Operator() const { \
2010 CYPostfix_("++", PostIncrement)
2011 CYPostfix_("--", PostDecrement)
2013 CYPrefix_(true, "delete", Delete)
2014 CYPrefix_(true, "void", Void)
2015 CYPrefix_(true, "typeof", TypeOf)
2016 CYPrefix_(false, "++", PreIncrement)
2017 CYPrefix_(false, "--", PreDecrement)
2018 CYPrefix_(false, "+", Affirm)
2019 CYPrefix_(false, "-", Negate)
2020 CYPrefix_(false, "~", BitwiseNot)
2021 CYPrefix_(false, "!", LogicalNot)
2023 CYInfix_(false, 5, "*", Multiply)
2024 CYInfix_(false, 5, "/", Divide)
2025 CYInfix_(false, 5, "%", Modulus)
2026 CYInfix_(false, 6, "+", Add, CYReplace)
2027 CYInfix_(false, 6, "-", Subtract)
2028 CYInfix_(false, 7, "<<", ShiftLeft)
2029 CYInfix_(false, 7, ">>", ShiftRightSigned)
2030 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2031 CYInfix_(false, 8, "<", Less)
2032 CYInfix_(false, 8, ">", Greater)
2033 CYInfix_(false, 8, "<=", LessOrEqual)
2034 CYInfix_(false, 8, ">=", GreaterOrEqual)
2035 CYInfix_(true, 8, "instanceof", InstanceOf)
2036 CYInfix_(true, 8, "in", In)
2037 CYInfix_(false, 9, "==", Equal)
2038 CYInfix_(false, 9, "!=", NotEqual)
2039 CYInfix_(false, 9, "===", Identical)
2040 CYInfix_(false, 9, "!==", NotIdentical)
2041 CYInfix_(false, 10, "&", BitwiseAnd)
2042 CYInfix_(false, 11, "^", BitwiseXOr)
2043 CYInfix_(false, 12, "|", BitwiseOr)
2044 CYInfix_(false, 13, "&&", LogicalAnd)
2045 CYInfix_(false, 14, "||", LogicalOr)
2047 CYAssignment_("=", )
2048 CYAssignment_("*=", Multiply)
2049 CYAssignment_("/=", Divide)
2050 CYAssignment_("%=", Modulus)
2051 CYAssignment_("+=", Add)
2052 CYAssignment_("-=", Subtract)
2053 CYAssignment_("<<=", ShiftLeft)
2054 CYAssignment_(">>=", ShiftRightSigned)
2055 CYAssignment_(">>>=", ShiftRightUnsigned)
2056 CYAssignment_("&=", BitwiseAnd)
2057 CYAssignment_("^=", BitwiseXOr)
2058 CYAssignment_("|=", BitwiseOr)
2060 #endif/*CYCRIPT_PARSER_HPP*/