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;
1094 virtual bool Elision() const = 0;
1096 virtual void Replace(CYContext &context) = 0;
1099 struct CYElementValue :
1103 CYExpression *value_;
1105 CYElementValue(CYExpression *value, CYElement *next = NULL) :
1106 CYNext<CYElement>(next),
1111 virtual bool Elision() const {
1112 return value_ == NULL;
1115 virtual void Replace(CYContext &context);
1116 virtual void Output(CYOutput &out) const;
1119 struct CYElementSpread :
1122 CYExpression *value_;
1124 CYElementSpread(CYExpression *value) :
1129 virtual bool Elision() const {
1133 virtual void Replace(CYContext &context);
1134 virtual void Output(CYOutput &out) const;
1140 CYElement *elements_;
1142 CYArray(CYElement *elements = NULL) :
1147 virtual CYTarget *Replace(CYContext &context);
1148 virtual void Output(CYOutput &out, CYFlags flags) const;
1152 CYIdentifier *identifier_;
1153 CYExpression *initializer_;
1155 CYBinding(CYIdentifier *identifier, CYExpression *initializer = NULL) :
1156 identifier_(identifier),
1157 initializer_(initializer)
1161 CYTarget *Target(CYContext &context);
1163 virtual CYAssignment *Replace(CYContext &context, CYIdentifierKind kind);
1164 virtual void Output(CYOutput &out, CYFlags flags) const;
1167 struct CYForLexical :
1171 CYBinding *binding_;
1173 CYForLexical(bool constant, CYBinding *binding) :
1174 constant_(constant),
1179 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1181 virtual CYTarget *Replace(CYContext &context);
1182 virtual void Output(CYOutput &out, CYFlags flags) const;
1185 struct CYForVariable :
1188 CYBinding *binding_;
1190 CYForVariable(CYBinding *binding) :
1195 virtual CYStatement *Initialize(CYContext &context, CYExpression *value);
1197 virtual CYTarget *Replace(CYContext &context);
1198 virtual void Output(CYOutput &out, CYFlags flags) const;
1205 CYBinding *binding_;
1207 CYBindings(CYBinding *binding, CYBindings *next = NULL) :
1208 CYNext<CYBindings>(next),
1213 CYExpression *Replace(CYContext &context, CYIdentifierKind kind);
1215 CYArgument *Argument(CYContext &context);
1216 CYFunctionParameter *Parameter(CYContext &context);
1218 virtual void Output(CYOutput &out) const;
1219 virtual void Output(CYOutput &out, CYFlags flags) const;
1225 CYBindings *bindings_;
1227 CYVar(CYBindings *bindings) :
1234 virtual CYForInitializer *Replace(CYContext &context);
1235 virtual void Output(CYOutput &out, CYFlags flags) const;
1242 CYBindings *bindings_;
1244 CYLexical(bool constant, CYBindings *bindings) :
1245 constant_(constant),
1252 virtual CYForInitializer *Replace(CYContext &context);
1253 virtual void Output(CYOutput &out, CYFlags flags) const;
1257 CYList<CYBindings> bindings_;
1258 CYList<CYStatement> statements_;
1260 operator bool() const {
1261 return statements_ != NULL;
1269 CYPropertyName *name_;
1271 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1272 CYNext<CYProperty>(next),
1277 virtual bool Update() const;
1279 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1280 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1282 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1284 virtual void Replace(CYContext &context) = 0;
1285 virtual void Output(CYOutput &out) const;
1288 struct CYPropertyValue :
1291 CYExpression *value_;
1293 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1294 CYProperty(name, next),
1299 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1300 virtual void Replace(CYContext &context);
1301 virtual void Output(CYOutput &out) const;
1307 CYForInitializer *initializer_;
1308 CYExpression *test_;
1309 CYExpression *increment_;
1312 CYFor(CYForInitializer *initializer, CYExpression *test, CYExpression *increment, CYStatement *code) :
1313 initializer_(initializer),
1315 increment_(increment),
1322 virtual CYStatement *Replace(CYContext &context);
1323 virtual void Output(CYOutput &out, CYFlags flags) const;
1329 CYForInInitializer *initializer_;
1330 CYExpression *iterable_;
1333 CYForIn(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1334 initializer_(initializer),
1335 iterable_(iterable),
1342 virtual CYStatement *Replace(CYContext &context);
1343 virtual void Output(CYOutput &out, CYFlags flags) const;
1346 struct CYForInitialized :
1349 CYBinding *binding_;
1350 CYExpression *iterable_;
1353 CYForInitialized(CYBinding *binding, CYExpression *iterable, CYStatement *code) :
1355 iterable_(iterable),
1362 virtual CYStatement *Replace(CYContext &context);
1363 virtual void Output(CYOutput &out, CYFlags flags) const;
1369 CYForInInitializer *initializer_;
1370 CYExpression *iterable_;
1373 CYForOf(CYForInInitializer *initializer, CYExpression *iterable, CYStatement *code) :
1374 initializer_(initializer),
1375 iterable_(iterable),
1382 virtual CYStatement *Replace(CYContext &context);
1383 virtual void Output(CYOutput &out, CYFlags flags) const;
1389 CYProperty *properties_;
1391 CYObject(CYProperty *properties = NULL) :
1392 properties_(properties)
1396 virtual CYTarget *Replace(CYContext &context);
1397 void Output(CYOutput &out, CYFlags flags) const;
1403 CYExpression *object_;
1404 CYExpression *property_;
1406 CYMember(CYExpression *object, CYExpression *property) :
1412 void SetLeft(CYExpression *object) {
1417 struct CYDirectMember :
1420 CYDirectMember(CYExpression *object, CYExpression *property) :
1421 CYMember(object, property)
1427 virtual CYTarget *Replace(CYContext &context);
1428 virtual void Output(CYOutput &out, CYFlags flags) const;
1431 struct CYIndirectMember :
1434 CYIndirectMember(CYExpression *object, CYExpression *property) :
1435 CYMember(object, property)
1441 virtual CYTarget *Replace(CYContext &context);
1442 virtual void Output(CYOutput &out, CYFlags flags) const;
1451 CYExpression *constructor_;
1452 CYArgument *arguments_;
1454 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1455 constructor_(constructor),
1456 arguments_(arguments)
1460 virtual int Precedence() const {
1461 return arguments_ == NULL ? 2 : 1;
1465 virtual CYTarget *Replace(CYContext &context);
1466 virtual void Output(CYOutput &out, CYFlags flags) const;
1468 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1476 CYArgument *arguments_;
1478 CYApply(CYArgument *arguments = NULL) :
1479 arguments_(arguments)
1485 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1491 CYExpression *function_;
1493 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1499 virtual void Output(CYOutput &out, CYFlags flags) const;
1500 virtual CYTarget *Replace(CYContext &context);
1506 CYEval(CYArgument *arguments) :
1511 virtual void Output(CYOutput &out, CYFlags flags) const;
1512 virtual CYTarget *Replace(CYContext &context);
1517 struct CYRubyBlock :
1520 CYExpression *call_;
1523 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1531 virtual CYTarget *Replace(CYContext &context);
1532 virtual void Output(CYOutput &out, CYFlags flags) const;
1534 virtual CYTarget *AddArgument(CYContext &context, CYExpression *value);
1540 CYExpression *test_;
1542 CYStatement *false_;
1544 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1553 virtual CYStatement *Replace(CYContext &context);
1554 virtual void Output(CYOutput &out, CYFlags flags) const;
1556 virtual CYStatement *Return();
1562 CYExpression *test_;
1565 CYDoWhile(CYExpression *test, CYStatement *code) :
1573 virtual CYStatement *Replace(CYContext &context);
1574 virtual void Output(CYOutput &out, CYFlags flags) const;
1580 CYExpression *test_;
1583 CYWhile(CYExpression *test, CYStatement *code) :
1591 virtual CYStatement *Replace(CYContext &context);
1592 virtual void Output(CYOutput &out, CYFlags flags) const;
1596 CYFunctionParameter *parameters_;
1599 CYNonLocal *nonlocal_;
1602 CYIdentifier *super_;
1604 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1605 parameters_(parameters),
1613 void Replace(CYContext &context);
1614 void Output(CYOutput &out) const;
1617 struct CYFunctionExpression :
1621 CYIdentifier *name_;
1623 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1624 CYFunction(parameters, code),
1631 CYTarget *Replace(CYContext &context) override;
1632 virtual void Output(CYOutput &out, CYFlags flags) const;
1639 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1640 CYFunction(parameters, code)
1646 CYExpression *Replace(CYContext &context) override;
1647 virtual void Output(CYOutput &out, CYFlags flags) const;
1654 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1655 CYFunction(parameters, code)
1661 CYTarget *Replace(CYContext &context) override;
1662 virtual void Output(CYOutput &out, CYFlags flags) const;
1665 struct CYFunctionStatement :
1669 CYIdentifier *name_;
1671 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1672 CYFunction(parameters, code),
1679 CYStatement *Replace(CYContext &context) override;
1680 virtual void Output(CYOutput &out, CYFlags flags) const;
1683 struct CYPropertyMethod;
1689 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1690 CYFunction(parameters, code),
1691 CYProperty(name, next)
1695 virtual CYFunctionExpression *Constructor();
1697 using CYProperty::Replace;
1698 virtual void Replace(CYContext &context);
1701 struct CYPropertyGetter :
1704 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1705 CYMethod(name, NULL, 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 CYPropertySetter :
1716 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1717 CYMethod(name, parameters, code, next)
1721 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1722 virtual void Output(CYOutput &out) const;
1725 struct CYPropertyMethod :
1728 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1729 CYMethod(name, parameters, code, next)
1733 bool Update() const override;
1735 virtual CYFunctionExpression *Constructor();
1737 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1738 virtual void Output(CYOutput &out) const;
1741 struct CYClassTail :
1744 CYExpression *extends_;
1746 CYFunctionExpression *constructor_;
1747 CYList<CYProperty> instance_;
1748 CYList<CYProperty> static_;
1750 CYClassTail(CYExpression *extends) :
1756 void Output(CYOutput &out) const;
1759 struct CYClassExpression :
1762 CYIdentifier *name_;
1765 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1773 CYTarget *Replace(CYContext &context) override;
1774 virtual void Output(CYOutput &out, CYFlags flags) const;
1777 struct CYClassStatement :
1780 CYIdentifier *name_;
1783 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1791 CYStatement *Replace(CYContext &context) override;
1792 virtual void Output(CYOutput &out, CYFlags flags) const;
1795 struct CYSuperCall :
1798 CYArgument *arguments_;
1800 CYSuperCall(CYArgument *arguments) :
1801 arguments_(arguments)
1807 CYTarget *Replace(CYContext &context) override;
1808 virtual void Output(CYOutput &out, CYFlags flags) const;
1811 struct CYSuperAccess :
1814 CYExpression *property_;
1816 CYSuperAccess(CYExpression *property) :
1823 CYTarget *Replace(CYContext &context) override;
1824 virtual void Output(CYOutput &out, CYFlags flags) const;
1830 CYExpression *expression_;
1832 CYExpress(CYExpression *expression) :
1833 expression_(expression)
1835 if (expression_ == NULL)
1841 CYForInitializer *Replace(CYContext &context) override;
1842 virtual void Output(CYOutput &out, CYFlags flags) const;
1844 virtual CYStatement *Return();
1850 CYIdentifier *label_;
1852 CYContinue(CYIdentifier *label) :
1859 CYStatement *Replace(CYContext &context) override;
1860 virtual void Output(CYOutput &out, CYFlags flags) const;
1866 CYIdentifier *label_;
1868 CYBreak(CYIdentifier *label) :
1875 CYStatement *Replace(CYContext &context) override;
1876 virtual void Output(CYOutput &out, CYFlags flags) const;
1882 CYExpression *value_;
1884 CYReturn(CYExpression *value) :
1891 CYStatement *Replace(CYContext &context) override;
1892 virtual void Output(CYOutput &out, CYFlags flags) const;
1895 struct CYYieldGenerator :
1898 CYExpression *value_;
1900 CYYieldGenerator(CYExpression *value) :
1907 CYExpression *Replace(CYContext &context) override;
1908 virtual void Output(CYOutput &out, CYFlags flags) const;
1911 struct CYYieldValue :
1914 CYExpression *value_;
1916 CYYieldValue(CYExpression *value) :
1923 virtual CYExpression *Replace(CYContext &context);
1924 virtual void Output(CYOutput &out, CYFlags flags) const;
1932 virtual CYForInitializer *Replace(CYContext &context);
1933 virtual void Output(CYOutput &out, CYFlags flags) const;
1941 CYFinally(CYStatement *code) :
1946 void Replace(CYContext &context);
1947 virtual void Output(CYOutput &out) const;
1950 struct CYTypeSpecifier :
1953 virtual CYTarget *Replace(CYContext &context) = 0;
1956 struct CYTypeError :
1962 virtual CYTarget *Replace(CYContext &context);
1963 virtual void Output(CYOutput &out) const;
1972 virtual CYTarget *Replace(CYContext &context);
1973 virtual void Output(CYOutput &out) const;
1976 struct CYTypeReference :
1979 CYIdentifier *name_;
1981 CYTypeReference(CYIdentifier *name) :
1986 virtual CYTarget *Replace(CYContext &context);
1987 virtual void Output(CYOutput &out) const;
1990 struct CYTypeVariable :
1993 CYIdentifier *name_;
1995 CYTypeVariable(CYIdentifier *name) :
2000 CYTypeVariable(const char *name) :
2001 name_(new($pool) CYIdentifier(name))
2005 virtual CYTarget *Replace(CYContext &context);
2006 virtual void Output(CYOutput &out) const;
2009 struct CYTypeUnsigned :
2012 CYTypeSpecifier *specifier_;
2014 CYTypeUnsigned(CYTypeSpecifier *specifier) :
2015 specifier_(specifier)
2019 virtual CYTarget *Replace(CYContext &context);
2020 virtual void Output(CYOutput &out) const;
2023 struct CYTypeSigned :
2026 CYTypeSpecifier *specifier_;
2028 CYTypeSigned(CYTypeSpecifier *specifier) :
2029 specifier_(specifier)
2033 virtual CYTarget *Replace(CYContext &context);
2034 virtual void Output(CYOutput &out) const;
2040 CYTypeSpecifier *specifier_;
2042 CYTypeLong(CYTypeSpecifier *specifier) :
2043 specifier_(specifier)
2047 virtual CYTarget *Replace(CYContext &context);
2048 virtual void Output(CYOutput &out) const;
2051 struct CYTypeShort :
2054 CYTypeSpecifier *specifier_;
2056 CYTypeShort(CYTypeSpecifier *specifier) :
2057 specifier_(specifier)
2061 virtual CYTarget *Replace(CYContext &context);
2062 virtual void Output(CYOutput &out) const;
2065 struct CYTypeFunctionWith;
2067 struct CYTypeModifier :
2068 CYNext<CYTypeModifier>
2070 CYTypeModifier(CYTypeModifier *next) :
2071 CYNext<CYTypeModifier>(next)
2075 virtual int Precedence() const = 0;
2077 virtual CYTarget *Replace_(CYContext &context, CYTarget *type) = 0;
2078 CYTarget *Replace(CYContext &context, CYTarget *type);
2080 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
2081 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
2083 virtual CYTypeFunctionWith *Function() { return NULL; }
2086 struct CYTypeArrayOf :
2089 CYExpression *size_;
2091 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
2092 CYTypeModifier(next),
2099 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2100 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2103 struct CYTypeConstant :
2106 CYTypeConstant(CYTypeModifier *next = NULL) :
2107 CYTypeModifier(next)
2113 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2114 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2117 struct CYTypePointerTo :
2120 CYTypePointerTo(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 CYTypeVolatile :
2134 CYTypeVolatile(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 CYTypedIdentifier :
2146 CYNext<CYTypedIdentifier>,
2149 CYLocation location_;
2150 CYIdentifier *identifier_;
2151 CYTypeSpecifier *specifier_;
2152 CYTypeModifier *modifier_;
2154 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2155 location_(location),
2156 identifier_(identifier),
2162 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2164 specifier_(specifier),
2169 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2170 CYSetLast(modifier_) = modifier;
2174 virtual CYTarget *Replace(CYContext &context);
2175 virtual void Output(CYOutput &out) const;
2177 CYTypeFunctionWith *Function();
2180 struct CYEncodedType :
2183 CYTypedIdentifier *typed_;
2185 CYEncodedType(CYTypedIdentifier *typed) :
2192 virtual CYTarget *Replace(CYContext &context);
2193 virtual void Output(CYOutput &out, CYFlags flags) const;
2196 struct CYTypedParameter :
2197 CYNext<CYTypedParameter>,
2200 CYTypedIdentifier *typed_;
2202 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2203 CYNext<CYTypedParameter>(next),
2208 CYArgument *Argument(CYContext &context);
2209 CYFunctionParameter *Parameters(CYContext &context);
2210 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2212 virtual void Output(CYOutput &out) const;
2218 CYTypedIdentifier *typed_;
2219 CYTypedParameter *parameters_;
2222 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2224 parameters_(parameters),
2231 virtual CYTarget *Replace(CYContext &context);
2232 virtual void Output(CYOutput &out, CYFlags flags) const;
2241 CYModule(CYWord *part, CYModule *next = NULL) :
2242 CYNext<CYModule>(next),
2247 CYString *Replace(CYContext &context, const char *separator) const;
2248 void Output(CYOutput &out) const;
2256 CYImport(CYModule *module) :
2263 virtual CYStatement *Replace(CYContext &context);
2264 virtual void Output(CYOutput &out, CYFlags flags) const;
2267 struct CYImportSpecifier :
2268 CYNext<CYImportSpecifier>
2271 CYIdentifier *binding_;
2273 CYImportSpecifier(CYWord *name, CYIdentifier *binding) :
2279 CYStatement *Replace(CYContext &context, CYIdentifier *module);
2282 struct CYImportDeclaration :
2285 CYImportSpecifier *specifiers_;
2288 CYImportDeclaration(CYImportSpecifier *specifiers, CYString *module) :
2289 specifiers_(specifiers),
2296 virtual CYStatement *Replace(CYContext &context);
2297 virtual void Output(CYOutput &out, CYFlags flags) const;
2304 CYTypedIdentifier *typed_;
2306 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2314 virtual CYStatement *Replace(CYContext &context);
2315 virtual void Output(CYOutput &out, CYFlags flags) const;
2318 struct CYTypeExpression :
2321 CYTypedIdentifier *typed_;
2323 CYTypeExpression(CYTypedIdentifier *typed) :
2330 virtual CYTarget *Replace(CYContext &context);
2331 virtual void Output(CYOutput &out, CYFlags flags) const;
2334 struct CYTypeDefinition :
2337 CYTypedIdentifier *typed_;
2339 CYTypeDefinition(CYTypedIdentifier *typed) :
2346 virtual CYStatement *Replace(CYContext &context);
2347 virtual void Output(CYOutput &out, CYFlags flags) const;
2350 struct CYTypeBlockWith :
2353 CYTypedParameter *parameters_;
2355 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2356 CYTypeModifier(next),
2357 parameters_(parameters)
2363 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2364 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2367 struct CYTypeFunctionWith :
2370 CYTypedParameter *parameters_;
2372 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2373 CYTypeModifier(next),
2374 parameters_(parameters)
2380 virtual CYTarget *Replace_(CYContext &context, CYTarget *type);
2381 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2383 virtual CYTypeFunctionWith *Function() { return this; }
2386 struct CYTypeStructField :
2387 CYNext<CYTypeStructField>
2389 CYTypedIdentifier *typed_;
2391 CYTypeStructField(CYTypedIdentifier *typed, CYTypeStructField *next = NULL) :
2392 CYNext<CYTypeStructField>(next),
2398 struct CYStructTail :
2401 CYTypeStructField *fields_;
2403 CYStructTail(CYTypeStructField *fields) :
2408 CYTarget *Replace(CYContext &context);
2409 virtual void Output(CYOutput &out) const;
2412 struct CYTypeStruct :
2415 CYIdentifier *name_;
2416 CYStructTail *tail_;
2418 CYTypeStruct(CYIdentifier *name, CYStructTail *tail) :
2424 virtual CYTarget *Replace(CYContext &context);
2425 virtual void Output(CYOutput &out) const;
2428 struct CYStructDefinition :
2431 CYIdentifier *name_;
2432 CYStructTail *tail_;
2434 CYStructDefinition(CYIdentifier *name, CYStructTail *tail) :
2442 virtual CYStatement *Replace(CYContext &context);
2443 virtual void Output(CYOutput &out, CYFlags flags) const;
2452 CYIdentifier *name_;
2455 Catch(CYIdentifier *name, CYStatement *code) :
2461 void Replace(CYContext &context);
2462 virtual void Output(CYOutput &out) const;
2470 CYFinally *finally_;
2472 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2481 virtual CYStatement *Replace(CYContext &context);
2482 virtual void Output(CYOutput &out, CYFlags flags) const;
2488 CYExpression *value_;
2490 Throw(CYExpression *value = NULL) :
2497 virtual CYStatement *Replace(CYContext &context);
2498 virtual void Output(CYOutput &out, CYFlags flags) const;
2506 CYExpression *scope_;
2509 CYWith(CYExpression *scope, CYStatement *code) :
2517 virtual CYStatement *Replace(CYContext &context);
2518 virtual void Output(CYOutput &out, CYFlags flags) const;
2524 CYExpression *value_;
2527 CYSwitch(CYExpression *value, CYClause *clauses) :
2535 virtual CYStatement *Replace(CYContext &context);
2536 virtual void Output(CYOutput &out, CYFlags flags) const;
2548 virtual CYStatement *Replace(CYContext &context);
2549 virtual void Output(CYOutput &out, CYFlags flags) const;
2552 struct CYCondition :
2555 CYExpression *test_;
2556 CYExpression *true_;
2557 CYExpression *false_;
2559 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2568 virtual CYExpression *Replace(CYContext &context);
2569 virtual void Output(CYOutput &out, CYFlags flags) const;
2572 struct CYAddressOf :
2575 CYAddressOf(CYExpression *rhs) :
2580 virtual const char *Operator() const {
2586 virtual CYExpression *Replace(CYContext &context);
2594 CYIndirect(CYExpression *rhs) :
2599 // XXX: this should be checked
2602 virtual CYTarget *Replace(CYContext &context);
2603 virtual void Output(CYOutput &out, CYFlags flags) const;
2607 virtual CYExpression *Replace(CYContext &context);
2609 #define CYPostfix_(op, name, args...) \
2610 struct CY ## name : \
2613 CY ## name(CYExpression *lhs) : \
2618 virtual const char *Operator() const { \
2623 #define CYPrefix_(alphabetic, op, name, args...) \
2624 struct CY ## name : \
2627 CY ## name(CYExpression *rhs) : \
2632 CYAlphabetic(alphabetic) \
2634 virtual const char *Operator() const { \
2639 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2640 struct CY ## name : \
2643 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2648 CYAlphabetic(alphabetic) \
2649 CYPrecedence(precedence) \
2651 virtual const char *Operator() const { \
2656 #define CYAssignment_(op, name, args...) \
2657 struct CY ## name ## Assign : \
2660 CY ## name ## Assign(CYTarget *lhs, CYExpression *rhs) : \
2661 CYAssignment(lhs, rhs) \
2665 virtual const char *Operator() const { \
2670 CYPostfix_("++", PostIncrement)
2671 CYPostfix_("--", PostDecrement)
2673 CYPrefix_(true, "delete", Delete)
2674 CYPrefix_(true, "void", Void)
2675 CYPrefix_(true, "typeof", TypeOf)
2676 CYPrefix_(false, "++", PreIncrement)
2677 CYPrefix_(false, "--", PreDecrement)
2678 CYPrefix_(false, "+", Affirm)
2679 CYPrefix_(false, "-", Negate)
2680 CYPrefix_(false, "~", BitwiseNot)
2681 CYPrefix_(false, "!", LogicalNot)
2683 CYInfix_(false, 5, "*", Multiply, CYReplace)
2684 CYInfix_(false, 5, "/", Divide)
2685 CYInfix_(false, 5, "%", Modulus)
2686 CYInfix_(false, 6, "+", Add, CYReplace)
2687 CYInfix_(false, 6, "-", Subtract)
2688 CYInfix_(false, 7, "<<", ShiftLeft)
2689 CYInfix_(false, 7, ">>", ShiftRightSigned)
2690 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2691 CYInfix_(false, 8, "<", Less)
2692 CYInfix_(false, 8, ">", Greater)
2693 CYInfix_(false, 8, "<=", LessOrEqual)
2694 CYInfix_(false, 8, ">=", GreaterOrEqual)
2695 CYInfix_(true, 8, "instanceof", InstanceOf)
2696 CYInfix_(true, 8, "in", In)
2697 CYInfix_(false, 9, "==", Equal)
2698 CYInfix_(false, 9, "!=", NotEqual)
2699 CYInfix_(false, 9, "===", Identical)
2700 CYInfix_(false, 9, "!==", NotIdentical)
2701 CYInfix_(false, 10, "&", BitwiseAnd)
2702 CYInfix_(false, 11, "^", BitwiseXOr)
2703 CYInfix_(false, 12, "|", BitwiseOr)
2704 CYInfix_(false, 13, "&&", LogicalAnd)
2705 CYInfix_(false, 14, "||", LogicalOr)
2707 CYAssignment_("=", )
2708 CYAssignment_("*=", Multiply)
2709 CYAssignment_("/=", Divide)
2710 CYAssignment_("%=", Modulus)
2711 CYAssignment_("+=", Add)
2712 CYAssignment_("-=", Subtract)
2713 CYAssignment_("<<=", ShiftLeft)
2714 CYAssignment_(">>=", ShiftRightSigned)
2715 CYAssignment_(">>>=", ShiftRightUnsigned)
2716 CYAssignment_("&=", BitwiseAnd)
2717 CYAssignment_("^=", BitwiseXOr)
2718 CYAssignment_("|=", BitwiseOr)
2720 #endif/*CYCRIPT_PARSER_HPP*/