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"
38 double CYCastDouble(const char *value, size_t size);
39 double CYCastDouble(const char *value);
40 double CYCastDouble(CYUTF8String value);
42 void CYNumerify(std::ostringstream &str, double value);
43 void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c = false);
45 // XXX: this really should not be here ... :/
46 void *CYPoolFile(CYPool &pool, const char *path, size_t *psize);
47 CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path);
52 virtual void Output(struct CYOutput &out) const = 0;
73 CYOutput(std::streambuf &out, CYOptions &options) :
84 void Check(char value);
87 _finline void operator ()(char value) {
88 _assert(out_.sputc(value) != EOF);
96 _finline void operator ()(const char *data, std::streamsize size) {
97 _assert(out_.sputn(data, size) == size);
99 position_.Columns(size);
102 _finline void operator ()(const char *data) {
103 return operator ()(data, strlen(data));
106 CYOutput &operator <<(char rhs);
107 CYOutput &operator <<(const char *rhs);
109 _finline CYOutput &operator <<(const CYThing *rhs) {
115 _finline CYOutput &operator <<(const CYThing &rhs) {
124 struct CYPropertyName {
125 virtual bool Computed() const {
129 virtual bool Constructor() const {
133 virtual CYExpression *PropertyName(CYContext &context) = 0;
134 virtual void PropertyName(CYOutput &out) const = 0;
145 CYNoBrace = (1 << 0),
146 CYNoFunction = (1 << 1),
147 CYNoClass = (1 << 2),
150 CYNoRightHand = (1 << 5),
151 CYNoDangle = (1 << 6),
152 CYNoInteger = (1 << 7),
153 CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
156 _finline CYFlags operator ~(CYFlags rhs) {
157 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
160 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
161 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
164 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
165 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
168 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
169 return lhs = lhs | rhs;
172 _finline CYFlags CYLeft(CYFlags flags) {
173 return flags & ~(CYNoDangle | CYNoInteger);
176 _finline CYFlags CYRight(CYFlags flags) {
177 return flags & ~CYNoBFC;
180 _finline CYFlags CYCenter(CYFlags flags) {
181 return CYLeft(CYRight(flags));
190 #define CYCompact(type) \
191 virtual CYCompactType Compact() const { \
192 return CYCompact ## type; \
199 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
200 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
201 virtual void Output(CYOutput &out) const;
203 virtual CYStatement *Replace(CYContext &context) = 0;
205 virtual CYCompactType Compact() const = 0;
206 virtual CYStatement *Return();
209 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
212 typedef CYList<CYStatement> CYStatements;
214 struct CYForInitializer :
217 virtual CYForInitializer *Replace(CYContext &context) = 0;
218 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
227 CYWord(const char *word) :
232 virtual bool Constructor() const {
233 return strcmp(word_, "constructor") == 0;
236 virtual const char *Word() const;
237 virtual void Output(CYOutput &out) const;
239 virtual CYExpression *PropertyName(CYContext &context);
240 virtual void PropertyName(CYOutput &out) const;
243 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
245 return lhs << rhs.Word();
248 enum CYIdentifierKind {
249 CYIdentifierArgument,
255 CYIdentifierVariable,
258 struct CYIdentifier :
259 CYNext<CYIdentifier>,
262 CYLocation location_;
266 CYIdentifier(const char *word) :
273 virtual const char *Word() const;
274 CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
281 CYStatement *statement_;
283 CYLabel(CYIdentifier *name, CYStatement *statement) :
285 statement_(statement)
291 virtual CYStatement *Replace(CYContext &context);
292 virtual void Output(CYOutput &out, CYFlags flags) const;
295 struct CYCStringLess :
296 std::binary_function<const char *, const char *, bool>
298 _finline bool operator ()(const char *lhs, const char *rhs) const {
299 return strcmp(lhs, rhs) < 0;
303 struct CYIdentifierValueLess :
304 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
306 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
307 return CYCStringLess()(lhs->Word(), rhs->Word());
311 struct CYIdentifierFlags :
312 CYNext<CYIdentifierFlags>
314 CYIdentifier *identifier_;
315 CYIdentifierKind kind_;
319 CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
320 CYNext<CYIdentifierFlags>(next),
321 identifier_(identifier),
333 CYIdentifierFlags *shadow_;
335 CYIdentifierFlags *internal_;
337 CYScope(bool transparent, CYContext &context);
339 CYIdentifierFlags *Lookup(CYContext &context, const char *word);
340 CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
342 CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
343 void Merge(CYContext &context, const CYIdentifierFlags *flags);
345 void Close(CYContext &context, CYStatement *&statements);
346 void Close(CYContext &context);
355 CYScript(CYStatement *code) :
360 virtual void Replace(CYContext &context);
361 virtual void Output(CYOutput &out) const;
372 CYIdentifier *super_;
374 CYNonLocal *nonlocal_;
375 CYNonLocal *nextlocal_;
378 std::vector<CYIdentifier *> replace_;
380 CYContext(CYOptions &options) :
391 void ReplaceAll(CYStatement *&statement) {
392 if (statement == NULL)
394 CYStatement *next(statement->next_);
399 if (statement == NULL)
402 statement->SetNext(next);
405 template <typename Type_>
406 void Replace(Type_ *&value) {
407 for (;;) if (value == NULL)
410 Type_ *replace(value->Replace(*this));
411 if (replace != value)
417 void NonLocal(CYStatement *&statements);
418 CYIdentifier *Unique();
422 CYIdentifier *identifier_;
429 CYIdentifier *Target(CYContext &context) {
430 if (identifier_ == NULL)
431 identifier_ = context.Unique();
439 CYIdentifier *identifier_;
446 CYIdentifier *Identifier(CYContext &context) {
448 return next_->Identifier(context);
449 if (identifier_ == NULL)
450 identifier_ = context.Unique();
460 CYBlock(CYStatement *code) :
467 virtual CYStatement *Replace(CYContext &context);
469 virtual void Output(CYOutput &out, CYFlags flags) const;
471 virtual CYStatement *Return();
477 struct CYForInInitializer {
478 virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
480 virtual CYTarget *Replace(CYContext &context) = 0;
481 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
484 struct CYFunctionParameter;
489 struct CYExpression :
492 virtual int Precedence() const = 0;
494 virtual bool RightHand() const {
498 virtual bool Eval() const {
502 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
504 virtual void Output(CYOutput &out) const;
505 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
506 void Output(CYOutput &out, int precedence, CYFlags flags) const;
508 virtual CYExpression *Replace(CYContext &context) = 0;
510 virtual CYExpression *Primitive(CYContext &context) {
514 virtual CYFunctionParameter *Parameter() const;
516 virtual CYNumber *Number(CYContext &context) {
520 virtual CYString *String(CYContext &context) {
524 virtual const char *Word() const {
533 virtual bool RightHand() const {
537 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
539 virtual CYTarget *Replace(CYContext &context) = 0;
540 using CYExpression::Output;
543 #define CYAlphabetic(value) \
544 virtual bool Alphabetic() const { \
548 #define CYPrecedence(value) \
549 static const int Precedence_ = value; \
550 virtual int Precedence() const { \
551 return Precedence_; \
557 CYExpression *expression_;
560 CYCompound(CYExpression *expression, CYExpression *next) :
561 expression_(expression),
564 _assert(expression_ != NULL);
565 _assert(next != NULL);
570 virtual CYExpression *Replace(CYContext &context);
571 void Output(CYOutput &out, CYFlags flags) const;
573 virtual CYFunctionParameter *Parameter() const;
576 struct CYParenthetical :
579 CYExpression *expression_;
581 CYParenthetical(CYExpression *expression) :
582 expression_(expression)
588 virtual CYTarget *Replace(CYContext &context);
589 void Output(CYOutput &out, CYFlags flags) const;
594 struct CYFunctionParameter :
595 CYNext<CYFunctionParameter>,
600 CYFunctionParameter(CYBinding *binding, CYFunctionParameter *next = NULL) :
601 CYNext<CYFunctionParameter>(next),
606 void Replace(CYContext &context, CYStatement *&statements);
607 void Output(CYOutput &out) const;
610 struct CYComprehension :
611 CYNext<CYComprehension>,
614 CYComprehension(CYComprehension *next = NULL) :
615 CYNext<CYComprehension>(next)
619 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
620 CYFunctionParameter *Parameters(CYContext &context) const;
621 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
622 virtual void Output(CYOutput &out) const = 0;
625 struct CYForInComprehension :
629 CYExpression *iterable_;
631 CYForInComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
632 CYComprehension(next),
638 virtual CYFunctionParameter *Parameter(CYContext &context) const;
639 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
640 virtual void Output(CYOutput &out) const;
643 struct CYForOfComprehension :
647 CYExpression *iterable_;
649 CYForOfComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
650 CYComprehension(next),
656 virtual CYFunctionParameter *Parameter(CYContext &context) const;
657 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
658 virtual void Output(CYOutput &out) const;
661 struct CYIfComprehension :
666 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
667 CYComprehension(next),
672 virtual CYFunctionParameter *Parameter(CYContext &context) const;
673 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
674 virtual void Output(CYOutput &out) const;
677 struct CYArrayComprehension :
680 CYExpression *expression_;
681 CYComprehension *comprehensions_;
683 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
684 expression_(expression),
685 comprehensions_(comprehensions)
691 virtual CYTarget *Replace(CYContext &context);
692 virtual void Output(CYOutput &out, CYFlags flags) const;
698 CYLocation location_;
702 virtual CYExpression *Primitive(CYContext &context) {
710 virtual CYTarget *Replace(CYContext &context);
723 CYRange(uint64_t lo, uint64_t hi) :
728 bool operator [](uint8_t value) const {
729 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
732 void operator()(uint8_t value) {
735 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
739 extern CYRange DigitRange_;
740 extern CYRange WordStartRange_;
741 extern CYRange WordEndRange_;
756 CYString(const char *value) :
762 CYString(const char *value, size_t size) :
768 CYString(const CYWord *word) :
769 value_(word->Word()),
770 size_(strlen(value_))
774 const char *Value() const {
778 virtual const char *Word() const;
780 virtual CYNumber *Number(CYContext &context);
781 virtual CYString *String(CYContext &context);
783 CYString *Concat(CYContext &out, CYString *rhs) const;
784 virtual void Output(CYOutput &out, CYFlags flags) const;
786 virtual CYExpression *PropertyName(CYContext &context);
787 virtual void PropertyName(CYOutput &out) const;
790 struct CYElementValue;
795 CYExpression *expression_;
798 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
799 CYNext<CYSpan>(next),
800 expression_(expression),
805 CYElementValue *Replace(CYContext &context);
814 CYTemplate(CYString *string, CYSpan *spans) :
822 virtual CYTarget *Replace(CYContext &context);
823 virtual void Output(CYOutput &out, CYFlags flags) const;
832 CYNumber(double value) :
837 double Value() const {
841 virtual CYNumber *Number(CYContext &context);
842 virtual CYString *String(CYContext &context);
844 virtual void Output(CYOutput &out, CYFlags flags) const;
846 virtual CYExpression *PropertyName(CYContext &context);
847 virtual void PropertyName(CYOutput &out) const;
853 CYExpression *expression_;
855 CYComputed(CYExpression *expression) :
856 expression_(expression)
860 virtual bool Computed() const {
864 virtual CYExpression *PropertyName(CYContext &context);
865 virtual void PropertyName(CYOutput &out) const;
874 CYRegEx(const char *value, size_t size) :
880 const char *Value() const {
884 virtual void Output(CYOutput &out, CYFlags flags) const;
890 virtual CYNumber *Number(CYContext &context);
891 virtual CYString *String(CYContext &context);
893 virtual void Output(CYOutput &out, CYFlags flags) const;
899 virtual CYTarget *Replace(CYContext &context);
900 virtual void Output(CYOutput &out, CYFlags flags) const;
908 virtual bool RightHand() const {
912 virtual bool Value() const = 0;
913 virtual void Output(CYOutput &out, CYFlags flags) const;
919 virtual bool Value() const {
923 virtual CYNumber *Number(CYContext &context);
924 virtual CYString *String(CYContext &context);
930 virtual bool Value() const {
934 virtual CYNumber *Number(CYContext &context);
935 virtual CYString *String(CYContext &context);
943 CYVariable(CYIdentifier *name) :
948 CYVariable(const char *name) :
949 name_(new($pool) CYIdentifier(name))
955 virtual bool Eval() const {
956 return strcmp(name_->Word(), "eval") == 0;
959 virtual CYTarget *Replace(CYContext &context);
960 virtual void Output(CYOutput &out, CYFlags flags) const;
962 virtual CYFunctionParameter *Parameter() const;
970 CYPrefix(CYExpression *rhs) :
975 virtual bool Alphabetic() const = 0;
976 virtual const char *Operator() const = 0;
980 virtual CYExpression *Replace(CYContext &context);
981 virtual void Output(CYOutput &out, CYFlags flags) const;
990 CYInfix(CYExpression *lhs, CYExpression *rhs) :
996 void SetLeft(CYExpression *lhs) {
1000 virtual bool Alphabetic() const = 0;
1001 virtual const char *Operator() const = 0;
1003 virtual CYExpression *Replace(CYContext &context);
1004 virtual void Output(CYOutput &out, CYFlags flags) const;
1012 CYPostfix(CYExpression *lhs) :
1017 virtual const char *Operator() const = 0;
1021 virtual CYExpression *Replace(CYContext &context);
1022 virtual void Output(CYOutput &out, CYFlags flags) const;
1025 struct CYAssignment :
1031 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1037 void SetRight(CYExpression *rhs) {
1041 virtual const char *Operator() const = 0;
1045 virtual CYExpression *Replace(CYContext &context);
1046 virtual void Output(CYOutput &out, CYFlags flags) const;
1054 CYExpression *value_;
1056 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1057 CYNext<CYArgument>(next),
1063 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1064 CYNext<CYArgument>(next),
1070 CYArgument *Replace(CYContext &context);
1071 void Output(CYOutput &out) const;
1078 CYExpression *value_;
1081 CYClause(CYExpression *value, CYStatement *code) :
1087 void Replace(CYContext &context);
1088 virtual void Output(CYOutput &out) const;
1095 CYElement(CYElement *next) :
1096 CYNext<CYElement>(next)
1100 virtual bool Elision() const = 0;
1102 virtual void Replace(CYContext &context) = 0;
1105 struct CYElementValue :
1108 CYExpression *value_;
1110 CYElementValue(CYExpression *value, CYElement *next = NULL) :
1116 virtual bool Elision() const {
1117 return value_ == NULL;
1120 virtual void Replace(CYContext &context);
1121 virtual void Output(CYOutput &out) const;
1124 struct CYElementSpread :
1127 CYExpression *value_;
1129 CYElementSpread(CYExpression *value, CYElement *next = NULL) :
1135 virtual bool Elision() const {
1139 virtual void Replace(CYContext &context);
1140 virtual void Output(CYOutput &out) const;
1146 CYElement *elements_;
1148 CYArray(CYElement *elements = NULL) :
1153 virtual CYTarget *Replace(CYContext &context);
1154 virtual void Output(CYOutput &out, CYFlags flags) const;
1158 CYIdentifier *identifier_;
1159 CYExpression *initializer_;
1161 CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) :
1162 identifier_(identifier),
1163 initializer_(initializer)
1167 CYTarget *Target(CYContext &context);
1169 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1170 virtual void Output(CYOutput &out, CYFlags flags) const;
1173 struct CYForLexical :
1177 CYBinding *binding_;
1179 CYForLexical(bool constant, CYBinding *binding) :
1180 constant_(constant),
1185 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1187 virtual CYTarget *Replace(CYContext &context);
1188 virtual void Output(CYOutput &out, CYFlags flags) const;
1191 struct CYForVariable :
1194 CYBinding *binding_;
1196 CYForVariable(CYBinding *binding) :
1201 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1203 virtual CYTarget *Replace(CYContext &context);
1204 virtual void Output(CYOutput &out, CYFlags flags) const;
1211 CYBinding *binding_;
1213 CYBindings(CYBinding *binding, CYBindings *next = NULL) :
1214 CYNext<CYBindings>(next),
1219 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1221 CYArgument *Argument(CYContext &context);
1222 CYFunctionParameter *Parameter(CYContext &context);
1224 virtual void Output(CYOutput &out) const;
1225 virtual void Output(CYOutput &out, CYFlags flags) const;
1231 CYBindings *bindings_;
1233 CYVar(CYBindings *bindings) :
1240 virtual CYForInitializer *Replace(CYContext &context);
1241 virtual void Output(CYOutput &out, CYFlags flags) const;
1248 CYBindings *bindings_;
1250 CYLexical(bool constant, CYBindings *bindings) :
1251 constant_(constant),
1258 virtual CYForInitializer *Replace(CYContext &context);
1259 virtual void Output(CYOutput &out, CYFlags flags) const;
1263 CYList<CYBindings> bindings_;
1264 CYList<CYStatement> statements_;
1266 operator bool() const {
1267 return statements_ != NULL;
1275 CYPropertyName *name_;
1277 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1278 CYNext<CYProperty>(next),
1283 virtual bool Update() const;
1285 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1286 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1288 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1290 virtual void Replace(CYContext &context) = 0;
1291 virtual void Output(CYOutput &out) const;
1294 struct CYPropertyValue :
1297 CYExpression *value_;
1299 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1300 CYProperty(name, next),
1305 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1306 virtual void Replace(CYContext &context);
1307 virtual void Output(CYOutput &out) const;
1313 CYForInitializer *initializer_;
1314 CYExpression *test_;
1315 CYExpression *increment_;
1318 CYFor(CYForInitializer *initializer, CYExpression *test, CYExpression *increment, CYStatement *code) :
1319 initializer_(initializer),
1321 increment_(increment),
1328 virtual CYStatement *Replace(CYContext &context);
1329 virtual void Output(CYOutput &out, CYFlags flags) const;
1335 CYForInInitializer *initializer_;
1336 CYExpression *iterable_;
1339 CYForIn(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1340 initializer_(initializer),
1341 iterable_(iterable),
1348 virtual CYStatement *Replace(CYContext &context);
1349 virtual void Output(CYOutput &out, CYFlags flags) const;
1352 struct CYForInitialized :
1355 CYBinding *binding_;
1356 CYExpression *iterable_;
1359 CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) :
1361 iterable_(iterable),
1368 virtual CYStatement *Replace(CYContext &context);
1369 virtual void Output(CYOutput &out, CYFlags flags) const;
1375 CYForInInitializer *initializer_;
1376 CYExpression *iterable_;
1379 CYForOf(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1380 initializer_(initializer),
1381 iterable_(iterable),
1388 virtual CYStatement *Replace(CYContext &context);
1389 virtual void Output(CYOutput &out, CYFlags flags) const;
1395 CYProperty *properties_;
1397 CYObject(CYProperty *properties = NULL) :
1398 properties_(properties)
1402 virtual CYTarget *Replace(CYContext &context);
1403 void Output(CYOutput &out, CYFlags flags) const;
1409 CYExpression *object_;
1410 CYExpression *property_;
1412 CYMember(CYExpression *object, CYExpression *property) :
1418 void SetLeft(CYExpression *object) {
1423 struct CYDirectMember :
1426 CYDirectMember(CYExpression *object, CYExpression *property) :
1427 CYMember(object, property)
1433 virtual CYTarget *Replace(CYContext &context);
1434 virtual void Output(CYOutput &out, CYFlags flags) const;
1437 struct CYIndirectMember :
1440 CYIndirectMember(CYExpression *object, CYExpression *property) :
1441 CYMember(object, property)
1447 virtual CYTarget *Replace(CYContext &context);
1448 virtual void Output(CYOutput &out, CYFlags flags) const;
1457 CYExpression *constructor_;
1458 CYArgument *arguments_;
1460 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1461 constructor_(constructor),
1462 arguments_(arguments)
1466 virtual int Precedence() const {
1467 return arguments_ == NULL ? 2 : 1;
1471 virtual CYTarget *Replace(CYContext &context);
1472 virtual void Output(CYOutput &out, CYFlags flags) const;
1474 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1482 CYArgument *arguments_;
1484 CYApply(CYArgument *arguments = NULL) :
1485 arguments_(arguments)
1491 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1497 CYExpression *function_;
1499 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1505 virtual void Output(CYOutput &out, CYFlags flags) const;
1506 virtual CYTarget *Replace(CYContext &context);
1512 CYEval(CYArgument *arguments) :
1517 virtual void Output(CYOutput &out, CYFlags flags) const;
1518 virtual CYTarget *Replace(CYContext &context);
1523 struct CYRubyBlock :
1526 CYExpression *call_;
1529 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1537 virtual CYTarget *Replace(CYContext &context);
1538 virtual void Output(CYOutput &out, CYFlags flags) const;
1540 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1546 CYExpression *test_;
1548 CYStatement *false_;
1550 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1559 virtual CYStatement *Replace(CYContext &context);
1560 virtual void Output(CYOutput &out, CYFlags flags) const;
1562 virtual CYStatement *Return();
1568 CYExpression *test_;
1571 CYDoWhile(CYExpression *test, CYStatement *code) :
1579 virtual CYStatement *Replace(CYContext &context);
1580 virtual void Output(CYOutput &out, CYFlags flags) const;
1586 CYExpression *test_;
1589 CYWhile(CYExpression *test, CYStatement *code) :
1597 virtual CYStatement *Replace(CYContext &context);
1598 virtual void Output(CYOutput &out, CYFlags flags) const;
1602 CYFunctionParameter *parameters_;
1605 CYNonLocal *nonlocal_;
1608 CYIdentifier *super_;
1610 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1611 parameters_(parameters),
1619 void Replace(CYContext &context);
1620 void Output(CYOutput &out) const;
1623 struct CYFunctionExpression :
1627 CYIdentifier *name_;
1629 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1630 CYFunction(parameters, code),
1637 CYTarget *Replace(CYContext &context) override;
1638 virtual void Output(CYOutput &out, CYFlags flags) const;
1645 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1646 CYFunction(parameters, code)
1652 CYExpression *Replace(CYContext &context) override;
1653 virtual void Output(CYOutput &out, CYFlags flags) const;
1660 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1661 CYFunction(parameters, code)
1667 CYTarget *Replace(CYContext &context) override;
1668 virtual void Output(CYOutput &out, CYFlags flags) const;
1671 struct CYFunctionStatement :
1675 CYIdentifier *name_;
1677 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1678 CYFunction(parameters, code),
1685 CYStatement *Replace(CYContext &context) override;
1686 virtual void Output(CYOutput &out, CYFlags flags) const;
1689 struct CYPropertyMethod;
1695 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1696 CYFunction(parameters, code),
1697 CYProperty(name, next)
1701 virtual CYFunctionExpression *Constructor();
1703 using CYProperty::Replace;
1704 virtual void Replace(CYContext &context);
1707 struct CYPropertyGetter :
1710 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1711 CYMethod(name, NULL, code, next)
1715 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1716 virtual void Output(CYOutput &out) const;
1719 struct CYPropertySetter :
1722 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1723 CYMethod(name, parameters, code, next)
1727 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1728 virtual void Output(CYOutput &out) const;
1731 struct CYPropertyMethod :
1734 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1735 CYMethod(name, parameters, code, next)
1739 bool Update() const override;
1741 virtual CYFunctionExpression *Constructor();
1743 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1744 virtual void Output(CYOutput &out) const;
1747 struct CYClassTail :
1750 CYExpression *extends_;
1752 CYFunctionExpression *constructor_;
1753 CYList<CYProperty> instance_;
1754 CYList<CYProperty> static_;
1756 CYClassTail(CYExpression *extends) :
1762 void Output(CYOutput &out) const;
1765 struct CYClassExpression :
1768 CYIdentifier *name_;
1771 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1779 CYTarget *Replace(CYContext &context) override;
1780 virtual void Output(CYOutput &out, CYFlags flags) const;
1783 struct CYClassStatement :
1786 CYIdentifier *name_;
1789 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1797 CYStatement *Replace(CYContext &context) override;
1798 virtual void Output(CYOutput &out, CYFlags flags) const;
1801 struct CYSuperCall :
1804 CYArgument *arguments_;
1806 CYSuperCall(CYArgument *arguments) :
1807 arguments_(arguments)
1813 CYTarget *Replace(CYContext &context) override;
1814 virtual void Output(CYOutput &out, CYFlags flags) const;
1817 struct CYSuperAccess :
1820 CYExpression *property_;
1822 CYSuperAccess(CYExpression *property) :
1829 CYTarget *Replace(CYContext &context) override;
1830 virtual void Output(CYOutput &out, CYFlags flags) const;
1836 CYExpression *expression_;
1838 CYExpress(CYExpression *expression) :
1839 expression_(expression)
1841 if (expression_ == NULL)
1847 CYForInitializer *Replace(CYContext &context) override;
1848 virtual void Output(CYOutput &out, CYFlags flags) const;
1850 virtual CYStatement *Return();
1856 CYIdentifier *label_;
1858 CYContinue(CYIdentifier *label) :
1865 CYStatement *Replace(CYContext &context) override;
1866 virtual void Output(CYOutput &out, CYFlags flags) const;
1872 CYIdentifier *label_;
1874 CYBreak(CYIdentifier *label) :
1881 CYStatement *Replace(CYContext &context) override;
1882 virtual void Output(CYOutput &out, CYFlags flags) const;
1888 CYExpression *value_;
1890 CYReturn(CYExpression *value) :
1897 CYStatement *Replace(CYContext &context) override;
1898 virtual void Output(CYOutput &out, CYFlags flags) const;
1901 struct CYYieldGenerator :
1904 CYExpression *value_;
1906 CYYieldGenerator(CYExpression *value) :
1913 CYExpression *Replace(CYContext &context) override;
1914 virtual void Output(CYOutput &out, CYFlags flags) const;
1917 struct CYYieldValue :
1920 CYExpression *value_;
1922 CYYieldValue(CYExpression *value) :
1929 virtual CYExpression *Replace(CYContext &context);
1930 virtual void Output(CYOutput &out, CYFlags flags) const;
1938 virtual CYForInitializer *Replace(CYContext &context);
1939 virtual void Output(CYOutput &out, CYFlags flags) const;
1947 CYFinally(CYStatement *code) :
1952 void Replace(CYContext &context);
1953 virtual void Output(CYOutput &out) const;
1956 struct CYTypeSpecifier :
1959 virtual CYTarget *Replace(CYContext &context) = 0;
1962 struct CYTypeError :
1968 virtual CYTarget *Replace(CYContext &context);
1969 virtual void Output(CYOutput &out) const;
1972 enum CYTypeSigning {
1978 struct CYTypeCharacter :
1981 CYTypeSigning signing_;
1983 CYTypeCharacter(CYTypeSigning signing) :
1988 virtual CYTarget *Replace(CYContext &context);
1989 virtual void Output(CYOutput &out) const;
1992 struct CYTypeIntegral :
1995 CYTypeSigning signing_;
1998 CYTypeIntegral(CYTypeSigning signing, int length = 1) :
2004 CYTypeIntegral *Long() {
2005 if (length_ != 1 && length_ != 2)
2011 CYTypeIntegral *Short() {
2018 CYTypeIntegral *Signed() {
2019 if (signing_ != CYTypeNeutral)
2021 signing_ = CYTypeSigned;
2025 CYTypeIntegral *Unsigned() {
2026 if (signing_ != CYTypeNeutral)
2028 signing_ = CYTypeUnsigned;
2032 virtual CYTarget *Replace(CYContext &context);
2033 virtual void Output(CYOutput &out) const;
2042 virtual CYTarget *Replace(CYContext &context);
2043 virtual void Output(CYOutput &out) const;
2046 struct CYTypeReference :
2049 CYIdentifier *name_;
2051 CYTypeReference(CYIdentifier *name) :
2056 virtual CYTarget *Replace(CYContext &context);
2057 virtual void Output(CYOutput &out) const;
2060 struct CYTypeVariable :
2063 CYIdentifier *name_;
2065 CYTypeVariable(CYIdentifier *name) :
2070 CYTypeVariable(const char *name) :
2071 name_(new($pool) CYIdentifier(name))
2075 virtual CYTarget *Replace(CYContext &context);
2076 virtual void Output(CYOutput &out) const;
2079 struct CYTypeFunctionWith;
2081 struct CYTypeModifier :
2082 CYNext<CYTypeModifier>
2084 CYTypeModifier(CYTypeModifier *next) :
2085 CYNext<CYTypeModifier>(next)
2089 virtual int Precedence() const = 0;
2091 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2092 CYTarget *Replace(CYContext &context, CYTarget *type);
2094 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2095 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2097 virtual CYTypeFunctionWith *Function() { return NULL; }
2100 struct CYTypeArrayOf :
2103 CYExpression *size_;
2105 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2106 CYTypeModifier(next),
2113 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2114 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2117 struct CYTypeConstant :
2120 CYTypeConstant(CYTypeModifier *next = NULL) :
2121 CYTypeModifier(next)
2127 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2128 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2131 struct CYTypePointerTo :
2134 CYTypePointerTo(CYTypeModifier *next = NULL) :
2135 CYTypeModifier(next)
2141 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2142 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2145 struct CYTypeVolatile :
2148 CYTypeVolatile(CYTypeModifier *next = NULL) :
2149 CYTypeModifier(next)
2155 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2156 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2159 struct CYTypedIdentifier :
2160 CYNext<CYTypedIdentifier>,
2163 CYLocation location_;
2164 CYIdentifier *identifier_;
2165 CYTypeSpecifier *specifier_;
2166 CYTypeModifier *modifier_;
2168 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2169 location_(location),
2170 identifier_(identifier),
2176 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2178 specifier_(specifier),
2183 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2184 CYSetLast(modifier_) = modifier;
2188 virtual CYTarget *Replace(CYContext &context);
2189 virtual void Output(CYOutput &out) const;
2191 CYTypeFunctionWith *Function();
2194 struct CYEncodedType :
2197 CYTypedIdentifier *typed_;
2199 CYEncodedType(CYTypedIdentifier *typed) :
2206 virtual CYTarget *Replace(CYContext &context);
2207 virtual void Output(CYOutput &out, CYFlags flags) const;
2210 struct CYTypedParameter :
2211 CYNext<CYTypedParameter>,
2214 CYTypedIdentifier *typed_;
2216 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2217 CYNext<CYTypedParameter>(next),
2222 CYArgument *Argument(CYContext &context);
2223 CYFunctionParameter *Parameters(CYContext &context);
2224 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2226 virtual void Output(CYOutput &out) const;
2232 CYTypedIdentifier *typed_;
2233 CYTypedParameter *parameters_;
2236 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2238 parameters_(parameters),
2245 virtual CYTarget *Replace(CYContext &context);
2246 virtual void Output(CYOutput &out, CYFlags flags) const;
2255 CYModule(CYWord *part, CYModule *next = NULL) :
2256 CYNext<CYModule>(next),
2261 CYString *Replace(CYContext &context, const char *separator) const;
2262 void Output(CYOutput &out) const;
2270 CYImport(CYModule *module) :
2277 virtual CYStatement *Replace(CYContext &context);
2278 virtual void Output(CYOutput &out, CYFlags flags) const;
2281 struct CYImportSpecifier :
2282 CYNext<CYImportSpecifier>
2285 CYIdentifier *binding_;
2287 CYImportSpecifier(CYWord *name, CYIdentifier *binding) :
2293 CYStatement *Replace(CYContext &context, CYIdentifier *module);
2296 struct CYImportDeclaration :
2299 CYImportSpecifier *specifiers_;
2302 CYImportDeclaration(CYImportSpecifier *specifiers, CYString *module) :
2303 specifiers_(specifiers),
2310 virtual CYStatement *Replace(CYContext &context);
2311 virtual void Output(CYOutput &out, CYFlags flags) const;
2318 CYTypedIdentifier *typed_;
2320 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2328 virtual CYStatement *Replace(CYContext &context);
2329 virtual void Output(CYOutput &out, CYFlags flags) const;
2332 struct CYTypeExpression :
2335 CYTypedIdentifier *typed_;
2337 CYTypeExpression(CYTypedIdentifier *typed) :
2344 virtual CYTarget *Replace(CYContext &context);
2345 virtual void Output(CYOutput &out, CYFlags flags) const;
2348 struct CYTypeDefinition :
2351 CYTypedIdentifier *typed_;
2353 CYTypeDefinition(CYTypedIdentifier *typed) :
2360 virtual CYStatement *Replace(CYContext &context);
2361 virtual void Output(CYOutput &out, CYFlags flags) const;
2364 struct CYTypeBlockWith :
2367 CYTypedParameter *parameters_;
2369 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2370 CYTypeModifier(next),
2371 parameters_(parameters)
2377 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2378 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2381 struct CYTypeFunctionWith :
2384 CYTypedParameter *parameters_;
2386 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2387 CYTypeModifier(next),
2388 parameters_(parameters)
2394 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2395 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2397 virtual CYTypeFunctionWith *Function() { return this; }
2400 struct CYTypeStructField :
2401 CYNext<CYTypeStructField>
2403 CYTypedIdentifier *typed_;
2405 CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) :
2406 CYNext<CYTypeStructField>(next),
2412 struct CYStructTail :
2415 CYTypeStructField *fields_;
2417 CYStructTail(CYTypeStructField *fields) :
2422 CYTarget *Replace(CYContext &context);
2423 virtual void Output(CYOutput &out) const;
2426 struct CYTypeStruct :
2429 CYIdentifier *name_;
2430 CYStructTail *tail_;
2432 CYTypeStruct(CYIdentifier *name, CYStructTail *tail) :
2438 virtual CYTarget *Replace(CYContext &context);
2439 virtual void Output(CYOutput &out) const;
2442 struct CYStructDefinition :
2445 CYIdentifier *name_;
2446 CYStructTail *tail_;
2448 CYStructDefinition(CYIdentifier *name, CYStructTail *tail) :
2456 virtual CYStatement *Replace(CYContext &context);
2457 virtual void Output(CYOutput &out, CYFlags flags) const;
2466 CYIdentifier *name_;
2469 Catch(CYIdentifier *name, CYStatement *code) :
2475 void Replace(CYContext &context);
2476 virtual void Output(CYOutput &out) const;
2484 CYFinally *finally_;
2486 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2495 virtual CYStatement *Replace(CYContext &context);
2496 virtual void Output(CYOutput &out, CYFlags flags) const;
2502 CYExpression *value_;
2504 Throw(CYExpression *value = NULL) :
2511 virtual CYStatement *Replace(CYContext &context);
2512 virtual void Output(CYOutput &out, CYFlags flags) const;
2520 CYExpression *scope_;
2523 CYWith(CYExpression *scope, CYStatement *code) :
2531 virtual CYStatement *Replace(CYContext &context);
2532 virtual void Output(CYOutput &out, CYFlags flags) const;
2538 CYExpression *value_;
2541 CYSwitch(CYExpression *value, CYClause *clauses) :
2549 virtual CYStatement *Replace(CYContext &context);
2550 virtual void Output(CYOutput &out, CYFlags flags) const;
2562 virtual CYStatement *Replace(CYContext &context);
2563 virtual void Output(CYOutput &out, CYFlags flags) const;
2566 struct CYCondition :
2569 CYExpression *test_;
2570 CYExpression *true_;
2571 CYExpression *false_;
2573 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2582 virtual CYExpression *Replace(CYContext &context);
2583 virtual void Output(CYOutput &out, CYFlags flags) const;
2586 struct CYAddressOf :
2589 CYAddressOf(CYExpression *rhs) :
2594 virtual const char *Operator() const {
2600 virtual CYExpression *Replace(CYContext &context);
2608 CYIndirect(CYExpression *rhs) :
2613 // XXX: this should be checked
2616 virtual CYTarget *Replace(CYContext &context);
2617 virtual void Output(CYOutput &out, CYFlags flags) const;
2621 virtual CYExpression *Replace(CYContext &context);
2623 #define CYPostfix_(op, name, args...) \
2624 struct CY ## name : \
2627 CY ## name(CYExpression *lhs) : \
2632 virtual const char *Operator() const { \
2637 #define CYPrefix_(alphabetic, op, name, args...) \
2638 struct CY ## name : \
2641 CY ## name(CYExpression *rhs) : \
2646 CYAlphabetic(alphabetic) \
2648 virtual const char *Operator() const { \
2653 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2654 struct CY ## name : \
2657 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2662 CYAlphabetic(alphabetic) \
2663 CYPrecedence(precedence) \
2665 virtual const char *Operator() const { \
2670 #define CYAssignment_(op, name, args...) \
2671 struct CY ## name ## Assign : \
2674 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2675 CYAssignment(lhs, rhs) \
2679 virtual const char *Operator() const { \
2684 CYPostfix_("++", PostIncrement)
2685 CYPostfix_("--", PostDecrement)
2687 CYPrefix_(true, "delete", Delete)
2688 CYPrefix_(true, "void", Void)
2689 CYPrefix_(true, "typeof", TypeOf)
2690 CYPrefix_(false, "++", PreIncrement)
2691 CYPrefix_(false, "--", PreDecrement)
2692 CYPrefix_(false, "+", Affirm)
2693 CYPrefix_(false, "-", Negate)
2694 CYPrefix_(false, "~", BitwiseNot)
2695 CYPrefix_(false, "!", LogicalNot)
2697 CYInfix_(false, 5, "*", Multiply, CYReplace)
2698 CYInfix_(false, 5, "/", Divide)
2699 CYInfix_(false, 5, "%", Modulus)
2700 CYInfix_(false, 6, "+", Add, CYReplace)
2701 CYInfix_(false, 6, "-", Subtract)
2702 CYInfix_(false, 7, "<<", ShiftLeft)
2703 CYInfix_(false, 7, ">>", ShiftRightSigned)
2704 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2705 CYInfix_(false, 8, "<", Less)
2706 CYInfix_(false, 8, ">", Greater)
2707 CYInfix_(false, 8, "<=", LessOrEqual)
2708 CYInfix_(false, 8, ">=", GreaterOrEqual)
2709 CYInfix_(true, 8, "instanceof", InstanceOf)
2710 CYInfix_(true, 8, "in", In)
2711 CYInfix_(false, 9, "==", Equal)
2712 CYInfix_(false, 9, "!=", NotEqual)
2713 CYInfix_(false, 9, "===", Identical)
2714 CYInfix_(false, 9, "!==", NotIdentical)
2715 CYInfix_(false, 10, "&", BitwiseAnd)
2716 CYInfix_(false, 11, "^", BitwiseXOr)
2717 CYInfix_(false, 12, "|", BitwiseOr)
2718 CYInfix_(false, 13, "&&", LogicalAnd)
2719 CYInfix_(false, 14, "||", LogicalOr)
2721 CYAssignment_("=", )
2722 CYAssignment_("*=", Multiply)
2723 CYAssignment_("/=", Divide)
2724 CYAssignment_("%=", Modulus)
2725 CYAssignment_("+=", Add)
2726 CYAssignment_("-=", Subtract)
2727 CYAssignment_("<<=", ShiftLeft)
2728 CYAssignment_(">>=", ShiftRightSigned)
2729 CYAssignment_(">>>=", ShiftRightUnsigned)
2730 CYAssignment_("&=", BitwiseAnd)
2731 CYAssignment_("^=", BitwiseXOr)
2732 CYAssignment_("|=", BitwiseOr)
2734 #endif/*CYCRIPT_PARSER_HPP*/