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
35 #include "Location.hpp"
36 #include "Pooling.hpp"
37 #include "Options.hpp"
42 virtual void Output(struct CYOutput &out) const = 0;
63 CYOutput(std::streambuf &out, CYOptions &options) :
74 void Check(char value);
77 _finline void operator ()(char value) {
78 _assert(out_.sputc(value) != EOF);
86 _finline void operator ()(const char *data, std::streamsize size) {
87 _assert(out_.sputn(data, size) == size);
89 position_.columns(size);
92 _finline void operator ()(const char *data) {
93 return operator ()(data, strlen(data));
96 CYOutput &operator <<(char rhs);
97 CYOutput &operator <<(const char *rhs);
99 _finline CYOutput &operator <<(const CYThing *rhs) {
105 _finline CYOutput &operator <<(const CYThing &rhs) {
111 struct CYPropertyName {
112 virtual void PropertyName(CYOutput &out) const = 0;
126 CYNoBrace = (1 << 0),
127 CYNoFunction = (1 << 1),
130 CYNoRightHand = (1 << 4),
131 CYNoDangle = (1 << 5),
132 CYNoInteger = (1 << 6),
133 CYNoBF = (CYNoBrace | CYNoFunction),
136 _finline CYFlags operator ~(CYFlags rhs) {
137 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
140 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
141 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
144 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
145 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
148 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
149 return lhs = lhs | rhs;
152 _finline CYFlags CYLeft(CYFlags flags) {
153 return flags & ~(CYNoDangle | CYNoInteger);
156 _finline CYFlags CYRight(CYFlags flags) {
157 return flags & ~CYNoBF;
160 _finline CYFlags CYCenter(CYFlags flags) {
161 return CYLeft(CYRight(flags));
170 #define CYCompact(type) \
171 virtual CYCompactType Compact() const { \
172 return CYCompact ## type; \
179 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
180 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
181 virtual void Output(CYOutput &out) const;
183 virtual CYStatement *Replace(CYContext &context) = 0;
185 virtual CYCompactType Compact() const = 0;
186 virtual CYStatement *Return();
189 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
192 struct CYStatements {
202 operator CYStatement *() const {
206 CYStatements &operator ->*(CYStatement *next) {
208 if (first_ == NULL) {
211 } else for (;; last_ = last_->next_)
212 if (last_->next_ == NULL) {
222 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
223 virtual void ClassName(CYOutput &out, bool object) const = 0;
233 CYWord(const char *word) :
238 void Set(const char *value) {
242 virtual const char *Word() const;
243 virtual void Output(CYOutput &out) const;
245 virtual CYExpression *ClassName(CYContext &context, bool object);
246 virtual void ClassName(CYOutput &out, bool object) const;
247 virtual void PropertyName(CYOutput &out) const;
250 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
252 return lhs << rhs.Word();
255 struct CYIdentifier :
256 CYNext<CYIdentifier>,
259 CYIdentifier *replace_;
263 CYIdentifier(const char *word) :
271 virtual const char *Word() const;
272 CYIdentifier *Replace(CYContext &context);
279 CYStatement *statement_;
281 CYLabel(CYIdentifier *name, CYStatement *statement) :
283 statement_(statement)
289 virtual CYStatement *Replace(CYContext &context);
290 virtual void Output(CYOutput &out, CYFlags flags) const;
293 struct CYCStringLess :
294 std::binary_function<const char *, const char *, bool>
296 _finline bool operator ()(const char *lhs, const char *rhs) const {
297 return strcmp(lhs, rhs) < 0;
301 struct CYIdentifierValueLess :
302 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
304 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
305 return CYCStringLess()(lhs->Word(), rhs->Word());
309 enum CYIdentifierFlags {
310 CYIdentifierArgument,
311 CYIdentifierVariable,
317 typedef std::set<const char *, CYCStringLess> CYCStringSet;
318 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
319 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
321 struct CYIdentifierUsage {
322 CYIdentifier *identifier_;
326 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
332 CYIdentifierAddressFlagsMap internal_;
333 CYIdentifierValueSet identifiers_;
335 CYScope(bool transparent, CYContext &context);
337 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
338 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
339 void Merge(CYContext &context, CYIdentifier *identifier);
340 void Close(CYContext &context, CYStatement *&statements);
348 CYScript(CYStatement *code) :
353 virtual void Replace(CYContext &context);
354 virtual void Output(CYOutput &out) const;
366 CYIdentifierUsageVector rename_;
368 CYNonLocal *nonlocal_;
369 CYNonLocal *nextlocal_;
372 CYContext(CYOptions &options) :
382 void ReplaceAll(CYStatement *&statement) {
383 if (statement == NULL)
385 CYStatement *next(statement->next_);
390 if (statement == NULL)
393 statement->SetNext(next);
396 template <typename Type_>
397 void Replace(Type_ *&value) {
398 for (;;) if (value == NULL)
401 Type_ *replace(value->Replace(*this));
402 if (replace != value)
408 void NonLocal(CYStatement *&statements);
409 CYIdentifier *Unique();
413 CYIdentifier *identifier_;
420 CYIdentifier *Target(CYContext &context) {
421 if (identifier_ == NULL)
422 identifier_ = context.Unique();
430 CYIdentifier *identifier_;
437 CYIdentifier *Identifier(CYContext &context) {
439 return next_->Identifier(context);
440 if (identifier_ == NULL)
441 identifier_ = context.Unique();
451 CYBlock(CYStatement *code) :
458 virtual CYStatement *Replace(CYContext &context);
460 virtual void Output(CYOutput &out, CYFlags flags) const;
462 virtual CYStatement *Return();
465 struct CYForInitializer {
466 virtual CYExpression *Replace(CYContext &context) = 0;
467 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
470 struct CYForInInitializer {
471 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
472 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
474 virtual CYExpression *Replace(CYContext &context) = 0;
475 virtual CYAssignment *Assignment(CYContext &context) = 0;
477 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
480 struct CYFunctionParameter;
485 struct CYExpression :
491 virtual int Precedence() const = 0;
493 virtual bool RightHand() const {
497 virtual void ForIn(CYOutput &out, CYFlags flags) const;
498 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
500 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
502 virtual void Output(CYOutput &out) const;
503 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
504 void Output(CYOutput &out, int precedence, CYFlags flags) const;
506 virtual CYExpression *ClassName(CYContext &context, bool object);
507 virtual void ClassName(CYOutput &out, bool object) const;
509 virtual CYExpression *Replace(CYContext &context) = 0;
510 virtual CYAssignment *Assignment(CYContext &context);
512 virtual CYExpression *Primitive(CYContext &context) {
516 virtual CYFunctionParameter *Parameter() const;
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) :
554 expression_(expression),
557 _assert(expression_ != NULL);
558 _assert(next != NULL);
563 virtual CYExpression *Replace(CYContext &context);
564 void Output(CYOutput &out, CYFlags flags) const;
566 virtual CYFunctionParameter *Parameter() const;
569 struct CYParenthetical :
572 CYExpression *expression_;
574 CYParenthetical(CYExpression *expression) :
575 expression_(expression)
581 virtual CYExpression *Replace(CYContext &context);
582 void Output(CYOutput &out, CYFlags flags) const;
585 struct CYDeclaration;
587 struct CYFunctionParameter :
588 CYNext<CYFunctionParameter>,
591 CYForInInitializer *initialiser_;
593 CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
594 CYNext<CYFunctionParameter>(next),
595 initialiser_(initialiser)
599 void Replace(CYContext &context, CYStatement *&statements);
600 void Output(CYOutput &out) const;
603 struct CYComprehension :
604 CYNext<CYComprehension>,
607 CYComprehension(CYComprehension *next = NULL) :
608 CYNext<CYComprehension>(next)
612 CYComprehension *Modify(CYComprehension *next) {
617 virtual const char *Name() const = 0;
619 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
620 CYFunctionParameter *Parameters(CYContext &context) const;
621 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
622 virtual void Output(CYOutput &out) const = 0;
625 struct CYForInComprehension :
631 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
632 CYComprehension(next),
638 virtual const char *Name() const {
639 return name_->Word();
642 virtual CYFunctionParameter *Parameter(CYContext &context) const;
643 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
644 virtual void Output(CYOutput &out) const;
647 struct CYForOfComprehension :
653 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
654 CYComprehension(next),
660 virtual const char *Name() const {
661 return name_->Word();
664 virtual CYFunctionParameter *Parameter(CYContext &context) const;
665 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
666 virtual void Output(CYOutput &out) const;
669 struct CYIfComprehension :
674 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
675 CYComprehension(next),
680 virtual const char *Name() const {
684 virtual CYFunctionParameter *Parameter(CYContext &context) const;
685 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
686 virtual void Output(CYOutput &out) const;
689 struct CYArrayComprehension :
692 CYExpression *expression_;
693 CYComprehension *comprehensions_;
695 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
696 expression_(expression),
697 comprehensions_(comprehensions)
703 virtual CYExpression *Replace(CYContext &context);
704 virtual void Output(CYOutput &out, CYFlags flags) const;
713 virtual CYExpression *Primitive(CYContext &context) {
721 virtual CYExpression *Replace(CYContext &context);
735 CYRange(uint64_t lo, uint64_t hi) :
740 bool operator [](uint8_t value) const {
741 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
744 void operator()(uint8_t value) {
747 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
751 extern CYRange DigitRange_;
752 extern CYRange WordStartRange_;
753 extern CYRange WordEndRange_;
768 CYString(const char *value) :
774 CYString(const char *value, size_t size) :
780 CYString(const CYWord *word) :
781 value_(word->Word()),
782 size_(strlen(value_))
786 const char *Value() const {
790 virtual const char *Word() const;
792 virtual CYNumber *Number(CYContext &context);
793 virtual CYString *String(CYContext &context);
795 CYString *Concat(CYContext &out, CYString *rhs) const;
796 virtual void Output(CYOutput &out, CYFlags flags) const;
797 virtual void PropertyName(CYOutput &out) const;
800 struct CYElementValue;
805 CYExpression *expression_;
808 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
809 CYNext<CYSpan>(next),
810 expression_(expression),
815 CYElementValue *Replace(CYContext &context);
824 CYTemplate(CYString *string, CYSpan *spans) :
833 virtual CYExpression *Replace(CYContext &context);
834 virtual void Output(CYOutput &out, CYFlags flags) const;
843 CYNumber(double value) :
848 double Value() const {
852 virtual CYNumber *Number(CYContext &context);
853 virtual CYString *String(CYContext &context);
855 virtual void Output(CYOutput &out, CYFlags flags) const;
856 virtual void PropertyName(CYOutput &out) const;
865 CYRegEx(const char *value, size_t size) :
871 const char *Value() const {
875 virtual void Output(CYOutput &out, CYFlags flags) const;
881 virtual CYNumber *Number(CYContext &context);
882 virtual CYString *String(CYContext &context);
884 virtual void Output(CYOutput &out, CYFlags flags) const;
890 virtual CYExpression *Replace(CYContext &context);
891 virtual void Output(CYOutput &out, CYFlags flags) const;
897 virtual bool Value() const = 0;
898 virtual void Output(CYOutput &out, CYFlags flags) const;
904 virtual bool Value() const {
908 virtual CYNumber *Number(CYContext &context);
909 virtual CYString *String(CYContext &context);
915 virtual bool Value() const {
919 virtual CYNumber *Number(CYContext &context);
920 virtual CYString *String(CYContext &context);
928 CYVariable(CYIdentifier *name) :
933 CYVariable(const char *name) :
934 name_(new($pool) CYIdentifier(name))
941 virtual CYExpression *Replace(CYContext &context);
942 virtual void Output(CYOutput &out, CYFlags flags) const;
944 virtual CYFunctionParameter *Parameter() const;
952 CYPrefix(CYExpression *rhs) :
957 virtual bool Alphabetic() const = 0;
958 virtual const char *Operator() const = 0;
962 virtual CYExpression *Replace(CYContext &context);
963 virtual void Output(CYOutput &out, CYFlags flags) const;
972 CYInfix(CYExpression *lhs, CYExpression *rhs) :
978 void SetLeft(CYExpression *lhs) {
982 virtual bool Alphabetic() const = 0;
983 virtual const char *Operator() const = 0;
985 virtual CYExpression *Replace(CYContext &context);
986 virtual void Output(CYOutput &out, CYFlags flags) const;
994 CYPostfix(CYExpression *lhs) :
999 virtual const char *Operator() const = 0;
1003 virtual CYExpression *Replace(CYContext &context);
1004 virtual void Output(CYOutput &out, CYFlags flags) const;
1007 struct CYAssignment :
1013 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1019 void SetLeft(CYExpression *lhs) {
1023 virtual const char *Operator() const = 0;
1027 virtual CYExpression *Replace(CYContext &context);
1028 virtual void Output(CYOutput &out, CYFlags flags) const;
1036 CYExpression *value_;
1038 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1039 CYNext<CYArgument>(next),
1045 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1046 CYNext<CYArgument>(next),
1052 CYArgument *Replace(CYContext &context);
1053 void Output(CYOutput &out) const;
1060 CYExpression *case_;
1063 CYClause(CYExpression *_case, CYStatement *code) :
1069 void Replace(CYContext &context);
1070 virtual void Output(CYOutput &out) const;
1076 virtual bool Elision() const = 0;
1078 virtual void Replace(CYContext &context) = 0;
1081 struct CYElementValue :
1085 CYExpression *value_;
1087 CYElementValue(CYExpression *value, CYElement *next) :
1088 CYNext<CYElement>(next),
1093 virtual bool Elision() const {
1094 return value_ == NULL;
1097 virtual void Replace(CYContext &context);
1098 virtual void Output(CYOutput &out) const;
1101 struct CYElementSpread :
1104 CYExpression *value_;
1106 CYElementSpread(CYExpression *value) :
1111 virtual bool Elision() const {
1115 virtual void Replace(CYContext &context);
1116 virtual void Output(CYOutput &out) const;
1122 CYElement *elements_;
1124 CYArray(CYElement *elements = NULL) :
1129 virtual CYExpression *Replace(CYContext &context);
1130 virtual void Output(CYOutput &out, CYFlags flags) const;
1137 CYPropertyName *name_;
1138 CYExpression *value_;
1140 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1141 CYNext<CYProperty>(next),
1147 void Replace(CYContext &context);
1148 virtual void Output(CYOutput &out) const;
1151 struct CYDeclaration :
1154 CYIdentifier *identifier_;
1155 CYExpression *initialiser_;
1157 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1158 identifier_(identifier),
1159 initialiser_(initialiser)
1163 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1164 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1166 virtual CYExpression *Replace(CYContext &context);
1168 virtual CYAssignment *Assignment(CYContext &context);
1169 CYVariable *Variable(CYContext &context);
1171 virtual void Output(CYOutput &out, CYFlags flags) const;
1174 struct CYDeclarations :
1175 CYNext<CYDeclarations>,
1178 CYDeclaration *declaration_;
1180 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1181 CYNext<CYDeclarations>(next),
1182 declaration_(declaration)
1186 void Replace(CYContext &context);
1188 CYExpression *Expression(CYContext &context);
1189 CYProperty *Property(CYContext &context);
1190 CYArgument *Argument(CYContext &context);
1191 CYFunctionParameter *Parameter(CYContext &context);
1193 virtual void Output(CYOutput &out) const;
1194 virtual void Output(CYOutput &out, CYFlags flags) const;
1197 struct CYForDeclarations :
1200 CYDeclarations *declarations_;
1202 CYForDeclarations(CYDeclarations *declarations) :
1203 declarations_(declarations)
1207 virtual CYExpression *Replace(CYContext &context);
1208 virtual void Output(CYOutput &out, CYFlags flags) const;
1214 CYDeclarations *declarations_;
1216 CYVar(CYDeclarations *declarations) :
1217 declarations_(declarations)
1223 virtual CYStatement *Replace(CYContext &context);
1224 virtual void Output(CYOutput &out, CYFlags flags) const;
1227 struct CYLetStatement :
1230 CYDeclarations *declarations_;
1233 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1234 declarations_(declarations),
1241 virtual CYStatement *Replace(CYContext &context);
1242 virtual void Output(CYOutput &out, CYFlags flags) const;
1248 CYForInitializer *initialiser_;
1249 CYExpression *test_;
1250 CYExpression *increment_;
1253 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1254 initialiser_(initialiser),
1256 increment_(increment),
1263 virtual CYStatement *Replace(CYContext &context);
1264 virtual void Output(CYOutput &out, CYFlags flags) const;
1270 CYForInInitializer *initialiser_;
1274 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1275 initialiser_(initialiser),
1283 virtual CYStatement *Replace(CYContext &context);
1284 virtual void Output(CYOutput &out, CYFlags flags) const;
1290 CYForInInitializer *initialiser_;
1294 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1295 initialiser_(initialiser),
1303 virtual CYStatement *Replace(CYContext &context);
1304 virtual void Output(CYOutput &out, CYFlags flags) const;
1310 CYProperty *properties_;
1312 CYObject(CYProperty *properties = NULL) :
1313 properties_(properties)
1317 virtual CYExpression *Replace(CYContext &context);
1318 void Output(CYOutput &out, CYFlags flags) const;
1324 CYExpression *object_;
1325 CYExpression *property_;
1327 CYMember(CYExpression *object, CYExpression *property) :
1333 void SetLeft(CYExpression *object) {
1338 struct CYDirectMember :
1341 CYDirectMember(CYExpression *object, CYExpression *property) :
1342 CYMember(object, property)
1349 virtual CYExpression *Replace(CYContext &context);
1350 virtual void Output(CYOutput &out, CYFlags flags) const;
1353 struct CYIndirectMember :
1356 CYIndirectMember(CYExpression *object, CYExpression *property) :
1357 CYMember(object, property)
1364 virtual CYExpression *Replace(CYContext &context);
1365 virtual void Output(CYOutput &out, CYFlags flags) const;
1374 CYExpression *constructor_;
1375 CYArgument *arguments_;
1377 New(CYExpression *constructor, CYArgument *arguments) :
1378 constructor_(constructor),
1379 arguments_(arguments)
1383 virtual int Precedence() const {
1384 return arguments_ == NULL ? 2 : 1;
1389 virtual CYExpression *Replace(CYContext &context);
1390 virtual void Output(CYOutput &out, CYFlags flags) const;
1392 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1400 CYExpression *function_;
1401 CYArgument *arguments_;
1403 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1404 function_(function),
1405 arguments_(arguments)
1412 virtual CYExpression *Replace(CYContext &context);
1413 virtual void Output(CYOutput &out, CYFlags flags) const;
1415 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1420 struct CYRubyBlock :
1423 CYExpression *call_;
1426 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1435 virtual CYExpression *Replace(CYContext &context);
1436 virtual void Output(CYOutput &out, CYFlags flags) const;
1442 CYExpression *test_;
1444 CYStatement *false_;
1446 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1455 virtual CYStatement *Replace(CYContext &context);
1456 virtual void Output(CYOutput &out, CYFlags flags) const;
1458 virtual CYStatement *Return();
1464 CYExpression *test_;
1467 CYDoWhile(CYExpression *test, CYStatement *code) :
1475 virtual CYStatement *Replace(CYContext &context);
1476 virtual void Output(CYOutput &out, CYFlags flags) const;
1482 CYExpression *test_;
1485 CYWhile(CYExpression *test, CYStatement *code) :
1493 virtual CYStatement *Replace(CYContext &context);
1494 virtual void Output(CYOutput &out, CYFlags flags) const;
1497 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1499 CYIdentifier *name_;
1500 CYFunctionParameter *parameters_;
1503 CYNonLocal *nonlocal_;
1507 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1509 parameters_(parameters),
1516 void Inject(CYContext &context);
1517 virtual void Replace_(CYContext &context, bool outer);
1518 virtual void Output(CYOutput &out, CYFlags flags) const;
1521 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1522 struct CYFunctionExpression :
1526 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1527 CYFunction(name, parameters, code)
1534 virtual CYExpression *Replace(CYContext &context);
1535 virtual void Output(CYOutput &out, CYFlags flags) const;
1538 // XXX: this should derive from CYAnonymousFunction
1543 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1544 CYFunction(NULL, parameters, code)
1551 virtual CYExpression *Replace(CYContext &context);
1552 virtual void Output(CYOutput &out, CYFlags flags) const;
1555 // XXX: this should derive from CYAnonymousFunctionExpression
1557 CYFunctionExpression
1559 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1560 CYFunctionExpression(NULL, parameters, code)
1564 virtual CYExpression *Replace(CYContext &context);
1565 virtual void Output(CYOutput &out, CYFlags flags) const;
1568 // XXX: this should derive from CYNamedFunction
1569 struct CYFunctionStatement :
1573 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1574 CYFunction(name, parameters, code)
1580 virtual CYStatement *Replace(CYContext &context);
1581 virtual void Output(CYOutput &out, CYFlags flags) const;
1587 CYExpression *expression_;
1589 CYExpress(CYExpression *expression) :
1590 expression_(expression)
1592 if (expression_ == NULL)
1598 virtual CYStatement *Replace(CYContext &context);
1599 virtual void Output(CYOutput &out, CYFlags flags) const;
1601 virtual CYStatement *Return();
1607 CYIdentifier *label_;
1609 CYContinue(CYIdentifier *label) :
1616 virtual CYStatement *Replace(CYContext &context);
1617 virtual void Output(CYOutput &out, CYFlags flags) const;
1623 CYIdentifier *label_;
1625 CYBreak(CYIdentifier *label) :
1632 virtual CYStatement *Replace(CYContext &context);
1633 virtual void Output(CYOutput &out, CYFlags flags) const;
1639 CYExpression *value_;
1641 CYReturn(CYExpression *value) :
1648 virtual CYStatement *Replace(CYContext &context);
1649 virtual void Output(CYOutput &out, CYFlags flags) const;
1652 struct CYYieldGenerator :
1655 CYExpression *value_;
1657 CYYieldGenerator(CYExpression *value) :
1664 virtual CYExpression *Replace(CYContext &context);
1665 virtual void Output(CYOutput &out, CYFlags flags) const;
1668 struct CYYieldValue :
1671 CYExpression *value_;
1673 CYYieldValue(CYExpression *value) :
1680 virtual CYExpression *Replace(CYContext &context);
1681 virtual void Output(CYOutput &out, CYFlags flags) const;
1689 virtual CYStatement *Replace(CYContext &context);
1690 virtual void Output(CYOutput &out, CYFlags flags) const;
1698 CYFinally(CYStatement *code) :
1703 void Replace(CYContext &context);
1704 virtual void Output(CYOutput &out) const;
1707 struct CYTypeSpecifier :
1710 virtual CYExpression *Replace(CYContext &context) = 0;
1713 struct CYTypeError :
1719 virtual CYExpression *Replace(CYContext &context);
1720 virtual void Output(CYOutput &out) const;
1729 virtual CYExpression *Replace(CYContext &context);
1730 virtual void Output(CYOutput &out) const;
1733 struct CYTypeVariable :
1736 CYIdentifier *name_;
1738 CYTypeVariable(CYIdentifier *name) :
1743 CYTypeVariable(const char *name) :
1744 name_(new($pool) CYIdentifier(name))
1748 virtual CYExpression *Replace(CYContext &context);
1749 virtual void Output(CYOutput &out) const;
1752 struct CYTypeUnsigned :
1755 CYTypeSpecifier *specifier_;
1757 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1758 specifier_(specifier)
1762 virtual CYExpression *Replace(CYContext &context);
1763 virtual void Output(CYOutput &out) const;
1766 struct CYTypeSigned :
1769 CYTypeSpecifier *specifier_;
1771 CYTypeSigned(CYTypeSpecifier *specifier) :
1772 specifier_(specifier)
1776 virtual CYExpression *Replace(CYContext &context);
1777 virtual void Output(CYOutput &out) const;
1783 CYTypeSpecifier *specifier_;
1785 CYTypeLong(CYTypeSpecifier *specifier) :
1786 specifier_(specifier)
1790 virtual CYExpression *Replace(CYContext &context);
1791 virtual void Output(CYOutput &out) const;
1794 struct CYTypeShort :
1797 CYTypeSpecifier *specifier_;
1799 CYTypeShort(CYTypeSpecifier *specifier) :
1800 specifier_(specifier)
1804 virtual CYExpression *Replace(CYContext &context);
1805 virtual void Output(CYOutput &out) const;
1808 struct CYTypeFunctionWith;
1810 struct CYTypeModifier :
1811 CYNext<CYTypeModifier>
1813 CYTypeModifier(CYTypeModifier *next) :
1814 CYNext<CYTypeModifier>(next)
1818 virtual int Precedence() const = 0;
1820 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1821 CYExpression *Replace(CYContext &context, CYExpression *type);
1823 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1824 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1826 virtual CYTypeFunctionWith *Function() { return NULL; }
1829 struct CYTypeArrayOf :
1832 CYExpression *size_;
1834 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1835 CYTypeModifier(next),
1842 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1843 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1846 struct CYTypeConstant :
1849 CYTypeConstant(CYTypeModifier *next = NULL) :
1850 CYTypeModifier(next)
1856 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1857 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1860 struct CYTypePointerTo :
1863 CYTypePointerTo(CYTypeModifier *next = NULL) :
1864 CYTypeModifier(next)
1870 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1871 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1874 struct CYTypeVolatile :
1877 CYTypeVolatile(CYTypeModifier *next = NULL) :
1878 CYTypeModifier(next)
1884 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1885 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1888 struct CYTypedIdentifier :
1889 CYNext<CYTypedIdentifier>,
1892 CYLocation location_;
1893 CYIdentifier *identifier_;
1894 CYTypeSpecifier *specifier_;
1895 CYTypeModifier *modifier_;
1897 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1898 location_(location),
1899 identifier_(identifier),
1905 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1907 specifier_(specifier),
1912 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1913 CYSetLast(modifier_) = modifier;
1917 virtual CYExpression *Replace(CYContext &context);
1918 virtual void Output(CYOutput &out) const;
1920 CYTypeFunctionWith *Function();
1923 struct CYEncodedType :
1926 CYTypedIdentifier *typed_;
1928 CYEncodedType(CYTypedIdentifier *typed) :
1935 virtual CYExpression *Replace(CYContext &context);
1936 virtual void Output(CYOutput &out, CYFlags flags) const;
1939 struct CYTypedParameter :
1940 CYNext<CYTypedParameter>,
1943 CYTypedIdentifier *typed_;
1945 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1946 CYNext<CYTypedParameter>(next),
1951 CYArgument *Argument(CYContext &context);
1952 CYFunctionParameter *Parameters(CYContext &context);
1953 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1955 virtual void Output(CYOutput &out) const;
1961 CYTypedIdentifier *typed_;
1962 CYTypedParameter *parameters_;
1965 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1967 parameters_(parameters),
1974 virtual CYExpression *Replace(CYContext &context);
1975 virtual void Output(CYOutput &out, CYFlags flags) const;
1984 CYModule(CYWord *part, CYModule *next = NULL) :
1985 CYNext<CYModule>(next),
1990 CYString *Replace(CYContext &context, const char *separator) const;
1991 void Output(CYOutput &out) const;
1999 CYImport(CYModule *module) :
2006 virtual CYStatement *Replace(CYContext &context);
2007 virtual void Output(CYOutput &out, CYFlags flags) const;
2014 CYTypedIdentifier *typed_;
2016 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2024 virtual CYStatement *Replace(CYContext &context);
2025 virtual void Output(CYOutput &out, CYFlags flags) const;
2028 struct CYTypeDefinition :
2031 CYTypedIdentifier *typed_;
2033 CYTypeDefinition(CYTypedIdentifier *typed) :
2040 virtual CYStatement *Replace(CYContext &context);
2041 virtual void Output(CYOutput &out, CYFlags flags) const;
2044 struct CYTypeBlockWith :
2047 CYTypedParameter *parameters_;
2049 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2050 CYTypeModifier(next),
2051 parameters_(parameters)
2057 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2058 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2061 struct CYTypeFunctionWith :
2064 CYTypedParameter *parameters_;
2066 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2067 CYTypeModifier(next),
2068 parameters_(parameters)
2074 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2075 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2077 virtual CYTypeFunctionWith *Function() { return this; }
2086 CYIdentifier *name_;
2089 Catch(CYIdentifier *name, CYStatement *code) :
2095 void Replace(CYContext &context);
2096 virtual void Output(CYOutput &out) const;
2104 CYFinally *finally_;
2106 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2115 virtual CYStatement *Replace(CYContext &context);
2116 virtual void Output(CYOutput &out, CYFlags flags) const;
2122 CYExpression *value_;
2124 Throw(CYExpression *value = NULL) :
2131 virtual CYStatement *Replace(CYContext &context);
2132 virtual void Output(CYOutput &out, CYFlags flags) const;
2140 CYExpression *scope_;
2143 CYWith(CYExpression *scope, CYStatement *code) :
2151 virtual CYStatement *Replace(CYContext &context);
2152 virtual void Output(CYOutput &out, CYFlags flags) const;
2158 CYExpression *value_;
2161 CYSwitch(CYExpression *value, CYClause *clauses) :
2169 virtual CYStatement *Replace(CYContext &context);
2170 virtual void Output(CYOutput &out, CYFlags flags) const;
2182 virtual CYStatement *Replace(CYContext &context);
2183 virtual void Output(CYOutput &out, CYFlags flags) const;
2186 struct CYCondition :
2189 CYExpression *test_;
2190 CYExpression *true_;
2191 CYExpression *false_;
2193 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2202 virtual CYExpression *Replace(CYContext &context);
2203 virtual void Output(CYOutput &out, CYFlags flags) const;
2206 struct CYAddressOf :
2209 CYAddressOf(CYExpression *rhs) :
2214 virtual const char *Operator() const {
2220 virtual CYExpression *Replace(CYContext &context);
2226 CYIndirect(CYExpression *rhs) :
2231 virtual const char *Operator() const {
2237 virtual CYExpression *Replace(CYContext &context);
2241 virtual CYExpression *Replace(CYContext &context);
2243 #define CYPostfix_(op, name, args...) \
2244 struct CY ## name : \
2247 CY ## name(CYExpression *lhs) : \
2252 virtual const char *Operator() const { \
2257 #define CYPrefix_(alphabetic, op, name, args...) \
2258 struct CY ## name : \
2261 CY ## name(CYExpression *rhs) : \
2266 CYAlphabetic(alphabetic) \
2268 virtual const char *Operator() const { \
2273 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2274 struct CY ## name : \
2277 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2282 CYAlphabetic(alphabetic) \
2283 CYPrecedence(precedence) \
2285 virtual const char *Operator() const { \
2290 #define CYAssignment_(op, name, args...) \
2291 struct CY ## name ## Assign : \
2294 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2295 CYAssignment(lhs, rhs) \
2299 virtual const char *Operator() const { \
2304 CYPostfix_("++", PostIncrement)
2305 CYPostfix_("--", PostDecrement)
2307 CYPrefix_(true, "delete", Delete)
2308 CYPrefix_(true, "void", Void)
2309 CYPrefix_(true, "typeof", TypeOf)
2310 CYPrefix_(false, "++", PreIncrement)
2311 CYPrefix_(false, "--", PreDecrement)
2312 CYPrefix_(false, "+", Affirm)
2313 CYPrefix_(false, "-", Negate)
2314 CYPrefix_(false, "~", BitwiseNot)
2315 CYPrefix_(false, "!", LogicalNot)
2317 CYInfix_(false, 5, "*", Multiply, CYReplace)
2318 CYInfix_(false, 5, "/", Divide)
2319 CYInfix_(false, 5, "%", Modulus)
2320 CYInfix_(false, 6, "+", Add, CYReplace)
2321 CYInfix_(false, 6, "-", Subtract)
2322 CYInfix_(false, 7, "<<", ShiftLeft)
2323 CYInfix_(false, 7, ">>", ShiftRightSigned)
2324 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2325 CYInfix_(false, 8, "<", Less)
2326 CYInfix_(false, 8, ">", Greater)
2327 CYInfix_(false, 8, "<=", LessOrEqual)
2328 CYInfix_(false, 8, ">=", GreaterOrEqual)
2329 CYInfix_(true, 8, "instanceof", InstanceOf)
2330 CYInfix_(true, 8, "in", In)
2331 CYInfix_(false, 9, "==", Equal)
2332 CYInfix_(false, 9, "!=", NotEqual)
2333 CYInfix_(false, 9, "===", Identical)
2334 CYInfix_(false, 9, "!==", NotIdentical)
2335 CYInfix_(false, 10, "&", BitwiseAnd)
2336 CYInfix_(false, 11, "^", BitwiseXOr)
2337 CYInfix_(false, 12, "|", BitwiseOr)
2338 CYInfix_(false, 13, "&&", LogicalAnd)
2339 CYInfix_(false, 14, "||", LogicalOr)
2341 CYAssignment_("=", )
2342 CYAssignment_("*=", Multiply)
2343 CYAssignment_("/=", Divide)
2344 CYAssignment_("%=", Modulus)
2345 CYAssignment_("+=", Add)
2346 CYAssignment_("-=", Subtract)
2347 CYAssignment_("<<=", ShiftLeft)
2348 CYAssignment_(">>=", ShiftRightSigned)
2349 CYAssignment_(">>>=", ShiftRightUnsigned)
2350 CYAssignment_("&=", BitwiseAnd)
2351 CYAssignment_("^=", BitwiseXOr)
2352 CYAssignment_("|=", BitwiseOr)
2354 #endif/*CYCRIPT_PARSER_HPP*/