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;
202 struct CYForInitializer :
205 virtual CYForInitializer *Replace(CYContext &context) = 0;
206 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
215 CYWord(const char *word) :
220 virtual bool Constructor() const {
221 return strcmp(word_, "constructor") == 0;
224 virtual const char *Word() const;
225 virtual void Output(CYOutput &out) const;
227 virtual CYExpression *PropertyName(CYContext &context);
228 virtual void PropertyName(CYOutput &out) const;
231 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
233 return lhs << rhs.Word();
236 enum CYIdentifierKind {
237 CYIdentifierArgument,
243 CYIdentifierVariable,
246 struct CYIdentifier :
247 CYNext<CYIdentifier>,
250 CYLocation location_;
254 CYIdentifier(const char *word) :
261 virtual const char *Word() const;
262 CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
269 CYStatement *statement_;
271 CYLabel(CYIdentifier *name, CYStatement *statement) :
273 statement_(statement)
279 virtual CYStatement *Replace(CYContext &context);
280 virtual void Output(CYOutput &out, CYFlags flags) const;
283 struct CYCStringLess :
284 std::binary_function<const char *, const char *, bool>
286 _finline bool operator ()(const char *lhs, const char *rhs) const {
287 return strcmp(lhs, rhs) < 0;
291 struct CYIdentifierValueLess :
292 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
294 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
295 return CYCStringLess()(lhs->Word(), rhs->Word());
299 struct CYIdentifierFlags :
300 CYNext<CYIdentifierFlags>
302 CYIdentifier *identifier_;
303 CYIdentifierKind kind_;
307 CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
308 CYNext<CYIdentifierFlags>(next),
309 identifier_(identifier),
321 CYIdentifierFlags *shadow_;
323 CYIdentifierFlags *internal_;
325 CYScope(bool transparent, CYContext &context);
327 CYIdentifierFlags *Lookup(CYContext &context, const char *word);
328 CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
330 CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
331 void Merge(CYContext &context, const CYIdentifierFlags *flags);
333 void Close(CYContext &context, CYStatement *&statements);
334 void Close(CYContext &context);
343 CYScript(CYStatement *code) :
348 virtual void Replace(CYContext &context);
349 virtual void Output(CYOutput &out) const;
360 CYIdentifier *super_;
362 CYNonLocal *nonlocal_;
363 CYNonLocal *nextlocal_;
366 std::vector<CYIdentifier *> replace_;
368 CYContext(CYOptions &options) :
379 void ReplaceAll(CYStatement *&statement) {
380 if (statement == NULL)
382 CYStatement *next(statement->next_);
387 if (statement == NULL)
390 statement->SetNext(next);
393 template <typename Type_>
394 void Replace(Type_ *&value) {
395 for (;;) if (value == NULL)
398 Type_ *replace(value->Replace(*this));
399 if (replace != value)
405 void NonLocal(CYStatement *&statements);
406 CYIdentifier *Unique();
410 CYIdentifier *identifier_;
417 CYIdentifier *Target(CYContext &context) {
418 if (identifier_ == NULL)
419 identifier_ = context.Unique();
427 CYIdentifier *identifier_;
434 CYIdentifier *Identifier(CYContext &context) {
436 return next_->Identifier(context);
437 if (identifier_ == NULL)
438 identifier_ = context.Unique();
448 CYBlock(CYStatement *code) :
455 virtual CYStatement *Replace(CYContext &context);
457 virtual void Output(CYOutput &out, CYFlags flags) const;
459 virtual CYStatement *Return();
465 struct CYForInInitializer {
466 virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
468 virtual CYTarget *Replace(CYContext &context) = 0;
469 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
472 struct CYFunctionParameter;
477 struct CYExpression :
480 virtual int Precedence() const = 0;
482 virtual bool RightHand() const {
486 virtual bool Eval() const {
490 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
492 virtual void Output(CYOutput &out) const;
493 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
494 void Output(CYOutput &out, int precedence, CYFlags flags) const;
496 virtual CYExpression *Replace(CYContext &context) = 0;
498 virtual CYExpression *Primitive(CYContext &context) {
502 virtual CYFunctionParameter *Parameter() const;
504 virtual CYNumber *Number(CYContext &context) {
508 virtual CYString *String(CYContext &context) {
512 virtual const char *Word() const {
521 virtual bool RightHand() const {
525 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
527 virtual CYTarget *Replace(CYContext &context) = 0;
528 using CYExpression::Output;
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_; \
545 CYExpression *expression_;
548 CYCompound(CYExpression *expression, CYExpression *next) :
549 expression_(expression),
552 _assert(expression_ != NULL);
553 _assert(next != NULL);
558 virtual CYExpression *Replace(CYContext &context);
559 void Output(CYOutput &out, CYFlags flags) const;
561 virtual CYFunctionParameter *Parameter() const;
564 struct CYParenthetical :
567 CYExpression *expression_;
569 CYParenthetical(CYExpression *expression) :
570 expression_(expression)
576 virtual CYTarget *Replace(CYContext &context);
577 void Output(CYOutput &out, CYFlags flags) const;
580 struct CYDeclaration;
582 struct CYFunctionParameter :
583 CYNext<CYFunctionParameter>,
586 CYDeclaration *initialiser_;
588 CYFunctionParameter(CYDeclaration *initialiser, CYFunctionParameter *next = NULL) :
589 CYNext<CYFunctionParameter>(next),
590 initialiser_(initialiser)
594 void Replace(CYContext &context, CYStatement *&statements);
595 void Output(CYOutput &out) const;
598 struct CYComprehension :
599 CYNext<CYComprehension>,
602 CYComprehension(CYComprehension *next = NULL) :
603 CYNext<CYComprehension>(next)
607 CYComprehension *Modify(CYComprehension *next) {
612 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
613 CYFunctionParameter *Parameters(CYContext &context) const;
614 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
615 virtual void Output(CYOutput &out) const = 0;
618 struct CYForInComprehension :
621 CYDeclaration *declaration_;
624 CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
625 CYComprehension(next),
626 declaration_(declaration),
631 virtual CYFunctionParameter *Parameter(CYContext &context) const;
632 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
633 virtual void Output(CYOutput &out) const;
636 struct CYForOfComprehension :
639 CYDeclaration *declaration_;
642 CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
643 CYComprehension(next),
644 declaration_(declaration),
649 virtual CYFunctionParameter *Parameter(CYContext &context) const;
650 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
651 virtual void Output(CYOutput &out) const;
654 struct CYIfComprehension :
659 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
660 CYComprehension(next),
665 virtual CYFunctionParameter *Parameter(CYContext &context) const;
666 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
667 virtual void Output(CYOutput &out) const;
670 struct CYArrayComprehension :
673 CYExpression *expression_;
674 CYComprehension *comprehensions_;
676 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
677 expression_(expression),
678 comprehensions_(comprehensions)
684 virtual CYTarget *Replace(CYContext &context);
685 virtual void Output(CYOutput &out, CYFlags flags) const;
691 CYLocation location_;
695 virtual CYExpression *Primitive(CYContext &context) {
703 virtual CYTarget *Replace(CYContext &context);
716 CYRange(uint64_t lo, uint64_t hi) :
721 bool operator [](uint8_t value) const {
722 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
725 void operator()(uint8_t value) {
728 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
732 extern CYRange DigitRange_;
733 extern CYRange WordStartRange_;
734 extern CYRange WordEndRange_;
749 CYString(const char *value) :
755 CYString(const char *value, size_t size) :
761 CYString(const CYWord *word) :
762 value_(word->Word()),
763 size_(strlen(value_))
767 const char *Value() const {
771 virtual const char *Word() const;
773 virtual CYNumber *Number(CYContext &context);
774 virtual CYString *String(CYContext &context);
776 CYString *Concat(CYContext &out, CYString *rhs) const;
777 virtual void Output(CYOutput &out, CYFlags flags) const;
779 virtual CYExpression *PropertyName(CYContext &context);
780 virtual void PropertyName(CYOutput &out) const;
783 struct CYElementValue;
788 CYExpression *expression_;
791 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
792 CYNext<CYSpan>(next),
793 expression_(expression),
798 CYElementValue *Replace(CYContext &context);
807 CYTemplate(CYString *string, CYSpan *spans) :
815 virtual CYTarget *Replace(CYContext &context);
816 virtual void Output(CYOutput &out, CYFlags flags) const;
825 CYNumber(double value) :
830 double Value() const {
834 virtual CYNumber *Number(CYContext &context);
835 virtual CYString *String(CYContext &context);
837 virtual void Output(CYOutput &out, CYFlags flags) const;
839 virtual CYExpression *PropertyName(CYContext &context);
840 virtual void PropertyName(CYOutput &out) const;
846 CYExpression *expression_;
848 CYComputed(CYExpression *expression) :
849 expression_(expression)
853 virtual bool Computed() const {
857 virtual CYExpression *PropertyName(CYContext &context);
858 virtual void PropertyName(CYOutput &out) const;
867 CYRegEx(const char *value, size_t size) :
873 const char *Value() const {
877 virtual void Output(CYOutput &out, CYFlags flags) const;
883 virtual CYNumber *Number(CYContext &context);
884 virtual CYString *String(CYContext &context);
886 virtual void Output(CYOutput &out, CYFlags flags) const;
892 virtual CYTarget *Replace(CYContext &context);
893 virtual void Output(CYOutput &out, CYFlags flags) const;
899 virtual bool Value() const = 0;
900 virtual void Output(CYOutput &out, CYFlags flags) const;
906 virtual bool Value() const {
910 virtual CYNumber *Number(CYContext &context);
911 virtual CYString *String(CYContext &context);
917 virtual bool Value() const {
921 virtual CYNumber *Number(CYContext &context);
922 virtual CYString *String(CYContext &context);
930 CYVariable(CYIdentifier *name) :
935 CYVariable(const char *name) :
936 name_(new($pool) CYIdentifier(name))
942 virtual bool Eval() const {
943 return strcmp(name_->Word(), "eval") == 0;
946 virtual CYTarget *Replace(CYContext &context);
947 virtual void Output(CYOutput &out, CYFlags flags) const;
949 virtual CYFunctionParameter *Parameter() const;
957 CYPrefix(CYExpression *rhs) :
962 virtual bool Alphabetic() const = 0;
963 virtual const char *Operator() const = 0;
967 virtual CYExpression *Replace(CYContext &context);
968 virtual void Output(CYOutput &out, CYFlags flags) const;
977 CYInfix(CYExpression *lhs, CYExpression *rhs) :
983 void SetLeft(CYExpression *lhs) {
987 virtual bool Alphabetic() const = 0;
988 virtual const char *Operator() const = 0;
990 virtual CYExpression *Replace(CYContext &context);
991 virtual void Output(CYOutput &out, CYFlags flags) const;
999 CYPostfix(CYExpression *lhs) :
1004 virtual const char *Operator() const = 0;
1008 virtual CYExpression *Replace(CYContext &context);
1009 virtual void Output(CYOutput &out, CYFlags flags) const;
1012 struct CYAssignment :
1018 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1024 void SetLeft(CYTarget *lhs) {
1028 virtual const char *Operator() const = 0;
1032 virtual CYExpression *Replace(CYContext &context);
1033 virtual void Output(CYOutput &out, CYFlags flags) const;
1041 CYExpression *value_;
1043 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1044 CYNext<CYArgument>(next),
1050 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1051 CYNext<CYArgument>(next),
1057 CYArgument *Replace(CYContext &context);
1058 void Output(CYOutput &out) const;
1065 CYExpression *case_;
1068 CYClause(CYExpression *_case, CYStatement *code) :
1074 void Replace(CYContext &context);
1075 virtual void Output(CYOutput &out) const;
1081 virtual bool Elision() const = 0;
1083 virtual void Replace(CYContext &context) = 0;
1086 struct CYElementValue :
1090 CYExpression *value_;
1092 CYElementValue(CYExpression *value, CYElement *next) :
1093 CYNext<CYElement>(next),
1098 virtual bool Elision() const {
1099 return value_ == NULL;
1102 virtual void Replace(CYContext &context);
1103 virtual void Output(CYOutput &out) const;
1106 struct CYElementSpread :
1109 CYExpression *value_;
1111 CYElementSpread(CYExpression *value) :
1116 virtual bool Elision() const {
1120 virtual void Replace(CYContext &context);
1121 virtual void Output(CYOutput &out) const;
1127 CYElement *elements_;
1129 CYArray(CYElement *elements = NULL) :
1134 virtual CYTarget *Replace(CYContext &context);
1135 virtual void Output(CYOutput &out, CYFlags flags) const;
1138 struct CYDeclaration {
1139 CYIdentifier *identifier_;
1140 CYExpression *initialiser_;
1142 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1143 identifier_(identifier),
1144 initialiser_(initialiser)
1148 CYTarget *Target(CYContext &context);
1150 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1151 virtual void Output(CYOutput &out, CYFlags flags) const;
1154 struct CYForLexical :
1158 CYDeclaration *declaration_;
1160 CYForLexical(bool constant, CYDeclaration *declaration) :
1161 constant_(constant),
1162 declaration_(declaration)
1166 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1168 virtual CYTarget *Replace(CYContext &context);
1169 virtual void Output(CYOutput &out, CYFlags flags) const;
1172 struct CYForVariable :
1175 CYDeclaration *declaration_;
1177 CYForVariable(CYDeclaration *declaration) :
1178 declaration_(declaration)
1182 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1184 virtual CYTarget *Replace(CYContext &context);
1185 virtual void Output(CYOutput &out, CYFlags flags) const;
1188 struct CYDeclarations :
1189 CYNext<CYDeclarations>,
1192 CYDeclaration *declaration_;
1194 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1195 CYNext<CYDeclarations>(next),
1196 declaration_(declaration)
1200 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1202 CYArgument *Argument(CYContext &context);
1203 CYFunctionParameter *Parameter(CYContext &context);
1205 virtual void Output(CYOutput &out) const;
1206 virtual void Output(CYOutput &out, CYFlags flags) const;
1212 CYDeclarations *declarations_;
1214 CYVar(CYDeclarations *declarations) :
1215 declarations_(declarations)
1221 virtual CYForInitializer *Replace(CYContext &context);
1222 virtual void Output(CYOutput &out, CYFlags flags) const;
1229 CYDeclarations *declarations_;
1231 CYLet(bool constant, CYDeclarations *declarations) :
1232 constant_(constant),
1233 declarations_(declarations)
1239 virtual CYForInitializer *Replace(CYContext &context);
1240 virtual void Output(CYOutput &out, CYFlags flags) const;
1244 CYList<CYDeclarations> declarations_;
1245 CYList<CYStatement> statements_;
1247 operator bool() const {
1248 return statements_ != NULL;
1256 CYPropertyName *name_;
1258 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1259 CYNext<CYProperty>(next),
1264 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1265 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1267 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1269 virtual void Replace(CYContext &context) = 0;
1270 virtual void Output(CYOutput &out) const;
1273 struct CYPropertyValue :
1276 CYExpression *value_;
1278 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1279 CYProperty(name, next),
1284 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1285 virtual void Replace(CYContext &context);
1286 virtual void Output(CYOutput &out) const;
1292 CYForInitializer *initialiser_;
1293 CYExpression *test_;
1294 CYExpression *increment_;
1297 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1298 initialiser_(initialiser),
1300 increment_(increment),
1307 virtual CYStatement *Replace(CYContext &context);
1308 virtual void Output(CYOutput &out, CYFlags flags) const;
1314 CYForInInitializer *initialiser_;
1318 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1319 initialiser_(initialiser),
1327 virtual CYStatement *Replace(CYContext &context);
1328 virtual void Output(CYOutput &out, CYFlags flags) const;
1331 struct CYForInitialized :
1334 CYDeclaration *declaration_;
1338 CYForInitialized(CYDeclaration *declaration, CYExpression *set, CYStatement *code) :
1339 declaration_(declaration),
1347 virtual CYStatement *Replace(CYContext &context);
1348 virtual void Output(CYOutput &out, CYFlags flags) const;
1354 CYForInInitializer *initialiser_;
1358 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1359 initialiser_(initialiser),
1367 virtual CYStatement *Replace(CYContext &context);
1368 virtual void Output(CYOutput &out, CYFlags flags) const;
1374 CYProperty *properties_;
1376 CYObject(CYProperty *properties = NULL) :
1377 properties_(properties)
1381 virtual CYTarget *Replace(CYContext &context);
1382 void Output(CYOutput &out, CYFlags flags) const;
1388 CYExpression *object_;
1389 CYExpression *property_;
1391 CYMember(CYExpression *object, CYExpression *property) :
1397 void SetLeft(CYExpression *object) {
1402 struct CYDirectMember :
1405 CYDirectMember(CYExpression *object, CYExpression *property) :
1406 CYMember(object, property)
1412 virtual CYTarget *Replace(CYContext &context);
1413 virtual void Output(CYOutput &out, CYFlags flags) const;
1416 struct CYIndirectMember :
1419 CYIndirectMember(CYExpression *object, CYExpression *property) :
1420 CYMember(object, property)
1426 virtual CYTarget *Replace(CYContext &context);
1427 virtual void Output(CYOutput &out, CYFlags flags) const;
1436 CYExpression *constructor_;
1437 CYArgument *arguments_;
1439 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1440 constructor_(constructor),
1441 arguments_(arguments)
1445 virtual int Precedence() const {
1446 return arguments_ == NULL ? 2 : 1;
1450 virtual CYTarget *Replace(CYContext &context);
1451 virtual void Output(CYOutput &out, CYFlags flags) const;
1453 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1461 CYArgument *arguments_;
1463 CYApply(CYArgument *arguments = NULL) :
1464 arguments_(arguments)
1470 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1476 CYExpression *function_;
1478 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1484 virtual void Output(CYOutput &out, CYFlags flags) const;
1485 virtual CYTarget *Replace(CYContext &context);
1491 CYEval(CYArgument *arguments) :
1496 virtual void Output(CYOutput &out, CYFlags flags) const;
1497 virtual CYTarget *Replace(CYContext &context);
1502 struct CYRubyBlock :
1505 CYExpression *call_;
1508 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1516 virtual CYTarget *Replace(CYContext &context);
1517 virtual void Output(CYOutput &out, CYFlags flags) const;
1519 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1525 CYExpression *test_;
1527 CYStatement *false_;
1529 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1538 virtual CYStatement *Replace(CYContext &context);
1539 virtual void Output(CYOutput &out, CYFlags flags) const;
1541 virtual CYStatement *Return();
1547 CYExpression *test_;
1550 CYDoWhile(CYExpression *test, CYStatement *code) :
1558 virtual CYStatement *Replace(CYContext &context);
1559 virtual void Output(CYOutput &out, CYFlags flags) const;
1565 CYExpression *test_;
1568 CYWhile(CYExpression *test, CYStatement *code) :
1576 virtual CYStatement *Replace(CYContext &context);
1577 virtual void Output(CYOutput &out, CYFlags flags) const;
1581 CYFunctionParameter *parameters_;
1584 CYNonLocal *nonlocal_;
1587 CYIdentifier *super_;
1589 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1590 parameters_(parameters),
1598 void Replace(CYContext &context);
1599 void Output(CYOutput &out) const;
1602 struct CYFunctionExpression :
1606 CYIdentifier *name_;
1608 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1609 CYFunction(parameters, code),
1616 CYTarget *Replace(CYContext &context) override;
1617 virtual void Output(CYOutput &out, CYFlags flags) const;
1624 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1625 CYFunction(parameters, code)
1631 CYExpression *Replace(CYContext &context) override;
1632 virtual void Output(CYOutput &out, CYFlags flags) const;
1639 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1640 CYFunction(parameters, code)
1646 CYTarget *Replace(CYContext &context) override;
1647 virtual void Output(CYOutput &out, CYFlags flags) const;
1650 struct CYFunctionStatement :
1654 CYIdentifier *name_;
1656 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1657 CYFunction(parameters, code),
1664 CYStatement *Replace(CYContext &context) override;
1665 virtual void Output(CYOutput &out, CYFlags flags) const;
1668 struct CYPropertyMethod;
1674 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1675 CYFunction(parameters, code),
1676 CYProperty(name, next)
1680 virtual CYFunctionExpression *Constructor();
1682 using CYProperty::Replace;
1683 virtual void Replace(CYContext &context);
1686 struct CYPropertyGetter :
1689 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1690 CYMethod(name, NULL, code, next)
1694 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1695 virtual void Output(CYOutput &out) const;
1698 struct CYPropertySetter :
1701 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1702 CYMethod(name, parameters, code, next)
1706 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1707 virtual void Output(CYOutput &out) const;
1710 struct CYPropertyMethod :
1713 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1714 CYMethod(name, parameters, code, next)
1718 virtual CYFunctionExpression *Constructor();
1720 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1721 virtual void Output(CYOutput &out) const;
1724 struct CYClassTail :
1727 CYExpression *extends_;
1729 CYFunctionExpression *constructor_;
1730 CYList<CYProperty> instance_;
1731 CYList<CYProperty> static_;
1733 CYClassTail(CYExpression *extends) :
1739 void Output(CYOutput &out) const;
1742 struct CYClassExpression :
1745 CYIdentifier *name_;
1748 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1756 CYTarget *Replace(CYContext &context) override;
1757 virtual void Output(CYOutput &out, CYFlags flags) const;
1760 struct CYClassStatement :
1763 CYIdentifier *name_;
1766 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1774 CYStatement *Replace(CYContext &context) override;
1775 virtual void Output(CYOutput &out, CYFlags flags) const;
1778 struct CYSuperCall :
1781 CYArgument *arguments_;
1783 CYSuperCall(CYArgument *arguments) :
1784 arguments_(arguments)
1790 CYTarget *Replace(CYContext &context) override;
1791 virtual void Output(CYOutput &out, CYFlags flags) const;
1794 struct CYSuperAccess :
1797 CYExpression *property_;
1799 CYSuperAccess(CYExpression *property) :
1806 CYTarget *Replace(CYContext &context) override;
1807 virtual void Output(CYOutput &out, CYFlags flags) const;
1813 CYExpression *expression_;
1815 CYExpress(CYExpression *expression) :
1816 expression_(expression)
1818 if (expression_ == NULL)
1824 CYForInitializer *Replace(CYContext &context) override;
1825 virtual void Output(CYOutput &out, CYFlags flags) const;
1827 virtual CYStatement *Return();
1833 CYIdentifier *label_;
1835 CYContinue(CYIdentifier *label) :
1842 CYStatement *Replace(CYContext &context) override;
1843 virtual void Output(CYOutput &out, CYFlags flags) const;
1849 CYIdentifier *label_;
1851 CYBreak(CYIdentifier *label) :
1858 CYStatement *Replace(CYContext &context) override;
1859 virtual void Output(CYOutput &out, CYFlags flags) const;
1865 CYExpression *value_;
1867 CYReturn(CYExpression *value) :
1874 CYStatement *Replace(CYContext &context) override;
1875 virtual void Output(CYOutput &out, CYFlags flags) const;
1878 struct CYYieldGenerator :
1881 CYExpression *value_;
1883 CYYieldGenerator(CYExpression *value) :
1890 CYExpression *Replace(CYContext &context) override;
1891 virtual void Output(CYOutput &out, CYFlags flags) const;
1894 struct CYYieldValue :
1897 CYExpression *value_;
1899 CYYieldValue(CYExpression *value) :
1906 virtual CYExpression *Replace(CYContext &context);
1907 virtual void Output(CYOutput &out, CYFlags flags) const;
1915 virtual CYForInitializer *Replace(CYContext &context);
1916 virtual void Output(CYOutput &out, CYFlags flags) const;
1924 CYFinally(CYStatement *code) :
1929 void Replace(CYContext &context);
1930 virtual void Output(CYOutput &out) const;
1933 struct CYTypeSpecifier :
1936 virtual CYTarget *Replace(CYContext &context) = 0;
1939 struct CYTypeError :
1945 virtual CYTarget *Replace(CYContext &context);
1946 virtual void Output(CYOutput &out) const;
1955 virtual CYTarget *Replace(CYContext &context);
1956 virtual void Output(CYOutput &out) const;
1959 struct CYTypeVariable :
1962 CYIdentifier *name_;
1964 CYTypeVariable(CYIdentifier *name) :
1969 CYTypeVariable(const char *name) :
1970 name_(new($pool) CYIdentifier(name))
1974 virtual CYTarget *Replace(CYContext &context);
1975 virtual void Output(CYOutput &out) const;
1978 struct CYTypeUnsigned :
1981 CYTypeSpecifier *specifier_;
1983 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1984 specifier_(specifier)
1988 virtual CYTarget *Replace(CYContext &context);
1989 virtual void Output(CYOutput &out) const;
1992 struct CYTypeSigned :
1995 CYTypeSpecifier *specifier_;
1997 CYTypeSigned(CYTypeSpecifier *specifier) :
1998 specifier_(specifier)
2002 virtual CYTarget *Replace(CYContext &context);
2003 virtual void Output(CYOutput &out) const;
2009 CYTypeSpecifier *specifier_;
2011 CYTypeLong(CYTypeSpecifier *specifier) :
2012 specifier_(specifier)
2016 virtual CYTarget *Replace(CYContext &context);
2017 virtual void Output(CYOutput &out) const;
2020 struct CYTypeShort :
2023 CYTypeSpecifier *specifier_;
2025 CYTypeShort(CYTypeSpecifier *specifier) :
2026 specifier_(specifier)
2030 virtual CYTarget *Replace(CYContext &context);
2031 virtual void Output(CYOutput &out) const;
2034 struct CYTypeFunctionWith;
2036 struct CYTypeModifier :
2037 CYNext<CYTypeModifier>
2039 CYTypeModifier(CYTypeModifier *next) :
2040 CYNext<CYTypeModifier>(next)
2044 virtual int Precedence() const = 0;
2046 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2047 CYTarget *Replace(CYContext &context, CYTarget *type);
2049 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2050 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2052 virtual CYTypeFunctionWith *Function() { return NULL; }
2055 struct CYTypeArrayOf :
2058 CYExpression *size_;
2060 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2061 CYTypeModifier(next),
2068 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2069 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2072 struct CYTypeConstant :
2075 CYTypeConstant(CYTypeModifier *next = NULL) :
2076 CYTypeModifier(next)
2082 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2083 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2086 struct CYTypePointerTo :
2089 CYTypePointerTo(CYTypeModifier *next = NULL) :
2090 CYTypeModifier(next)
2096 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2097 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2100 struct CYTypeVolatile :
2103 CYTypeVolatile(CYTypeModifier *next = NULL) :
2104 CYTypeModifier(next)
2110 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2111 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2114 struct CYTypedIdentifier :
2115 CYNext<CYTypedIdentifier>,
2118 CYLocation location_;
2119 CYIdentifier *identifier_;
2120 CYTypeSpecifier *specifier_;
2121 CYTypeModifier *modifier_;
2123 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2124 location_(location),
2125 identifier_(identifier),
2131 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2133 specifier_(specifier),
2138 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2139 CYSetLast(modifier_) = modifier;
2143 virtual CYTarget *Replace(CYContext &context);
2144 virtual void Output(CYOutput &out) const;
2146 CYTypeFunctionWith *Function();
2149 struct CYEncodedType :
2152 CYTypedIdentifier *typed_;
2154 CYEncodedType(CYTypedIdentifier *typed) :
2161 virtual CYTarget *Replace(CYContext &context);
2162 virtual void Output(CYOutput &out, CYFlags flags) const;
2165 struct CYTypedParameter :
2166 CYNext<CYTypedParameter>,
2169 CYTypedIdentifier *typed_;
2171 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2172 CYNext<CYTypedParameter>(next),
2177 CYArgument *Argument(CYContext &context);
2178 CYFunctionParameter *Parameters(CYContext &context);
2179 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2181 virtual void Output(CYOutput &out) const;
2187 CYTypedIdentifier *typed_;
2188 CYTypedParameter *parameters_;
2191 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2193 parameters_(parameters),
2200 virtual CYTarget *Replace(CYContext &context);
2201 virtual void Output(CYOutput &out, CYFlags flags) const;
2210 CYModule(CYWord *part, CYModule *next = NULL) :
2211 CYNext<CYModule>(next),
2216 CYString *Replace(CYContext &context, const char *separator) const;
2217 void Output(CYOutput &out) const;
2225 CYImport(CYModule *module) :
2232 virtual CYStatement *Replace(CYContext &context);
2233 virtual void Output(CYOutput &out, CYFlags flags) const;
2240 CYTypedIdentifier *typed_;
2242 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2250 virtual CYStatement *Replace(CYContext &context);
2251 virtual void Output(CYOutput &out, CYFlags flags) const;
2254 struct CYTypeDefinition :
2257 CYTypedIdentifier *typed_;
2259 CYTypeDefinition(CYTypedIdentifier *typed) :
2266 virtual CYStatement *Replace(CYContext &context);
2267 virtual void Output(CYOutput &out, CYFlags flags) const;
2270 struct CYTypeBlockWith :
2273 CYTypedParameter *parameters_;
2275 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2276 CYTypeModifier(next),
2277 parameters_(parameters)
2283 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2284 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2287 struct CYTypeFunctionWith :
2290 CYTypedParameter *parameters_;
2292 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2293 CYTypeModifier(next),
2294 parameters_(parameters)
2300 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2301 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2303 virtual CYTypeFunctionWith *Function() { return this; }
2312 CYIdentifier *name_;
2315 Catch(CYIdentifier *name, CYStatement *code) :
2321 void Replace(CYContext &context);
2322 virtual void Output(CYOutput &out) const;
2330 CYFinally *finally_;
2332 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2341 virtual CYStatement *Replace(CYContext &context);
2342 virtual void Output(CYOutput &out, CYFlags flags) const;
2348 CYExpression *value_;
2350 Throw(CYExpression *value = NULL) :
2357 virtual CYStatement *Replace(CYContext &context);
2358 virtual void Output(CYOutput &out, CYFlags flags) const;
2366 CYExpression *scope_;
2369 CYWith(CYExpression *scope, CYStatement *code) :
2377 virtual CYStatement *Replace(CYContext &context);
2378 virtual void Output(CYOutput &out, CYFlags flags) const;
2384 CYExpression *value_;
2387 CYSwitch(CYExpression *value, CYClause *clauses) :
2395 virtual CYStatement *Replace(CYContext &context);
2396 virtual void Output(CYOutput &out, CYFlags flags) const;
2408 virtual CYStatement *Replace(CYContext &context);
2409 virtual void Output(CYOutput &out, CYFlags flags) const;
2412 struct CYCondition :
2415 CYExpression *test_;
2416 CYExpression *true_;
2417 CYExpression *false_;
2419 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2428 virtual CYExpression *Replace(CYContext &context);
2429 virtual void Output(CYOutput &out, CYFlags flags) const;
2432 struct CYAddressOf :
2435 CYAddressOf(CYExpression *rhs) :
2440 virtual const char *Operator() const {
2446 virtual CYExpression *Replace(CYContext &context);
2454 CYIndirect(CYExpression *rhs) :
2459 // XXX: this should be checked
2462 virtual CYTarget *Replace(CYContext &context);
2463 virtual void Output(CYOutput &out, CYFlags flags) const;
2467 virtual CYExpression *Replace(CYContext &context);
2469 #define CYPostfix_(op, name, args...) \
2470 struct CY ## name : \
2473 CY ## name(CYExpression *lhs) : \
2478 virtual const char *Operator() const { \
2483 #define CYPrefix_(alphabetic, op, name, args...) \
2484 struct CY ## name : \
2487 CY ## name(CYExpression *rhs) : \
2492 CYAlphabetic(alphabetic) \
2494 virtual const char *Operator() const { \
2499 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2500 struct CY ## name : \
2503 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2508 CYAlphabetic(alphabetic) \
2509 CYPrecedence(precedence) \
2511 virtual const char *Operator() const { \
2516 #define CYAssignment_(op, name, args...) \
2517 struct CY ## name ## Assign : \
2520 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2521 CYAssignment(lhs, rhs) \
2525 virtual const char *Operator() const { \
2530 CYPostfix_("++", PostIncrement)
2531 CYPostfix_("--", PostDecrement)
2533 CYPrefix_(true, "delete", Delete)
2534 CYPrefix_(true, "void", Void)
2535 CYPrefix_(true, "typeof", TypeOf)
2536 CYPrefix_(false, "++", PreIncrement)
2537 CYPrefix_(false, "--", PreDecrement)
2538 CYPrefix_(false, "+", Affirm)
2539 CYPrefix_(false, "-", Negate)
2540 CYPrefix_(false, "~", BitwiseNot)
2541 CYPrefix_(false, "!", LogicalNot)
2543 CYInfix_(false, 5, "*", Multiply, CYReplace)
2544 CYInfix_(false, 5, "/", Divide)
2545 CYInfix_(false, 5, "%", Modulus)
2546 CYInfix_(false, 6, "+", Add, CYReplace)
2547 CYInfix_(false, 6, "-", Subtract)
2548 CYInfix_(false, 7, "<<", ShiftLeft)
2549 CYInfix_(false, 7, ">>", ShiftRightSigned)
2550 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2551 CYInfix_(false, 8, "<", Less)
2552 CYInfix_(false, 8, ">", Greater)
2553 CYInfix_(false, 8, "<=", LessOrEqual)
2554 CYInfix_(false, 8, ">=", GreaterOrEqual)
2555 CYInfix_(true, 8, "instanceof", InstanceOf)
2556 CYInfix_(true, 8, "in", In)
2557 CYInfix_(false, 9, "==", Equal)
2558 CYInfix_(false, 9, "!=", NotEqual)
2559 CYInfix_(false, 9, "===", Identical)
2560 CYInfix_(false, 9, "!==", NotIdentical)
2561 CYInfix_(false, 10, "&", BitwiseAnd)
2562 CYInfix_(false, 11, "^", BitwiseXOr)
2563 CYInfix_(false, 12, "|", BitwiseOr)
2564 CYInfix_(false, 13, "&&", LogicalAnd)
2565 CYInfix_(false, 14, "||", LogicalOr)
2567 CYAssignment_("=", )
2568 CYAssignment_("*=", Multiply)
2569 CYAssignment_("/=", Divide)
2570 CYAssignment_("%=", Modulus)
2571 CYAssignment_("+=", Add)
2572 CYAssignment_("-=", Subtract)
2573 CYAssignment_("<<=", ShiftLeft)
2574 CYAssignment_(">>=", ShiftRightSigned)
2575 CYAssignment_(">>>=", ShiftRightUnsigned)
2576 CYAssignment_("&=", BitwiseAnd)
2577 CYAssignment_("^=", BitwiseXOr)
2578 CYAssignment_("|=", BitwiseOr)
2580 #endif/*CYCRIPT_PARSER_HPP*/