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 CYNoColon = (1 << 8),
154 CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
157 _finline CYFlags operator ~(CYFlags rhs) {
158 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
161 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
162 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
165 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
166 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
169 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
170 return lhs = lhs | rhs;
173 _finline CYFlags CYLeft(CYFlags flags) {
174 return flags & ~(CYNoDangle | CYNoInteger);
177 _finline CYFlags CYRight(CYFlags flags) {
178 return flags & ~CYNoBFC;
181 _finline CYFlags CYCenter(CYFlags flags) {
182 return CYLeft(CYRight(flags));
191 #define CYCompact(type) \
192 virtual CYCompactType Compact() const { \
193 return CYCompact ## type; \
200 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
201 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
202 virtual void Output(CYOutput &out) const;
204 virtual CYStatement *Replace(CYContext &context) = 0;
206 virtual CYCompactType Compact() const = 0;
207 virtual CYStatement *Return();
210 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
213 typedef CYList<CYStatement> CYStatements;
215 struct CYForInitializer :
218 virtual CYForInitializer *Replace(CYContext &context) = 0;
219 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
228 CYWord(const char *word) :
233 virtual bool Constructor() const {
234 return strcmp(word_, "constructor") == 0;
237 virtual const char *Word() const;
238 virtual void Output(CYOutput &out) const;
240 virtual CYExpression *PropertyName(CYContext &context);
241 virtual void PropertyName(CYOutput &out) const;
244 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
246 return lhs << rhs.Word();
249 enum CYIdentifierKind {
250 CYIdentifierArgument,
256 CYIdentifierVariable,
259 struct CYIdentifier :
260 CYNext<CYIdentifier>,
263 CYLocation location_;
267 CYIdentifier(const char *word) :
274 virtual const char *Word() const;
275 CYIdentifier *Replace(CYContext &context, CYIdentifierKind);
282 CYStatement *statement_;
284 CYLabel(CYIdentifier *name, CYStatement *statement) :
286 statement_(statement)
292 virtual CYStatement *Replace(CYContext &context);
293 virtual void Output(CYOutput &out, CYFlags flags) const;
296 struct CYCStringLess :
297 std::binary_function<const char *, const char *, bool>
299 _finline bool operator ()(const char *lhs, const char *rhs) const {
300 return strcmp(lhs, rhs) < 0;
304 struct CYIdentifierValueLess :
305 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
307 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
308 return CYCStringLess()(lhs->Word(), rhs->Word());
312 struct CYIdentifierFlags :
313 CYNext<CYIdentifierFlags>
315 CYIdentifier *identifier_;
316 CYIdentifierKind kind_;
320 CYIdentifierFlags(CYIdentifier *identifier, CYIdentifierKind kind, CYIdentifierFlags *next = NULL) :
321 CYNext<CYIdentifierFlags>(next),
322 identifier_(identifier),
334 CYIdentifierFlags *shadow_;
336 CYIdentifierFlags *internal_;
338 CYScope(bool transparent, CYContext &context);
340 CYIdentifierFlags *Lookup(CYContext &context, const char *word);
341 CYIdentifierFlags *Lookup(CYContext &context, CYIdentifier *identifier);
343 CYIdentifierFlags *Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind);
344 void Merge(CYContext &context, const CYIdentifierFlags *flags);
346 void Close(CYContext &context, CYStatement *&statements);
347 void Close(CYContext &context);
356 CYScript(CYStatement *code) :
361 virtual void Replace(CYContext &context);
362 virtual void Output(CYOutput &out) const;
373 CYIdentifier *super_;
375 CYNonLocal *nonlocal_;
376 CYNonLocal *nextlocal_;
379 std::vector<CYIdentifier *> replace_;
381 CYContext(CYOptions &options) :
392 void ReplaceAll(CYStatement *&statement) {
393 if (statement == NULL)
395 CYStatement *next(statement->next_);
400 if (statement == NULL)
403 statement->SetNext(next);
406 template <typename Type_>
407 void Replace(Type_ *&value) {
408 for (;;) if (value == NULL)
411 Type_ *replace(value->Replace(*this));
412 if (replace != value)
418 void NonLocal(CYStatement *&statements);
419 CYIdentifier *Unique();
423 CYIdentifier *identifier_;
430 CYIdentifier *Target(CYContext &context) {
431 if (identifier_ == NULL)
432 identifier_ = context.Unique();
440 CYIdentifier *identifier_;
447 CYIdentifier *Identifier(CYContext &context) {
449 return next_->Identifier(context);
450 if (identifier_ == NULL)
451 identifier_ = context.Unique();
461 CYBlock(CYStatement *code) :
468 virtual CYStatement *Replace(CYContext &context);
470 virtual void Output(CYOutput &out, CYFlags flags) const;
472 virtual CYStatement *Return();
478 struct CYForInInitializer {
479 virtual CYStatement *Initialize(CYContext &context, CYExpression *value) = 0;
481 virtual CYTarget *Replace(CYContext &context) = 0;
482 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
485 struct CYFunctionParameter;
490 struct CYExpression :
493 virtual int Precedence() const = 0;
495 virtual bool RightHand() const {
499 virtual bool Eval() const {
503 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
505 virtual void Output(CYOutput &out) const;
506 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
507 void Output(CYOutput &out, int precedence, CYFlags flags) const;
509 virtual CYExpression *Replace(CYContext &context) = 0;
511 virtual CYExpression *Primitive(CYContext &context) {
515 virtual CYFunctionParameter *Parameter() const;
517 virtual CYNumber *Number(CYContext &context) {
521 virtual CYString *String(CYContext &context) {
525 virtual const char *Word() const {
534 virtual bool RightHand() const {
538 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
540 virtual CYTarget *Replace(CYContext &context) = 0;
541 using CYExpression::Output;
544 #define CYAlphabetic(value) \
545 virtual bool Alphabetic() const { \
549 #define CYPrecedence(value) \
550 static const int Precedence_ = value; \
551 virtual int Precedence() const { \
552 return Precedence_; \
558 CYExpression *expression_;
561 CYCompound(CYExpression *expression, CYExpression *next) :
562 expression_(expression),
565 _assert(expression_ != NULL);
566 _assert(next != NULL);
571 virtual CYExpression *Replace(CYContext &context);
572 void Output(CYOutput &out, CYFlags flags) const;
574 virtual CYFunctionParameter *Parameter() const;
577 struct CYParenthetical :
580 CYExpression *expression_;
582 CYParenthetical(CYExpression *expression) :
583 expression_(expression)
589 virtual CYTarget *Replace(CYContext &context);
590 void Output(CYOutput &out, CYFlags flags) const;
595 struct CYFunctionParameter :
596 CYNext<CYFunctionParameter>,
601 CYFunctionParameter(CYBinding *binding, CYFunctionParameter *next = NULL) :
602 CYNext<CYFunctionParameter>(next),
607 void Replace(CYContext &context, CYStatement *&statements);
608 void Output(CYOutput &out) const;
611 struct CYComprehension :
612 CYNext<CYComprehension>,
615 CYComprehension(CYComprehension *next = NULL) :
616 CYNext<CYComprehension>(next)
620 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
621 CYFunctionParameter *Parameters(CYContext &context) const;
622 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
623 virtual void Output(CYOutput &out) const = 0;
626 struct CYForInComprehension :
630 CYExpression *iterable_;
632 CYForInComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
633 CYComprehension(next),
639 virtual CYFunctionParameter *Parameter(CYContext &context) const;
640 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
641 virtual void Output(CYOutput &out) const;
644 struct CYForOfComprehension :
648 CYExpression *iterable_;
650 CYForOfComprehension(CYBinding *binding, CYExpression *iterable, CYComprehension *next = NULL) :
651 CYComprehension(next),
657 virtual CYFunctionParameter *Parameter(CYContext &context) const;
658 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
659 virtual void Output(CYOutput &out) const;
662 struct CYIfComprehension :
667 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
668 CYComprehension(next),
673 virtual CYFunctionParameter *Parameter(CYContext &context) const;
674 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
675 virtual void Output(CYOutput &out) const;
678 struct CYArrayComprehension :
681 CYExpression *expression_;
682 CYComprehension *comprehensions_;
684 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
685 expression_(expression),
686 comprehensions_(comprehensions)
692 virtual CYTarget *Replace(CYContext &context);
693 virtual void Output(CYOutput &out, CYFlags flags) const;
699 CYLocation location_;
703 virtual CYExpression *Primitive(CYContext &context) {
711 virtual CYTarget *Replace(CYContext &context);
724 CYRange(uint64_t lo, uint64_t hi) :
729 bool operator [](uint8_t value) const {
730 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
733 void operator()(uint8_t value) {
736 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
740 extern CYRange DigitRange_;
741 extern CYRange WordStartRange_;
742 extern CYRange WordEndRange_;
757 CYString(const char *value) :
763 CYString(const char *value, size_t size) :
769 CYString(const CYWord *word) :
770 value_(word->Word()),
771 size_(strlen(value_))
775 const char *Value() const {
779 virtual const char *Word() const;
781 virtual CYNumber *Number(CYContext &context);
782 virtual CYString *String(CYContext &context);
784 CYString *Concat(CYContext &out, CYString *rhs) const;
785 virtual void Output(CYOutput &out, CYFlags flags) const;
787 virtual CYExpression *PropertyName(CYContext &context);
788 virtual void PropertyName(CYOutput &out) const;
791 struct CYElementValue;
796 CYExpression *expression_;
799 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
800 CYNext<CYSpan>(next),
801 expression_(expression),
806 CYElementValue *Replace(CYContext &context);
815 CYTemplate(CYString *string, CYSpan *spans) :
823 virtual CYTarget *Replace(CYContext &context);
824 virtual void Output(CYOutput &out, CYFlags flags) const;
833 CYNumber(double value) :
838 double Value() const {
842 virtual CYNumber *Number(CYContext &context);
843 virtual CYString *String(CYContext &context);
845 virtual void Output(CYOutput &out, CYFlags flags) const;
847 virtual CYExpression *PropertyName(CYContext &context);
848 virtual void PropertyName(CYOutput &out) const;
854 CYExpression *expression_;
856 CYComputed(CYExpression *expression) :
857 expression_(expression)
861 virtual bool Computed() const {
865 virtual CYExpression *PropertyName(CYContext &context);
866 virtual void PropertyName(CYOutput &out) const;
875 CYRegEx(const char *value, size_t size) :
881 const char *Value() const {
885 virtual void Output(CYOutput &out, CYFlags flags) const;
891 virtual CYNumber *Number(CYContext &context);
892 virtual CYString *String(CYContext &context);
894 virtual void Output(CYOutput &out, CYFlags flags) const;
900 virtual CYTarget *Replace(CYContext &context);
901 virtual void Output(CYOutput &out, CYFlags flags) const;
909 virtual bool RightHand() const {
913 virtual bool Value() const = 0;
914 virtual void Output(CYOutput &out, CYFlags flags) const;
920 virtual bool Value() const {
924 virtual CYNumber *Number(CYContext &context);
925 virtual CYString *String(CYContext &context);
931 virtual bool Value() const {
935 virtual CYNumber *Number(CYContext &context);
936 virtual CYString *String(CYContext &context);
944 CYVariable(CYIdentifier *name) :
949 CYVariable(const char *name) :
950 name_(new($pool) CYIdentifier(name))
956 virtual bool Eval() const {
957 return strcmp(name_->Word(), "eval") == 0;
960 virtual CYTarget *Replace(CYContext &context);
961 virtual void Output(CYOutput &out, CYFlags flags) const;
963 virtual CYFunctionParameter *Parameter() const;
971 CYSymbol(const char *name) :
978 virtual CYTarget *Replace(CYContext &context);
979 virtual void Output(CYOutput &out, CYFlags flags) const;
987 CYPrefix(CYExpression *rhs) :
992 virtual bool Alphabetic() const = 0;
993 virtual const char *Operator() const = 0;
997 virtual CYExpression *Replace(CYContext &context);
998 virtual void Output(CYOutput &out, CYFlags flags) const;
1007 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1013 void SetLeft(CYExpression *lhs) {
1017 virtual bool Alphabetic() const = 0;
1018 virtual const char *Operator() const = 0;
1020 virtual CYExpression *Replace(CYContext &context);
1021 virtual void Output(CYOutput &out, CYFlags flags) const;
1029 CYPostfix(CYExpression *lhs) :
1034 virtual const char *Operator() const = 0;
1038 virtual CYExpression *Replace(CYContext &context);
1039 virtual void Output(CYOutput &out, CYFlags flags) const;
1042 struct CYAssignment :
1048 CYAssignment(CYTarget *lhs, CYExpression *rhs) :
1054 void SetRight(CYExpression *rhs) {
1058 virtual const char *Operator() const = 0;
1062 virtual CYExpression *Replace(CYContext &context);
1063 virtual void Output(CYOutput &out, CYFlags flags) const;
1071 CYExpression *value_;
1073 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1074 CYNext<CYArgument>(next),
1080 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1081 CYNext<CYArgument>(next),
1087 CYArgument *Replace(CYContext &context);
1088 void Output(CYOutput &out) const;
1095 CYExpression *value_;
1098 CYClause(CYExpression *value, CYStatement *code) :
1104 void Replace(CYContext &context);
1105 virtual void Output(CYOutput &out) const;
1112 CYElement(CYElement *next) :
1113 CYNext<CYElement>(next)
1117 virtual bool Elision() const = 0;
1119 virtual void Replace(CYContext &context) = 0;
1122 struct CYElementValue :
1125 CYExpression *value_;
1127 CYElementValue(CYExpression *value, CYElement *next = NULL) :
1133 virtual bool Elision() const {
1134 return value_ == NULL;
1137 virtual void Replace(CYContext &context);
1138 virtual void Output(CYOutput &out) const;
1141 struct CYElementSpread :
1144 CYExpression *value_;
1146 CYElementSpread(CYExpression *value, CYElement *next = NULL) :
1152 virtual bool Elision() const {
1156 virtual void Replace(CYContext &context);
1157 virtual void Output(CYOutput &out) const;
1163 CYElement *elements_;
1165 CYArray(CYElement *elements = NULL) :
1170 virtual CYTarget *Replace(CYContext &context);
1171 virtual void Output(CYOutput &out, CYFlags flags) const;
1175 CYIdentifier *identifier_;
1176 CYExpression *initializer_;
1178 CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) :
1179 identifier_(identifier),
1180 initializer_(initializer)
1184 CYTarget *Target(CYContext &context);
1186 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1187 virtual void Output(CYOutput &out, CYFlags flags) const;
1190 struct CYForLexical :
1194 CYBinding *binding_;
1196 CYForLexical(bool constant, CYBinding *binding) :
1197 constant_(constant),
1202 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1204 virtual CYTarget *Replace(CYContext &context);
1205 virtual void Output(CYOutput &out, CYFlags flags) const;
1208 struct CYForVariable :
1211 CYBinding *binding_;
1213 CYForVariable(CYBinding *binding) :
1218 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1220 virtual CYTarget *Replace(CYContext &context);
1221 virtual void Output(CYOutput &out, CYFlags flags) const;
1228 CYBinding *binding_;
1230 CYBindings(CYBinding *binding, CYBindings *next = NULL) :
1231 CYNext<CYBindings>(next),
1236 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1238 CYArgument *Argument(CYContext &context);
1239 CYFunctionParameter *Parameter(CYContext &context);
1241 virtual void Output(CYOutput &out) const;
1242 virtual void Output(CYOutput &out, CYFlags flags) const;
1248 CYBindings *bindings_;
1250 CYVar(CYBindings *bindings) :
1257 virtual CYForInitializer *Replace(CYContext &context);
1258 virtual void Output(CYOutput &out, CYFlags flags) const;
1265 CYBindings *bindings_;
1267 CYLexical(bool constant, CYBindings *bindings) :
1268 constant_(constant),
1275 virtual CYForInitializer *Replace(CYContext &context);
1276 virtual void Output(CYOutput &out, CYFlags flags) const;
1280 CYList<CYBindings> bindings_;
1281 CYList<CYStatement> statements_;
1283 operator bool() const {
1284 return statements_ != NULL;
1292 CYPropertyName *name_;
1294 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1295 CYNext<CYProperty>(next),
1300 virtual bool Update() const;
1302 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1303 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1305 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1307 virtual void Replace(CYContext &context) = 0;
1308 virtual void Output(CYOutput &out) const;
1311 struct CYPropertyValue :
1314 CYExpression *value_;
1316 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1317 CYProperty(name, next),
1322 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1323 virtual void Replace(CYContext &context);
1324 virtual void Output(CYOutput &out) const;
1330 CYForInitializer *initializer_;
1331 CYExpression *test_;
1332 CYExpression *increment_;
1335 CYFor(CYForInitializer *initializer, CYExpression *test, CYExpression *increment, CYStatement *code) :
1336 initializer_(initializer),
1338 increment_(increment),
1345 virtual CYStatement *Replace(CYContext &context);
1346 virtual void Output(CYOutput &out, CYFlags flags) const;
1352 CYForInInitializer *initializer_;
1353 CYExpression *iterable_;
1356 CYForIn(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1357 initializer_(initializer),
1358 iterable_(iterable),
1365 virtual CYStatement *Replace(CYContext &context);
1366 virtual void Output(CYOutput &out, CYFlags flags) const;
1369 struct CYForInitialized :
1372 CYBinding *binding_;
1373 CYExpression *iterable_;
1376 CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) :
1378 iterable_(iterable),
1385 virtual CYStatement *Replace(CYContext &context);
1386 virtual void Output(CYOutput &out, CYFlags flags) const;
1392 CYForInInitializer *initializer_;
1393 CYExpression *iterable_;
1396 CYForOf(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1397 initializer_(initializer),
1398 iterable_(iterable),
1405 virtual CYStatement *Replace(CYContext &context);
1406 virtual void Output(CYOutput &out, CYFlags flags) const;
1412 CYProperty *properties_;
1414 CYObject(CYProperty *properties = NULL) :
1415 properties_(properties)
1419 virtual CYTarget *Replace(CYContext &context);
1420 void Output(CYOutput &out, CYFlags flags) const;
1426 CYExpression *object_;
1427 CYExpression *property_;
1429 CYMember(CYExpression *object, CYExpression *property) :
1435 void SetLeft(CYExpression *object) {
1440 struct CYDirectMember :
1443 CYDirectMember(CYExpression *object, CYExpression *property) :
1444 CYMember(object, property)
1450 virtual CYTarget *Replace(CYContext &context);
1451 virtual void Output(CYOutput &out, CYFlags flags) const;
1454 struct CYIndirectMember :
1457 CYIndirectMember(CYExpression *object, CYExpression *property) :
1458 CYMember(object, property)
1464 virtual CYTarget *Replace(CYContext &context);
1465 virtual void Output(CYOutput &out, CYFlags flags) const;
1468 struct CYResolveMember :
1471 CYResolveMember(CYExpression *object, CYExpression *property) :
1472 CYMember(object, property)
1478 virtual CYTarget *Replace(CYContext &context);
1479 virtual void Output(CYOutput &out, CYFlags flags) const;
1488 CYExpression *constructor_;
1489 CYArgument *arguments_;
1491 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1492 constructor_(constructor),
1493 arguments_(arguments)
1497 virtual int Precedence() const {
1498 return arguments_ == NULL ? 2 : 1;
1502 virtual CYTarget *Replace(CYContext &context);
1503 virtual void Output(CYOutput &out, CYFlags flags) const;
1505 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1513 CYArgument *arguments_;
1515 CYApply(CYArgument *arguments = NULL) :
1516 arguments_(arguments)
1522 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1528 CYExpression *function_;
1530 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1536 virtual void Output(CYOutput &out, CYFlags flags) const;
1537 virtual CYTarget *Replace(CYContext &context);
1543 CYEval(CYArgument *arguments) :
1548 virtual void Output(CYOutput &out, CYFlags flags) const;
1549 virtual CYTarget *Replace(CYContext &context);
1554 struct CYRubyBlock :
1557 CYExpression *call_;
1560 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1568 virtual CYTarget *Replace(CYContext &context);
1569 virtual void Output(CYOutput &out, CYFlags flags) const;
1571 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1577 CYExpression *test_;
1579 CYStatement *false_;
1581 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1590 virtual CYStatement *Replace(CYContext &context);
1591 virtual void Output(CYOutput &out, CYFlags flags) const;
1593 virtual CYStatement *Return();
1599 CYExpression *test_;
1602 CYDoWhile(CYExpression *test, CYStatement *code) :
1610 virtual CYStatement *Replace(CYContext &context);
1611 virtual void Output(CYOutput &out, CYFlags flags) const;
1617 CYExpression *test_;
1620 CYWhile(CYExpression *test, CYStatement *code) :
1628 virtual CYStatement *Replace(CYContext &context);
1629 virtual void Output(CYOutput &out, CYFlags flags) const;
1633 CYFunctionParameter *parameters_;
1636 CYNonLocal *nonlocal_;
1639 CYIdentifier *super_;
1641 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1642 parameters_(parameters),
1650 void Replace(CYContext &context);
1651 void Output(CYOutput &out) const;
1654 struct CYFunctionExpression :
1658 CYIdentifier *name_;
1660 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1661 CYFunction(parameters, code),
1668 CYTarget *Replace(CYContext &context) override;
1669 virtual void Output(CYOutput &out, CYFlags flags) const;
1676 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1677 CYFunction(parameters, code)
1683 CYExpression *Replace(CYContext &context) override;
1684 virtual void Output(CYOutput &out, CYFlags flags) const;
1691 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1692 CYFunction(parameters, code)
1698 CYTarget *Replace(CYContext &context) override;
1699 virtual void Output(CYOutput &out, CYFlags flags) const;
1702 struct CYFunctionStatement :
1706 CYIdentifier *name_;
1708 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1709 CYFunction(parameters, code),
1716 CYStatement *Replace(CYContext &context) override;
1717 virtual void Output(CYOutput &out, CYFlags flags) const;
1720 struct CYPropertyMethod;
1726 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1727 CYFunction(parameters, code),
1728 CYProperty(name, next)
1732 virtual CYFunctionExpression *Constructor();
1734 using CYProperty::Replace;
1735 virtual void Replace(CYContext &context);
1738 struct CYPropertyGetter :
1741 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1742 CYMethod(name, NULL, code, next)
1746 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1747 virtual void Output(CYOutput &out) const;
1750 struct CYPropertySetter :
1753 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1754 CYMethod(name, parameters, code, next)
1758 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1759 virtual void Output(CYOutput &out) const;
1762 struct CYPropertyMethod :
1765 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1766 CYMethod(name, parameters, code, next)
1770 bool Update() const override;
1772 virtual CYFunctionExpression *Constructor();
1774 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1775 virtual void Output(CYOutput &out) const;
1778 struct CYClassTail :
1781 CYExpression *extends_;
1783 CYFunctionExpression *constructor_;
1784 CYList<CYProperty> instance_;
1785 CYList<CYProperty> static_;
1787 CYClassTail(CYExpression *extends) :
1793 void Output(CYOutput &out) const;
1796 struct CYClassExpression :
1799 CYIdentifier *name_;
1802 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1810 CYTarget *Replace(CYContext &context) override;
1811 virtual void Output(CYOutput &out, CYFlags flags) const;
1814 struct CYClassStatement :
1817 CYIdentifier *name_;
1820 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1828 CYStatement *Replace(CYContext &context) override;
1829 virtual void Output(CYOutput &out, CYFlags flags) const;
1832 struct CYSuperCall :
1835 CYArgument *arguments_;
1837 CYSuperCall(CYArgument *arguments) :
1838 arguments_(arguments)
1844 CYTarget *Replace(CYContext &context) override;
1845 virtual void Output(CYOutput &out, CYFlags flags) const;
1848 struct CYSuperAccess :
1851 CYExpression *property_;
1853 CYSuperAccess(CYExpression *property) :
1860 CYTarget *Replace(CYContext &context) override;
1861 virtual void Output(CYOutput &out, CYFlags flags) const;
1867 CYExpression *expression_;
1869 CYExpress(CYExpression *expression) :
1870 expression_(expression)
1872 if (expression_ == NULL)
1878 CYForInitializer *Replace(CYContext &context) override;
1879 virtual void Output(CYOutput &out, CYFlags flags) const;
1881 virtual CYStatement *Return();
1887 CYIdentifier *label_;
1889 CYContinue(CYIdentifier *label) :
1896 CYStatement *Replace(CYContext &context) override;
1897 virtual void Output(CYOutput &out, CYFlags flags) const;
1903 CYIdentifier *label_;
1905 CYBreak(CYIdentifier *label) :
1912 CYStatement *Replace(CYContext &context) override;
1913 virtual void Output(CYOutput &out, CYFlags flags) const;
1919 CYExpression *value_;
1921 CYReturn(CYExpression *value) :
1928 CYStatement *Replace(CYContext &context) override;
1929 virtual void Output(CYOutput &out, CYFlags flags) const;
1932 struct CYYieldGenerator :
1935 CYExpression *value_;
1937 CYYieldGenerator(CYExpression *value) :
1944 CYExpression *Replace(CYContext &context) override;
1945 virtual void Output(CYOutput &out, CYFlags flags) const;
1948 struct CYYieldValue :
1951 CYExpression *value_;
1953 CYYieldValue(CYExpression *value) :
1960 virtual CYExpression *Replace(CYContext &context);
1961 virtual void Output(CYOutput &out, CYFlags flags) const;
1969 virtual CYForInitializer *Replace(CYContext &context);
1970 virtual void Output(CYOutput &out, CYFlags flags) const;
1978 CYFinally(CYStatement *code) :
1983 void Replace(CYContext &context);
1984 virtual void Output(CYOutput &out) const;
1987 struct CYTypeSpecifier :
1990 virtual CYTarget *Replace(CYContext &context) = 0;
1993 struct CYTypeError :
1999 virtual CYTarget *Replace(CYContext &context);
2000 virtual void Output(CYOutput &out) const;
2003 enum CYTypeSigning {
2009 struct CYTypeCharacter :
2012 CYTypeSigning signing_;
2014 CYTypeCharacter(CYTypeSigning signing) :
2019 virtual CYTarget *Replace(CYContext &context);
2020 virtual void Output(CYOutput &out) const;
2023 struct CYTypeIntegral :
2026 CYTypeSigning signing_;
2029 CYTypeIntegral(CYTypeSigning signing, int length = 1) :
2035 CYTypeIntegral *Long() {
2036 if (length_ != 1 && length_ != 2)
2042 CYTypeIntegral *Short() {
2049 CYTypeIntegral *Signed() {
2050 if (signing_ != CYTypeNeutral)
2052 signing_ = CYTypeSigned;
2056 CYTypeIntegral *Unsigned() {
2057 if (signing_ != CYTypeNeutral)
2059 signing_ = CYTypeUnsigned;
2063 virtual CYTarget *Replace(CYContext &context);
2064 virtual void Output(CYOutput &out) const;
2073 virtual CYTarget *Replace(CYContext &context);
2074 virtual void Output(CYOutput &out) const;
2077 struct CYTypeReference :
2080 CYIdentifier *name_;
2082 CYTypeReference(CYIdentifier *name) :
2087 virtual CYTarget *Replace(CYContext &context);
2088 virtual void Output(CYOutput &out) const;
2091 struct CYTypeVariable :
2094 CYIdentifier *name_;
2096 CYTypeVariable(CYIdentifier *name) :
2101 CYTypeVariable(const char *name) :
2102 name_(new($pool) CYIdentifier(name))
2106 virtual CYTarget *Replace(CYContext &context);
2107 virtual void Output(CYOutput &out) const;
2110 struct CYTypeFunctionWith;
2112 struct CYTypeModifier :
2113 CYNext<CYTypeModifier>
2115 CYTypeModifier(CYTypeModifier *next) :
2116 CYNext<CYTypeModifier>(next)
2120 virtual int Precedence() const = 0;
2122 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2123 CYTarget *Replace(CYContext &context, CYTarget *type);
2125 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2126 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2128 virtual CYTypeFunctionWith *Function() { return NULL; }
2131 struct CYTypeArrayOf :
2134 CYExpression *size_;
2136 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2137 CYTypeModifier(next),
2144 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2145 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2148 struct CYTypeConstant :
2151 CYTypeConstant(CYTypeModifier *next = NULL) :
2152 CYTypeModifier(next)
2158 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2159 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2162 struct CYTypePointerTo :
2165 CYTypePointerTo(CYTypeModifier *next = NULL) :
2166 CYTypeModifier(next)
2172 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2173 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2176 struct CYTypeVolatile :
2179 CYTypeVolatile(CYTypeModifier *next = NULL) :
2180 CYTypeModifier(next)
2186 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2187 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2190 struct CYTypedIdentifier :
2191 CYNext<CYTypedIdentifier>,
2194 CYLocation location_;
2195 CYIdentifier *identifier_;
2196 CYTypeSpecifier *specifier_;
2197 CYTypeModifier *modifier_;
2199 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2200 location_(location),
2201 identifier_(identifier),
2207 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2209 specifier_(specifier),
2214 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2215 CYSetLast(modifier_) = modifier;
2219 virtual CYTarget *Replace(CYContext &context);
2220 virtual void Output(CYOutput &out) const;
2222 CYTypeFunctionWith *Function();
2225 struct CYEncodedType :
2228 CYTypedIdentifier *typed_;
2230 CYEncodedType(CYTypedIdentifier *typed) :
2237 virtual CYTarget *Replace(CYContext &context);
2238 virtual void Output(CYOutput &out, CYFlags flags) const;
2241 struct CYTypedParameter :
2242 CYNext<CYTypedParameter>,
2245 CYTypedIdentifier *typed_;
2247 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2248 CYNext<CYTypedParameter>(next),
2253 CYArgument *Argument(CYContext &context);
2254 CYFunctionParameter *Parameters(CYContext &context);
2255 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2257 virtual void Output(CYOutput &out) const;
2260 struct CYTypedFormal {
2262 CYTypedParameter *parameters_;
2264 CYTypedFormal(bool variadic) :
2265 variadic_(variadic),
2274 CYTypedIdentifier *typed_;
2275 CYTypedParameter *parameters_;
2278 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2280 parameters_(parameters),
2287 virtual CYTarget *Replace(CYContext &context);
2288 virtual void Output(CYOutput &out, CYFlags flags) const;
2297 CYModule(CYWord *part, CYModule *next = NULL) :
2298 CYNext<CYModule>(next),
2303 CYString *Replace(CYContext &context, const char *separator) const;
2304 void Output(CYOutput &out) const;
2312 CYImport(CYModule *module) :
2319 virtual CYStatement *Replace(CYContext &context);
2320 virtual void Output(CYOutput &out, CYFlags flags) const;
2323 struct CYImportSpecifier :
2324 CYNext<CYImportSpecifier>
2327 CYIdentifier *binding_;
2329 CYImportSpecifier(CYWord *name, CYIdentifier *binding) :
2335 CYStatement *Replace(CYContext &context, CYIdentifier *module);
2338 struct CYImportDeclaration :
2341 CYImportSpecifier *specifiers_;
2344 CYImportDeclaration(CYImportSpecifier *specifiers, CYString *module) :
2345 specifiers_(specifiers),
2352 virtual CYStatement *Replace(CYContext &context);
2353 virtual void Output(CYOutput &out, CYFlags flags) const;
2360 CYTypedIdentifier *typed_;
2362 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2370 virtual CYStatement *Replace(CYContext &context);
2371 virtual void Output(CYOutput &out, CYFlags flags) const;
2374 struct CYTypeExpression :
2377 CYTypedIdentifier *typed_;
2379 CYTypeExpression(CYTypedIdentifier *typed) :
2386 virtual CYTarget *Replace(CYContext &context);
2387 virtual void Output(CYOutput &out, CYFlags flags) const;
2390 struct CYTypeDefinition :
2393 CYTypedIdentifier *typed_;
2395 CYTypeDefinition(CYTypedIdentifier *typed) :
2402 virtual CYStatement *Replace(CYContext &context);
2403 virtual void Output(CYOutput &out, CYFlags flags) const;
2406 struct CYTypeBlockWith :
2409 CYTypedParameter *parameters_;
2411 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2412 CYTypeModifier(next),
2413 parameters_(parameters)
2419 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2420 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2423 struct CYTypeFunctionWith :
2427 CYTypedParameter *parameters_;
2429 CYTypeFunctionWith(bool variadic, CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2430 CYTypeModifier(next),
2431 variadic_(variadic),
2432 parameters_(parameters)
2438 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2439 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2441 virtual CYTypeFunctionWith *Function() { return this; }
2444 struct CYTypeStructField :
2445 CYNext<CYTypeStructField>
2447 CYTypedIdentifier *typed_;
2449 CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) :
2450 CYNext<CYTypeStructField>(next),
2456 struct CYStructTail :
2459 CYTypeStructField *fields_;
2461 CYStructTail(CYTypeStructField *fields) :
2466 CYTarget *Replace(CYContext &context);
2467 virtual void Output(CYOutput &out) const;
2470 struct CYTypeStruct :
2473 CYIdentifier *name_;
2474 CYStructTail *tail_;
2476 CYTypeStruct(CYIdentifier *name, CYStructTail *tail) :
2482 virtual CYTarget *Replace(CYContext &context);
2483 virtual void Output(CYOutput &out) const;
2486 struct CYStructDefinition :
2489 CYIdentifier *name_;
2490 CYStructTail *tail_;
2492 CYStructDefinition(CYIdentifier *name, CYStructTail *tail) :
2500 virtual CYStatement *Replace(CYContext &context);
2501 virtual void Output(CYOutput &out, CYFlags flags) const;
2510 CYIdentifier *name_;
2513 Catch(CYIdentifier *name, CYStatement *code) :
2519 void Replace(CYContext &context);
2520 virtual void Output(CYOutput &out) const;
2528 CYFinally *finally_;
2530 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2539 virtual CYStatement *Replace(CYContext &context);
2540 virtual void Output(CYOutput &out, CYFlags flags) const;
2546 CYExpression *value_;
2548 Throw(CYExpression *value = NULL) :
2555 virtual CYStatement *Replace(CYContext &context);
2556 virtual void Output(CYOutput &out, CYFlags flags) const;
2564 CYExpression *scope_;
2567 CYWith(CYExpression *scope, CYStatement *code) :
2575 virtual CYStatement *Replace(CYContext &context);
2576 virtual void Output(CYOutput &out, CYFlags flags) const;
2582 CYExpression *value_;
2585 CYSwitch(CYExpression *value, CYClause *clauses) :
2593 virtual CYStatement *Replace(CYContext &context);
2594 virtual void Output(CYOutput &out, CYFlags flags) const;
2606 virtual CYStatement *Replace(CYContext &context);
2607 virtual void Output(CYOutput &out, CYFlags flags) const;
2610 struct CYCondition :
2613 CYExpression *test_;
2614 CYExpression *true_;
2615 CYExpression *false_;
2617 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2626 virtual CYExpression *Replace(CYContext &context);
2627 virtual void Output(CYOutput &out, CYFlags flags) const;
2630 struct CYAddressOf :
2633 CYAddressOf(CYExpression *rhs) :
2638 virtual const char *Operator() const {
2644 virtual CYExpression *Replace(CYContext &context);
2652 CYIndirect(CYExpression *rhs) :
2657 // XXX: this should be checked
2660 virtual CYTarget *Replace(CYContext &context);
2661 virtual void Output(CYOutput &out, CYFlags flags) const;
2665 virtual CYExpression *Replace(CYContext &context);
2667 #define CYPostfix_(op, name, args...) \
2668 struct CY ## name : \
2671 CY ## name(CYExpression *lhs) : \
2676 virtual const char *Operator() const { \
2681 #define CYPrefix_(alphabetic, op, name, args...) \
2682 struct CY ## name : \
2685 CY ## name(CYExpression *rhs) : \
2690 CYAlphabetic(alphabetic) \
2692 virtual const char *Operator() const { \
2697 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2698 struct CY ## name : \
2701 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2706 CYAlphabetic(alphabetic) \
2707 CYPrecedence(precedence) \
2709 virtual const char *Operator() const { \
2714 #define CYAssignment_(op, name, args...) \
2715 struct CY ## name ## Assign : \
2718 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2719 CYAssignment(lhs, rhs) \
2723 virtual const char *Operator() const { \
2728 CYPostfix_("++", PostIncrement)
2729 CYPostfix_("--", PostDecrement)
2731 CYPrefix_(true, "delete", Delete)
2732 CYPrefix_(true, "void", Void)
2733 CYPrefix_(true, "typeof", TypeOf)
2734 CYPrefix_(false, "++", PreIncrement)
2735 CYPrefix_(false, "--", PreDecrement)
2736 CYPrefix_(false, "+", Affirm)
2737 CYPrefix_(false, "-", Negate)
2738 CYPrefix_(false, "~", BitwiseNot)
2739 CYPrefix_(false, "!", LogicalNot)
2741 CYInfix_(false, 5, "*", Multiply, CYReplace)
2742 CYInfix_(false, 5, "/", Divide)
2743 CYInfix_(false, 5, "%", Modulus)
2744 CYInfix_(false, 6, "+", Add, CYReplace)
2745 CYInfix_(false, 6, "-", Subtract)
2746 CYInfix_(false, 7, "<<", ShiftLeft)
2747 CYInfix_(false, 7, ">>", ShiftRightSigned)
2748 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2749 CYInfix_(false, 8, "<", Less)
2750 CYInfix_(false, 8, ">", Greater)
2751 CYInfix_(false, 8, "<=", LessOrEqual)
2752 CYInfix_(false, 8, ">=", GreaterOrEqual)
2753 CYInfix_(true, 8, "instanceof", InstanceOf)
2754 CYInfix_(true, 8, "in", In)
2755 CYInfix_(false, 9, "==", Equal)
2756 CYInfix_(false, 9, "!=", NotEqual)
2757 CYInfix_(false, 9, "===", Identical)
2758 CYInfix_(false, 9, "!==", NotIdentical)
2759 CYInfix_(false, 10, "&", BitwiseAnd)
2760 CYInfix_(false, 11, "^", BitwiseXOr)
2761 CYInfix_(false, 12, "|", BitwiseOr)
2762 CYInfix_(false, 13, "&&", LogicalAnd)
2763 CYInfix_(false, 14, "||", LogicalOr)
2765 CYAssignment_("=", )
2766 CYAssignment_("*=", Multiply)
2767 CYAssignment_("/=", Divide)
2768 CYAssignment_("%=", Modulus)
2769 CYAssignment_("+=", Add)
2770 CYAssignment_("-=", Subtract)
2771 CYAssignment_("<<=", ShiftLeft)
2772 CYAssignment_(">>=", ShiftRightSigned)
2773 CYAssignment_(">>>=", ShiftRightUnsigned)
2774 CYAssignment_("&=", BitwiseAnd)
2775 CYAssignment_("^=", BitwiseXOr)
2776 CYAssignment_("|=", BitwiseOr)
2778 #endif/*CYCRIPT_PARSER_HPP*/