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_SYNTAX_HPP
23 #define CYCRIPT_SYNTAX_HPP
33 #include "Location.hpp"
34 #include "Options.hpp"
35 #include "Pooling.hpp"
40 virtual void Output(struct CYOutput &out) const = 0;
61 CYOutput(std::streambuf &out, CYOptions &options) :
72 void Check(char value);
75 _finline void operator ()(char value) {
76 _assert(out_.sputc(value) != EOF);
84 _finline void operator ()(const char *data, std::streamsize size) {
85 _assert(out_.sputn(data, size) == size);
87 position_.Columns(size);
90 _finline void operator ()(const char *data) {
91 return operator ()(data, strlen(data));
94 CYOutput &operator <<(char rhs);
95 CYOutput &operator <<(const char *rhs);
97 _finline CYOutput &operator <<(const CYThing *rhs) {
103 _finline CYOutput &operator <<(const CYThing &rhs) {
112 struct CYPropertyName {
113 virtual bool Computed() const {
117 virtual bool Constructor() const {
121 virtual CYExpression *PropertyName(CYContext &context) = 0;
122 virtual void PropertyName(CYOutput &out) const = 0;
133 CYNoBrace = (1 << 0),
134 CYNoFunction = (1 << 1),
135 CYNoClass = (1 << 2),
138 CYNoRightHand = (1 << 5),
139 CYNoDangle = (1 << 6),
140 CYNoInteger = (1 << 7),
141 CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
144 _finline CYFlags operator ~(CYFlags rhs) {
145 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
148 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
149 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
152 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
153 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
156 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
157 return lhs = lhs | rhs;
160 _finline CYFlags CYLeft(CYFlags flags) {
161 return flags & ~(CYNoDangle | CYNoInteger);
164 _finline CYFlags CYRight(CYFlags flags) {
165 return flags & ~CYNoBFC;
168 _finline CYFlags CYCenter(CYFlags flags) {
169 return CYLeft(CYRight(flags));
178 #define CYCompact(type) \
179 virtual CYCompactType Compact() const { \
180 return CYCompact ## type; \
187 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
188 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
189 virtual void Output(CYOutput &out) const;
191 virtual CYStatement *Replace(CYContext &context) = 0;
193 virtual CYCompactType Compact() const = 0;
194 virtual CYStatement *Return();
197 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
200 typedef CYList<CYStatement> CYStatements;
208 CYWord(const char *word) :
213 virtual bool Constructor() const {
214 return strcmp(word_, "constructor") == 0;
217 virtual const char *Word() const;
218 virtual void Output(CYOutput &out) const;
220 virtual CYExpression *PropertyName(CYContext &context);
221 virtual void PropertyName(CYOutput &out) const;
224 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
226 return lhs << rhs.Word();
229 enum CYIdentifierKind {
230 CYIdentifierArgument,
236 CYIdentifierVariable,
239 struct CYIdentifier :
240 CYNext<CYIdentifier>,
243 CYLocation location_;
247 CYIdentifier(const char *word) :
254 virtual const char *Word() const;
255 CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
262 CYStatement *statement_;
264 CYLabel(CYIdentifier *name, CYStatement *statement) :
266 statement_(statement)
272 virtual CYStatement *Replace(CYContext &context);
273 virtual void Output(CYOutput &out, CYFlags flags) const;
276 struct CYCStringLess :
277 std::binary_function<const char *, const char *, bool>
279 _finline bool operator ()(const char *lhs, const char *rhs) const {
280 return strcmp(lhs, rhs) < 0;
284 struct CYIdentifierValueLess :
285 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
287 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
288 return CYCStringLess()(lhs->Word(), rhs->Word());
292 struct CYIdentifierFlags :
293 CYNext<CYIdentifierFlags>
295 CYIdentifier *identifier_;
296 CYIdentifierKind kind_;
300 CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
301 CYNext<CYIdentifierFlags>(next),
302 identifier_(identifier),
314 CYIdentifierFlags *shadow_;
316 CYIdentifierFlags *internal_;
318 CYScope(bool transparent, CYContext &context);
320 CYIdentifierFlags *Lookup(CYContext &context, const char *word);
321 CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
323 CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
324 void Merge(CYContext &context, const CYIdentifierFlags *flags);
326 void Close(CYContext &context, CYStatement *&statements);
327 void Close(CYContext &context);
336 CYScript(CYStatement *code) :
341 virtual void Replace(CYContext &context);
342 virtual void Output(CYOutput &out) const;
353 CYIdentifier *super_;
355 CYNonLocal *nonlocal_;
356 CYNonLocal *nextlocal_;
359 std::vector<CYIdentifier *> replace_;
361 CYContext(CYOptions &options) :
372 void ReplaceAll(CYStatement *&statement) {
373 if (statement == NULL)
375 CYStatement *next(statement->next_);
380 if (statement == NULL)
383 statement->SetNext(next);
386 template <typename Type_>
387 void Replace(Type_ *&value) {
388 for (;;) if (value == NULL)
391 Type_ *replace(value->Replace(*this));
392 if (replace != value)
398 void NonLocal(CYStatement *&statements);
399 CYIdentifier *Unique();
403 CYIdentifier *identifier_;
410 CYIdentifier *Target(CYContext &context) {
411 if (identifier_ == NULL)
412 identifier_ = context.Unique();
420 CYIdentifier *identifier_;
427 CYIdentifier *Identifier(CYContext &context) {
429 return next_->Identifier(context);
430 if (identifier_ == NULL)
431 identifier_ = context.Unique();
441 CYBlock(CYStatement *code) :
448 virtual CYStatement *Replace(CYContext &context);
450 virtual void Output(CYOutput &out, CYFlags flags) const;
452 virtual CYStatement *Return();
455 struct CYForInitializer {
456 virtual CYExpression *Replace(CYContext &context) = 0;
457 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
463 struct CYForInInitializer {
464 virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
466 virtual CYTarget *Replace(CYContext &context) = 0;
467 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
470 struct CYFunctionParameter;
475 struct CYExpression :
479 virtual int Precedence() const = 0;
481 virtual bool RightHand() const {
485 virtual bool Eval() const {
489 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
491 virtual void Output(CYOutput &out) const;
492 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
493 void Output(CYOutput &out, int precedence, CYFlags flags) const;
495 virtual CYExpression *Replace(CYContext &context) = 0;
497 virtual CYExpression *Primitive(CYContext &context) {
501 virtual CYFunctionParameter *Parameter() const;
503 virtual CYNumber *Number(CYContext &context) {
507 virtual CYString *String(CYContext &context) {
511 virtual const char *Word() const {
520 virtual bool RightHand() const {
524 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
526 virtual CYTarget *Replace(CYContext &context) = 0;
527 using CYExpression::Output;
530 #define CYAlphabetic(value) \
531 virtual bool Alphabetic() const { \
535 #define CYPrecedence(value) \
536 static const int Precedence_ = value; \
537 virtual int Precedence() const { \
538 return Precedence_; \
544 CYExpression *expression_;
547 CYCompound(CYExpression *expression, CYExpression *next) :
548 expression_(expression),
551 _assert(expression_ != NULL);
552 _assert(next != NULL);
557 virtual CYExpression *Replace(CYContext &context);
558 void Output(CYOutput &out, CYFlags flags) const;
560 virtual CYFunctionParameter *Parameter() const;
563 struct CYParenthetical :
566 CYExpression *expression_;
568 CYParenthetical(CYExpression *expression) :
569 expression_(expression)
575 virtual CYTarget *Replace(CYContext &context);
576 void Output(CYOutput &out, CYFlags flags) const;
579 struct CYDeclaration;
581 struct CYFunctionParameter :
582 CYNext<CYFunctionParameter>,
585 CYDeclaration *initialiser_;
587 CYFunctionParameter(CYDeclaration *initialiser, CYFunctionParameter *next = NULL) :
588 CYNext<CYFunctionParameter>(next),
589 initialiser_(initialiser)
593 void Replace(CYContext &context, CYStatement *&statements);
594 void Output(CYOutput &out) const;
597 struct CYComprehension :
598 CYNext<CYComprehension>,
601 CYComprehension(CYComprehension *next = NULL) :
602 CYNext<CYComprehension>(next)
606 CYComprehension *Modify(CYComprehension *next) {
611 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
612 CYFunctionParameter *Parameters(CYContext &context) const;
613 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
614 virtual void Output(CYOutput &out) const = 0;
617 struct CYForInComprehension :
620 CYDeclaration *declaration_;
623 CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
624 CYComprehension(next),
625 declaration_(declaration),
630 virtual CYFunctionParameter *Parameter(CYContext &context) const;
631 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
632 virtual void Output(CYOutput &out) const;
635 struct CYForOfComprehension :
638 CYDeclaration *declaration_;
641 CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
642 CYComprehension(next),
643 declaration_(declaration),
648 virtual CYFunctionParameter *Parameter(CYContext &context) const;
649 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
650 virtual void Output(CYOutput &out) const;
653 struct CYIfComprehension :
658 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
659 CYComprehension(next),
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 CYArrayComprehension :
672 CYExpression *expression_;
673 CYComprehension *comprehensions_;
675 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
676 expression_(expression),
677 comprehensions_(comprehensions)
683 virtual CYTarget *Replace(CYContext &context);
684 virtual void Output(CYOutput &out, CYFlags flags) const;
690 CYLocation location_;
694 virtual CYExpression *Primitive(CYContext &context) {
702 virtual CYTarget *Replace(CYContext &context);
715 CYRange(uint64_t lo, uint64_t hi) :
720 bool operator [](uint8_t value) const {
721 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
724 void operator()(uint8_t value) {
727 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
731 extern CYRange DigitRange_;
732 extern CYRange WordStartRange_;
733 extern CYRange WordEndRange_;
748 CYString(const char *value) :
754 CYString(const char *value, size_t size) :
760 CYString(const CYWord *word) :
761 value_(word->Word()),
762 size_(strlen(value_))
766 const char *Value() const {
770 virtual const char *Word() const;
772 virtual CYNumber *Number(CYContext &context);
773 virtual CYString *String(CYContext &context);
775 CYString *Concat(CYContext &out, CYString *rhs) const;
776 virtual void Output(CYOutput &out, CYFlags flags) const;
778 virtual CYExpression *PropertyName(CYContext &context);
779 virtual void PropertyName(CYOutput &out) const;
782 struct CYElementValue;
787 CYExpression *expression_;
790 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
791 CYNext<CYSpan>(next),
792 expression_(expression),
797 CYElementValue *Replace(CYContext &context);
806 CYTemplate(CYString *string, CYSpan *spans) :
814 virtual CYTarget *Replace(CYContext &context);
815 virtual void Output(CYOutput &out, CYFlags flags) const;
824 CYNumber(double value) :
829 double Value() const {
833 virtual CYNumber *Number(CYContext &context);
834 virtual CYString *String(CYContext &context);
836 virtual void Output(CYOutput &out, CYFlags flags) const;
838 virtual CYExpression *PropertyName(CYContext &context);
839 virtual void PropertyName(CYOutput &out) const;
845 CYExpression *expression_;
847 CYComputed(CYExpression *expression) :
848 expression_(expression)
852 virtual bool Computed() const {
856 virtual CYExpression *PropertyName(CYContext &context);
857 virtual void PropertyName(CYOutput &out) const;
866 CYRegEx(const char *value, size_t size) :
872 const char *Value() const {
876 virtual void Output(CYOutput &out, CYFlags flags) const;
882 virtual CYNumber *Number(CYContext &context);
883 virtual CYString *String(CYContext &context);
885 virtual void Output(CYOutput &out, CYFlags flags) const;
891 virtual CYTarget *Replace(CYContext &context);
892 virtual void Output(CYOutput &out, CYFlags flags) const;
898 virtual bool Value() const = 0;
899 virtual void Output(CYOutput &out, CYFlags flags) const;
905 virtual bool Value() const {
909 virtual CYNumber *Number(CYContext &context);
910 virtual CYString *String(CYContext &context);
916 virtual bool Value() const {
920 virtual CYNumber *Number(CYContext &context);
921 virtual CYString *String(CYContext &context);
929 CYVariable(CYIdentifier *name) :
934 CYVariable(const char *name) :
935 name_(new($pool) CYIdentifier(name))
941 virtual bool Eval() const {
942 return strcmp(name_->Word(), "eval") == 0;
945 virtual CYTarget *Replace(CYContext &context);
946 virtual void Output(CYOutput &out, CYFlags flags) const;
948 virtual CYFunctionParameter *Parameter() const;
956 CYPrefix(CYExpression *rhs) :
961 virtual bool Alphabetic() const = 0;
962 virtual const char *Operator() const = 0;
966 virtual CYExpression *Replace(CYContext &context);
967 virtual void Output(CYOutput &out, CYFlags flags) const;
976 CYInfix(CYExpression *lhs, CYExpression *rhs) :
982 void SetLeft(CYExpression *lhs) {
986 virtual bool Alphabetic() const = 0;
987 virtual const char *Operator() const = 0;
989 virtual CYExpression *Replace(CYContext &context);
990 virtual void Output(CYOutput &out, CYFlags flags) const;
998 CYPostfix(CYExpression *lhs) :
1003 virtual const char *Operator() const = 0;
1007 virtual CYExpression *Replace(CYContext &context);
1008 virtual void Output(CYOutput &out, CYFlags flags) const;
1011 struct CYAssignment :
1017 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1023 void SetLeft(CYTarget *lhs) {
1027 virtual const char *Operator() const = 0;
1031 virtual CYExpression *Replace(CYContext &context);
1032 virtual void Output(CYOutput &out, CYFlags flags) const;
1040 CYExpression *value_;
1042 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1043 CYNext<CYArgument>(next),
1049 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1050 CYNext<CYArgument>(next),
1056 CYArgument *Replace(CYContext &context);
1057 void Output(CYOutput &out) const;
1064 CYExpression *case_;
1067 CYClause(CYExpression *_case, CYStatement *code) :
1073 void Replace(CYContext &context);
1074 virtual void Output(CYOutput &out) const;
1080 virtual bool Elision() const = 0;
1082 virtual void Replace(CYContext &context) = 0;
1085 struct CYElementValue :
1089 CYExpression *value_;
1091 CYElementValue(CYExpression *value, CYElement *next) :
1092 CYNext<CYElement>(next),
1097 virtual bool Elision() const {
1098 return value_ == NULL;
1101 virtual void Replace(CYContext &context);
1102 virtual void Output(CYOutput &out) const;
1105 struct CYElementSpread :
1108 CYExpression *value_;
1110 CYElementSpread(CYExpression *value) :
1115 virtual bool Elision() const {
1119 virtual void Replace(CYContext &context);
1120 virtual void Output(CYOutput &out) const;
1126 CYElement *elements_;
1128 CYArray(CYElement *elements = NULL) :
1133 virtual CYTarget *Replace(CYContext &context);
1134 virtual void Output(CYOutput &out, CYFlags flags) const;
1137 struct CYDeclaration {
1138 CYIdentifier *identifier_;
1139 CYExpression *initialiser_;
1141 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1142 identifier_(identifier),
1143 initialiser_(initialiser)
1147 CYTarget *Target(CYContext &context);
1149 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1150 virtual void Output(CYOutput &out, CYFlags flags) const;
1153 struct CYForLexical :
1157 CYDeclaration *declaration_;
1159 CYForLexical(bool constant, CYDeclaration *declaration) :
1160 constant_(constant),
1161 declaration_(declaration)
1165 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1167 virtual CYTarget *Replace(CYContext &context);
1168 virtual void Output(CYOutput &out, CYFlags flags) const;
1171 struct CYForVariable :
1174 CYDeclaration *declaration_;
1176 CYForVariable(CYDeclaration *declaration) :
1177 declaration_(declaration)
1181 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1183 virtual CYTarget *Replace(CYContext &context);
1184 virtual void Output(CYOutput &out, CYFlags flags) const;
1187 struct CYDeclarations :
1188 CYNext<CYDeclarations>,
1191 CYDeclaration *declaration_;
1193 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1194 CYNext<CYDeclarations>(next),
1195 declaration_(declaration)
1199 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1201 CYArgument *Argument(CYContext &context);
1202 CYFunctionParameter *Parameter(CYContext &context);
1204 virtual void Output(CYOutput &out) const;
1205 virtual void Output(CYOutput &out, CYFlags flags) const;
1208 struct CYForDeclarations :
1211 CYDeclarations *declarations_;
1213 CYForDeclarations(CYDeclarations *declarations) :
1214 declarations_(declarations)
1218 virtual CYExpression *Replace(CYContext &context);
1219 virtual void Output(CYOutput &out, CYFlags flags) const;
1225 CYDeclarations *declarations_;
1227 CYVar(CYDeclarations *declarations) :
1228 declarations_(declarations)
1234 virtual CYStatement *Replace(CYContext &context);
1235 virtual void Output(CYOutput &out, CYFlags flags) const;
1242 CYDeclarations *declarations_;
1244 CYLet(bool constant, CYDeclarations *declarations) :
1245 constant_(constant),
1246 declarations_(declarations)
1252 virtual CYStatement *Replace(CYContext &context);
1253 virtual void Output(CYOutput &out, CYFlags flags) const;
1257 CYList<CYDeclarations> declarations_;
1258 CYList<CYStatement> statements_;
1260 operator bool() const {
1261 return statements_ != NULL;
1269 CYPropertyName *name_;
1271 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1272 CYNext<CYProperty>(next),
1277 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1278 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1280 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1282 virtual void Replace(CYContext &context) = 0;
1283 virtual void Output(CYOutput &out) const;
1286 struct CYPropertyValue :
1289 CYExpression *value_;
1291 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1292 CYProperty(name, next),
1297 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1298 virtual void Replace(CYContext &context);
1299 virtual void Output(CYOutput &out) const;
1305 CYForInitializer *initialiser_;
1306 CYExpression *test_;
1307 CYExpression *increment_;
1310 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1311 initialiser_(initialiser),
1313 increment_(increment),
1320 virtual CYStatement *Replace(CYContext &context);
1321 virtual void Output(CYOutput &out, CYFlags flags) const;
1327 CYForInInitializer *initialiser_;
1331 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1332 initialiser_(initialiser),
1340 virtual CYStatement *Replace(CYContext &context);
1341 virtual void Output(CYOutput &out, CYFlags flags) const;
1347 CYForInInitializer *initialiser_;
1351 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1352 initialiser_(initialiser),
1360 virtual CYStatement *Replace(CYContext &context);
1361 virtual void Output(CYOutput &out, CYFlags flags) const;
1367 CYProperty *properties_;
1369 CYObject(CYProperty *properties = NULL) :
1370 properties_(properties)
1374 virtual CYTarget *Replace(CYContext &context);
1375 void Output(CYOutput &out, CYFlags flags) const;
1381 CYExpression *object_;
1382 CYExpression *property_;
1384 CYMember(CYExpression *object, CYExpression *property) :
1390 void SetLeft(CYExpression *object) {
1395 struct CYDirectMember :
1398 CYDirectMember(CYExpression *object, CYExpression *property) :
1399 CYMember(object, property)
1405 virtual CYTarget *Replace(CYContext &context);
1406 virtual void Output(CYOutput &out, CYFlags flags) const;
1409 struct CYIndirectMember :
1412 CYIndirectMember(CYExpression *object, CYExpression *property) :
1413 CYMember(object, property)
1419 virtual CYTarget *Replace(CYContext &context);
1420 virtual void Output(CYOutput &out, CYFlags flags) const;
1429 CYExpression *constructor_;
1430 CYArgument *arguments_;
1432 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1433 constructor_(constructor),
1434 arguments_(arguments)
1438 virtual int Precedence() const {
1439 return arguments_ == NULL ? 2 : 1;
1443 virtual CYTarget *Replace(CYContext &context);
1444 virtual void Output(CYOutput &out, CYFlags flags) const;
1446 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1454 CYArgument *arguments_;
1456 CYApply(CYArgument *arguments = NULL) :
1457 arguments_(arguments)
1463 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1469 CYExpression *function_;
1471 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1477 virtual void Output(CYOutput &out, CYFlags flags) const;
1478 virtual CYTarget *Replace(CYContext &context);
1484 CYEval(CYArgument *arguments) :
1489 virtual void Output(CYOutput &out, CYFlags flags) const;
1490 virtual CYTarget *Replace(CYContext &context);
1495 struct CYRubyBlock :
1498 CYExpression *call_;
1501 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1509 virtual CYTarget *Replace(CYContext &context);
1510 virtual void Output(CYOutput &out, CYFlags flags) const;
1512 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1518 CYExpression *test_;
1520 CYStatement *false_;
1522 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1531 virtual CYStatement *Replace(CYContext &context);
1532 virtual void Output(CYOutput &out, CYFlags flags) const;
1534 virtual CYStatement *Return();
1540 CYExpression *test_;
1543 CYDoWhile(CYExpression *test, CYStatement *code) :
1551 virtual CYStatement *Replace(CYContext &context);
1552 virtual void Output(CYOutput &out, CYFlags flags) const;
1558 CYExpression *test_;
1561 CYWhile(CYExpression *test, CYStatement *code) :
1569 virtual CYStatement *Replace(CYContext &context);
1570 virtual void Output(CYOutput &out, CYFlags flags) const;
1574 CYFunctionParameter *parameters_;
1577 CYNonLocal *nonlocal_;
1580 CYIdentifier *super_;
1582 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1583 parameters_(parameters),
1591 void Replace(CYContext &context);
1592 void Output(CYOutput &out) const;
1595 struct CYFunctionExpression :
1599 CYIdentifier *name_;
1601 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1602 CYFunction(parameters, code),
1609 CYTarget *Replace(CYContext &context) override;
1610 virtual void Output(CYOutput &out, CYFlags flags) const;
1617 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1618 CYFunction(parameters, code)
1624 CYExpression *Replace(CYContext &context) override;
1625 virtual void Output(CYOutput &out, CYFlags flags) const;
1632 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1633 CYFunction(parameters, code)
1639 CYTarget *Replace(CYContext &context) override;
1640 virtual void Output(CYOutput &out, CYFlags flags) const;
1643 struct CYFunctionStatement :
1647 CYIdentifier *name_;
1649 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1650 CYFunction(parameters, code),
1657 CYStatement *Replace(CYContext &context) override;
1658 virtual void Output(CYOutput &out, CYFlags flags) const;
1661 struct CYPropertyMethod;
1667 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1668 CYFunction(parameters, code),
1669 CYProperty(name, next)
1673 virtual CYFunctionExpression *Constructor();
1675 using CYProperty::Replace;
1676 virtual void Replace(CYContext &context);
1679 struct CYPropertyGetter :
1682 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1683 CYMethod(name, NULL, code, next)
1687 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1688 virtual void Output(CYOutput &out) const;
1691 struct CYPropertySetter :
1694 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1695 CYMethod(name, parameters, code, next)
1699 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1700 virtual void Output(CYOutput &out) const;
1703 struct CYPropertyMethod :
1706 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1707 CYMethod(name, parameters, code, next)
1711 virtual CYFunctionExpression *Constructor();
1713 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1714 virtual void Output(CYOutput &out) const;
1717 struct CYClassTail :
1720 CYExpression *extends_;
1722 CYFunctionExpression *constructor_;
1723 CYList<CYProperty> instance_;
1724 CYList<CYProperty> static_;
1726 CYClassTail(CYExpression *extends) :
1732 void Output(CYOutput &out) const;
1735 struct CYClassExpression :
1738 CYIdentifier *name_;
1741 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1749 CYTarget *Replace(CYContext &context) override;
1750 virtual void Output(CYOutput &out, CYFlags flags) const;
1753 struct CYClassStatement :
1756 CYIdentifier *name_;
1759 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1767 CYStatement *Replace(CYContext &context) override;
1768 virtual void Output(CYOutput &out, CYFlags flags) const;
1771 struct CYSuperCall :
1774 CYArgument *arguments_;
1776 CYSuperCall(CYArgument *arguments) :
1777 arguments_(arguments)
1783 CYTarget *Replace(CYContext &context) override;
1784 virtual void Output(CYOutput &out, CYFlags flags) const;
1787 struct CYSuperAccess :
1790 CYExpression *property_;
1792 CYSuperAccess(CYExpression *property) :
1799 CYTarget *Replace(CYContext &context) override;
1800 virtual void Output(CYOutput &out, CYFlags flags) const;
1806 CYExpression *expression_;
1808 CYExpress(CYExpression *expression) :
1809 expression_(expression)
1811 if (expression_ == NULL)
1817 CYStatement *Replace(CYContext &context) override;
1818 virtual void Output(CYOutput &out, CYFlags flags) const;
1820 virtual CYStatement *Return();
1826 CYIdentifier *label_;
1828 CYContinue(CYIdentifier *label) :
1835 CYStatement *Replace(CYContext &context) override;
1836 virtual void Output(CYOutput &out, CYFlags flags) const;
1842 CYIdentifier *label_;
1844 CYBreak(CYIdentifier *label) :
1851 CYStatement *Replace(CYContext &context) override;
1852 virtual void Output(CYOutput &out, CYFlags flags) const;
1858 CYExpression *value_;
1860 CYReturn(CYExpression *value) :
1867 CYStatement *Replace(CYContext &context) override;
1868 virtual void Output(CYOutput &out, CYFlags flags) const;
1871 struct CYYieldGenerator :
1874 CYExpression *value_;
1876 CYYieldGenerator(CYExpression *value) :
1883 CYExpression *Replace(CYContext &context) override;
1884 virtual void Output(CYOutput &out, CYFlags flags) const;
1887 struct CYYieldValue :
1890 CYExpression *value_;
1892 CYYieldValue(CYExpression *value) :
1899 virtual CYExpression *Replace(CYContext &context);
1900 virtual void Output(CYOutput &out, CYFlags flags) const;
1908 virtual CYStatement *Replace(CYContext &context);
1909 virtual void Output(CYOutput &out, CYFlags flags) const;
1917 CYFinally(CYStatement *code) :
1922 void Replace(CYContext &context);
1923 virtual void Output(CYOutput &out) const;
1926 struct CYTypeSpecifier :
1929 virtual CYTarget *Replace(CYContext &context) = 0;
1932 struct CYTypeError :
1938 virtual CYTarget *Replace(CYContext &context);
1939 virtual void Output(CYOutput &out) const;
1948 virtual CYTarget *Replace(CYContext &context);
1949 virtual void Output(CYOutput &out) const;
1952 struct CYTypeVariable :
1955 CYIdentifier *name_;
1957 CYTypeVariable(CYIdentifier *name) :
1962 CYTypeVariable(const char *name) :
1963 name_(new($pool) CYIdentifier(name))
1967 virtual CYTarget *Replace(CYContext &context);
1968 virtual void Output(CYOutput &out) const;
1971 struct CYTypeUnsigned :
1974 CYTypeSpecifier *specifier_;
1976 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1977 specifier_(specifier)
1981 virtual CYTarget *Replace(CYContext &context);
1982 virtual void Output(CYOutput &out) const;
1985 struct CYTypeSigned :
1988 CYTypeSpecifier *specifier_;
1990 CYTypeSigned(CYTypeSpecifier *specifier) :
1991 specifier_(specifier)
1995 virtual CYTarget *Replace(CYContext &context);
1996 virtual void Output(CYOutput &out) const;
2002 CYTypeSpecifier *specifier_;
2004 CYTypeLong(CYTypeSpecifier *specifier) :
2005 specifier_(specifier)
2009 virtual CYTarget *Replace(CYContext &context);
2010 virtual void Output(CYOutput &out) const;
2013 struct CYTypeShort :
2016 CYTypeSpecifier *specifier_;
2018 CYTypeShort(CYTypeSpecifier *specifier) :
2019 specifier_(specifier)
2023 virtual CYTarget *Replace(CYContext &context);
2024 virtual void Output(CYOutput &out) const;
2027 struct CYTypeFunctionWith;
2029 struct CYTypeModifier :
2030 CYNext<CYTypeModifier>
2032 CYTypeModifier(CYTypeModifier *next) :
2033 CYNext<CYTypeModifier>(next)
2037 virtual int Precedence() const = 0;
2039 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2040 CYTarget *Replace(CYContext &context, CYTarget *type);
2042 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2043 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2045 virtual CYTypeFunctionWith *Function() { return NULL; }
2048 struct CYTypeArrayOf :
2051 CYExpression *size_;
2053 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2054 CYTypeModifier(next),
2061 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2062 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2065 struct CYTypeConstant :
2068 CYTypeConstant(CYTypeModifier *next = NULL) :
2069 CYTypeModifier(next)
2075 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2076 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2079 struct CYTypePointerTo :
2082 CYTypePointerTo(CYTypeModifier *next = NULL) :
2083 CYTypeModifier(next)
2089 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2090 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2093 struct CYTypeVolatile :
2096 CYTypeVolatile(CYTypeModifier *next = NULL) :
2097 CYTypeModifier(next)
2103 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2104 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2107 struct CYTypedIdentifier :
2108 CYNext<CYTypedIdentifier>,
2111 CYLocation location_;
2112 CYIdentifier *identifier_;
2113 CYTypeSpecifier *specifier_;
2114 CYTypeModifier *modifier_;
2116 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2117 location_(location),
2118 identifier_(identifier),
2124 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2126 specifier_(specifier),
2131 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2132 CYSetLast(modifier_) = modifier;
2136 virtual CYTarget *Replace(CYContext &context);
2137 virtual void Output(CYOutput &out) const;
2139 CYTypeFunctionWith *Function();
2142 struct CYEncodedType :
2145 CYTypedIdentifier *typed_;
2147 CYEncodedType(CYTypedIdentifier *typed) :
2154 virtual CYTarget *Replace(CYContext &context);
2155 virtual void Output(CYOutput &out, CYFlags flags) const;
2158 struct CYTypedParameter :
2159 CYNext<CYTypedParameter>,
2162 CYTypedIdentifier *typed_;
2164 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2165 CYNext<CYTypedParameter>(next),
2170 CYArgument *Argument(CYContext &context);
2171 CYFunctionParameter *Parameters(CYContext &context);
2172 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2174 virtual void Output(CYOutput &out) const;
2180 CYTypedIdentifier *typed_;
2181 CYTypedParameter *parameters_;
2184 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2186 parameters_(parameters),
2193 virtual CYTarget *Replace(CYContext &context);
2194 virtual void Output(CYOutput &out, CYFlags flags) const;
2203 CYModule(CYWord *part, CYModule *next = NULL) :
2204 CYNext<CYModule>(next),
2209 CYString *Replace(CYContext &context, const char *separator) const;
2210 void Output(CYOutput &out) const;
2218 CYImport(CYModule *module) :
2225 virtual CYStatement *Replace(CYContext &context);
2226 virtual void Output(CYOutput &out, CYFlags flags) const;
2233 CYTypedIdentifier *typed_;
2235 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2243 virtual CYStatement *Replace(CYContext &context);
2244 virtual void Output(CYOutput &out, CYFlags flags) const;
2247 struct CYTypeDefinition :
2250 CYTypedIdentifier *typed_;
2252 CYTypeDefinition(CYTypedIdentifier *typed) :
2259 virtual CYStatement *Replace(CYContext &context);
2260 virtual void Output(CYOutput &out, CYFlags flags) const;
2263 struct CYTypeBlockWith :
2266 CYTypedParameter *parameters_;
2268 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2269 CYTypeModifier(next),
2270 parameters_(parameters)
2276 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2277 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2280 struct CYTypeFunctionWith :
2283 CYTypedParameter *parameters_;
2285 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2286 CYTypeModifier(next),
2287 parameters_(parameters)
2293 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2294 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2296 virtual CYTypeFunctionWith *Function() { return this; }
2305 CYIdentifier *name_;
2308 Catch(CYIdentifier *name, CYStatement *code) :
2314 void Replace(CYContext &context);
2315 virtual void Output(CYOutput &out) const;
2323 CYFinally *finally_;
2325 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2334 virtual CYStatement *Replace(CYContext &context);
2335 virtual void Output(CYOutput &out, CYFlags flags) const;
2341 CYExpression *value_;
2343 Throw(CYExpression *value = NULL) :
2350 virtual CYStatement *Replace(CYContext &context);
2351 virtual void Output(CYOutput &out, CYFlags flags) const;
2359 CYExpression *scope_;
2362 CYWith(CYExpression *scope, CYStatement *code) :
2370 virtual CYStatement *Replace(CYContext &context);
2371 virtual void Output(CYOutput &out, CYFlags flags) const;
2377 CYExpression *value_;
2380 CYSwitch(CYExpression *value, CYClause *clauses) :
2388 virtual CYStatement *Replace(CYContext &context);
2389 virtual void Output(CYOutput &out, CYFlags flags) const;
2401 virtual CYStatement *Replace(CYContext &context);
2402 virtual void Output(CYOutput &out, CYFlags flags) const;
2405 struct CYCondition :
2408 CYExpression *test_;
2409 CYExpression *true_;
2410 CYExpression *false_;
2412 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2421 virtual CYExpression *Replace(CYContext &context);
2422 virtual void Output(CYOutput &out, CYFlags flags) const;
2425 struct CYAddressOf :
2428 CYAddressOf(CYExpression *rhs) :
2433 virtual const char *Operator() const {
2439 virtual CYExpression *Replace(CYContext &context);
2447 CYIndirect(CYExpression *rhs) :
2452 // XXX: this should be checked
2455 virtual CYTarget *Replace(CYContext &context);
2456 virtual void Output(CYOutput &out, CYFlags flags) const;
2460 virtual CYExpression *Replace(CYContext &context);
2462 #define CYPostfix_(op, name, args...) \
2463 struct CY ## name : \
2466 CY ## name(CYExpression *lhs) : \
2471 virtual const char *Operator() const { \
2476 #define CYPrefix_(alphabetic, op, name, args...) \
2477 struct CY ## name : \
2480 CY ## name(CYExpression *rhs) : \
2485 CYAlphabetic(alphabetic) \
2487 virtual const char *Operator() const { \
2492 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2493 struct CY ## name : \
2496 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2501 CYAlphabetic(alphabetic) \
2502 CYPrecedence(precedence) \
2504 virtual const char *Operator() const { \
2509 #define CYAssignment_(op, name, args...) \
2510 struct CY ## name ## Assign : \
2513 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2514 CYAssignment(lhs, rhs) \
2518 virtual const char *Operator() const { \
2523 CYPostfix_("++", PostIncrement)
2524 CYPostfix_("--", PostDecrement)
2526 CYPrefix_(true, "delete", Delete)
2527 CYPrefix_(true, "void", Void)
2528 CYPrefix_(true, "typeof", TypeOf)
2529 CYPrefix_(false, "++", PreIncrement)
2530 CYPrefix_(false, "--", PreDecrement)
2531 CYPrefix_(false, "+", Affirm)
2532 CYPrefix_(false, "-", Negate)
2533 CYPrefix_(false, "~", BitwiseNot)
2534 CYPrefix_(false, "!", LogicalNot)
2536 CYInfix_(false, 5, "*", Multiply, CYReplace)
2537 CYInfix_(false, 5, "/", Divide)
2538 CYInfix_(false, 5, "%", Modulus)
2539 CYInfix_(false, 6, "+", Add, CYReplace)
2540 CYInfix_(false, 6, "-", Subtract)
2541 CYInfix_(false, 7, "<<", ShiftLeft)
2542 CYInfix_(false, 7, ">>", ShiftRightSigned)
2543 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2544 CYInfix_(false, 8, "<", Less)
2545 CYInfix_(false, 8, ">", Greater)
2546 CYInfix_(false, 8, "<=", LessOrEqual)
2547 CYInfix_(false, 8, ">=", GreaterOrEqual)
2548 CYInfix_(true, 8, "instanceof", InstanceOf)
2549 CYInfix_(true, 8, "in", In)
2550 CYInfix_(false, 9, "==", Equal)
2551 CYInfix_(false, 9, "!=", NotEqual)
2552 CYInfix_(false, 9, "===", Identical)
2553 CYInfix_(false, 9, "!==", NotIdentical)
2554 CYInfix_(false, 10, "&", BitwiseAnd)
2555 CYInfix_(false, 11, "^", BitwiseXOr)
2556 CYInfix_(false, 12, "|", BitwiseOr)
2557 CYInfix_(false, 13, "&&", LogicalAnd)
2558 CYInfix_(false, 14, "||", LogicalOr)
2560 CYAssignment_("=", )
2561 CYAssignment_("*=", Multiply)
2562 CYAssignment_("/=", Divide)
2563 CYAssignment_("%=", Modulus)
2564 CYAssignment_("+=", Add)
2565 CYAssignment_("-=", Subtract)
2566 CYAssignment_("<<=", ShiftLeft)
2567 CYAssignment_(">>=", ShiftRightSigned)
2568 CYAssignment_(">>>=", ShiftRightUnsigned)
2569 CYAssignment_("&=", BitwiseAnd)
2570 CYAssignment_("^=", BitwiseXOr)
2571 CYAssignment_("|=", BitwiseOr)
2573 #endif/*CYCRIPT_PARSER_HPP*/