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;
582 struct CYFunctionParameter :
583 CYNext<CYFunctionParameter>,
588 CYFunctionParameter(CYBinding *binding, CYFunctionParameter *next = NULL) :
589 CYNext<CYFunctionParameter>(next),
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 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
608 CYFunctionParameter *Parameters(CYContext &context) const;
609 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
610 virtual void Output(CYOutput &out) const = 0;
613 struct CYForInComprehension :
617 CYExpression *iterable_;
619 CYForInComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
620 CYComprehension(next),
626 virtual CYFunctionParameter *Parameter(CYContext &context) const;
627 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
628 virtual void Output(CYOutput &out) const;
631 struct CYForOfComprehension :
635 CYExpression *iterable_;
637 CYForOfComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
638 CYComprehension(next),
644 virtual CYFunctionParameter *Parameter(CYContext &context) const;
645 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
646 virtual void Output(CYOutput &out) const;
649 struct CYIfComprehension :
654 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
655 CYComprehension(next),
660 virtual CYFunctionParameter *Parameter(CYContext &context) const;
661 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
662 virtual void Output(CYOutput &out) const;
665 struct CYArrayComprehension :
668 CYExpression *expression_;
669 CYComprehension *comprehensions_;
671 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
672 expression_(expression),
673 comprehensions_(comprehensions)
679 virtual CYTarget *Replace(CYContext &context);
680 virtual void Output(CYOutput &out, CYFlags flags) const;
686 CYLocation location_;
690 virtual CYExpression *Primitive(CYContext &context) {
698 virtual CYTarget *Replace(CYContext &context);
711 CYRange(uint64_t lo, uint64_t hi) :
716 bool operator [](uint8_t value) const {
717 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
720 void operator()(uint8_t value) {
723 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
727 extern CYRange DigitRange_;
728 extern CYRange WordStartRange_;
729 extern CYRange WordEndRange_;
744 CYString(const char *value) :
750 CYString(const char *value, size_t size) :
756 CYString(const CYWord *word) :
757 value_(word->Word()),
758 size_(strlen(value_))
762 const char *Value() const {
766 virtual const char *Word() const;
768 virtual CYNumber *Number(CYContext &context);
769 virtual CYString *String(CYContext &context);
771 CYString *Concat(CYContext &out, CYString *rhs) const;
772 virtual void Output(CYOutput &out, CYFlags flags) const;
774 virtual CYExpression *PropertyName(CYContext &context);
775 virtual void PropertyName(CYOutput &out) const;
778 struct CYElementValue;
783 CYExpression *expression_;
786 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
787 CYNext<CYSpan>(next),
788 expression_(expression),
793 CYElementValue *Replace(CYContext &context);
802 CYTemplate(CYString *string, CYSpan *spans) :
810 virtual CYTarget *Replace(CYContext &context);
811 virtual void Output(CYOutput &out, CYFlags flags) const;
820 CYNumber(double value) :
825 double Value() const {
829 virtual CYNumber *Number(CYContext &context);
830 virtual CYString *String(CYContext &context);
832 virtual void Output(CYOutput &out, CYFlags flags) const;
834 virtual CYExpression *PropertyName(CYContext &context);
835 virtual void PropertyName(CYOutput &out) const;
841 CYExpression *expression_;
843 CYComputed(CYExpression *expression) :
844 expression_(expression)
848 virtual bool Computed() const {
852 virtual CYExpression *PropertyName(CYContext &context);
853 virtual void PropertyName(CYOutput &out) const;
862 CYRegEx(const char *value, size_t size) :
868 const char *Value() const {
872 virtual void Output(CYOutput &out, CYFlags flags) const;
878 virtual CYNumber *Number(CYContext &context);
879 virtual CYString *String(CYContext &context);
881 virtual void Output(CYOutput &out, CYFlags flags) const;
887 virtual CYTarget *Replace(CYContext &context);
888 virtual void Output(CYOutput &out, CYFlags flags) const;
896 virtual bool RightHand() const {
900 virtual bool Value() const = 0;
901 virtual void Output(CYOutput &out, CYFlags flags) const;
907 virtual bool Value() const {
911 virtual CYNumber *Number(CYContext &context);
912 virtual CYString *String(CYContext &context);
918 virtual bool Value() const {
922 virtual CYNumber *Number(CYContext &context);
923 virtual CYString *String(CYContext &context);
931 CYVariable(CYIdentifier *name) :
936 CYVariable(const char *name) :
937 name_(new($pool) CYIdentifier(name))
943 virtual bool Eval() const {
944 return strcmp(name_->Word(), "eval") == 0;
947 virtual CYTarget *Replace(CYContext &context);
948 virtual void Output(CYOutput &out, CYFlags flags) const;
950 virtual CYFunctionParameter *Parameter() const;
958 CYPrefix(CYExpression *rhs) :
963 virtual bool Alphabetic() const = 0;
964 virtual const char *Operator() const = 0;
968 virtual CYExpression *Replace(CYContext &context);
969 virtual void Output(CYOutput &out, CYFlags flags) const;
978 CYInfix(CYExpression *lhs, CYExpression *rhs) :
984 void SetLeft(CYExpression *lhs) {
988 virtual bool Alphabetic() const = 0;
989 virtual const char *Operator() const = 0;
991 virtual CYExpression *Replace(CYContext &context);
992 virtual void Output(CYOutput &out, CYFlags flags) const;
1000 CYPostfix(CYExpression *lhs) :
1005 virtual const char *Operator() const = 0;
1009 virtual CYExpression *Replace(CYContext &context);
1010 virtual void Output(CYOutput &out, CYFlags flags) const;
1013 struct CYAssignment :
1019 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1025 void SetRight(CYExpression *rhs) {
1029 virtual const char *Operator() const = 0;
1033 virtual CYExpression *Replace(CYContext &context);
1034 virtual void Output(CYOutput &out, CYFlags flags) const;
1042 CYExpression *value_;
1044 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1045 CYNext<CYArgument>(next),
1051 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1052 CYNext<CYArgument>(next),
1058 CYArgument *Replace(CYContext &context);
1059 void Output(CYOutput &out) const;
1066 CYExpression *value_;
1069 CYClause(CYExpression *value, CYStatement *code) :
1075 void Replace(CYContext &context);
1076 virtual void Output(CYOutput &out) const;
1082 virtual bool Elision() const = 0;
1084 virtual void Replace(CYContext &context) = 0;
1087 struct CYElementValue :
1091 CYExpression *value_;
1093 CYElementValue(CYExpression *value, CYElement *next = NULL) :
1094 CYNext<CYElement>(next),
1099 virtual bool Elision() const {
1100 return value_ == NULL;
1103 virtual void Replace(CYContext &context);
1104 virtual void Output(CYOutput &out) const;
1107 struct CYElementSpread :
1110 CYExpression *value_;
1112 CYElementSpread(CYExpression *value) :
1117 virtual bool Elision() const {
1121 virtual void Replace(CYContext &context);
1122 virtual void Output(CYOutput &out) const;
1128 CYElement *elements_;
1130 CYArray(CYElement *elements = NULL) :
1135 virtual CYTarget *Replace(CYContext &context);
1136 virtual void Output(CYOutput &out, CYFlags flags) const;
1140 CYIdentifier *identifier_;
1141 CYExpression *initializer_;
1143 CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) :
1144 identifier_(identifier),
1145 initializer_(initializer)
1149 CYTarget *Target(CYContext &context);
1151 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1152 virtual void Output(CYOutput &out, CYFlags flags) const;
1155 struct CYForLexical :
1159 CYBinding *binding_;
1161 CYForLexical(bool constant, CYBinding *binding) :
1162 constant_(constant),
1167 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1169 virtual CYTarget *Replace(CYContext &context);
1170 virtual void Output(CYOutput &out, CYFlags flags) const;
1173 struct CYForVariable :
1176 CYBinding *binding_;
1178 CYForVariable(CYBinding *binding) :
1183 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1185 virtual CYTarget *Replace(CYContext &context);
1186 virtual void Output(CYOutput &out, CYFlags flags) const;
1193 CYBinding *binding_;
1195 CYBindings(CYBinding *binding, CYBindings *next = NULL) :
1196 CYNext<CYBindings>(next),
1201 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1203 CYArgument *Argument(CYContext &context);
1204 CYFunctionParameter *Parameter(CYContext &context);
1206 virtual void Output(CYOutput &out) const;
1207 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 CYBindings *bindings_;
1215 CYVar(CYBindings *bindings) :
1222 virtual CYForInitializer *Replace(CYContext &context);
1223 virtual void Output(CYOutput &out, CYFlags flags) const;
1230 CYBindings *bindings_;
1232 CYLexical(bool constant, CYBindings *bindings) :
1233 constant_(constant),
1240 virtual CYForInitializer *Replace(CYContext &context);
1241 virtual void Output(CYOutput &out, CYFlags flags) const;
1245 CYList<CYBindings> bindings_;
1246 CYList<CYStatement> statements_;
1248 operator bool() const {
1249 return statements_ != NULL;
1257 CYPropertyName *name_;
1259 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1260 CYNext<CYProperty>(next),
1265 virtual bool Update() const;
1267 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1268 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1270 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1272 virtual void Replace(CYContext &context) = 0;
1273 virtual void Output(CYOutput &out) const;
1276 struct CYPropertyValue :
1279 CYExpression *value_;
1281 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1282 CYProperty(name, next),
1287 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1288 virtual void Replace(CYContext &context);
1289 virtual void Output(CYOutput &out) const;
1295 CYForInitializer *initializer_;
1296 CYExpression *test_;
1297 CYExpression *increment_;
1300 CYFor(CYForInitializer *initializer, CYExpression *test, CYExpression *increment, CYStatement *code) :
1301 initializer_(initializer),
1303 increment_(increment),
1310 virtual CYStatement *Replace(CYContext &context);
1311 virtual void Output(CYOutput &out, CYFlags flags) const;
1317 CYForInInitializer *initializer_;
1318 CYExpression *iterable_;
1321 CYForIn(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1322 initializer_(initializer),
1323 iterable_(iterable),
1330 virtual CYStatement *Replace(CYContext &context);
1331 virtual void Output(CYOutput &out, CYFlags flags) const;
1334 struct CYForInitialized :
1337 CYBinding *binding_;
1338 CYExpression *iterable_;
1341 CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) :
1343 iterable_(iterable),
1350 virtual CYStatement *Replace(CYContext &context);
1351 virtual void Output(CYOutput &out, CYFlags flags) const;
1357 CYForInInitializer *initializer_;
1358 CYExpression *iterable_;
1361 CYForOf(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1362 initializer_(initializer),
1363 iterable_(iterable),
1370 virtual CYStatement *Replace(CYContext &context);
1371 virtual void Output(CYOutput &out, CYFlags flags) const;
1377 CYProperty *properties_;
1379 CYObject(CYProperty *properties = NULL) :
1380 properties_(properties)
1384 virtual CYTarget *Replace(CYContext &context);
1385 void Output(CYOutput &out, CYFlags flags) const;
1391 CYExpression *object_;
1392 CYExpression *property_;
1394 CYMember(CYExpression *object, CYExpression *property) :
1400 void SetLeft(CYExpression *object) {
1405 struct CYDirectMember :
1408 CYDirectMember(CYExpression *object, CYExpression *property) :
1409 CYMember(object, property)
1415 virtual CYTarget *Replace(CYContext &context);
1416 virtual void Output(CYOutput &out, CYFlags flags) const;
1419 struct CYIndirectMember :
1422 CYIndirectMember(CYExpression *object, CYExpression *property) :
1423 CYMember(object, property)
1429 virtual CYTarget *Replace(CYContext &context);
1430 virtual void Output(CYOutput &out, CYFlags flags) const;
1439 CYExpression *constructor_;
1440 CYArgument *arguments_;
1442 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1443 constructor_(constructor),
1444 arguments_(arguments)
1448 virtual int Precedence() const {
1449 return arguments_ == NULL ? 2 : 1;
1453 virtual CYTarget *Replace(CYContext &context);
1454 virtual void Output(CYOutput &out, CYFlags flags) const;
1456 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1464 CYArgument *arguments_;
1466 CYApply(CYArgument *arguments = NULL) :
1467 arguments_(arguments)
1473 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1479 CYExpression *function_;
1481 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1487 virtual void Output(CYOutput &out, CYFlags flags) const;
1488 virtual CYTarget *Replace(CYContext &context);
1494 CYEval(CYArgument *arguments) :
1499 virtual void Output(CYOutput &out, CYFlags flags) const;
1500 virtual CYTarget *Replace(CYContext &context);
1505 struct CYRubyBlock :
1508 CYExpression *call_;
1511 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1519 virtual CYTarget *Replace(CYContext &context);
1520 virtual void Output(CYOutput &out, CYFlags flags) const;
1522 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1528 CYExpression *test_;
1530 CYStatement *false_;
1532 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1541 virtual CYStatement *Replace(CYContext &context);
1542 virtual void Output(CYOutput &out, CYFlags flags) const;
1544 virtual CYStatement *Return();
1550 CYExpression *test_;
1553 CYDoWhile(CYExpression *test, CYStatement *code) :
1561 virtual CYStatement *Replace(CYContext &context);
1562 virtual void Output(CYOutput &out, CYFlags flags) const;
1568 CYExpression *test_;
1571 CYWhile(CYExpression *test, CYStatement *code) :
1579 virtual CYStatement *Replace(CYContext &context);
1580 virtual void Output(CYOutput &out, CYFlags flags) const;
1584 CYFunctionParameter *parameters_;
1587 CYNonLocal *nonlocal_;
1590 CYIdentifier *super_;
1592 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1593 parameters_(parameters),
1601 void Replace(CYContext &context);
1602 void Output(CYOutput &out) const;
1605 struct CYFunctionExpression :
1609 CYIdentifier *name_;
1611 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1612 CYFunction(parameters, code),
1619 CYTarget *Replace(CYContext &context) override;
1620 virtual void Output(CYOutput &out, CYFlags flags) const;
1627 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1628 CYFunction(parameters, code)
1634 CYExpression *Replace(CYContext &context) override;
1635 virtual void Output(CYOutput &out, CYFlags flags) const;
1642 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1643 CYFunction(parameters, code)
1649 CYTarget *Replace(CYContext &context) override;
1650 virtual void Output(CYOutput &out, CYFlags flags) const;
1653 struct CYFunctionStatement :
1657 CYIdentifier *name_;
1659 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1660 CYFunction(parameters, code),
1667 CYStatement *Replace(CYContext &context) override;
1668 virtual void Output(CYOutput &out, CYFlags flags) const;
1671 struct CYPropertyMethod;
1677 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1678 CYFunction(parameters, code),
1679 CYProperty(name, next)
1683 virtual CYFunctionExpression *Constructor();
1685 using CYProperty::Replace;
1686 virtual void Replace(CYContext &context);
1689 struct CYPropertyGetter :
1692 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1693 CYMethod(name, NULL, code, next)
1697 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1698 virtual void Output(CYOutput &out) const;
1701 struct CYPropertySetter :
1704 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1705 CYMethod(name, parameters, code, next)
1709 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1710 virtual void Output(CYOutput &out) const;
1713 struct CYPropertyMethod :
1716 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1717 CYMethod(name, parameters, code, next)
1721 bool Update() const override;
1723 virtual CYFunctionExpression *Constructor();
1725 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1726 virtual void Output(CYOutput &out) const;
1729 struct CYClassTail :
1732 CYExpression *extends_;
1734 CYFunctionExpression *constructor_;
1735 CYList<CYProperty> instance_;
1736 CYList<CYProperty> static_;
1738 CYClassTail(CYExpression *extends) :
1744 void Output(CYOutput &out) const;
1747 struct CYClassExpression :
1750 CYIdentifier *name_;
1753 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1761 CYTarget *Replace(CYContext &context) override;
1762 virtual void Output(CYOutput &out, CYFlags flags) const;
1765 struct CYClassStatement :
1768 CYIdentifier *name_;
1771 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1779 CYStatement *Replace(CYContext &context) override;
1780 virtual void Output(CYOutput &out, CYFlags flags) const;
1783 struct CYSuperCall :
1786 CYArgument *arguments_;
1788 CYSuperCall(CYArgument *arguments) :
1789 arguments_(arguments)
1795 CYTarget *Replace(CYContext &context) override;
1796 virtual void Output(CYOutput &out, CYFlags flags) const;
1799 struct CYSuperAccess :
1802 CYExpression *property_;
1804 CYSuperAccess(CYExpression *property) :
1811 CYTarget *Replace(CYContext &context) override;
1812 virtual void Output(CYOutput &out, CYFlags flags) const;
1818 CYExpression *expression_;
1820 CYExpress(CYExpression *expression) :
1821 expression_(expression)
1823 if (expression_ == NULL)
1829 CYForInitializer *Replace(CYContext &context) override;
1830 virtual void Output(CYOutput &out, CYFlags flags) const;
1832 virtual CYStatement *Return();
1838 CYIdentifier *label_;
1840 CYContinue(CYIdentifier *label) :
1847 CYStatement *Replace(CYContext &context) override;
1848 virtual void Output(CYOutput &out, CYFlags flags) const;
1854 CYIdentifier *label_;
1856 CYBreak(CYIdentifier *label) :
1863 CYStatement *Replace(CYContext &context) override;
1864 virtual void Output(CYOutput &out, CYFlags flags) const;
1870 CYExpression *value_;
1872 CYReturn(CYExpression *value) :
1879 CYStatement *Replace(CYContext &context) override;
1880 virtual void Output(CYOutput &out, CYFlags flags) const;
1883 struct CYYieldGenerator :
1886 CYExpression *value_;
1888 CYYieldGenerator(CYExpression *value) :
1895 CYExpression *Replace(CYContext &context) override;
1896 virtual void Output(CYOutput &out, CYFlags flags) const;
1899 struct CYYieldValue :
1902 CYExpression *value_;
1904 CYYieldValue(CYExpression *value) :
1911 virtual CYExpression *Replace(CYContext &context);
1912 virtual void Output(CYOutput &out, CYFlags flags) const;
1920 virtual CYForInitializer *Replace(CYContext &context);
1921 virtual void Output(CYOutput &out, CYFlags flags) const;
1929 CYFinally(CYStatement *code) :
1934 void Replace(CYContext &context);
1935 virtual void Output(CYOutput &out) const;
1938 struct CYTypeSpecifier :
1941 virtual CYTarget *Replace(CYContext &context) = 0;
1944 struct CYTypeError :
1950 virtual CYTarget *Replace(CYContext &context);
1951 virtual void Output(CYOutput &out) const;
1960 virtual CYTarget *Replace(CYContext &context);
1961 virtual void Output(CYOutput &out) const;
1964 struct CYTypeVariable :
1967 CYIdentifier *name_;
1969 CYTypeVariable(CYIdentifier *name) :
1974 CYTypeVariable(const char *name) :
1975 name_(new($pool) CYIdentifier(name))
1979 virtual CYTarget *Replace(CYContext &context);
1980 virtual void Output(CYOutput &out) const;
1983 struct CYTypeUnsigned :
1986 CYTypeSpecifier *specifier_;
1988 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1989 specifier_(specifier)
1993 virtual CYTarget *Replace(CYContext &context);
1994 virtual void Output(CYOutput &out) const;
1997 struct CYTypeSigned :
2000 CYTypeSpecifier *specifier_;
2002 CYTypeSigned(CYTypeSpecifier *specifier) :
2003 specifier_(specifier)
2007 virtual CYTarget *Replace(CYContext &context);
2008 virtual void Output(CYOutput &out) const;
2014 CYTypeSpecifier *specifier_;
2016 CYTypeLong(CYTypeSpecifier *specifier) :
2017 specifier_(specifier)
2021 virtual CYTarget *Replace(CYContext &context);
2022 virtual void Output(CYOutput &out) const;
2025 struct CYTypeShort :
2028 CYTypeSpecifier *specifier_;
2030 CYTypeShort(CYTypeSpecifier *specifier) :
2031 specifier_(specifier)
2035 virtual CYTarget *Replace(CYContext &context);
2036 virtual void Output(CYOutput &out) const;
2039 struct CYTypeFunctionWith;
2041 struct CYTypeModifier :
2042 CYNext<CYTypeModifier>
2044 CYTypeModifier(CYTypeModifier *next) :
2045 CYNext<CYTypeModifier>(next)
2049 virtual int Precedence() const = 0;
2051 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2052 CYTarget *Replace(CYContext &context, CYTarget *type);
2054 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2055 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2057 virtual CYTypeFunctionWith *Function() { return NULL; }
2060 struct CYTypeArrayOf :
2063 CYExpression *size_;
2065 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2066 CYTypeModifier(next),
2073 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2074 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2077 struct CYTypeConstant :
2080 CYTypeConstant(CYTypeModifier *next = NULL) :
2081 CYTypeModifier(next)
2087 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2088 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2091 struct CYTypePointerTo :
2094 CYTypePointerTo(CYTypeModifier *next = NULL) :
2095 CYTypeModifier(next)
2101 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2102 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2105 struct CYTypeVolatile :
2108 CYTypeVolatile(CYTypeModifier *next = NULL) :
2109 CYTypeModifier(next)
2115 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2116 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2119 struct CYTypedIdentifier :
2120 CYNext<CYTypedIdentifier>,
2123 CYLocation location_;
2124 CYIdentifier *identifier_;
2125 CYTypeSpecifier *specifier_;
2126 CYTypeModifier *modifier_;
2128 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2129 location_(location),
2130 identifier_(identifier),
2136 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2138 specifier_(specifier),
2143 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2144 CYSetLast(modifier_) = modifier;
2148 virtual CYTarget *Replace(CYContext &context);
2149 virtual void Output(CYOutput &out) const;
2151 CYTypeFunctionWith *Function();
2154 struct CYEncodedType :
2157 CYTypedIdentifier *typed_;
2159 CYEncodedType(CYTypedIdentifier *typed) :
2166 virtual CYTarget *Replace(CYContext &context);
2167 virtual void Output(CYOutput &out, CYFlags flags) const;
2170 struct CYTypedParameter :
2171 CYNext<CYTypedParameter>,
2174 CYTypedIdentifier *typed_;
2176 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2177 CYNext<CYTypedParameter>(next),
2182 CYArgument *Argument(CYContext &context);
2183 CYFunctionParameter *Parameters(CYContext &context);
2184 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2186 virtual void Output(CYOutput &out) const;
2192 CYTypedIdentifier *typed_;
2193 CYTypedParameter *parameters_;
2196 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2198 parameters_(parameters),
2205 virtual CYTarget *Replace(CYContext &context);
2206 virtual void Output(CYOutput &out, CYFlags flags) const;
2215 CYModule(CYWord *part, CYModule *next = NULL) :
2216 CYNext<CYModule>(next),
2221 CYString *Replace(CYContext &context, const char *separator) const;
2222 void Output(CYOutput &out) const;
2230 CYImport(CYModule *module) :
2237 virtual CYStatement *Replace(CYContext &context);
2238 virtual void Output(CYOutput &out, CYFlags flags) const;
2245 CYTypedIdentifier *typed_;
2247 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2255 virtual CYStatement *Replace(CYContext &context);
2256 virtual void Output(CYOutput &out, CYFlags flags) const;
2259 struct CYTypeExpression :
2262 CYTypedIdentifier *typed_;
2264 CYTypeExpression(CYTypedIdentifier *typed) :
2271 virtual CYTarget *Replace(CYContext &context);
2272 virtual void Output(CYOutput &out, CYFlags flags) const;
2275 struct CYTypeDefinition :
2278 CYTypedIdentifier *typed_;
2280 CYTypeDefinition(CYTypedIdentifier *typed) :
2287 virtual CYStatement *Replace(CYContext &context);
2288 virtual void Output(CYOutput &out, CYFlags flags) const;
2291 struct CYTypeBlockWith :
2294 CYTypedParameter *parameters_;
2296 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2297 CYTypeModifier(next),
2298 parameters_(parameters)
2304 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2305 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2308 struct CYTypeFunctionWith :
2311 CYTypedParameter *parameters_;
2313 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2314 CYTypeModifier(next),
2315 parameters_(parameters)
2321 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2322 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2324 virtual CYTypeFunctionWith *Function() { return this; }
2327 struct CYTypeStructField :
2328 CYNext<CYTypeStructField>
2330 CYTypedIdentifier *typed_;
2332 CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) :
2333 CYNext<CYTypeStructField>(next),
2339 struct CYTypeStruct :
2342 CYIdentifier *name_;
2343 CYTypeStructField *fields_;
2345 CYTypeStruct(CYIdentifier *name, CYTypeStructField *fields) :
2351 virtual CYTarget *Replace(CYContext &context);
2352 virtual void Output(CYOutput &out) const;
2361 CYIdentifier *name_;
2364 Catch(CYIdentifier *name, CYStatement *code) :
2370 void Replace(CYContext &context);
2371 virtual void Output(CYOutput &out) const;
2379 CYFinally *finally_;
2381 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2390 virtual CYStatement *Replace(CYContext &context);
2391 virtual void Output(CYOutput &out, CYFlags flags) const;
2397 CYExpression *value_;
2399 Throw(CYExpression *value = NULL) :
2406 virtual CYStatement *Replace(CYContext &context);
2407 virtual void Output(CYOutput &out, CYFlags flags) const;
2415 CYExpression *scope_;
2418 CYWith(CYExpression *scope, CYStatement *code) :
2426 virtual CYStatement *Replace(CYContext &context);
2427 virtual void Output(CYOutput &out, CYFlags flags) const;
2433 CYExpression *value_;
2436 CYSwitch(CYExpression *value, CYClause *clauses) :
2444 virtual CYStatement *Replace(CYContext &context);
2445 virtual void Output(CYOutput &out, CYFlags flags) const;
2457 virtual CYStatement *Replace(CYContext &context);
2458 virtual void Output(CYOutput &out, CYFlags flags) const;
2461 struct CYCondition :
2464 CYExpression *test_;
2465 CYExpression *true_;
2466 CYExpression *false_;
2468 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2477 virtual CYExpression *Replace(CYContext &context);
2478 virtual void Output(CYOutput &out, CYFlags flags) const;
2481 struct CYAddressOf :
2484 CYAddressOf(CYExpression *rhs) :
2489 virtual const char *Operator() const {
2495 virtual CYExpression *Replace(CYContext &context);
2503 CYIndirect(CYExpression *rhs) :
2508 // XXX: this should be checked
2511 virtual CYTarget *Replace(CYContext &context);
2512 virtual void Output(CYOutput &out, CYFlags flags) const;
2516 virtual CYExpression *Replace(CYContext &context);
2518 #define CYPostfix_(op, name, args...) \
2519 struct CY ## name : \
2522 CY ## name(CYExpression *lhs) : \
2527 virtual const char *Operator() const { \
2532 #define CYPrefix_(alphabetic, op, name, args...) \
2533 struct CY ## name : \
2536 CY ## name(CYExpression *rhs) : \
2541 CYAlphabetic(alphabetic) \
2543 virtual const char *Operator() const { \
2548 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2549 struct CY ## name : \
2552 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2557 CYAlphabetic(alphabetic) \
2558 CYPrecedence(precedence) \
2560 virtual const char *Operator() const { \
2565 #define CYAssignment_(op, name, args...) \
2566 struct CY ## name ## Assign : \
2569 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2570 CYAssignment(lhs, rhs) \
2574 virtual const char *Operator() const { \
2579 CYPostfix_("++", PostIncrement)
2580 CYPostfix_("--", PostDecrement)
2582 CYPrefix_(true, "delete", Delete)
2583 CYPrefix_(true, "void", Void)
2584 CYPrefix_(true, "typeof", TypeOf)
2585 CYPrefix_(false, "++", PreIncrement)
2586 CYPrefix_(false, "--", PreDecrement)
2587 CYPrefix_(false, "+", Affirm)
2588 CYPrefix_(false, "-", Negate)
2589 CYPrefix_(false, "~", BitwiseNot)
2590 CYPrefix_(false, "!", LogicalNot)
2592 CYInfix_(false, 5, "*", Multiply, CYReplace)
2593 CYInfix_(false, 5, "/", Divide)
2594 CYInfix_(false, 5, "%", Modulus)
2595 CYInfix_(false, 6, "+", Add, CYReplace)
2596 CYInfix_(false, 6, "-", Subtract)
2597 CYInfix_(false, 7, "<<", ShiftLeft)
2598 CYInfix_(false, 7, ">>", ShiftRightSigned)
2599 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2600 CYInfix_(false, 8, "<", Less)
2601 CYInfix_(false, 8, ">", Greater)
2602 CYInfix_(false, 8, "<=", LessOrEqual)
2603 CYInfix_(false, 8, ">=", GreaterOrEqual)
2604 CYInfix_(true, 8, "instanceof", InstanceOf)
2605 CYInfix_(true, 8, "in", In)
2606 CYInfix_(false, 9, "==", Equal)
2607 CYInfix_(false, 9, "!=", NotEqual)
2608 CYInfix_(false, 9, "===", Identical)
2609 CYInfix_(false, 9, "!==", NotIdentical)
2610 CYInfix_(false, 10, "&", BitwiseAnd)
2611 CYInfix_(false, 11, "^", BitwiseXOr)
2612 CYInfix_(false, 12, "|", BitwiseOr)
2613 CYInfix_(false, 13, "&&", LogicalAnd)
2614 CYInfix_(false, 14, "||", LogicalOr)
2616 CYAssignment_("=", )
2617 CYAssignment_("*=", Multiply)
2618 CYAssignment_("/=", Divide)
2619 CYAssignment_("%=", Modulus)
2620 CYAssignment_("+=", Add)
2621 CYAssignment_("-=", Subtract)
2622 CYAssignment_("<<=", ShiftLeft)
2623 CYAssignment_(">>=", ShiftRightSigned)
2624 CYAssignment_(">>>=", ShiftRightUnsigned)
2625 CYAssignment_("&=", BitwiseAnd)
2626 CYAssignment_("^=", BitwiseXOr)
2627 CYAssignment_("|=", BitwiseOr)
2629 #endif/*CYCRIPT_PARSER_HPP*/