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;
901 virtual bool RightHand() const {
905 virtual bool Value() const = 0;
906 virtual void Output(CYOutput &out, CYFlags flags) const;
912 virtual bool Value() const {
916 virtual CYNumber *Number(CYContext &context);
917 virtual CYString *String(CYContext &context);
923 virtual bool Value() const {
927 virtual CYNumber *Number(CYContext &context);
928 virtual CYString *String(CYContext &context);
936 CYVariable(CYIdentifier *name) :
941 CYVariable(const char *name) :
942 name_(new($pool) CYIdentifier(name))
948 virtual bool Eval() const {
949 return strcmp(name_->Word(), "eval") == 0;
952 virtual CYTarget *Replace(CYContext &context);
953 virtual void Output(CYOutput &out, CYFlags flags) const;
955 virtual CYFunctionParameter *Parameter() const;
963 CYPrefix(CYExpression *rhs) :
968 virtual bool Alphabetic() const = 0;
969 virtual const char *Operator() const = 0;
973 virtual CYExpression *Replace(CYContext &context);
974 virtual void Output(CYOutput &out, CYFlags flags) const;
983 CYInfix(CYExpression *lhs, CYExpression *rhs) :
989 void SetLeft(CYExpression *lhs) {
993 virtual bool Alphabetic() const = 0;
994 virtual const char *Operator() const = 0;
996 virtual CYExpression *Replace(CYContext &context);
997 virtual void Output(CYOutput &out, CYFlags flags) const;
1005 CYPostfix(CYExpression *lhs) :
1010 virtual const char *Operator() const = 0;
1014 virtual CYExpression *Replace(CYContext &context);
1015 virtual void Output(CYOutput &out, CYFlags flags) const;
1018 struct CYAssignment :
1024 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1030 void SetRight(CYExpression *rhs) {
1034 virtual const char *Operator() const = 0;
1038 virtual CYExpression *Replace(CYContext &context);
1039 virtual void Output(CYOutput &out, CYFlags flags) const;
1047 CYExpression *value_;
1049 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1050 CYNext<CYArgument>(next),
1056 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1057 CYNext<CYArgument>(next),
1063 CYArgument *Replace(CYContext &context);
1064 void Output(CYOutput &out) const;
1071 CYExpression *case_;
1074 CYClause(CYExpression *_case, CYStatement *code) :
1080 void Replace(CYContext &context);
1081 virtual void Output(CYOutput &out) const;
1087 virtual bool Elision() const = 0;
1089 virtual void Replace(CYContext &context) = 0;
1092 struct CYElementValue :
1096 CYExpression *value_;
1098 CYElementValue(CYExpression *value, CYElement *next) :
1099 CYNext<CYElement>(next),
1104 virtual bool Elision() const {
1105 return value_ == NULL;
1108 virtual void Replace(CYContext &context);
1109 virtual void Output(CYOutput &out) const;
1112 struct CYElementSpread :
1115 CYExpression *value_;
1117 CYElementSpread(CYExpression *value) :
1122 virtual bool Elision() const {
1126 virtual void Replace(CYContext &context);
1127 virtual void Output(CYOutput &out) const;
1133 CYElement *elements_;
1135 CYArray(CYElement *elements = NULL) :
1140 virtual CYTarget *Replace(CYContext &context);
1141 virtual void Output(CYOutput &out, CYFlags flags) const;
1144 struct CYDeclaration {
1145 CYIdentifier *identifier_;
1146 CYExpression *initialiser_;
1148 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1149 identifier_(identifier),
1150 initialiser_(initialiser)
1154 CYTarget *Target(CYContext &context);
1156 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1157 virtual void Output(CYOutput &out, CYFlags flags) const;
1160 struct CYForLexical :
1164 CYDeclaration *declaration_;
1166 CYForLexical(bool constant, CYDeclaration *declaration) :
1167 constant_(constant),
1168 declaration_(declaration)
1172 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1174 virtual CYTarget *Replace(CYContext &context);
1175 virtual void Output(CYOutput &out, CYFlags flags) const;
1178 struct CYForVariable :
1181 CYDeclaration *declaration_;
1183 CYForVariable(CYDeclaration *declaration) :
1184 declaration_(declaration)
1188 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1190 virtual CYTarget *Replace(CYContext &context);
1191 virtual void Output(CYOutput &out, CYFlags flags) const;
1194 struct CYDeclarations :
1195 CYNext<CYDeclarations>,
1198 CYDeclaration *declaration_;
1200 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1201 CYNext<CYDeclarations>(next),
1202 declaration_(declaration)
1206 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1208 CYArgument *Argument(CYContext &context);
1209 CYFunctionParameter *Parameter(CYContext &context);
1211 virtual void Output(CYOutput &out) const;
1212 virtual void Output(CYOutput &out, CYFlags flags) const;
1218 CYDeclarations *declarations_;
1220 CYVar(CYDeclarations *declarations) :
1221 declarations_(declarations)
1227 virtual CYForInitializer *Replace(CYContext &context);
1228 virtual void Output(CYOutput &out, CYFlags flags) const;
1235 CYDeclarations *declarations_;
1237 CYLet(bool constant, CYDeclarations *declarations) :
1238 constant_(constant),
1239 declarations_(declarations)
1245 virtual CYForInitializer *Replace(CYContext &context);
1246 virtual void Output(CYOutput &out, CYFlags flags) const;
1250 CYList<CYDeclarations> declarations_;
1251 CYList<CYStatement> statements_;
1253 operator bool() const {
1254 return statements_ != NULL;
1262 CYPropertyName *name_;
1264 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1265 CYNext<CYProperty>(next),
1270 virtual bool Update() const;
1272 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1273 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1275 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1277 virtual void Replace(CYContext &context) = 0;
1278 virtual void Output(CYOutput &out) const;
1281 struct CYPropertyValue :
1284 CYExpression *value_;
1286 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1287 CYProperty(name, next),
1292 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1293 virtual void Replace(CYContext &context);
1294 virtual void Output(CYOutput &out) const;
1300 CYForInitializer *initialiser_;
1301 CYExpression *test_;
1302 CYExpression *increment_;
1305 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1306 initialiser_(initialiser),
1308 increment_(increment),
1315 virtual CYStatement *Replace(CYContext &context);
1316 virtual void Output(CYOutput &out, CYFlags flags) const;
1322 CYForInInitializer *initialiser_;
1326 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1327 initialiser_(initialiser),
1335 virtual CYStatement *Replace(CYContext &context);
1336 virtual void Output(CYOutput &out, CYFlags flags) const;
1339 struct CYForInitialized :
1342 CYDeclaration *declaration_;
1346 CYForInitialized(CYDeclaration *declaration, CYExpression *set, CYStatement *code) :
1347 declaration_(declaration),
1355 virtual CYStatement *Replace(CYContext &context);
1356 virtual void Output(CYOutput &out, CYFlags flags) const;
1362 CYForInInitializer *initialiser_;
1366 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1367 initialiser_(initialiser),
1375 virtual CYStatement *Replace(CYContext &context);
1376 virtual void Output(CYOutput &out, CYFlags flags) const;
1382 CYProperty *properties_;
1384 CYObject(CYProperty *properties = NULL) :
1385 properties_(properties)
1389 virtual CYTarget *Replace(CYContext &context);
1390 void Output(CYOutput &out, CYFlags flags) const;
1396 CYExpression *object_;
1397 CYExpression *property_;
1399 CYMember(CYExpression *object, CYExpression *property) :
1405 void SetLeft(CYExpression *object) {
1410 struct CYDirectMember :
1413 CYDirectMember(CYExpression *object, CYExpression *property) :
1414 CYMember(object, property)
1420 virtual CYTarget *Replace(CYContext &context);
1421 virtual void Output(CYOutput &out, CYFlags flags) const;
1424 struct CYIndirectMember :
1427 CYIndirectMember(CYExpression *object, CYExpression *property) :
1428 CYMember(object, property)
1434 virtual CYTarget *Replace(CYContext &context);
1435 virtual void Output(CYOutput &out, CYFlags flags) const;
1444 CYExpression *constructor_;
1445 CYArgument *arguments_;
1447 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1448 constructor_(constructor),
1449 arguments_(arguments)
1453 virtual int Precedence() const {
1454 return arguments_ == NULL ? 2 : 1;
1458 virtual CYTarget *Replace(CYContext &context);
1459 virtual void Output(CYOutput &out, CYFlags flags) const;
1461 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1469 CYArgument *arguments_;
1471 CYApply(CYArgument *arguments = NULL) :
1472 arguments_(arguments)
1478 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1484 CYExpression *function_;
1486 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1492 virtual void Output(CYOutput &out, CYFlags flags) const;
1493 virtual CYTarget *Replace(CYContext &context);
1499 CYEval(CYArgument *arguments) :
1504 virtual void Output(CYOutput &out, CYFlags flags) const;
1505 virtual CYTarget *Replace(CYContext &context);
1510 struct CYRubyBlock :
1513 CYExpression *call_;
1516 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1524 virtual CYTarget *Replace(CYContext &context);
1525 virtual void Output(CYOutput &out, CYFlags flags) const;
1527 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1533 CYExpression *test_;
1535 CYStatement *false_;
1537 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1546 virtual CYStatement *Replace(CYContext &context);
1547 virtual void Output(CYOutput &out, CYFlags flags) const;
1549 virtual CYStatement *Return();
1555 CYExpression *test_;
1558 CYDoWhile(CYExpression *test, CYStatement *code) :
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1573 CYExpression *test_;
1576 CYWhile(CYExpression *test, CYStatement *code) :
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1589 CYFunctionParameter *parameters_;
1592 CYNonLocal *nonlocal_;
1595 CYIdentifier *super_;
1597 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1598 parameters_(parameters),
1606 void Replace(CYContext &context);
1607 void Output(CYOutput &out) const;
1610 struct CYFunctionExpression :
1614 CYIdentifier *name_;
1616 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1617 CYFunction(parameters, code),
1624 CYTarget *Replace(CYContext &context) override;
1625 virtual void Output(CYOutput &out, CYFlags flags) const;
1632 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1633 CYFunction(parameters, code)
1639 CYExpression *Replace(CYContext &context) override;
1640 virtual void Output(CYOutput &out, CYFlags flags) const;
1647 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1648 CYFunction(parameters, code)
1654 CYTarget *Replace(CYContext &context) override;
1655 virtual void Output(CYOutput &out, CYFlags flags) const;
1658 struct CYFunctionStatement :
1662 CYIdentifier *name_;
1664 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1665 CYFunction(parameters, code),
1672 CYStatement *Replace(CYContext &context) override;
1673 virtual void Output(CYOutput &out, CYFlags flags) const;
1676 struct CYPropertyMethod;
1682 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1683 CYFunction(parameters, code),
1684 CYProperty(name, next)
1688 virtual CYFunctionExpression *Constructor();
1690 using CYProperty::Replace;
1691 virtual void Replace(CYContext &context);
1694 struct CYPropertyGetter :
1697 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1698 CYMethod(name, NULL, code, next)
1702 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1703 virtual void Output(CYOutput &out) const;
1706 struct CYPropertySetter :
1709 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1710 CYMethod(name, parameters, code, next)
1714 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1715 virtual void Output(CYOutput &out) const;
1718 struct CYPropertyMethod :
1721 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1722 CYMethod(name, parameters, code, next)
1726 bool Update() const override;
1728 virtual CYFunctionExpression *Constructor();
1730 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1731 virtual void Output(CYOutput &out) const;
1734 struct CYClassTail :
1737 CYExpression *extends_;
1739 CYFunctionExpression *constructor_;
1740 CYList<CYProperty> instance_;
1741 CYList<CYProperty> static_;
1743 CYClassTail(CYExpression *extends) :
1749 void Output(CYOutput &out) const;
1752 struct CYClassExpression :
1755 CYIdentifier *name_;
1758 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1766 CYTarget *Replace(CYContext &context) override;
1767 virtual void Output(CYOutput &out, CYFlags flags) const;
1770 struct CYClassStatement :
1773 CYIdentifier *name_;
1776 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1784 CYStatement *Replace(CYContext &context) override;
1785 virtual void Output(CYOutput &out, CYFlags flags) const;
1788 struct CYSuperCall :
1791 CYArgument *arguments_;
1793 CYSuperCall(CYArgument *arguments) :
1794 arguments_(arguments)
1800 CYTarget *Replace(CYContext &context) override;
1801 virtual void Output(CYOutput &out, CYFlags flags) const;
1804 struct CYSuperAccess :
1807 CYExpression *property_;
1809 CYSuperAccess(CYExpression *property) :
1816 CYTarget *Replace(CYContext &context) override;
1817 virtual void Output(CYOutput &out, CYFlags flags) const;
1823 CYExpression *expression_;
1825 CYExpress(CYExpression *expression) :
1826 expression_(expression)
1828 if (expression_ == NULL)
1834 CYForInitializer *Replace(CYContext &context) override;
1835 virtual void Output(CYOutput &out, CYFlags flags) const;
1837 virtual CYStatement *Return();
1843 CYIdentifier *label_;
1845 CYContinue(CYIdentifier *label) :
1852 CYStatement *Replace(CYContext &context) override;
1853 virtual void Output(CYOutput &out, CYFlags flags) const;
1859 CYIdentifier *label_;
1861 CYBreak(CYIdentifier *label) :
1868 CYStatement *Replace(CYContext &context) override;
1869 virtual void Output(CYOutput &out, CYFlags flags) const;
1875 CYExpression *value_;
1877 CYReturn(CYExpression *value) :
1884 CYStatement *Replace(CYContext &context) override;
1885 virtual void Output(CYOutput &out, CYFlags flags) const;
1888 struct CYYieldGenerator :
1891 CYExpression *value_;
1893 CYYieldGenerator(CYExpression *value) :
1900 CYExpression *Replace(CYContext &context) override;
1901 virtual void Output(CYOutput &out, CYFlags flags) const;
1904 struct CYYieldValue :
1907 CYExpression *value_;
1909 CYYieldValue(CYExpression *value) :
1916 virtual CYExpression *Replace(CYContext &context);
1917 virtual void Output(CYOutput &out, CYFlags flags) const;
1925 virtual CYForInitializer *Replace(CYContext &context);
1926 virtual void Output(CYOutput &out, CYFlags flags) const;
1934 CYFinally(CYStatement *code) :
1939 void Replace(CYContext &context);
1940 virtual void Output(CYOutput &out) const;
1943 struct CYTypeSpecifier :
1946 virtual CYTarget *Replace(CYContext &context) = 0;
1949 struct CYTypeError :
1955 virtual CYTarget *Replace(CYContext &context);
1956 virtual void Output(CYOutput &out) const;
1965 virtual CYTarget *Replace(CYContext &context);
1966 virtual void Output(CYOutput &out) const;
1969 struct CYTypeVariable :
1972 CYIdentifier *name_;
1974 CYTypeVariable(CYIdentifier *name) :
1979 CYTypeVariable(const char *name) :
1980 name_(new($pool) CYIdentifier(name))
1984 virtual CYTarget *Replace(CYContext &context);
1985 virtual void Output(CYOutput &out) const;
1988 struct CYTypeUnsigned :
1991 CYTypeSpecifier *specifier_;
1993 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1994 specifier_(specifier)
1998 virtual CYTarget *Replace(CYContext &context);
1999 virtual void Output(CYOutput &out) const;
2002 struct CYTypeSigned :
2005 CYTypeSpecifier *specifier_;
2007 CYTypeSigned(CYTypeSpecifier *specifier) :
2008 specifier_(specifier)
2012 virtual CYTarget *Replace(CYContext &context);
2013 virtual void Output(CYOutput &out) const;
2019 CYTypeSpecifier *specifier_;
2021 CYTypeLong(CYTypeSpecifier *specifier) :
2022 specifier_(specifier)
2026 virtual CYTarget *Replace(CYContext &context);
2027 virtual void Output(CYOutput &out) const;
2030 struct CYTypeShort :
2033 CYTypeSpecifier *specifier_;
2035 CYTypeShort(CYTypeSpecifier *specifier) :
2036 specifier_(specifier)
2040 virtual CYTarget *Replace(CYContext &context);
2041 virtual void Output(CYOutput &out) const;
2044 struct CYTypeFunctionWith;
2046 struct CYTypeModifier :
2047 CYNext<CYTypeModifier>
2049 CYTypeModifier(CYTypeModifier *next) :
2050 CYNext<CYTypeModifier>(next)
2054 virtual int Precedence() const = 0;
2056 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2057 CYTarget *Replace(CYContext &context, CYTarget *type);
2059 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2060 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2062 virtual CYTypeFunctionWith *Function() { return NULL; }
2065 struct CYTypeArrayOf :
2068 CYExpression *size_;
2070 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2071 CYTypeModifier(next),
2078 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2079 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2082 struct CYTypeConstant :
2085 CYTypeConstant(CYTypeModifier *next = NULL) :
2086 CYTypeModifier(next)
2092 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2093 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2096 struct CYTypePointerTo :
2099 CYTypePointerTo(CYTypeModifier *next = NULL) :
2100 CYTypeModifier(next)
2106 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2107 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2110 struct CYTypeVolatile :
2113 CYTypeVolatile(CYTypeModifier *next = NULL) :
2114 CYTypeModifier(next)
2120 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2121 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2124 struct CYTypedIdentifier :
2125 CYNext<CYTypedIdentifier>,
2128 CYLocation location_;
2129 CYIdentifier *identifier_;
2130 CYTypeSpecifier *specifier_;
2131 CYTypeModifier *modifier_;
2133 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2134 location_(location),
2135 identifier_(identifier),
2141 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2143 specifier_(specifier),
2148 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2149 CYSetLast(modifier_) = modifier;
2153 virtual CYTarget *Replace(CYContext &context);
2154 virtual void Output(CYOutput &out) const;
2156 CYTypeFunctionWith *Function();
2159 struct CYEncodedType :
2162 CYTypedIdentifier *typed_;
2164 CYEncodedType(CYTypedIdentifier *typed) :
2171 virtual CYTarget *Replace(CYContext &context);
2172 virtual void Output(CYOutput &out, CYFlags flags) const;
2175 struct CYTypedParameter :
2176 CYNext<CYTypedParameter>,
2179 CYTypedIdentifier *typed_;
2181 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2182 CYNext<CYTypedParameter>(next),
2187 CYArgument *Argument(CYContext &context);
2188 CYFunctionParameter *Parameters(CYContext &context);
2189 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2191 virtual void Output(CYOutput &out) const;
2197 CYTypedIdentifier *typed_;
2198 CYTypedParameter *parameters_;
2201 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2203 parameters_(parameters),
2210 virtual CYTarget *Replace(CYContext &context);
2211 virtual void Output(CYOutput &out, CYFlags flags) const;
2220 CYModule(CYWord *part, CYModule *next = NULL) :
2221 CYNext<CYModule>(next),
2226 CYString *Replace(CYContext &context, const char *separator) const;
2227 void Output(CYOutput &out) const;
2235 CYImport(CYModule *module) :
2242 virtual CYStatement *Replace(CYContext &context);
2243 virtual void Output(CYOutput &out, CYFlags flags) const;
2250 CYTypedIdentifier *typed_;
2252 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2260 virtual CYStatement *Replace(CYContext &context);
2261 virtual void Output(CYOutput &out, CYFlags flags) const;
2264 struct CYTypeDefinition :
2267 CYTypedIdentifier *typed_;
2269 CYTypeDefinition(CYTypedIdentifier *typed) :
2276 virtual CYStatement *Replace(CYContext &context);
2277 virtual void Output(CYOutput &out, CYFlags flags) const;
2280 struct CYTypeBlockWith :
2283 CYTypedParameter *parameters_;
2285 CYTypeBlockWith(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;
2297 struct CYTypeFunctionWith :
2300 CYTypedParameter *parameters_;
2302 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2303 CYTypeModifier(next),
2304 parameters_(parameters)
2310 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2311 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2313 virtual CYTypeFunctionWith *Function() { return this; }
2322 CYIdentifier *name_;
2325 Catch(CYIdentifier *name, CYStatement *code) :
2331 void Replace(CYContext &context);
2332 virtual void Output(CYOutput &out) const;
2340 CYFinally *finally_;
2342 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2351 virtual CYStatement *Replace(CYContext &context);
2352 virtual void Output(CYOutput &out, CYFlags flags) const;
2358 CYExpression *value_;
2360 Throw(CYExpression *value = NULL) :
2367 virtual CYStatement *Replace(CYContext &context);
2368 virtual void Output(CYOutput &out, CYFlags flags) const;
2376 CYExpression *scope_;
2379 CYWith(CYExpression *scope, CYStatement *code) :
2387 virtual CYStatement *Replace(CYContext &context);
2388 virtual void Output(CYOutput &out, CYFlags flags) const;
2394 CYExpression *value_;
2397 CYSwitch(CYExpression *value, CYClause *clauses) :
2405 virtual CYStatement *Replace(CYContext &context);
2406 virtual void Output(CYOutput &out, CYFlags flags) const;
2418 virtual CYStatement *Replace(CYContext &context);
2419 virtual void Output(CYOutput &out, CYFlags flags) const;
2422 struct CYCondition :
2425 CYExpression *test_;
2426 CYExpression *true_;
2427 CYExpression *false_;
2429 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2438 virtual CYExpression *Replace(CYContext &context);
2439 virtual void Output(CYOutput &out, CYFlags flags) const;
2442 struct CYAddressOf :
2445 CYAddressOf(CYExpression *rhs) :
2450 virtual const char *Operator() const {
2456 virtual CYExpression *Replace(CYContext &context);
2464 CYIndirect(CYExpression *rhs) :
2469 // XXX: this should be checked
2472 virtual CYTarget *Replace(CYContext &context);
2473 virtual void Output(CYOutput &out, CYFlags flags) const;
2477 virtual CYExpression *Replace(CYContext &context);
2479 #define CYPostfix_(op, name, args...) \
2480 struct CY ## name : \
2483 CY ## name(CYExpression *lhs) : \
2488 virtual const char *Operator() const { \
2493 #define CYPrefix_(alphabetic, op, name, args...) \
2494 struct CY ## name : \
2497 CY ## name(CYExpression *rhs) : \
2502 CYAlphabetic(alphabetic) \
2504 virtual const char *Operator() const { \
2509 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2510 struct CY ## name : \
2513 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2518 CYAlphabetic(alphabetic) \
2519 CYPrecedence(precedence) \
2521 virtual const char *Operator() const { \
2526 #define CYAssignment_(op, name, args...) \
2527 struct CY ## name ## Assign : \
2530 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2531 CYAssignment(lhs, rhs) \
2535 virtual const char *Operator() const { \
2540 CYPostfix_("++", PostIncrement)
2541 CYPostfix_("--", PostDecrement)
2543 CYPrefix_(true, "delete", Delete)
2544 CYPrefix_(true, "void", Void)
2545 CYPrefix_(true, "typeof", TypeOf)
2546 CYPrefix_(false, "++", PreIncrement)
2547 CYPrefix_(false, "--", PreDecrement)
2548 CYPrefix_(false, "+", Affirm)
2549 CYPrefix_(false, "-", Negate)
2550 CYPrefix_(false, "~", BitwiseNot)
2551 CYPrefix_(false, "!", LogicalNot)
2553 CYInfix_(false, 5, "*", Multiply, CYReplace)
2554 CYInfix_(false, 5, "/", Divide)
2555 CYInfix_(false, 5, "%", Modulus)
2556 CYInfix_(false, 6, "+", Add, CYReplace)
2557 CYInfix_(false, 6, "-", Subtract)
2558 CYInfix_(false, 7, "<<", ShiftLeft)
2559 CYInfix_(false, 7, ">>", ShiftRightSigned)
2560 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2561 CYInfix_(false, 8, "<", Less)
2562 CYInfix_(false, 8, ">", Greater)
2563 CYInfix_(false, 8, "<=", LessOrEqual)
2564 CYInfix_(false, 8, ">=", GreaterOrEqual)
2565 CYInfix_(true, 8, "instanceof", InstanceOf)
2566 CYInfix_(true, 8, "in", In)
2567 CYInfix_(false, 9, "==", Equal)
2568 CYInfix_(false, 9, "!=", NotEqual)
2569 CYInfix_(false, 9, "===", Identical)
2570 CYInfix_(false, 9, "!==", NotIdentical)
2571 CYInfix_(false, 10, "&", BitwiseAnd)
2572 CYInfix_(false, 11, "^", BitwiseXOr)
2573 CYInfix_(false, 12, "|", BitwiseOr)
2574 CYInfix_(false, 13, "&&", LogicalAnd)
2575 CYInfix_(false, 14, "||", LogicalOr)
2577 CYAssignment_("=", )
2578 CYAssignment_("*=", Multiply)
2579 CYAssignment_("/=", Divide)
2580 CYAssignment_("%=", Modulus)
2581 CYAssignment_("+=", Add)
2582 CYAssignment_("-=", Subtract)
2583 CYAssignment_("<<=", ShiftLeft)
2584 CYAssignment_(">>=", ShiftRightSigned)
2585 CYAssignment_(">>>=", ShiftRightUnsigned)
2586 CYAssignment_("&=", BitwiseAnd)
2587 CYAssignment_("^=", BitwiseXOr)
2588 CYAssignment_("|=", BitwiseOr)
2590 #endif/*CYCRIPT_PARSER_HPP*/