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
35 #include "Location.hpp"
36 #include "Options.hpp"
37 #include "Pooling.hpp"
42 virtual void Output(struct CYOutput &out) const = 0;
63 CYOutput(std::streambuf &out, CYOptions &options) :
74 void Check(char value);
77 _finline void operator ()(char value) {
78 _assert(out_.sputc(value) != EOF);
86 _finline void operator ()(const char *data, std::streamsize size) {
87 _assert(out_.sputn(data, size) == size);
89 position_.columns(size);
92 _finline void operator ()(const char *data) {
93 return operator ()(data, strlen(data));
96 CYOutput &operator <<(char rhs);
97 CYOutput &operator <<(const char *rhs);
99 _finline CYOutput &operator <<(const CYThing *rhs) {
105 _finline CYOutput &operator <<(const CYThing &rhs) {
114 struct CYPropertyName {
115 virtual bool Computed() const {
119 virtual bool Constructor() const {
123 virtual CYExpression *PropertyName(CYContext &context) = 0;
124 virtual void PropertyName(CYOutput &out) const = 0;
135 CYNoBrace = (1 << 0),
136 CYNoFunction = (1 << 1),
137 CYNoClass = (1 << 2),
140 CYNoRightHand = (1 << 5),
141 CYNoDangle = (1 << 6),
142 CYNoInteger = (1 << 7),
143 CYNoBFC = (CYNoBrace | CYNoFunction | CYNoClass),
146 _finline CYFlags operator ~(CYFlags rhs) {
147 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
150 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
151 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
154 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
155 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
158 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
159 return lhs = lhs | rhs;
162 _finline CYFlags CYLeft(CYFlags flags) {
163 return flags & ~(CYNoDangle | CYNoInteger);
166 _finline CYFlags CYRight(CYFlags flags) {
167 return flags & ~CYNoBFC;
170 _finline CYFlags CYCenter(CYFlags flags) {
171 return CYLeft(CYRight(flags));
180 #define CYCompact(type) \
181 virtual CYCompactType Compact() const { \
182 return CYCompact ## type; \
189 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
190 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
191 virtual void Output(CYOutput &out) const;
193 virtual CYStatement *Replace(CYContext &context) = 0;
195 virtual CYCompactType Compact() const = 0;
196 virtual CYStatement *Return();
199 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
202 typedef CYList<CYStatement> CYStatements;
210 CYWord(const char *word) :
215 void Set(const char *value) {
219 virtual bool Constructor() const {
220 return strcmp(word_, "constructor") == 0;
223 virtual const char *Word() const;
224 virtual void Output(CYOutput &out) const;
226 virtual CYExpression *PropertyName(CYContext &context);
227 virtual void PropertyName(CYOutput &out) const;
230 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
232 return lhs << rhs.Word();
235 struct CYIdentifier :
236 CYNext<CYIdentifier>,
239 CYIdentifier *replace_;
243 CYIdentifier(const char *word) :
251 virtual const char *Word() const;
252 CYIdentifier *Replace(CYContext &context);
259 CYStatement *statement_;
261 CYLabel(CYIdentifier *name, CYStatement *statement) :
263 statement_(statement)
269 virtual CYStatement *Replace(CYContext &context);
270 virtual void Output(CYOutput &out, CYFlags flags) const;
273 struct CYCStringLess :
274 std::binary_function<const char *, const char *, bool>
276 _finline bool operator ()(const char *lhs, const char *rhs) const {
277 return strcmp(lhs, rhs) < 0;
281 struct CYIdentifierValueLess :
282 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
284 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
285 return CYCStringLess()(lhs->Word(), rhs->Word());
289 enum CYIdentifierFlags {
290 CYIdentifierArgument,
291 CYIdentifierVariable,
297 typedef std::set<const char *, CYCStringLess> CYCStringSet;
298 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
299 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
301 struct CYIdentifierUsage {
302 CYIdentifier *identifier_;
306 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
312 CYIdentifierAddressFlagsMap internal_;
313 CYIdentifierValueSet identifiers_;
315 CYScope(bool transparent, CYContext &context);
317 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
318 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
319 void Merge(CYContext &context, CYIdentifier *identifier);
320 void Close(CYContext &context, CYStatement *&statements);
328 CYScript(CYStatement *code) :
333 virtual void Replace(CYContext &context);
334 virtual void Output(CYOutput &out) const;
345 CYIdentifier *super_;
347 CYIdentifierUsageVector rename_;
349 CYNonLocal *nonlocal_;
350 CYNonLocal *nextlocal_;
353 CYContext(CYOptions &options) :
364 void ReplaceAll(CYStatement *&statement) {
365 if (statement == NULL)
367 CYStatement *next(statement->next_);
372 if (statement == NULL)
375 statement->SetNext(next);
378 template <typename Type_>
379 void Replace(Type_ *&value) {
380 for (;;) if (value == NULL)
383 Type_ *replace(value->Replace(*this));
384 if (replace != value)
390 void NonLocal(CYStatement *&statements);
391 CYIdentifier *Unique();
395 CYIdentifier *identifier_;
402 CYIdentifier *Target(CYContext &context) {
403 if (identifier_ == NULL)
404 identifier_ = context.Unique();
412 CYIdentifier *identifier_;
419 CYIdentifier *Identifier(CYContext &context) {
421 return next_->Identifier(context);
422 if (identifier_ == NULL)
423 identifier_ = context.Unique();
433 CYBlock(CYStatement *code) :
440 virtual CYStatement *Replace(CYContext &context);
442 virtual void Output(CYOutput &out, CYFlags flags) const;
444 virtual CYStatement *Return();
447 struct CYForInitializer {
448 virtual CYExpression *Replace(CYContext &context) = 0;
449 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
452 struct CYForInInitializer {
453 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
454 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
456 virtual CYExpression *Replace(CYContext &context) = 0;
457 virtual CYAssignment *Assignment(CYContext &context) = 0;
459 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
462 struct CYFunctionParameter;
467 struct CYExpression :
472 virtual int Precedence() const = 0;
474 virtual bool RightHand() const {
478 virtual void ForIn(CYOutput &out, CYFlags flags) const;
479 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
481 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
483 virtual void Output(CYOutput &out) const;
484 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
485 void Output(CYOutput &out, int precedence, CYFlags flags) const;
487 virtual CYExpression *Replace(CYContext &context) = 0;
488 virtual CYAssignment *Assignment(CYContext &context);
490 virtual CYExpression *Primitive(CYContext &context) {
494 virtual CYFunctionParameter *Parameter() const;
496 virtual CYNumber *Number(CYContext &context) {
500 virtual CYString *String(CYContext &context) {
504 virtual const char *Word() const {
509 #define CYAlphabetic(value) \
510 virtual bool Alphabetic() const { \
514 #define CYPrecedence(value) \
515 static const int Precedence_ = value; \
516 virtual int Precedence() const { \
517 return Precedence_; \
520 #define CYRightHand(value) \
521 virtual bool RightHand() const { \
528 CYExpression *expression_;
531 CYCompound(CYExpression *expression, CYExpression *next) :
532 expression_(expression),
535 _assert(expression_ != NULL);
536 _assert(next != NULL);
541 virtual CYExpression *Replace(CYContext &context);
542 void Output(CYOutput &out, CYFlags flags) const;
544 virtual CYFunctionParameter *Parameter() const;
547 struct CYParenthetical :
550 CYExpression *expression_;
552 CYParenthetical(CYExpression *expression) :
553 expression_(expression)
559 virtual CYExpression *Replace(CYContext &context);
560 void Output(CYOutput &out, CYFlags flags) const;
563 struct CYDeclaration;
565 struct CYFunctionParameter :
566 CYNext<CYFunctionParameter>,
569 CYForInInitializer *initialiser_;
571 CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
572 CYNext<CYFunctionParameter>(next),
573 initialiser_(initialiser)
577 void Replace(CYContext &context, CYStatement *&statements);
578 void Output(CYOutput &out) const;
581 struct CYComprehension :
582 CYNext<CYComprehension>,
585 CYComprehension(CYComprehension *next = NULL) :
586 CYNext<CYComprehension>(next)
590 CYComprehension *Modify(CYComprehension *next) {
595 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
596 CYFunctionParameter *Parameters(CYContext &context) const;
597 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
598 virtual void Output(CYOutput &out) const = 0;
601 struct CYForInComprehension :
604 CYDeclaration *declaration_;
607 CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
608 CYComprehension(next),
609 declaration_(declaration),
614 virtual CYFunctionParameter *Parameter(CYContext &context) const;
615 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
616 virtual void Output(CYOutput &out) const;
619 struct CYForOfComprehension :
622 CYDeclaration *declaration_;
625 CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
626 CYComprehension(next),
627 declaration_(declaration),
632 virtual CYFunctionParameter *Parameter(CYContext &context) const;
633 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
634 virtual void Output(CYOutput &out) const;
637 struct CYIfComprehension :
642 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
643 CYComprehension(next),
648 virtual CYFunctionParameter *Parameter(CYContext &context) const;
649 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
650 virtual void Output(CYOutput &out) const;
653 struct CYArrayComprehension :
656 CYExpression *expression_;
657 CYComprehension *comprehensions_;
659 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
660 expression_(expression),
661 comprehensions_(comprehensions)
667 virtual CYExpression *Replace(CYContext &context);
668 virtual void Output(CYOutput &out, CYFlags flags) const;
677 virtual CYExpression *Primitive(CYContext &context) {
685 virtual CYExpression *Replace(CYContext &context);
699 CYRange(uint64_t lo, uint64_t hi) :
704 bool operator [](uint8_t value) const {
705 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
708 void operator()(uint8_t value) {
711 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
715 extern CYRange DigitRange_;
716 extern CYRange WordStartRange_;
717 extern CYRange WordEndRange_;
732 CYString(const char *value) :
738 CYString(const char *value, size_t size) :
744 CYString(const CYWord *word) :
745 value_(word->Word()),
746 size_(strlen(value_))
750 const char *Value() const {
754 virtual const char *Word() const;
756 virtual CYNumber *Number(CYContext &context);
757 virtual CYString *String(CYContext &context);
759 CYString *Concat(CYContext &out, CYString *rhs) const;
760 virtual void Output(CYOutput &out, CYFlags flags) const;
762 virtual CYExpression *PropertyName(CYContext &context);
763 virtual void PropertyName(CYOutput &out) const;
766 struct CYElementValue;
771 CYExpression *expression_;
774 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
775 CYNext<CYSpan>(next),
776 expression_(expression),
781 CYElementValue *Replace(CYContext &context);
790 CYTemplate(CYString *string, CYSpan *spans) :
799 virtual CYExpression *Replace(CYContext &context);
800 virtual void Output(CYOutput &out, CYFlags flags) const;
809 CYNumber(double value) :
814 double Value() const {
818 virtual CYNumber *Number(CYContext &context);
819 virtual CYString *String(CYContext &context);
821 virtual void Output(CYOutput &out, CYFlags flags) const;
823 virtual CYExpression *PropertyName(CYContext &context);
824 virtual void PropertyName(CYOutput &out) const;
830 CYExpression *expression_;
832 CYComputed(CYExpression *expression) :
833 expression_(expression)
837 virtual bool Computed() const {
841 virtual CYExpression *PropertyName(CYContext &context);
842 virtual void PropertyName(CYOutput &out) const;
851 CYRegEx(const char *value, size_t size) :
857 const char *Value() const {
861 virtual void Output(CYOutput &out, CYFlags flags) const;
867 virtual CYNumber *Number(CYContext &context);
868 virtual CYString *String(CYContext &context);
870 virtual void Output(CYOutput &out, CYFlags flags) const;
876 virtual CYExpression *Replace(CYContext &context);
877 virtual void Output(CYOutput &out, CYFlags flags) const;
883 virtual bool Value() const = 0;
884 virtual void Output(CYOutput &out, CYFlags flags) const;
890 virtual bool Value() const {
894 virtual CYNumber *Number(CYContext &context);
895 virtual CYString *String(CYContext &context);
901 virtual bool Value() const {
905 virtual CYNumber *Number(CYContext &context);
906 virtual CYString *String(CYContext &context);
914 CYVariable(CYIdentifier *name) :
919 CYVariable(const char *name) :
920 name_(new($pool) CYIdentifier(name))
927 virtual CYExpression *Replace(CYContext &context);
928 virtual void Output(CYOutput &out, CYFlags flags) const;
930 virtual CYFunctionParameter *Parameter() const;
938 CYPrefix(CYExpression *rhs) :
943 virtual bool Alphabetic() const = 0;
944 virtual const char *Operator() const = 0;
948 virtual CYExpression *Replace(CYContext &context);
949 virtual void Output(CYOutput &out, CYFlags flags) const;
958 CYInfix(CYExpression *lhs, CYExpression *rhs) :
964 void SetLeft(CYExpression *lhs) {
968 virtual bool Alphabetic() const = 0;
969 virtual const char *Operator() const = 0;
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
980 CYPostfix(CYExpression *lhs) :
985 virtual const char *Operator() const = 0;
989 virtual CYExpression *Replace(CYContext &context);
990 virtual void Output(CYOutput &out, CYFlags flags) const;
993 struct CYAssignment :
999 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1005 void SetLeft(CYExpression *lhs) {
1009 virtual const char *Operator() const = 0;
1013 virtual CYExpression *Replace(CYContext &context);
1014 virtual void Output(CYOutput &out, CYFlags flags) const;
1022 CYExpression *value_;
1024 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1025 CYNext<CYArgument>(next),
1031 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1032 CYNext<CYArgument>(next),
1038 CYArgument *Replace(CYContext &context);
1039 void Output(CYOutput &out) const;
1046 CYExpression *case_;
1049 CYClause(CYExpression *_case, CYStatement *code) :
1055 void Replace(CYContext &context);
1056 virtual void Output(CYOutput &out) const;
1062 virtual bool Elision() const = 0;
1064 virtual void Replace(CYContext &context) = 0;
1067 struct CYElementValue :
1071 CYExpression *value_;
1073 CYElementValue(CYExpression *value, CYElement *next) :
1074 CYNext<CYElement>(next),
1079 virtual bool Elision() const {
1080 return value_ == NULL;
1083 virtual void Replace(CYContext &context);
1084 virtual void Output(CYOutput &out) const;
1087 struct CYElementSpread :
1090 CYExpression *value_;
1092 CYElementSpread(CYExpression *value) :
1097 virtual bool Elision() const {
1101 virtual void Replace(CYContext &context);
1102 virtual void Output(CYOutput &out) const;
1108 CYElement *elements_;
1110 CYArray(CYElement *elements = NULL) :
1115 virtual CYExpression *Replace(CYContext &context);
1116 virtual void Output(CYOutput &out, CYFlags flags) const;
1119 struct CYDeclaration :
1122 CYIdentifier *identifier_;
1123 CYExpression *initialiser_;
1125 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1126 identifier_(identifier),
1127 initialiser_(initialiser)
1131 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1132 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1134 virtual CYExpression *Replace(CYContext &context);
1136 virtual CYAssignment *Assignment(CYContext &context);
1137 CYVariable *Variable(CYContext &context);
1139 virtual void Output(CYOutput &out, CYFlags flags) const;
1142 struct CYDeclarations :
1143 CYNext<CYDeclarations>,
1146 CYDeclaration *declaration_;
1148 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1149 CYNext<CYDeclarations>(next),
1150 declaration_(declaration)
1154 void Replace(CYContext &context);
1156 CYExpression *Expression(CYContext &context);
1157 CYArgument *Argument(CYContext &context);
1158 CYFunctionParameter *Parameter(CYContext &context);
1160 virtual void Output(CYOutput &out) const;
1161 virtual void Output(CYOutput &out, CYFlags flags) const;
1164 struct CYForDeclarations :
1167 CYDeclarations *declarations_;
1169 CYForDeclarations(CYDeclarations *declarations) :
1170 declarations_(declarations)
1174 virtual CYExpression *Replace(CYContext &context);
1175 virtual void Output(CYOutput &out, CYFlags flags) const;
1181 CYDeclarations *declarations_;
1183 CYVar(CYDeclarations *declarations) :
1184 declarations_(declarations)
1190 virtual CYStatement *Replace(CYContext &context);
1191 virtual void Output(CYOutput &out, CYFlags flags) const;
1197 CYDeclarations *declarations_;
1199 CYLet(CYDeclarations *declarations) :
1200 declarations_(declarations)
1206 virtual CYStatement *Replace(CYContext &context);
1207 virtual void Output(CYOutput &out, CYFlags flags) const;
1211 CYList<CYDeclarations> declarations_;
1212 CYList<CYStatement> statements_;
1214 operator bool() const {
1215 return statements_ != NULL;
1223 CYPropertyName *name_;
1225 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1226 CYNext<CYProperty>(next),
1231 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1232 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect);
1234 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) = 0;
1236 virtual void Replace(CYContext &context) = 0;
1237 virtual void Output(CYOutput &out) const;
1240 struct CYPropertyValue :
1243 CYExpression *value_;
1245 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1246 CYProperty(name, next),
1251 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1252 virtual void Replace(CYContext &context);
1253 virtual void Output(CYOutput &out) const;
1259 CYForInitializer *initialiser_;
1260 CYExpression *test_;
1261 CYExpression *increment_;
1264 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1265 initialiser_(initialiser),
1267 increment_(increment),
1274 virtual CYStatement *Replace(CYContext &context);
1275 virtual void Output(CYOutput &out, CYFlags flags) const;
1281 CYForInInitializer *initialiser_;
1285 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1286 initialiser_(initialiser),
1294 virtual CYStatement *Replace(CYContext &context);
1295 virtual void Output(CYOutput &out, CYFlags flags) const;
1301 CYForInInitializer *initialiser_;
1305 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1306 initialiser_(initialiser),
1314 virtual CYStatement *Replace(CYContext &context);
1315 virtual void Output(CYOutput &out, CYFlags flags) const;
1321 CYProperty *properties_;
1323 CYObject(CYProperty *properties = NULL) :
1324 properties_(properties)
1328 virtual CYExpression *Replace(CYContext &context);
1329 void Output(CYOutput &out, CYFlags flags) const;
1335 CYExpression *object_;
1336 CYExpression *property_;
1338 CYMember(CYExpression *object, CYExpression *property) :
1344 void SetLeft(CYExpression *object) {
1349 struct CYDirectMember :
1352 CYDirectMember(CYExpression *object, CYExpression *property) :
1353 CYMember(object, property)
1360 virtual CYExpression *Replace(CYContext &context);
1361 virtual void Output(CYOutput &out, CYFlags flags) const;
1364 struct CYIndirectMember :
1367 CYIndirectMember(CYExpression *object, CYExpression *property) :
1368 CYMember(object, property)
1375 virtual CYExpression *Replace(CYContext &context);
1376 virtual void Output(CYOutput &out, CYFlags flags) const;
1385 CYExpression *constructor_;
1386 CYArgument *arguments_;
1388 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1389 constructor_(constructor),
1390 arguments_(arguments)
1394 virtual int Precedence() const {
1395 return arguments_ == NULL ? 2 : 1;
1400 virtual CYExpression *Replace(CYContext &context);
1401 virtual void Output(CYOutput &out, CYFlags flags) const;
1403 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1411 CYExpression *function_;
1412 CYArgument *arguments_;
1414 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1415 function_(function),
1416 arguments_(arguments)
1423 virtual CYExpression *Replace(CYContext &context);
1424 virtual void Output(CYOutput &out, CYFlags flags) const;
1426 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1431 struct CYRubyBlock :
1434 CYExpression *call_;
1437 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1446 virtual CYExpression *Replace(CYContext &context);
1447 virtual void Output(CYOutput &out, CYFlags flags) const;
1449 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1455 CYExpression *test_;
1457 CYStatement *false_;
1459 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1468 virtual CYStatement *Replace(CYContext &context);
1469 virtual void Output(CYOutput &out, CYFlags flags) const;
1471 virtual CYStatement *Return();
1477 CYExpression *test_;
1480 CYDoWhile(CYExpression *test, CYStatement *code) :
1488 virtual CYStatement *Replace(CYContext &context);
1489 virtual void Output(CYOutput &out, CYFlags flags) const;
1495 CYExpression *test_;
1498 CYWhile(CYExpression *test, CYStatement *code) :
1506 virtual CYStatement *Replace(CYContext &context);
1507 virtual void Output(CYOutput &out, CYFlags flags) const;
1511 CYFunctionParameter *parameters_;
1514 CYNonLocal *nonlocal_;
1517 CYIdentifier *super_;
1519 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1520 parameters_(parameters),
1528 void Replace(CYContext &context);
1529 void Output(CYOutput &out) const;
1532 struct CYFunctionExpression :
1536 CYIdentifier *name_;
1538 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1539 CYFunction(parameters, code),
1547 virtual CYExpression *Replace(CYContext &context);
1548 virtual void Output(CYOutput &out, CYFlags flags) const;
1555 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1556 CYFunction(parameters, code)
1563 virtual CYExpression *Replace(CYContext &context);
1564 virtual void Output(CYOutput &out, CYFlags flags) const;
1571 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1572 CYFunction(parameters, code)
1579 virtual CYExpression *Replace(CYContext &context);
1580 virtual void Output(CYOutput &out, CYFlags flags) const;
1583 struct CYFunctionStatement :
1587 CYIdentifier *name_;
1589 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1590 CYFunction(parameters, code),
1597 virtual CYStatement *Replace(CYContext &context);
1598 virtual void Output(CYOutput &out, CYFlags flags) const;
1601 struct CYPropertyMethod;
1607 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1608 CYFunction(parameters, code),
1609 CYProperty(name, next)
1613 virtual CYFunctionExpression *Constructor();
1615 using CYProperty::Replace;
1616 virtual void Replace(CYContext &context);
1619 struct CYPropertyGetter :
1622 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1623 CYMethod(name, NULL, code, next)
1627 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1628 virtual void Output(CYOutput &out) const;
1631 struct CYPropertySetter :
1634 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1635 CYMethod(name, parameters, code, next)
1639 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1640 virtual void Output(CYOutput &out) const;
1643 struct CYPropertyMethod :
1646 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1647 CYMethod(name, parameters, code, next)
1651 virtual CYFunctionExpression *Constructor();
1653 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect);
1654 virtual void Output(CYOutput &out) const;
1657 struct CYClassTail :
1660 CYExpression *extends_;
1662 CYFunctionExpression *constructor_;
1663 CYList<CYProperty> instance_;
1664 CYList<CYProperty> static_;
1666 CYClassTail(CYExpression *extends) :
1672 void Output(CYOutput &out) const;
1675 struct CYClassExpression :
1678 CYIdentifier *name_;
1681 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1690 virtual CYExpression *Replace(CYContext &context);
1691 virtual void Output(CYOutput &out, CYFlags flags) const;
1694 struct CYClassStatement :
1697 CYIdentifier *name_;
1700 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1708 virtual CYStatement *Replace(CYContext &context);
1709 virtual void Output(CYOutput &out, CYFlags flags) const;
1712 struct CYSuperCall :
1715 CYArgument *arguments_;
1717 CYSuperCall(CYArgument *arguments) :
1718 arguments_(arguments)
1725 virtual CYExpression *Replace(CYContext &context);
1726 virtual void Output(CYOutput &out, CYFlags flags) const;
1729 struct CYSuperAccess :
1732 CYExpression *property_;
1734 CYSuperAccess(CYExpression *property) :
1742 virtual CYExpression *Replace(CYContext &context);
1743 virtual void Output(CYOutput &out, CYFlags flags) const;
1749 CYExpression *expression_;
1751 CYExpress(CYExpression *expression) :
1752 expression_(expression)
1754 if (expression_ == NULL)
1760 virtual CYStatement *Replace(CYContext &context);
1761 virtual void Output(CYOutput &out, CYFlags flags) const;
1763 virtual CYStatement *Return();
1769 CYIdentifier *label_;
1771 CYContinue(CYIdentifier *label) :
1778 virtual CYStatement *Replace(CYContext &context);
1779 virtual void Output(CYOutput &out, CYFlags flags) const;
1785 CYIdentifier *label_;
1787 CYBreak(CYIdentifier *label) :
1794 virtual CYStatement *Replace(CYContext &context);
1795 virtual void Output(CYOutput &out, CYFlags flags) const;
1801 CYExpression *value_;
1803 CYReturn(CYExpression *value) :
1810 virtual CYStatement *Replace(CYContext &context);
1811 virtual void Output(CYOutput &out, CYFlags flags) const;
1814 struct CYYieldGenerator :
1817 CYExpression *value_;
1819 CYYieldGenerator(CYExpression *value) :
1826 virtual CYExpression *Replace(CYContext &context);
1827 virtual void Output(CYOutput &out, CYFlags flags) const;
1830 struct CYYieldValue :
1833 CYExpression *value_;
1835 CYYieldValue(CYExpression *value) :
1842 virtual CYExpression *Replace(CYContext &context);
1843 virtual void Output(CYOutput &out, CYFlags flags) const;
1851 virtual CYStatement *Replace(CYContext &context);
1852 virtual void Output(CYOutput &out, CYFlags flags) const;
1860 CYFinally(CYStatement *code) :
1865 void Replace(CYContext &context);
1866 virtual void Output(CYOutput &out) const;
1869 struct CYTypeSpecifier :
1872 virtual CYExpression *Replace(CYContext &context) = 0;
1875 struct CYTypeError :
1881 virtual CYExpression *Replace(CYContext &context);
1882 virtual void Output(CYOutput &out) const;
1891 virtual CYExpression *Replace(CYContext &context);
1892 virtual void Output(CYOutput &out) const;
1895 struct CYTypeVariable :
1898 CYIdentifier *name_;
1900 CYTypeVariable(CYIdentifier *name) :
1905 CYTypeVariable(const char *name) :
1906 name_(new($pool) CYIdentifier(name))
1910 virtual CYExpression *Replace(CYContext &context);
1911 virtual void Output(CYOutput &out) const;
1914 struct CYTypeUnsigned :
1917 CYTypeSpecifier *specifier_;
1919 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1920 specifier_(specifier)
1924 virtual CYExpression *Replace(CYContext &context);
1925 virtual void Output(CYOutput &out) const;
1928 struct CYTypeSigned :
1931 CYTypeSpecifier *specifier_;
1933 CYTypeSigned(CYTypeSpecifier *specifier) :
1934 specifier_(specifier)
1938 virtual CYExpression *Replace(CYContext &context);
1939 virtual void Output(CYOutput &out) const;
1945 CYTypeSpecifier *specifier_;
1947 CYTypeLong(CYTypeSpecifier *specifier) :
1948 specifier_(specifier)
1952 virtual CYExpression *Replace(CYContext &context);
1953 virtual void Output(CYOutput &out) const;
1956 struct CYTypeShort :
1959 CYTypeSpecifier *specifier_;
1961 CYTypeShort(CYTypeSpecifier *specifier) :
1962 specifier_(specifier)
1966 virtual CYExpression *Replace(CYContext &context);
1967 virtual void Output(CYOutput &out) const;
1970 struct CYTypeFunctionWith;
1972 struct CYTypeModifier :
1973 CYNext<CYTypeModifier>
1975 CYTypeModifier(CYTypeModifier *next) :
1976 CYNext<CYTypeModifier>(next)
1980 virtual int Precedence() const = 0;
1982 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1983 CYExpression *Replace(CYContext &context, CYExpression *type);
1985 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1986 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1988 virtual CYTypeFunctionWith *Function() { return NULL; }
1991 struct CYTypeArrayOf :
1994 CYExpression *size_;
1996 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1997 CYTypeModifier(next),
2004 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2005 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2008 struct CYTypeConstant :
2011 CYTypeConstant(CYTypeModifier *next = NULL) :
2012 CYTypeModifier(next)
2018 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2019 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2022 struct CYTypePointerTo :
2025 CYTypePointerTo(CYTypeModifier *next = NULL) :
2026 CYTypeModifier(next)
2032 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2033 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2036 struct CYTypeVolatile :
2039 CYTypeVolatile(CYTypeModifier *next = NULL) :
2040 CYTypeModifier(next)
2046 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2047 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2050 struct CYTypedIdentifier :
2051 CYNext<CYTypedIdentifier>,
2054 CYLocation location_;
2055 CYIdentifier *identifier_;
2056 CYTypeSpecifier *specifier_;
2057 CYTypeModifier *modifier_;
2059 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2060 location_(location),
2061 identifier_(identifier),
2067 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2069 specifier_(specifier),
2074 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2075 CYSetLast(modifier_) = modifier;
2079 virtual CYExpression *Replace(CYContext &context);
2080 virtual void Output(CYOutput &out) const;
2082 CYTypeFunctionWith *Function();
2085 struct CYEncodedType :
2088 CYTypedIdentifier *typed_;
2090 CYEncodedType(CYTypedIdentifier *typed) :
2097 virtual CYExpression *Replace(CYContext &context);
2098 virtual void Output(CYOutput &out, CYFlags flags) const;
2101 struct CYTypedParameter :
2102 CYNext<CYTypedParameter>,
2105 CYTypedIdentifier *typed_;
2107 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2108 CYNext<CYTypedParameter>(next),
2113 CYArgument *Argument(CYContext &context);
2114 CYFunctionParameter *Parameters(CYContext &context);
2115 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2117 virtual void Output(CYOutput &out) const;
2123 CYTypedIdentifier *typed_;
2124 CYTypedParameter *parameters_;
2127 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2129 parameters_(parameters),
2136 virtual CYExpression *Replace(CYContext &context);
2137 virtual void Output(CYOutput &out, CYFlags flags) const;
2146 CYModule(CYWord *part, CYModule *next = NULL) :
2147 CYNext<CYModule>(next),
2152 CYString *Replace(CYContext &context, const char *separator) const;
2153 void Output(CYOutput &out) const;
2161 CYImport(CYModule *module) :
2168 virtual CYStatement *Replace(CYContext &context);
2169 virtual void Output(CYOutput &out, CYFlags flags) const;
2176 CYTypedIdentifier *typed_;
2178 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2186 virtual CYStatement *Replace(CYContext &context);
2187 virtual void Output(CYOutput &out, CYFlags flags) const;
2190 struct CYTypeDefinition :
2193 CYTypedIdentifier *typed_;
2195 CYTypeDefinition(CYTypedIdentifier *typed) :
2202 virtual CYStatement *Replace(CYContext &context);
2203 virtual void Output(CYOutput &out, CYFlags flags) const;
2206 struct CYTypeBlockWith :
2209 CYTypedParameter *parameters_;
2211 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2212 CYTypeModifier(next),
2213 parameters_(parameters)
2219 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2220 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2223 struct CYTypeFunctionWith :
2226 CYTypedParameter *parameters_;
2228 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2229 CYTypeModifier(next),
2230 parameters_(parameters)
2236 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2237 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2239 virtual CYTypeFunctionWith *Function() { return this; }
2248 CYIdentifier *name_;
2251 Catch(CYIdentifier *name, CYStatement *code) :
2257 void Replace(CYContext &context);
2258 virtual void Output(CYOutput &out) const;
2266 CYFinally *finally_;
2268 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2277 virtual CYStatement *Replace(CYContext &context);
2278 virtual void Output(CYOutput &out, CYFlags flags) const;
2284 CYExpression *value_;
2286 Throw(CYExpression *value = NULL) :
2293 virtual CYStatement *Replace(CYContext &context);
2294 virtual void Output(CYOutput &out, CYFlags flags) const;
2302 CYExpression *scope_;
2305 CYWith(CYExpression *scope, CYStatement *code) :
2313 virtual CYStatement *Replace(CYContext &context);
2314 virtual void Output(CYOutput &out, CYFlags flags) const;
2320 CYExpression *value_;
2323 CYSwitch(CYExpression *value, CYClause *clauses) :
2331 virtual CYStatement *Replace(CYContext &context);
2332 virtual void Output(CYOutput &out, CYFlags flags) const;
2344 virtual CYStatement *Replace(CYContext &context);
2345 virtual void Output(CYOutput &out, CYFlags flags) const;
2348 struct CYCondition :
2351 CYExpression *test_;
2352 CYExpression *true_;
2353 CYExpression *false_;
2355 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2364 virtual CYExpression *Replace(CYContext &context);
2365 virtual void Output(CYOutput &out, CYFlags flags) const;
2368 struct CYAddressOf :
2371 CYAddressOf(CYExpression *rhs) :
2376 virtual const char *Operator() const {
2382 virtual CYExpression *Replace(CYContext &context);
2388 CYIndirect(CYExpression *rhs) :
2393 virtual const char *Operator() const {
2399 virtual CYExpression *Replace(CYContext &context);
2403 virtual CYExpression *Replace(CYContext &context);
2405 #define CYPostfix_(op, name, args...) \
2406 struct CY ## name : \
2409 CY ## name(CYExpression *lhs) : \
2414 virtual const char *Operator() const { \
2419 #define CYPrefix_(alphabetic, op, name, args...) \
2420 struct CY ## name : \
2423 CY ## name(CYExpression *rhs) : \
2428 CYAlphabetic(alphabetic) \
2430 virtual const char *Operator() const { \
2435 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2436 struct CY ## name : \
2439 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2444 CYAlphabetic(alphabetic) \
2445 CYPrecedence(precedence) \
2447 virtual const char *Operator() const { \
2452 #define CYAssignment_(op, name, args...) \
2453 struct CY ## name ## Assign : \
2456 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2457 CYAssignment(lhs, rhs) \
2461 virtual const char *Operator() const { \
2466 CYPostfix_("++", PostIncrement)
2467 CYPostfix_("--", PostDecrement)
2469 CYPrefix_(true, "delete", Delete)
2470 CYPrefix_(true, "void", Void)
2471 CYPrefix_(true, "typeof", TypeOf)
2472 CYPrefix_(false, "++", PreIncrement)
2473 CYPrefix_(false, "--", PreDecrement)
2474 CYPrefix_(false, "+", Affirm)
2475 CYPrefix_(false, "-", Negate)
2476 CYPrefix_(false, "~", BitwiseNot)
2477 CYPrefix_(false, "!", LogicalNot)
2479 CYInfix_(false, 5, "*", Multiply, CYReplace)
2480 CYInfix_(false, 5, "/", Divide)
2481 CYInfix_(false, 5, "%", Modulus)
2482 CYInfix_(false, 6, "+", Add, CYReplace)
2483 CYInfix_(false, 6, "-", Subtract)
2484 CYInfix_(false, 7, "<<", ShiftLeft)
2485 CYInfix_(false, 7, ">>", ShiftRightSigned)
2486 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2487 CYInfix_(false, 8, "<", Less)
2488 CYInfix_(false, 8, ">", Greater)
2489 CYInfix_(false, 8, "<=", LessOrEqual)
2490 CYInfix_(false, 8, ">=", GreaterOrEqual)
2491 CYInfix_(true, 8, "instanceof", InstanceOf)
2492 CYInfix_(true, 8, "in", In)
2493 CYInfix_(false, 9, "==", Equal)
2494 CYInfix_(false, 9, "!=", NotEqual)
2495 CYInfix_(false, 9, "===", Identical)
2496 CYInfix_(false, 9, "!==", NotIdentical)
2497 CYInfix_(false, 10, "&", BitwiseAnd)
2498 CYInfix_(false, 11, "^", BitwiseXOr)
2499 CYInfix_(false, 12, "|", BitwiseOr)
2500 CYInfix_(false, 13, "&&", LogicalAnd)
2501 CYInfix_(false, 14, "||", LogicalOr)
2503 CYAssignment_("=", )
2504 CYAssignment_("*=", Multiply)
2505 CYAssignment_("/=", Divide)
2506 CYAssignment_("%=", Modulus)
2507 CYAssignment_("+=", Add)
2508 CYAssignment_("-=", Subtract)
2509 CYAssignment_("<<=", ShiftLeft)
2510 CYAssignment_(">>=", ShiftRightSigned)
2511 CYAssignment_(">>>=", ShiftRightUnsigned)
2512 CYAssignment_("&=", BitwiseAnd)
2513 CYAssignment_("^=", BitwiseXOr)
2514 CYAssignment_("|=", BitwiseOr)
2516 #endif/*CYCRIPT_PARSER_HPP*/