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;
1428 CYExpression *test_;
1430 CYStatement *false_;
1432 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1441 virtual CYStatement *Replace(CYContext &context);
1442 virtual void Output(CYOutput &out, CYFlags flags) const;
1444 virtual CYStatement *Return();
1450 CYExpression *test_;
1453 CYDoWhile(CYExpression *test, CYStatement *code) :
1461 virtual CYStatement *Replace(CYContext &context);
1462 virtual void Output(CYOutput &out, CYFlags flags) const;
1468 CYExpression *test_;
1471 CYWhile(CYExpression *test, CYStatement *code) :
1479 virtual CYStatement *Replace(CYContext &context);
1480 virtual void Output(CYOutput &out, CYFlags flags) const;
1483 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1485 CYIdentifier *name_;
1486 CYFunctionParameter *parameters_;
1489 CYNonLocal *nonlocal_;
1493 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1495 parameters_(parameters),
1502 void Inject(CYContext &context);
1503 virtual void Replace_(CYContext &context, bool outer);
1504 virtual void Output(CYOutput &out, CYFlags flags) const;
1507 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
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;
1524 // XXX: this should derive from CYAnonymousFunction
1529 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1530 CYFunction(NULL, parameters, code)
1537 virtual CYExpression *Replace(CYContext &context);
1538 virtual void Output(CYOutput &out, CYFlags flags) const;
1541 // XXX: this should derive from CYAnonymousFunctionExpression
1543 CYFunctionExpression
1545 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1546 CYFunctionExpression(NULL, parameters, code)
1550 virtual CYExpression *Replace(CYContext &context);
1551 virtual void Output(CYOutput &out, CYFlags flags) const;
1554 // XXX: this should derive from CYNamedFunction
1555 struct CYFunctionStatement :
1559 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1560 CYFunction(name, parameters, code)
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1573 CYExpression *expression_;
1575 CYExpress(CYExpression *expression) :
1576 expression_(expression)
1578 if (expression_ == NULL)
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1587 virtual CYStatement *Return();
1593 CYIdentifier *label_;
1595 CYContinue(CYIdentifier *label) :
1602 virtual CYStatement *Replace(CYContext &context);
1603 virtual void Output(CYOutput &out, CYFlags flags) const;
1609 CYIdentifier *label_;
1611 CYBreak(CYIdentifier *label) :
1618 virtual CYStatement *Replace(CYContext &context);
1619 virtual void Output(CYOutput &out, CYFlags flags) const;
1625 CYExpression *value_;
1627 CYReturn(CYExpression *value) :
1634 virtual CYStatement *Replace(CYContext &context);
1635 virtual void Output(CYOutput &out, CYFlags flags) const;
1638 struct CYYieldGenerator :
1641 CYExpression *value_;
1643 CYYieldGenerator(CYExpression *value) :
1650 virtual CYExpression *Replace(CYContext &context);
1651 virtual void Output(CYOutput &out, CYFlags flags) const;
1654 struct CYYieldValue :
1657 CYExpression *value_;
1659 CYYieldValue(CYExpression *value) :
1666 virtual CYExpression *Replace(CYContext &context);
1667 virtual void Output(CYOutput &out, CYFlags flags) const;
1675 virtual CYStatement *Replace(CYContext &context);
1676 virtual void Output(CYOutput &out, CYFlags flags) const;
1684 CYFinally(CYStatement *code) :
1689 void Replace(CYContext &context);
1690 virtual void Output(CYOutput &out) const;
1693 struct CYTypeSpecifier :
1696 virtual CYExpression *Replace(CYContext &context) = 0;
1699 struct CYTypeError :
1705 virtual CYExpression *Replace(CYContext &context);
1706 virtual void Output(CYOutput &out) const;
1715 virtual CYExpression *Replace(CYContext &context);
1716 virtual void Output(CYOutput &out) const;
1719 struct CYTypeVariable :
1722 CYIdentifier *name_;
1724 CYTypeVariable(CYIdentifier *name) :
1729 CYTypeVariable(const char *name) :
1730 name_(new($pool) CYIdentifier(name))
1734 virtual CYExpression *Replace(CYContext &context);
1735 virtual void Output(CYOutput &out) const;
1738 struct CYTypeUnsigned :
1741 CYTypeSpecifier *specifier_;
1743 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1744 specifier_(specifier)
1748 virtual CYExpression *Replace(CYContext &context);
1749 virtual void Output(CYOutput &out) const;
1752 struct CYTypeSigned :
1755 CYTypeSpecifier *specifier_;
1757 CYTypeSigned(CYTypeSpecifier *specifier) :
1758 specifier_(specifier)
1762 virtual CYExpression *Replace(CYContext &context);
1763 virtual void Output(CYOutput &out) const;
1769 CYTypeSpecifier *specifier_;
1771 CYTypeLong(CYTypeSpecifier *specifier) :
1772 specifier_(specifier)
1776 virtual CYExpression *Replace(CYContext &context);
1777 virtual void Output(CYOutput &out) const;
1780 struct CYTypeShort :
1783 CYTypeSpecifier *specifier_;
1785 CYTypeShort(CYTypeSpecifier *specifier) :
1786 specifier_(specifier)
1790 virtual CYExpression *Replace(CYContext &context);
1791 virtual void Output(CYOutput &out) const;
1794 struct CYTypeFunctionWith;
1796 struct CYTypeModifier :
1797 CYNext<CYTypeModifier>
1799 CYTypeModifier(CYTypeModifier *next) :
1800 CYNext<CYTypeModifier>(next)
1804 virtual int Precedence() const = 0;
1806 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1807 CYExpression *Replace(CYContext &context, CYExpression *type);
1809 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1810 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1812 virtual CYTypeFunctionWith *Function() { return NULL; }
1815 struct CYTypeArrayOf :
1818 CYExpression *size_;
1820 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1821 CYTypeModifier(next),
1828 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1829 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1832 struct CYTypeConstant :
1835 CYTypeConstant(CYTypeModifier *next = NULL) :
1836 CYTypeModifier(next)
1842 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1843 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1846 struct CYTypePointerTo :
1849 CYTypePointerTo(CYTypeModifier *next = NULL) :
1850 CYTypeModifier(next)
1856 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1857 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1860 struct CYTypeVolatile :
1863 CYTypeVolatile(CYTypeModifier *next = NULL) :
1864 CYTypeModifier(next)
1870 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1871 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1874 struct CYTypedIdentifier :
1875 CYNext<CYTypedIdentifier>,
1878 CYLocation location_;
1879 CYIdentifier *identifier_;
1880 CYTypeSpecifier *specifier_;
1881 CYTypeModifier *modifier_;
1883 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1884 location_(location),
1885 identifier_(identifier),
1891 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1893 specifier_(specifier),
1898 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1899 CYSetLast(modifier_) = modifier;
1903 virtual CYExpression *Replace(CYContext &context);
1904 virtual void Output(CYOutput &out) const;
1906 CYTypeFunctionWith *Function();
1909 struct CYEncodedType :
1912 CYTypedIdentifier *typed_;
1914 CYEncodedType(CYTypedIdentifier *typed) :
1921 virtual CYExpression *Replace(CYContext &context);
1922 virtual void Output(CYOutput &out, CYFlags flags) const;
1925 struct CYTypedParameter :
1926 CYNext<CYTypedParameter>,
1929 CYTypedIdentifier *typed_;
1931 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1932 CYNext<CYTypedParameter>(next),
1937 CYArgument *Argument(CYContext &context);
1938 CYFunctionParameter *Parameters(CYContext &context);
1939 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1941 virtual void Output(CYOutput &out) const;
1947 CYTypedIdentifier *typed_;
1948 CYTypedParameter *parameters_;
1951 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1953 parameters_(parameters),
1960 virtual CYExpression *Replace(CYContext &context);
1961 virtual void Output(CYOutput &out, CYFlags flags) const;
1970 CYModule(CYWord *part, CYModule *next = NULL) :
1971 CYNext<CYModule>(next),
1976 CYString *Replace(CYContext &context, const char *separator) const;
1977 void Output(CYOutput &out) const;
1985 CYImport(CYModule *module) :
1992 virtual CYStatement *Replace(CYContext &context);
1993 virtual void Output(CYOutput &out, CYFlags flags) const;
2000 CYTypedIdentifier *typed_;
2002 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2010 virtual CYStatement *Replace(CYContext &context);
2011 virtual void Output(CYOutput &out, CYFlags flags) const;
2014 struct CYTypeDefinition :
2017 CYTypedIdentifier *typed_;
2019 CYTypeDefinition(CYTypedIdentifier *typed) :
2026 virtual CYStatement *Replace(CYContext &context);
2027 virtual void Output(CYOutput &out, CYFlags flags) const;
2030 struct CYTypeBlockWith :
2033 CYTypedParameter *parameters_;
2035 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2036 CYTypeModifier(next),
2037 parameters_(parameters)
2043 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2044 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2047 struct CYTypeFunctionWith :
2050 CYTypedParameter *parameters_;
2052 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2053 CYTypeModifier(next),
2054 parameters_(parameters)
2060 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2061 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2063 virtual CYTypeFunctionWith *Function() { return this; }
2072 CYIdentifier *name_;
2075 Catch(CYIdentifier *name, CYStatement *code) :
2081 void Replace(CYContext &context);
2082 virtual void Output(CYOutput &out) const;
2090 CYFinally *finally_;
2092 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2101 virtual CYStatement *Replace(CYContext &context);
2102 virtual void Output(CYOutput &out, CYFlags flags) const;
2108 CYExpression *value_;
2110 Throw(CYExpression *value = NULL) :
2117 virtual CYStatement *Replace(CYContext &context);
2118 virtual void Output(CYOutput &out, CYFlags flags) const;
2126 CYExpression *scope_;
2129 CYWith(CYExpression *scope, CYStatement *code) :
2137 virtual CYStatement *Replace(CYContext &context);
2138 virtual void Output(CYOutput &out, CYFlags flags) const;
2144 CYExpression *value_;
2147 CYSwitch(CYExpression *value, CYClause *clauses) :
2155 virtual CYStatement *Replace(CYContext &context);
2156 virtual void Output(CYOutput &out, CYFlags flags) const;
2168 virtual CYStatement *Replace(CYContext &context);
2169 virtual void Output(CYOutput &out, CYFlags flags) const;
2172 struct CYCondition :
2175 CYExpression *test_;
2176 CYExpression *true_;
2177 CYExpression *false_;
2179 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2188 virtual CYExpression *Replace(CYContext &context);
2189 virtual void Output(CYOutput &out, CYFlags flags) const;
2192 struct CYAddressOf :
2195 CYAddressOf(CYExpression *rhs) :
2200 virtual const char *Operator() const {
2206 virtual CYExpression *Replace(CYContext &context);
2212 CYIndirect(CYExpression *rhs) :
2217 virtual const char *Operator() const {
2223 virtual CYExpression *Replace(CYContext &context);
2227 virtual CYExpression *Replace(CYContext &context);
2229 #define CYPostfix_(op, name, args...) \
2230 struct CY ## name : \
2233 CY ## name(CYExpression *lhs) : \
2238 virtual const char *Operator() const { \
2243 #define CYPrefix_(alphabetic, op, name, args...) \
2244 struct CY ## name : \
2247 CY ## name(CYExpression *rhs) : \
2252 CYAlphabetic(alphabetic) \
2254 virtual const char *Operator() const { \
2259 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2260 struct CY ## name : \
2263 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2268 CYAlphabetic(alphabetic) \
2269 CYPrecedence(precedence) \
2271 virtual const char *Operator() const { \
2276 #define CYAssignment_(op, name, args...) \
2277 struct CY ## name ## Assign : \
2280 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2281 CYAssignment(lhs, rhs) \
2285 virtual const char *Operator() const { \
2290 CYPostfix_("++", PostIncrement)
2291 CYPostfix_("--", PostDecrement)
2293 CYPrefix_(true, "delete", Delete)
2294 CYPrefix_(true, "void", Void)
2295 CYPrefix_(true, "typeof", TypeOf)
2296 CYPrefix_(false, "++", PreIncrement)
2297 CYPrefix_(false, "--", PreDecrement)
2298 CYPrefix_(false, "+", Affirm)
2299 CYPrefix_(false, "-", Negate)
2300 CYPrefix_(false, "~", BitwiseNot)
2301 CYPrefix_(false, "!", LogicalNot)
2303 CYInfix_(false, 5, "*", Multiply, CYReplace)
2304 CYInfix_(false, 5, "/", Divide)
2305 CYInfix_(false, 5, "%", Modulus)
2306 CYInfix_(false, 6, "+", Add, CYReplace)
2307 CYInfix_(false, 6, "-", Subtract)
2308 CYInfix_(false, 7, "<<", ShiftLeft)
2309 CYInfix_(false, 7, ">>", ShiftRightSigned)
2310 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2311 CYInfix_(false, 8, "<", Less)
2312 CYInfix_(false, 8, ">", Greater)
2313 CYInfix_(false, 8, "<=", LessOrEqual)
2314 CYInfix_(false, 8, ">=", GreaterOrEqual)
2315 CYInfix_(true, 8, "instanceof", InstanceOf)
2316 CYInfix_(true, 8, "in", In)
2317 CYInfix_(false, 9, "==", Equal)
2318 CYInfix_(false, 9, "!=", NotEqual)
2319 CYInfix_(false, 9, "===", Identical)
2320 CYInfix_(false, 9, "!==", NotIdentical)
2321 CYInfix_(false, 10, "&", BitwiseAnd)
2322 CYInfix_(false, 11, "^", BitwiseXOr)
2323 CYInfix_(false, 12, "|", BitwiseOr)
2324 CYInfix_(false, 13, "&&", LogicalAnd)
2325 CYInfix_(false, 14, "||", LogicalOr)
2327 CYAssignment_("=", )
2328 CYAssignment_("*=", Multiply)
2329 CYAssignment_("/=", Divide)
2330 CYAssignment_("%=", Modulus)
2331 CYAssignment_("+=", Add)
2332 CYAssignment_("-=", Subtract)
2333 CYAssignment_("<<=", ShiftLeft)
2334 CYAssignment_(">>=", ShiftRightSigned)
2335 CYAssignment_(">>>=", ShiftRightUnsigned)
2336 CYAssignment_("&=", BitwiseAnd)
2337 CYAssignment_("^=", BitwiseXOr)
2338 CYAssignment_("|=", BitwiseOr)
2340 #endif/*CYCRIPT_PARSER_HPP*/