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) {
111 struct CYPropertyName {
112 virtual void PropertyName(CYOutput &out) const = 0;
126 CYNoBrace = (1 << 0),
127 CYNoFunction = (1 << 1),
130 CYNoRightHand = (1 << 4),
131 CYNoDangle = (1 << 5),
132 CYNoInteger = (1 << 6),
133 CYNoBF = (CYNoBrace | CYNoFunction),
136 _finline CYFlags operator ~(CYFlags rhs) {
137 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
140 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
141 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
144 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
145 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
148 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
149 return lhs = lhs | rhs;
152 _finline CYFlags CYLeft(CYFlags flags) {
153 return flags & ~(CYNoDangle | CYNoInteger);
156 _finline CYFlags CYRight(CYFlags flags) {
157 return flags & ~CYNoBF;
160 _finline CYFlags CYCenter(CYFlags flags) {
161 return CYLeft(CYRight(flags));
170 #define CYCompact(type) \
171 virtual CYCompactType Compact() const { \
172 return CYCompact ## type; \
179 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
180 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
181 virtual void Output(CYOutput &out) const;
183 virtual CYStatement *Replace(CYContext &context) = 0;
185 virtual CYCompactType Compact() const = 0;
186 virtual CYStatement *Return();
189 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
192 struct CYStatements {
202 operator CYStatement *() const {
206 CYStatements &operator ->*(CYStatement *next) {
208 if (first_ == NULL) {
211 } else for (;; last_ = last_->next_)
212 if (last_->next_ == NULL) {
222 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
223 virtual void ClassName(CYOutput &out, bool object) const = 0;
233 CYWord(const char *word) :
238 void Set(const char *value) {
242 virtual const char *Word() const;
243 virtual void Output(CYOutput &out) const;
245 virtual CYExpression *ClassName(CYContext &context, bool object);
246 virtual void ClassName(CYOutput &out, bool object) const;
247 virtual void PropertyName(CYOutput &out) const;
250 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
252 return lhs << rhs.Word();
255 struct CYIdentifier :
256 CYNext<CYIdentifier>,
259 CYIdentifier *replace_;
263 CYIdentifier(const char *word) :
271 virtual const char *Word() const;
272 CYIdentifier *Replace(CYContext &context);
279 CYStatement *statement_;
281 CYLabel(CYIdentifier *name, CYStatement *statement) :
283 statement_(statement)
289 virtual CYStatement *Replace(CYContext &context);
290 virtual void Output(CYOutput &out, CYFlags flags) const;
293 struct CYCStringLess :
294 std::binary_function<const char *, const char *, bool>
296 _finline bool operator ()(const char *lhs, const char *rhs) const {
297 return strcmp(lhs, rhs) < 0;
301 struct CYIdentifierValueLess :
302 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
304 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
305 return CYCStringLess()(lhs->Word(), rhs->Word());
309 enum CYIdentifierFlags {
310 CYIdentifierArgument,
311 CYIdentifierVariable,
317 typedef std::set<const char *, CYCStringLess> CYCStringSet;
318 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
319 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
321 struct CYIdentifierUsage {
322 CYIdentifier *identifier_;
326 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
332 CYIdentifierAddressFlagsMap internal_;
333 CYIdentifierValueSet identifiers_;
335 CYScope(bool transparent, CYContext &context);
337 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
338 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
339 void Merge(CYContext &context, CYIdentifier *identifier);
340 void Close(CYContext &context, CYStatement *&statements);
348 CYScript(CYStatement *code) :
353 virtual void Replace(CYContext &context);
354 virtual void Output(CYOutput &out) const;
366 CYIdentifierUsageVector rename_;
368 CYNonLocal *nonlocal_;
369 CYNonLocal *nextlocal_;
372 CYContext(CYOptions &options) :
382 void ReplaceAll(CYStatement *&statement) {
383 if (statement == NULL)
385 CYStatement *next(statement->next_);
390 if (statement == NULL)
393 statement->SetNext(next);
396 template <typename Type_>
397 void Replace(Type_ *&value) {
398 for (;;) if (value == NULL)
401 Type_ *replace(value->Replace(*this));
402 if (replace != value)
408 void NonLocal(CYStatement *&statements);
409 CYIdentifier *Unique();
413 CYIdentifier *identifier_;
420 CYIdentifier *Target(CYContext &context) {
421 if (identifier_ == NULL)
422 identifier_ = context.Unique();
430 CYIdentifier *identifier_;
437 CYIdentifier *Identifier(CYContext &context) {
439 return next_->Identifier(context);
440 if (identifier_ == NULL)
441 identifier_ = context.Unique();
451 CYBlock(CYStatement *code) :
458 virtual CYStatement *Replace(CYContext &context);
460 virtual void Output(CYOutput &out, CYFlags flags) const;
462 virtual CYStatement *Return();
465 struct CYForInitializer {
466 virtual CYExpression *Replace(CYContext &context) = 0;
467 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
470 struct CYForInInitializer {
471 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
472 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
474 virtual CYExpression *Replace(CYContext &context) = 0;
475 virtual CYAssignment *Assignment(CYContext &context) = 0;
477 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
480 struct CYFunctionParameter;
485 struct CYExpression :
491 virtual int Precedence() const = 0;
493 virtual bool RightHand() const {
497 virtual void ForIn(CYOutput &out, CYFlags flags) const;
498 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
500 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
502 virtual void Output(CYOutput &out) const;
503 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
504 void Output(CYOutput &out, int precedence, CYFlags flags) const;
506 virtual CYExpression *ClassName(CYContext &context, bool object);
507 virtual void ClassName(CYOutput &out, bool object) const;
509 virtual CYExpression *Replace(CYContext &context) = 0;
510 virtual CYAssignment *Assignment(CYContext &context);
512 virtual CYExpression *Primitive(CYContext &context) {
516 virtual CYFunctionParameter *Parameter() const;
518 virtual CYNumber *Number(CYContext &context) {
522 virtual CYString *String(CYContext &context) {
526 virtual const char *Word() const {
531 #define CYAlphabetic(value) \
532 virtual bool Alphabetic() const { \
536 #define CYPrecedence(value) \
537 static const int Precedence_ = value; \
538 virtual int Precedence() const { \
539 return Precedence_; \
542 #define CYRightHand(value) \
543 virtual bool RightHand() const { \
550 CYExpression *expression_;
553 CYCompound(CYExpression *expression, CYExpression *next) :
554 expression_(expression),
557 _assert(expression_ != NULL);
558 _assert(next != NULL);
563 virtual CYExpression *Replace(CYContext &context);
564 void Output(CYOutput &out, CYFlags flags) const;
566 virtual CYFunctionParameter *Parameter() const;
569 struct CYParenthetical :
572 CYExpression *expression_;
574 CYParenthetical(CYExpression *expression) :
575 expression_(expression)
581 virtual CYExpression *Replace(CYContext &context);
582 void Output(CYOutput &out, CYFlags flags) const;
585 struct CYDeclaration;
587 struct CYFunctionParameter :
588 CYNext<CYFunctionParameter>,
591 CYForInInitializer *initialiser_;
593 CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) :
594 CYNext<CYFunctionParameter>(next),
595 initialiser_(initialiser)
599 void Replace(CYContext &context, CYStatement *&statements);
600 void Output(CYOutput &out) const;
603 struct CYComprehension :
604 CYNext<CYComprehension>,
607 CYComprehension(CYComprehension *next = NULL) :
608 CYNext<CYComprehension>(next)
612 CYComprehension *Modify(CYComprehension *next) {
617 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
618 CYFunctionParameter *Parameters(CYContext &context) const;
619 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
620 virtual void Output(CYOutput &out) const = 0;
623 struct CYForInComprehension :
626 CYDeclaration *declaration_;
629 CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
630 CYComprehension(next),
631 declaration_(declaration),
636 virtual CYFunctionParameter *Parameter(CYContext &context) const;
637 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
638 virtual void Output(CYOutput &out) const;
641 struct CYForOfComprehension :
644 CYDeclaration *declaration_;
647 CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
648 CYComprehension(next),
649 declaration_(declaration),
654 virtual CYFunctionParameter *Parameter(CYContext &context) const;
655 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
656 virtual void Output(CYOutput &out) const;
659 struct CYIfComprehension :
664 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
665 CYComprehension(next),
670 virtual CYFunctionParameter *Parameter(CYContext &context) const;
671 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
672 virtual void Output(CYOutput &out) const;
675 struct CYArrayComprehension :
678 CYExpression *expression_;
679 CYComprehension *comprehensions_;
681 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
682 expression_(expression),
683 comprehensions_(comprehensions)
689 virtual CYExpression *Replace(CYContext &context);
690 virtual void Output(CYOutput &out, CYFlags flags) const;
699 virtual CYExpression *Primitive(CYContext &context) {
707 virtual CYExpression *Replace(CYContext &context);
721 CYRange(uint64_t lo, uint64_t hi) :
726 bool operator [](uint8_t value) const {
727 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
730 void operator()(uint8_t value) {
733 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
737 extern CYRange DigitRange_;
738 extern CYRange WordStartRange_;
739 extern CYRange WordEndRange_;
754 CYString(const char *value) :
760 CYString(const char *value, size_t size) :
766 CYString(const CYWord *word) :
767 value_(word->Word()),
768 size_(strlen(value_))
772 const char *Value() const {
776 virtual const char *Word() const;
778 virtual CYNumber *Number(CYContext &context);
779 virtual CYString *String(CYContext &context);
781 CYString *Concat(CYContext &out, CYString *rhs) const;
782 virtual void Output(CYOutput &out, CYFlags flags) const;
783 virtual void PropertyName(CYOutput &out) const;
786 struct CYElementValue;
791 CYExpression *expression_;
794 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
795 CYNext<CYSpan>(next),
796 expression_(expression),
801 CYElementValue *Replace(CYContext &context);
810 CYTemplate(CYString *string, CYSpan *spans) :
819 virtual CYExpression *Replace(CYContext &context);
820 virtual void Output(CYOutput &out, CYFlags flags) const;
829 CYNumber(double value) :
834 double Value() const {
838 virtual CYNumber *Number(CYContext &context);
839 virtual CYString *String(CYContext &context);
841 virtual void Output(CYOutput &out, CYFlags flags) const;
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;
1123 CYPropertyName *name_;
1124 CYExpression *value_;
1126 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1127 CYNext<CYProperty>(next),
1133 void Replace(CYContext &context);
1134 virtual void Output(CYOutput &out) const;
1137 struct CYDeclaration :
1140 CYIdentifier *identifier_;
1141 CYExpression *initialiser_;
1143 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1144 identifier_(identifier),
1145 initialiser_(initialiser)
1149 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1150 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1152 virtual CYExpression *Replace(CYContext &context);
1154 virtual CYAssignment *Assignment(CYContext &context);
1155 CYVariable *Variable(CYContext &context);
1157 virtual void Output(CYOutput &out, CYFlags flags) const;
1160 struct CYDeclarations :
1161 CYNext<CYDeclarations>,
1164 CYDeclaration *declaration_;
1166 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1167 CYNext<CYDeclarations>(next),
1168 declaration_(declaration)
1172 void Replace(CYContext &context);
1174 CYExpression *Expression(CYContext &context);
1175 CYProperty *Property(CYContext &context);
1176 CYArgument *Argument(CYContext &context);
1177 CYFunctionParameter *Parameter(CYContext &context);
1179 virtual void Output(CYOutput &out) const;
1180 virtual void Output(CYOutput &out, CYFlags flags) const;
1183 struct CYForDeclarations :
1186 CYDeclarations *declarations_;
1188 CYForDeclarations(CYDeclarations *declarations) :
1189 declarations_(declarations)
1193 virtual CYExpression *Replace(CYContext &context);
1194 virtual void Output(CYOutput &out, CYFlags flags) const;
1200 CYDeclarations *declarations_;
1202 CYVar(CYDeclarations *declarations) :
1203 declarations_(declarations)
1209 virtual CYStatement *Replace(CYContext &context);
1210 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 struct CYLetStatement :
1216 CYDeclarations *declarations_;
1219 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1220 declarations_(declarations),
1227 virtual CYStatement *Replace(CYContext &context);
1228 virtual void Output(CYOutput &out, CYFlags flags) const;
1234 CYForInitializer *initialiser_;
1235 CYExpression *test_;
1236 CYExpression *increment_;
1239 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1240 initialiser_(initialiser),
1242 increment_(increment),
1249 virtual CYStatement *Replace(CYContext &context);
1250 virtual void Output(CYOutput &out, CYFlags flags) const;
1256 CYForInInitializer *initialiser_;
1260 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1261 initialiser_(initialiser),
1269 virtual CYStatement *Replace(CYContext &context);
1270 virtual void Output(CYOutput &out, CYFlags flags) const;
1276 CYForInInitializer *initialiser_;
1280 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1281 initialiser_(initialiser),
1289 virtual CYStatement *Replace(CYContext &context);
1290 virtual void Output(CYOutput &out, CYFlags flags) const;
1296 CYProperty *properties_;
1298 CYObject(CYProperty *properties = NULL) :
1299 properties_(properties)
1303 virtual CYExpression *Replace(CYContext &context);
1304 void Output(CYOutput &out, CYFlags flags) const;
1310 CYExpression *object_;
1311 CYExpression *property_;
1313 CYMember(CYExpression *object, CYExpression *property) :
1319 void SetLeft(CYExpression *object) {
1324 struct CYDirectMember :
1327 CYDirectMember(CYExpression *object, CYExpression *property) :
1328 CYMember(object, property)
1335 virtual CYExpression *Replace(CYContext &context);
1336 virtual void Output(CYOutput &out, CYFlags flags) const;
1339 struct CYIndirectMember :
1342 CYIndirectMember(CYExpression *object, CYExpression *property) :
1343 CYMember(object, property)
1350 virtual CYExpression *Replace(CYContext &context);
1351 virtual void Output(CYOutput &out, CYFlags flags) const;
1360 CYExpression *constructor_;
1361 CYArgument *arguments_;
1363 New(CYExpression *constructor, CYArgument *arguments) :
1364 constructor_(constructor),
1365 arguments_(arguments)
1369 virtual int Precedence() const {
1370 return arguments_ == NULL ? 2 : 1;
1375 virtual CYExpression *Replace(CYContext &context);
1376 virtual void Output(CYOutput &out, CYFlags flags) const;
1378 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1386 CYExpression *function_;
1387 CYArgument *arguments_;
1389 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1390 function_(function),
1391 arguments_(arguments)
1398 virtual CYExpression *Replace(CYContext &context);
1399 virtual void Output(CYOutput &out, CYFlags flags) const;
1401 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1406 struct CYRubyBlock :
1409 CYExpression *call_;
1412 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1421 virtual CYExpression *Replace(CYContext &context);
1422 virtual void Output(CYOutput &out, CYFlags flags) const;
1424 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1430 CYExpression *test_;
1432 CYStatement *false_;
1434 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1443 virtual CYStatement *Replace(CYContext &context);
1444 virtual void Output(CYOutput &out, CYFlags flags) const;
1446 virtual CYStatement *Return();
1452 CYExpression *test_;
1455 CYDoWhile(CYExpression *test, CYStatement *code) :
1463 virtual CYStatement *Replace(CYContext &context);
1464 virtual void Output(CYOutput &out, CYFlags flags) const;
1470 CYExpression *test_;
1473 CYWhile(CYExpression *test, CYStatement *code) :
1481 virtual CYStatement *Replace(CYContext &context);
1482 virtual void Output(CYOutput &out, CYFlags flags) const;
1486 CYIdentifier *name_;
1487 CYFunctionParameter *parameters_;
1490 CYNonLocal *nonlocal_;
1494 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1496 parameters_(parameters),
1503 void Inject(CYContext &context);
1504 virtual void Replace_(CYContext &context, bool outer);
1505 virtual void Output(CYOutput &out, CYFlags flags) const;
1508 struct CYFunctionExpression :
1512 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1513 CYFunction(name, parameters, code)
1520 virtual CYExpression *Replace(CYContext &context);
1521 virtual void Output(CYOutput &out, CYFlags flags) const;
1528 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1529 CYFunction(NULL, parameters, code)
1536 virtual CYExpression *Replace(CYContext &context);
1537 virtual void Output(CYOutput &out, CYFlags flags) const;
1544 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1545 CYFunction(NULL, parameters, code)
1549 virtual CYExpression *Replace(CYContext &context);
1550 virtual void Output(CYOutput &out, CYFlags flags) const;
1553 struct CYFunctionStatement :
1557 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1558 CYFunction(name, parameters, code)
1564 virtual CYStatement *Replace(CYContext &context);
1565 virtual void Output(CYOutput &out, CYFlags flags) const;
1571 CYExpression *expression_;
1573 CYExpress(CYExpression *expression) :
1574 expression_(expression)
1576 if (expression_ == NULL)
1582 virtual CYStatement *Replace(CYContext &context);
1583 virtual void Output(CYOutput &out, CYFlags flags) const;
1585 virtual CYStatement *Return();
1591 CYIdentifier *label_;
1593 CYContinue(CYIdentifier *label) :
1600 virtual CYStatement *Replace(CYContext &context);
1601 virtual void Output(CYOutput &out, CYFlags flags) const;
1607 CYIdentifier *label_;
1609 CYBreak(CYIdentifier *label) :
1616 virtual CYStatement *Replace(CYContext &context);
1617 virtual void Output(CYOutput &out, CYFlags flags) const;
1623 CYExpression *value_;
1625 CYReturn(CYExpression *value) :
1632 virtual CYStatement *Replace(CYContext &context);
1633 virtual void Output(CYOutput &out, CYFlags flags) const;
1636 struct CYYieldGenerator :
1639 CYExpression *value_;
1641 CYYieldGenerator(CYExpression *value) :
1648 virtual CYExpression *Replace(CYContext &context);
1649 virtual void Output(CYOutput &out, CYFlags flags) const;
1652 struct CYYieldValue :
1655 CYExpression *value_;
1657 CYYieldValue(CYExpression *value) :
1664 virtual CYExpression *Replace(CYContext &context);
1665 virtual void Output(CYOutput &out, CYFlags flags) const;
1673 virtual CYStatement *Replace(CYContext &context);
1674 virtual void Output(CYOutput &out, CYFlags flags) const;
1682 CYFinally(CYStatement *code) :
1687 void Replace(CYContext &context);
1688 virtual void Output(CYOutput &out) const;
1691 struct CYTypeSpecifier :
1694 virtual CYExpression *Replace(CYContext &context) = 0;
1697 struct CYTypeError :
1703 virtual CYExpression *Replace(CYContext &context);
1704 virtual void Output(CYOutput &out) const;
1713 virtual CYExpression *Replace(CYContext &context);
1714 virtual void Output(CYOutput &out) const;
1717 struct CYTypeVariable :
1720 CYIdentifier *name_;
1722 CYTypeVariable(CYIdentifier *name) :
1727 CYTypeVariable(const char *name) :
1728 name_(new($pool) CYIdentifier(name))
1732 virtual CYExpression *Replace(CYContext &context);
1733 virtual void Output(CYOutput &out) const;
1736 struct CYTypeUnsigned :
1739 CYTypeSpecifier *specifier_;
1741 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1742 specifier_(specifier)
1746 virtual CYExpression *Replace(CYContext &context);
1747 virtual void Output(CYOutput &out) const;
1750 struct CYTypeSigned :
1753 CYTypeSpecifier *specifier_;
1755 CYTypeSigned(CYTypeSpecifier *specifier) :
1756 specifier_(specifier)
1760 virtual CYExpression *Replace(CYContext &context);
1761 virtual void Output(CYOutput &out) const;
1767 CYTypeSpecifier *specifier_;
1769 CYTypeLong(CYTypeSpecifier *specifier) :
1770 specifier_(specifier)
1774 virtual CYExpression *Replace(CYContext &context);
1775 virtual void Output(CYOutput &out) const;
1778 struct CYTypeShort :
1781 CYTypeSpecifier *specifier_;
1783 CYTypeShort(CYTypeSpecifier *specifier) :
1784 specifier_(specifier)
1788 virtual CYExpression *Replace(CYContext &context);
1789 virtual void Output(CYOutput &out) const;
1792 struct CYTypeFunctionWith;
1794 struct CYTypeModifier :
1795 CYNext<CYTypeModifier>
1797 CYTypeModifier(CYTypeModifier *next) :
1798 CYNext<CYTypeModifier>(next)
1802 virtual int Precedence() const = 0;
1804 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1805 CYExpression *Replace(CYContext &context, CYExpression *type);
1807 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1808 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1810 virtual CYTypeFunctionWith *Function() { return NULL; }
1813 struct CYTypeArrayOf :
1816 CYExpression *size_;
1818 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1819 CYTypeModifier(next),
1826 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1827 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1830 struct CYTypeConstant :
1833 CYTypeConstant(CYTypeModifier *next = NULL) :
1834 CYTypeModifier(next)
1840 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1841 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1844 struct CYTypePointerTo :
1847 CYTypePointerTo(CYTypeModifier *next = NULL) :
1848 CYTypeModifier(next)
1854 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1855 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1858 struct CYTypeVolatile :
1861 CYTypeVolatile(CYTypeModifier *next = NULL) :
1862 CYTypeModifier(next)
1868 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1869 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1872 struct CYTypedIdentifier :
1873 CYNext<CYTypedIdentifier>,
1876 CYLocation location_;
1877 CYIdentifier *identifier_;
1878 CYTypeSpecifier *specifier_;
1879 CYTypeModifier *modifier_;
1881 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1882 location_(location),
1883 identifier_(identifier),
1889 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1891 specifier_(specifier),
1896 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1897 CYSetLast(modifier_) = modifier;
1901 virtual CYExpression *Replace(CYContext &context);
1902 virtual void Output(CYOutput &out) const;
1904 CYTypeFunctionWith *Function();
1907 struct CYEncodedType :
1910 CYTypedIdentifier *typed_;
1912 CYEncodedType(CYTypedIdentifier *typed) :
1919 virtual CYExpression *Replace(CYContext &context);
1920 virtual void Output(CYOutput &out, CYFlags flags) const;
1923 struct CYTypedParameter :
1924 CYNext<CYTypedParameter>,
1927 CYTypedIdentifier *typed_;
1929 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1930 CYNext<CYTypedParameter>(next),
1935 CYArgument *Argument(CYContext &context);
1936 CYFunctionParameter *Parameters(CYContext &context);
1937 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1939 virtual void Output(CYOutput &out) const;
1945 CYTypedIdentifier *typed_;
1946 CYTypedParameter *parameters_;
1949 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1951 parameters_(parameters),
1958 virtual CYExpression *Replace(CYContext &context);
1959 virtual void Output(CYOutput &out, CYFlags flags) const;
1968 CYModule(CYWord *part, CYModule *next = NULL) :
1969 CYNext<CYModule>(next),
1974 CYString *Replace(CYContext &context, const char *separator) const;
1975 void Output(CYOutput &out) const;
1983 CYImport(CYModule *module) :
1990 virtual CYStatement *Replace(CYContext &context);
1991 virtual void Output(CYOutput &out, CYFlags flags) const;
1998 CYTypedIdentifier *typed_;
2000 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2008 virtual CYStatement *Replace(CYContext &context);
2009 virtual void Output(CYOutput &out, CYFlags flags) const;
2012 struct CYTypeDefinition :
2015 CYTypedIdentifier *typed_;
2017 CYTypeDefinition(CYTypedIdentifier *typed) :
2024 virtual CYStatement *Replace(CYContext &context);
2025 virtual void Output(CYOutput &out, CYFlags flags) const;
2028 struct CYTypeBlockWith :
2031 CYTypedParameter *parameters_;
2033 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2034 CYTypeModifier(next),
2035 parameters_(parameters)
2041 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2042 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2045 struct CYTypeFunctionWith :
2048 CYTypedParameter *parameters_;
2050 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2051 CYTypeModifier(next),
2052 parameters_(parameters)
2058 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2059 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2061 virtual CYTypeFunctionWith *Function() { return this; }
2070 CYIdentifier *name_;
2073 Catch(CYIdentifier *name, CYStatement *code) :
2079 void Replace(CYContext &context);
2080 virtual void Output(CYOutput &out) const;
2088 CYFinally *finally_;
2090 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2099 virtual CYStatement *Replace(CYContext &context);
2100 virtual void Output(CYOutput &out, CYFlags flags) const;
2106 CYExpression *value_;
2108 Throw(CYExpression *value = NULL) :
2115 virtual CYStatement *Replace(CYContext &context);
2116 virtual void Output(CYOutput &out, CYFlags flags) const;
2124 CYExpression *scope_;
2127 CYWith(CYExpression *scope, CYStatement *code) :
2135 virtual CYStatement *Replace(CYContext &context);
2136 virtual void Output(CYOutput &out, CYFlags flags) const;
2142 CYExpression *value_;
2145 CYSwitch(CYExpression *value, CYClause *clauses) :
2153 virtual CYStatement *Replace(CYContext &context);
2154 virtual void Output(CYOutput &out, CYFlags flags) const;
2166 virtual CYStatement *Replace(CYContext &context);
2167 virtual void Output(CYOutput &out, CYFlags flags) const;
2170 struct CYCondition :
2173 CYExpression *test_;
2174 CYExpression *true_;
2175 CYExpression *false_;
2177 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2186 virtual CYExpression *Replace(CYContext &context);
2187 virtual void Output(CYOutput &out, CYFlags flags) const;
2190 struct CYAddressOf :
2193 CYAddressOf(CYExpression *rhs) :
2198 virtual const char *Operator() const {
2204 virtual CYExpression *Replace(CYContext &context);
2210 CYIndirect(CYExpression *rhs) :
2215 virtual const char *Operator() const {
2221 virtual CYExpression *Replace(CYContext &context);
2225 virtual CYExpression *Replace(CYContext &context);
2227 #define CYPostfix_(op, name, args...) \
2228 struct CY ## name : \
2231 CY ## name(CYExpression *lhs) : \
2236 virtual const char *Operator() const { \
2241 #define CYPrefix_(alphabetic, op, name, args...) \
2242 struct CY ## name : \
2245 CY ## name(CYExpression *rhs) : \
2250 CYAlphabetic(alphabetic) \
2252 virtual const char *Operator() const { \
2257 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2258 struct CY ## name : \
2261 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2266 CYAlphabetic(alphabetic) \
2267 CYPrecedence(precedence) \
2269 virtual const char *Operator() const { \
2274 #define CYAssignment_(op, name, args...) \
2275 struct CY ## name ## Assign : \
2278 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2279 CYAssignment(lhs, rhs) \
2283 virtual const char *Operator() const { \
2288 CYPostfix_("++", PostIncrement)
2289 CYPostfix_("--", PostDecrement)
2291 CYPrefix_(true, "delete", Delete)
2292 CYPrefix_(true, "void", Void)
2293 CYPrefix_(true, "typeof", TypeOf)
2294 CYPrefix_(false, "++", PreIncrement)
2295 CYPrefix_(false, "--", PreDecrement)
2296 CYPrefix_(false, "+", Affirm)
2297 CYPrefix_(false, "-", Negate)
2298 CYPrefix_(false, "~", BitwiseNot)
2299 CYPrefix_(false, "!", LogicalNot)
2301 CYInfix_(false, 5, "*", Multiply, CYReplace)
2302 CYInfix_(false, 5, "/", Divide)
2303 CYInfix_(false, 5, "%", Modulus)
2304 CYInfix_(false, 6, "+", Add, CYReplace)
2305 CYInfix_(false, 6, "-", Subtract)
2306 CYInfix_(false, 7, "<<", ShiftLeft)
2307 CYInfix_(false, 7, ">>", ShiftRightSigned)
2308 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2309 CYInfix_(false, 8, "<", Less)
2310 CYInfix_(false, 8, ">", Greater)
2311 CYInfix_(false, 8, "<=", LessOrEqual)
2312 CYInfix_(false, 8, ">=", GreaterOrEqual)
2313 CYInfix_(true, 8, "instanceof", InstanceOf)
2314 CYInfix_(true, 8, "in", In)
2315 CYInfix_(false, 9, "==", Equal)
2316 CYInfix_(false, 9, "!=", NotEqual)
2317 CYInfix_(false, 9, "===", Identical)
2318 CYInfix_(false, 9, "!==", NotIdentical)
2319 CYInfix_(false, 10, "&", BitwiseAnd)
2320 CYInfix_(false, 11, "^", BitwiseXOr)
2321 CYInfix_(false, 12, "|", BitwiseOr)
2322 CYInfix_(false, 13, "&&", LogicalAnd)
2323 CYInfix_(false, 14, "||", LogicalOr)
2325 CYAssignment_("=", )
2326 CYAssignment_("*=", Multiply)
2327 CYAssignment_("/=", Divide)
2328 CYAssignment_("%=", Modulus)
2329 CYAssignment_("+=", Add)
2330 CYAssignment_("-=", Subtract)
2331 CYAssignment_("<<=", ShiftLeft)
2332 CYAssignment_(">>=", ShiftRightSigned)
2333 CYAssignment_(">>>=", ShiftRightUnsigned)
2334 CYAssignment_("&=", BitwiseAnd)
2335 CYAssignment_("^=", BitwiseXOr)
2336 CYAssignment_("|=", BitwiseOr)
2338 #endif/*CYCRIPT_PARSER_HPP*/