1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
36 #include "Location.hpp"
37 #include "Pooling.hpp"
38 #include "Options.hpp"
46 virtual void Output(struct CYOutput &out) const = 0;
64 CYOutput(std::ostream &out, CYOptions &options) :
74 void Check(char value);
77 CYOutput &operator <<(char rhs);
78 CYOutput &operator <<(const char *rhs);
80 _finline CYOutput &operator <<(const CYThing *rhs) {
86 _finline CYOutput &operator <<(const CYThing &rhs) {
92 struct CYPropertyName {
93 virtual void PropertyName(CYOutput &out) const = 0;
95 virtual ~CYPropertyName() {
110 CYNoBrace = (1 << 0),
111 CYNoFunction = (1 << 1),
114 CYNoRightHand = (1 << 4),
115 CYNoDangle = (1 << 5),
116 CYNoInteger = (1 << 6),
117 CYNoBF = (CYNoBrace | CYNoFunction),
120 _finline CYFlags operator ~(CYFlags rhs) {
121 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
124 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
125 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
128 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
129 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
132 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
133 return lhs = lhs | rhs;
136 _finline CYFlags CYLeft(CYFlags flags) {
137 return flags & ~(CYNoDangle | CYNoInteger);
140 _finline CYFlags CYRight(CYFlags flags) {
141 return flags & ~CYNoBF;
144 _finline CYFlags CYCenter(CYFlags flags) {
145 return CYLeft(CYRight(flags));
151 virtual ~CYStatement() {
154 void Single(CYOutput &out, CYFlags flags) const;
155 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
157 virtual CYStatement *Replace(CYContext &context) = 0;
160 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
163 struct CYStatements {
173 operator CYStatement *() const {
177 CYStatements &operator ->*(CYStatement *next) {
179 if (first_ == NULL) {
182 } else for (;; last_ = last_->next_)
183 if (last_->next_ == NULL) {
193 virtual ~CYClassName() {
196 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
197 virtual void ClassName(CYOutput &out, bool object) const = 0;
207 CYWord(const char *word) :
212 void Set(const char *value) {
216 virtual const char *Word() const;
217 virtual void Output(CYOutput &out) const;
219 virtual CYExpression *ClassName(CYContext &context, bool object);
220 virtual void ClassName(CYOutput &out, bool object) const;
221 virtual void PropertyName(CYOutput &out) const;
224 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
226 return lhs << rhs.Word();
229 struct CYIdentifier :
230 CYNext<CYIdentifier>,
233 CYIdentifier *replace_;
237 CYIdentifier(const char *word) :
245 virtual const char *Word() const;
246 CYIdentifier *Replace(CYContext &context);
254 CYComment(const char *value) :
259 virtual CYStatement *Replace(CYContext &context);
260 virtual void Output(CYOutput &out, CYFlags flags) const;
267 CYStatement *statement_;
269 CYLabel(CYIdentifier *name, CYStatement *statement) :
271 statement_(statement)
275 virtual CYStatement *Replace(CYContext &context);
276 virtual void Output(CYOutput &out, CYFlags flags) const;
279 struct CYCStringLess :
280 std::binary_function<const char *, const char *, bool>
282 _finline bool operator ()(const char *lhs, const char *rhs) const {
283 return strcmp(lhs, rhs) < 0;
287 struct CYIdentifierValueLess :
288 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
290 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
291 return CYCStringLess()(lhs->Word(), rhs->Word());
295 enum CYIdentifierFlags {
296 CYIdentifierArgument,
297 CYIdentifierVariable,
303 typedef std::set<const char *, CYCStringLess> CYCStringSet;
304 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
305 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
307 struct CYIdentifierUsage {
308 CYIdentifier *identifier_;
312 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
318 CYStatement *&statements_;
322 CYIdentifierAddressFlagsMap internal_;
323 CYIdentifierValueSet identifiers_;
325 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
330 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
331 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
332 void Merge(CYContext &context, CYIdentifier *identifier);
333 void Scope(CYContext &context, CYStatement *&statements);
339 CYStatement *statements_;
341 CYProgram(CYStatement *statements) :
342 statements_(statements)
346 virtual void Replace(CYContext &context);
347 virtual void Output(CYOutput &out) const;
359 CYIdentifierUsageVector rename_;
361 CYNonLocal *nonlocal_;
362 CYNonLocal *nextlocal_;
365 CYContext(CYOptions &options) :
375 virtual ~CYContext() {
378 template <typename Type_>
379 void ReplaceAll(Type_ *&values) {
380 Type_ **last(&values);
381 CYForEach (next, values) {
382 Replace(*last = next);
384 last = &(*last)->next_;
388 template <typename Type_>
389 void Replace(Type_ *&value) {
390 for (;;) if (value == NULL)
393 Type_ *replace(value->Replace(*this));
394 if (replace != value)
400 void NonLocal(CYStatement *&statements);
401 CYIdentifier *Unique();
405 CYIdentifier *identifier_;
412 CYIdentifier *Target(CYContext &context) {
413 if (identifier_ == NULL)
414 identifier_ = context.Unique();
422 CYIdentifier *identifier_;
429 CYIdentifier *Identifier(CYContext &context) {
431 return next_->Identifier(context);
432 if (identifier_ == NULL)
433 identifier_ = context.Unique();
442 CYStatement *statements_;
444 CYBlock(CYStatement *statements) :
445 statements_(statements)
449 operator CYStatement *() const {
453 void AddPrev(CYStatement *statement) {
454 CYSetLast(statement) = statements_;
455 statements_ = statement;
458 virtual CYStatement *Replace(CYContext &context);
460 virtual void Output(CYOutput &out) const;
461 virtual void Output(CYOutput &out, CYFlags flags) const;
464 struct CYForInitialiser {
465 virtual ~CYForInitialiser() {
468 virtual CYExpression *Replace(CYContext &context) = 0;
469 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
472 struct CYForInInitialiser {
473 virtual ~CYForInInitialiser() {
476 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
477 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
479 virtual CYExpression *Replace(CYContext &context) = 0;
480 virtual CYAssignment *Assignment(CYContext &context) = 0;
482 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
488 struct CYExpression :
494 virtual int Precedence() const = 0;
496 virtual bool RightHand() const {
500 virtual void ForIn(CYOutput &out, CYFlags flags) const;
501 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
503 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
505 virtual void Output(CYOutput &out) const;
506 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
507 void Output(CYOutput &out, int precedence, CYFlags flags) const;
509 virtual CYExpression *ClassName(CYContext &context, bool object);
510 virtual void ClassName(CYOutput &out, bool object) const;
512 virtual CYExpression *Replace(CYContext &context) = 0;
513 virtual CYAssignment *Assignment(CYContext &context);
515 virtual CYExpression *Primitive(CYContext &context) {
519 virtual CYNumber *Number(CYContext &context) {
523 virtual CYString *String(CYContext &context) {
527 virtual const char *Word() const {
532 #define CYAlphabetic(value) \
533 virtual bool Alphabetic() const { \
537 #define CYPrecedence(value) \
538 static const int Precedence_ = value; \
539 virtual int Precedence() const { \
540 return Precedence_; \
543 #define CYRightHand(value) \
544 virtual bool RightHand() const { \
551 CYExpression *expression_;
554 CYCompound(CYExpression *expression, CYExpression *next = NULL) :
555 expression_(expression),
558 if (expression_ == NULL)
560 _assert(expression_ != NULL);
565 virtual CYExpression *Replace(CYContext &context);
566 void Output(CYOutput &out, CYFlags flags) const;
568 virtual CYExpression *Primitive(CYContext &context);
571 struct CYDeclaration;
573 struct CYFunctionParameter :
574 CYNext<CYFunctionParameter>,
577 CYForInInitialiser *initialiser_;
579 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
580 CYNext<CYFunctionParameter>(next),
581 initialiser_(initialiser)
585 void Replace(CYContext &context, CYBlock &code);
586 void Output(CYOutput &out) const;
589 struct CYComprehension :
590 CYNext<CYComprehension>,
593 CYComprehension(CYComprehension *next = NULL) :
594 CYNext<CYComprehension>(next)
598 CYComprehension *Modify(CYComprehension *next) {
603 virtual const char *Name() const = 0;
605 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
606 CYFunctionParameter *Parameters(CYContext &context) const;
607 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
608 virtual void Output(CYOutput &out) const = 0;
611 struct CYForInComprehension :
617 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
618 CYComprehension(next),
624 virtual const char *Name() const {
625 return name_->Word();
628 virtual CYFunctionParameter *Parameter(CYContext &context) const;
629 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
630 virtual void Output(CYOutput &out) const;
633 struct CYForOfComprehension :
639 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
640 CYComprehension(next),
646 virtual const char *Name() const {
647 return name_->Word();
650 virtual CYFunctionParameter *Parameter(CYContext &context) const;
651 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
652 virtual void Output(CYOutput &out) const;
655 struct CYIfComprehension :
660 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
661 CYComprehension(next),
666 virtual const char *Name() const {
670 virtual CYFunctionParameter *Parameter(CYContext &context) const;
671 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
672 virtual void Output(CYOutput &out) const;
675 struct CYArrayComprehension :
678 CYExpression *expression_;
679 CYComprehension *comprehensions_;
681 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
682 expression_(expression),
683 comprehensions_(comprehensions)
689 virtual CYExpression *Replace(CYContext &context);
690 virtual void Output(CYOutput &out, CYFlags flags) const;
699 virtual CYExpression *Primitive(CYContext &context) {
707 virtual CYExpression *Replace(CYContext &context);
721 CYRange(uint64_t lo, uint64_t hi) :
726 bool operator [](uint8_t value) const {
727 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
730 void operator()(uint8_t value) {
733 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
737 extern CYRange DigitRange_;
738 extern CYRange WordStartRange_;
739 extern CYRange WordEndRange_;
754 CYString(const char *value) :
760 CYString(const char *value, size_t size) :
766 CYString(const CYWord *word) :
767 value_(word->Word()),
768 size_(strlen(value_))
772 const char *Value() const {
776 virtual const char *Word() const;
778 virtual CYNumber *Number(CYContext &context);
779 virtual CYString *String(CYContext &context);
781 CYString *Concat(CYContext &out, CYString *rhs) const;
782 virtual void Output(CYOutput &out, CYFlags flags) const;
783 virtual void PropertyName(CYOutput &out) const;
792 CYNumber(double value) :
797 double Value() const {
801 virtual CYNumber *Number(CYContext &context);
802 virtual CYString *String(CYContext &context);
804 virtual void Output(CYOutput &out, CYFlags flags) const;
805 virtual void PropertyName(CYOutput &out) const;
813 CYRegEx(const char *value) :
818 const char *Value() const {
822 virtual void Output(CYOutput &out, CYFlags flags) const;
834 virtual CYNumber *Number(CYContext &context);
835 virtual CYString *String(CYContext &context);
837 virtual void Output(CYOutput &out, CYFlags flags) const;
849 virtual CYExpression *Replace(CYContext &context);
850 virtual void Output(CYOutput &out, CYFlags flags) const;
856 virtual bool Value() const = 0;
857 virtual void Output(CYOutput &out, CYFlags flags) const;
869 virtual bool Value() const {
873 virtual CYNumber *Number(CYContext &context);
874 virtual CYString *String(CYContext &context);
886 virtual bool Value() const {
890 virtual CYNumber *Number(CYContext &context);
891 virtual CYString *String(CYContext &context);
899 CYVariable(CYIdentifier *name) :
904 CYVariable(const char *name) :
905 name_(new($pool) CYIdentifier(name))
912 virtual CYExpression *Replace(CYContext &context);
913 virtual void Output(CYOutput &out, CYFlags flags) const;
921 CYPrefix(CYExpression *rhs) :
926 virtual bool Alphabetic() const = 0;
927 virtual const char *Operator() const = 0;
931 virtual CYExpression *Replace(CYContext &context);
932 virtual void Output(CYOutput &out, CYFlags flags) const;
941 CYInfix(CYExpression *lhs, CYExpression *rhs) :
947 void SetLeft(CYExpression *lhs) {
951 virtual bool Alphabetic() const = 0;
952 virtual const char *Operator() const = 0;
954 virtual CYExpression *Replace(CYContext &context);
955 virtual void Output(CYOutput &out, CYFlags flags) const;
963 CYPostfix(CYExpression *lhs) :
968 virtual const char *Operator() const = 0;
972 virtual CYExpression *Replace(CYContext &context);
973 virtual void Output(CYOutput &out, CYFlags flags) const;
976 struct CYAssignment :
982 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
988 void SetLeft(CYExpression *lhs) {
992 virtual const char *Operator() const = 0;
996 virtual CYExpression *Replace(CYContext &context);
997 virtual void Output(CYOutput &out, CYFlags flags) const;
1005 CYExpression *value_;
1007 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1008 CYNext<CYArgument>(next),
1014 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1015 CYNext<CYArgument>(next),
1021 CYArgument *Replace(CYContext &context);
1022 void Output(CYOutput &out) const;
1038 CYExpression *case_;
1039 CYStatement *statements_;
1041 CYClause(CYExpression *_case, CYStatement *statements) :
1043 statements_(statements)
1047 void Replace(CYContext &context);
1048 virtual void Output(CYOutput &out) const;
1055 CYExpression *value_;
1057 CYElement(CYExpression *value, CYElement *next) :
1058 CYNext<CYElement>(next),
1063 void Replace(CYContext &context);
1064 void Output(CYOutput &out) const;
1070 CYElement *elements_;
1072 CYArray(CYElement *elements = NULL) :
1077 virtual CYExpression *Replace(CYContext &context);
1078 virtual void Output(CYOutput &out, CYFlags flags) const;
1085 CYPropertyName *name_;
1086 CYExpression *value_;
1088 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1089 CYNext<CYProperty>(next),
1095 void Replace(CYContext &context);
1096 virtual void Output(CYOutput &out) const;
1099 struct CYDeclaration :
1102 CYIdentifier *identifier_;
1103 CYExpression *initialiser_;
1105 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1106 identifier_(identifier),
1107 initialiser_(initialiser)
1111 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1112 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1114 virtual CYExpression *Replace(CYContext &context);
1116 virtual CYAssignment *Assignment(CYContext &context);
1117 CYVariable *Variable(CYContext &context);
1119 virtual void Output(CYOutput &out, CYFlags flags) const;
1122 struct CYDeclarations :
1123 CYNext<CYDeclarations>,
1126 CYDeclaration *declaration_;
1128 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1129 CYNext<CYDeclarations>(next),
1130 declaration_(declaration)
1134 void Replace(CYContext &context);
1136 CYCompound *Compound(CYContext &context);
1137 CYProperty *Property(CYContext &context);
1138 CYArgument *Argument(CYContext &context);
1139 CYFunctionParameter *Parameter(CYContext &context);
1141 virtual void Output(CYOutput &out) const;
1142 virtual void Output(CYOutput &out, CYFlags flags) const;
1145 struct CYForDeclarations :
1148 CYDeclarations *declarations_;
1150 CYForDeclarations(CYDeclarations *declarations) :
1151 declarations_(declarations)
1155 virtual CYCompound *Replace(CYContext &context);
1156 virtual void Output(CYOutput &out, CYFlags flags) const;
1162 CYDeclarations *declarations_;
1164 CYVar(CYDeclarations *declarations) :
1165 declarations_(declarations)
1169 virtual CYStatement *Replace(CYContext &context);
1170 virtual void Output(CYOutput &out, CYFlags flags) const;
1173 struct CYLetStatement :
1176 CYDeclarations *declarations_;
1179 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1180 declarations_(declarations),
1185 virtual CYStatement *Replace(CYContext &context);
1186 virtual void Output(CYOutput &out, CYFlags flags) const;
1192 CYForInitialiser *initialiser_;
1193 CYExpression *test_;
1194 CYExpression *increment_;
1197 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1198 initialiser_(initialiser),
1200 increment_(increment),
1205 virtual CYStatement *Replace(CYContext &context);
1206 virtual void Output(CYOutput &out, CYFlags flags) const;
1212 CYForInInitialiser *initialiser_;
1216 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1217 initialiser_(initialiser),
1223 virtual CYStatement *Replace(CYContext &context);
1224 virtual void Output(CYOutput &out, CYFlags flags) const;
1230 CYForInInitialiser *initialiser_;
1234 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1235 initialiser_(initialiser),
1241 virtual CYStatement *Replace(CYContext &context);
1242 virtual void Output(CYOutput &out, CYFlags flags) const;
1248 CYProperty *properties_;
1250 CYObject(CYProperty *properties = NULL) :
1251 properties_(properties)
1255 virtual CYExpression *Replace(CYContext &context);
1256 void Output(CYOutput &out, CYFlags flags) const;
1262 CYExpression *object_;
1263 CYExpression *property_;
1265 CYMember(CYExpression *object, CYExpression *property) :
1271 void SetLeft(CYExpression *object) {
1276 struct CYDirectMember :
1279 CYDirectMember(CYExpression *object, CYExpression *property) :
1280 CYMember(object, property)
1287 virtual CYExpression *Replace(CYContext &context);
1288 virtual void Output(CYOutput &out, CYFlags flags) const;
1291 struct CYIndirectMember :
1294 CYIndirectMember(CYExpression *object, CYExpression *property) :
1295 CYMember(object, property)
1302 virtual CYExpression *Replace(CYContext &context);
1303 virtual void Output(CYOutput &out, CYFlags flags) const;
1312 CYExpression *constructor_;
1313 CYArgument *arguments_;
1315 New(CYExpression *constructor, CYArgument *arguments) :
1316 constructor_(constructor),
1317 arguments_(arguments)
1321 virtual int Precedence() const {
1322 return arguments_ == NULL ? 2 : 1;
1327 virtual CYExpression *Replace(CYContext &context);
1328 virtual void Output(CYOutput &out, CYFlags flags) const;
1330 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1338 CYExpression *function_;
1339 CYArgument *arguments_;
1341 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1342 function_(function),
1343 arguments_(arguments)
1350 virtual CYExpression *Replace(CYContext &context);
1351 virtual void Output(CYOutput &out, CYFlags flags) const;
1353 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1358 struct CYRubyBlock :
1361 CYExpression *call_;
1364 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1373 virtual CYExpression *Replace(CYContext &context);
1374 virtual void Output(CYOutput &out, CYFlags flags) const;
1380 CYExpression *test_;
1382 CYStatement *false_;
1384 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1391 virtual CYStatement *Replace(CYContext &context);
1392 virtual void Output(CYOutput &out, CYFlags flags) const;
1398 CYExpression *test_;
1401 CYDoWhile(CYExpression *test, CYStatement *code) :
1407 virtual CYStatement *Replace(CYContext &context);
1408 virtual void Output(CYOutput &out, CYFlags flags) const;
1414 CYExpression *test_;
1417 CYWhile(CYExpression *test, CYStatement *code) :
1423 virtual CYStatement *Replace(CYContext &context);
1424 virtual void Output(CYOutput &out, CYFlags flags) const;
1427 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1429 CYIdentifier *name_;
1430 CYFunctionParameter *parameters_;
1433 CYNonLocal *nonlocal_;
1436 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1438 parameters_(parameters),
1444 virtual ~CYFunction() {
1447 void Inject(CYContext &context);
1448 virtual void Replace_(CYContext &context, bool outer);
1449 virtual void Output(CYOutput &out, CYFlags flags) const;
1452 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1453 struct CYFunctionExpression :
1457 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1458 CYFunction(name, parameters, statements)
1465 virtual CYExpression *Replace(CYContext &context);
1466 virtual void Output(CYOutput &out, CYFlags flags) const;
1469 // XXX: this should derive from CYAnonymousFunction
1474 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1475 CYFunction(NULL, parameters, statements)
1482 virtual CYExpression *Replace(CYContext &context);
1483 virtual void Output(CYOutput &out, CYFlags flags) const;
1486 // XXX: this should derive from CYAnonymousFunctionExpression
1488 CYFunctionExpression
1490 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1491 CYFunctionExpression(NULL, parameters, statements)
1495 virtual CYExpression *Replace(CYContext &context);
1496 virtual void Output(CYOutput &out, CYFlags flags) const;
1499 // XXX: this should derive from CYNamedFunction
1500 struct CYFunctionStatement :
1504 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1505 CYFunction(name, parameters, statements)
1509 virtual CYStatement *Replace(CYContext &context);
1510 virtual void Output(CYOutput &out, CYFlags flags) const;
1516 CYExpression *expression_;
1518 CYExpress(CYExpression *expression) :
1519 expression_(expression)
1521 if (expression_ == NULL)
1525 virtual CYStatement *Replace(CYContext &context);
1526 virtual void Output(CYOutput &out, CYFlags flags) const;
1532 CYIdentifier *label_;
1534 CYContinue(CYIdentifier *label) :
1539 virtual CYStatement *Replace(CYContext &context);
1540 virtual void Output(CYOutput &out, CYFlags flags) const;
1546 CYIdentifier *label_;
1548 CYBreak(CYIdentifier *label) :
1553 virtual CYStatement *Replace(CYContext &context);
1554 virtual void Output(CYOutput &out, CYFlags flags) const;
1560 CYExpression *value_;
1562 CYReturn(CYExpression *value) :
1567 virtual CYStatement *Replace(CYContext &context);
1568 virtual void Output(CYOutput &out, CYFlags flags) const;
1574 virtual CYStatement *Replace(CYContext &context);
1575 virtual void Output(CYOutput &out, CYFlags flags) const;
1583 CYFinally(CYStatement *statements) :
1588 void Replace(CYContext &context);
1589 virtual void Output(CYOutput &out) const;
1592 struct CYTypeSpecifier :
1595 virtual CYExpression *Replace(CYContext &context) = 0;
1598 struct CYTypeError :
1604 virtual CYExpression *Replace(CYContext &context);
1605 virtual void Output(CYOutput &out) const;
1614 virtual CYExpression *Replace(CYContext &context);
1615 virtual void Output(CYOutput &out) const;
1618 struct CYTypeVariable :
1621 CYIdentifier *name_;
1623 CYTypeVariable(CYIdentifier *name) :
1628 CYTypeVariable(const char *name) :
1629 name_(new($pool) CYIdentifier(name))
1633 virtual CYExpression *Replace(CYContext &context);
1634 virtual void Output(CYOutput &out) const;
1637 struct CYTypeUnsigned :
1640 CYTypeSpecifier *specifier_;
1642 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1643 specifier_(specifier)
1647 virtual CYExpression *Replace(CYContext &context);
1648 virtual void Output(CYOutput &out) const;
1651 struct CYTypeSigned :
1654 CYTypeSpecifier *specifier_;
1656 CYTypeSigned(CYTypeSpecifier *specifier) :
1657 specifier_(specifier)
1661 virtual CYExpression *Replace(CYContext &context);
1662 virtual void Output(CYOutput &out) const;
1668 CYTypeSpecifier *specifier_;
1670 CYTypeLong(CYTypeSpecifier *specifier) :
1671 specifier_(specifier)
1675 virtual CYExpression *Replace(CYContext &context);
1676 virtual void Output(CYOutput &out) const;
1679 struct CYTypeShort :
1682 CYTypeSpecifier *specifier_;
1684 CYTypeShort(CYTypeSpecifier *specifier) :
1685 specifier_(specifier)
1689 virtual CYExpression *Replace(CYContext &context);
1690 virtual void Output(CYOutput &out) const;
1693 struct CYTypeFunctionWith;
1695 struct CYTypeModifier :
1696 CYNext<CYTypeModifier>
1698 CYTypeModifier(CYTypeModifier *next) :
1699 CYNext<CYTypeModifier>(next)
1703 virtual int Precedence() const = 0;
1705 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1706 CYExpression *Replace(CYContext &context, CYExpression *type);
1708 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1709 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1711 virtual CYTypeFunctionWith *Function() { return NULL; }
1714 struct CYTypeArrayOf :
1717 CYExpression *size_;
1719 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1720 CYTypeModifier(next),
1727 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1728 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1731 struct CYTypeConstant :
1734 CYTypeConstant(CYTypeModifier *next = NULL) :
1735 CYTypeModifier(next)
1741 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1742 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1745 struct CYTypePointerTo :
1748 CYTypePointerTo(CYTypeModifier *next = NULL) :
1749 CYTypeModifier(next)
1755 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1756 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1759 struct CYTypeVolatile :
1762 CYTypeVolatile(CYTypeModifier *next = NULL) :
1763 CYTypeModifier(next)
1769 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1770 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1773 struct CYTypedIdentifier :
1774 CYNext<CYTypedIdentifier>,
1777 CYLocation location_;
1778 CYIdentifier *identifier_;
1779 CYTypeSpecifier *specifier_;
1780 CYTypeModifier *modifier_;
1782 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1783 location_(location),
1784 identifier_(identifier),
1790 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1792 specifier_(specifier),
1797 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1798 CYSetLast(modifier_) = modifier;
1802 virtual CYExpression *Replace(CYContext &context);
1803 virtual void Output(CYOutput &out) const;
1805 CYTypeFunctionWith *Function();
1808 struct CYEncodedType :
1811 CYTypedIdentifier *typed_;
1813 CYEncodedType(CYTypedIdentifier *typed) :
1820 virtual CYExpression *Replace(CYContext &context);
1821 virtual void Output(CYOutput &out, CYFlags flags) const;
1824 struct CYTypedParameter :
1825 CYNext<CYTypedParameter>,
1828 CYTypedIdentifier *typed_;
1830 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1831 CYNext<CYTypedParameter>(next),
1836 CYArgument *Argument(CYContext &context);
1837 CYFunctionParameter *Parameters(CYContext &context);
1838 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1840 virtual void Output(CYOutput &out) const;
1846 CYTypedIdentifier *typed_;
1847 CYTypedParameter *parameters_;
1848 CYStatement *statements_;
1850 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1852 parameters_(parameters),
1853 statements_(statements)
1859 virtual CYExpression *Replace(CYContext &context);
1860 virtual void Output(CYOutput &out, CYFlags flags) const;
1869 CYModule(CYWord *part, CYModule *next = NULL) :
1870 CYNext<CYModule>(next),
1875 CYString *Replace(CYContext &context, const char *separator) const;
1876 void Output(CYOutput &out) const;
1884 CYImport(CYModule *module) :
1889 virtual CYStatement *Replace(CYContext &context);
1890 virtual void Output(CYOutput &out, CYFlags flags) const;
1897 CYTypedIdentifier *typed_;
1899 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1905 virtual CYStatement *Replace(CYContext &context);
1906 virtual void Output(CYOutput &out, CYFlags flags) const;
1909 struct CYTypeDefinition :
1912 CYTypedIdentifier *typed_;
1914 CYTypeDefinition(CYTypedIdentifier *typed) :
1919 virtual CYStatement *Replace(CYContext &context);
1920 virtual void Output(CYOutput &out, CYFlags flags) const;
1923 struct CYTypeBlockWith :
1926 CYTypedParameter *parameters_;
1928 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1929 CYTypeModifier(next),
1930 parameters_(parameters)
1936 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1937 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1940 struct CYTypeFunctionWith :
1943 CYTypedParameter *parameters_;
1945 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1946 CYTypeModifier(next),
1947 parameters_(parameters)
1953 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1954 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1956 virtual CYTypeFunctionWith *Function() { return this; }
1965 CYIdentifier *name_;
1968 Catch(CYIdentifier *name, CYStatement *statements) :
1974 void Replace(CYContext &context);
1975 virtual void Output(CYOutput &out) const;
1983 CYFinally *finally_;
1985 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1992 virtual CYStatement *Replace(CYContext &context);
1993 virtual void Output(CYOutput &out, CYFlags flags) const;
1999 CYExpression *value_;
2001 Throw(CYExpression *value = NULL) :
2006 virtual CYStatement *Replace(CYContext &context);
2007 virtual void Output(CYOutput &out, CYFlags flags) const;
2015 CYExpression *scope_;
2018 CYWith(CYExpression *scope, CYStatement *code) :
2024 virtual CYStatement *Replace(CYContext &context);
2025 virtual void Output(CYOutput &out, CYFlags flags) const;
2031 CYExpression *value_;
2034 CYSwitch(CYExpression *value, CYClause *clauses) :
2040 virtual CYStatement *Replace(CYContext &context);
2041 virtual void Output(CYOutput &out, CYFlags flags) const;
2051 virtual CYStatement *Replace(CYContext &context);
2052 virtual void Output(CYOutput &out, CYFlags flags) const;
2055 struct CYCondition :
2058 CYExpression *test_;
2059 CYExpression *true_;
2060 CYExpression *false_;
2062 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2071 virtual CYExpression *Replace(CYContext &context);
2072 virtual void Output(CYOutput &out, CYFlags flags) const;
2075 struct CYAddressOf :
2078 CYAddressOf(CYExpression *rhs) :
2083 virtual const char *Operator() const {
2089 virtual CYExpression *Replace(CYContext &context);
2095 CYIndirect(CYExpression *rhs) :
2100 virtual const char *Operator() const {
2106 virtual CYExpression *Replace(CYContext &context);
2110 virtual CYExpression *Replace(CYContext &context);
2112 #define CYPostfix_(op, name, args...) \
2113 struct CY ## name : \
2116 CY ## name(CYExpression *lhs) : \
2121 virtual const char *Operator() const { \
2126 #define CYPrefix_(alphabetic, op, name, args...) \
2127 struct CY ## name : \
2130 CY ## name(CYExpression *rhs) : \
2135 CYAlphabetic(alphabetic) \
2137 virtual const char *Operator() const { \
2142 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2143 struct CY ## name : \
2146 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2151 CYAlphabetic(alphabetic) \
2152 CYPrecedence(precedence) \
2154 virtual const char *Operator() const { \
2159 #define CYAssignment_(op, name, args...) \
2160 struct CY ## name ## Assign : \
2163 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2164 CYAssignment(lhs, rhs) \
2168 virtual const char *Operator() const { \
2173 CYPostfix_("++", PostIncrement)
2174 CYPostfix_("--", PostDecrement)
2176 CYPrefix_(true, "delete", Delete)
2177 CYPrefix_(true, "void", Void)
2178 CYPrefix_(true, "typeof", TypeOf)
2179 CYPrefix_(false, "++", PreIncrement)
2180 CYPrefix_(false, "--", PreDecrement)
2181 CYPrefix_(false, "+", Affirm)
2182 CYPrefix_(false, "-", Negate)
2183 CYPrefix_(false, "~", BitwiseNot)
2184 CYPrefix_(false, "!", LogicalNot)
2186 CYInfix_(false, 5, "*", Multiply, CYReplace)
2187 CYInfix_(false, 5, "/", Divide)
2188 CYInfix_(false, 5, "%", Modulus)
2189 CYInfix_(false, 6, "+", Add, CYReplace)
2190 CYInfix_(false, 6, "-", Subtract)
2191 CYInfix_(false, 7, "<<", ShiftLeft)
2192 CYInfix_(false, 7, ">>", ShiftRightSigned)
2193 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2194 CYInfix_(false, 8, "<", Less)
2195 CYInfix_(false, 8, ">", Greater)
2196 CYInfix_(false, 8, "<=", LessOrEqual)
2197 CYInfix_(false, 8, ">=", GreaterOrEqual)
2198 CYInfix_(true, 8, "instanceof", InstanceOf)
2199 CYInfix_(true, 8, "in", In)
2200 CYInfix_(false, 9, "==", Equal)
2201 CYInfix_(false, 9, "!=", NotEqual)
2202 CYInfix_(false, 9, "===", Identical)
2203 CYInfix_(false, 9, "!==", NotIdentical)
2204 CYInfix_(false, 10, "&", BitwiseAnd)
2205 CYInfix_(false, 11, "^", BitwiseXOr)
2206 CYInfix_(false, 12, "|", BitwiseOr)
2207 CYInfix_(false, 13, "&&", LogicalAnd)
2208 CYInfix_(false, 14, "||", LogicalOr)
2210 CYAssignment_("=", )
2211 CYAssignment_("*=", Multiply)
2212 CYAssignment_("/=", Divide)
2213 CYAssignment_("%=", Modulus)
2214 CYAssignment_("+=", Add)
2215 CYAssignment_("-=", Subtract)
2216 CYAssignment_("<<=", ShiftLeft)
2217 CYAssignment_(">>=", ShiftRightSigned)
2218 CYAssignment_(">>>=", ShiftRightUnsigned)
2219 CYAssignment_("&=", BitwiseAnd)
2220 CYAssignment_("^=", BitwiseXOr)
2221 CYAssignment_("|=", BitwiseOr)
2223 #endif/*CYCRIPT_PARSER_HPP*/