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_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
36 #include "Location.hpp"
37 #include "Pooling.hpp"
38 #include "Options.hpp"
46 virtual void Output(struct CYOutput &out) const = 0;
64 CYOutput(std::ostream &out, CYOptions &options) :
74 void Check(char value);
77 CYOutput &operator <<(char rhs);
78 CYOutput &operator <<(const char *rhs);
80 _finline CYOutput &operator <<(const CYThing *rhs) {
86 _finline CYOutput &operator <<(const CYThing &rhs) {
92 struct CYPropertyName {
93 virtual void PropertyName(CYOutput &out) const = 0;
95 virtual ~CYPropertyName() {
110 CYNoBrace = (1 << 0),
111 CYNoFunction = (1 << 1),
114 CYNoRightHand = (1 << 4),
115 CYNoDangle = (1 << 5),
116 CYNoInteger = (1 << 6),
117 CYNoBF = (CYNoBrace | CYNoFunction),
120 _finline CYFlags operator ~(CYFlags rhs) {
121 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
124 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
125 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
128 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
129 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
132 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
133 return lhs = lhs | rhs;
136 _finline CYFlags CYLeft(CYFlags flags) {
137 return flags & ~(CYNoDangle | CYNoInteger);
140 _finline CYFlags CYRight(CYFlags flags) {
141 return flags & ~CYNoBF;
144 _finline CYFlags CYCenter(CYFlags flags) {
145 return CYLeft(CYRight(flags));
152 virtual ~CYStatement() {
155 void Single(CYOutput &out, CYFlags flags) const;
156 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
157 virtual void Output(CYOutput &out) const;
159 virtual CYStatement *Replace(CYContext &context) = 0;
161 virtual CYStatement *Return();
164 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
167 struct CYStatements {
177 operator CYStatement *() const {
181 CYStatements &operator ->*(CYStatement *next) {
183 if (first_ == NULL) {
186 } else for (;; last_ = last_->next_)
187 if (last_->next_ == NULL) {
197 virtual ~CYClassName() {
200 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
201 virtual void ClassName(CYOutput &out, bool object) const = 0;
211 CYWord(const char *word) :
216 void Set(const char *value) {
220 virtual const char *Word() const;
221 virtual void Output(CYOutput &out) const;
223 virtual CYExpression *ClassName(CYContext &context, bool object);
224 virtual void ClassName(CYOutput &out, bool object) const;
225 virtual void PropertyName(CYOutput &out) const;
228 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
230 return lhs << rhs.Word();
233 struct CYIdentifier :
234 CYNext<CYIdentifier>,
237 CYIdentifier *replace_;
241 CYIdentifier(const char *word) :
249 virtual const char *Word() const;
250 CYIdentifier *Replace(CYContext &context);
258 CYComment(const char *value) :
263 virtual CYStatement *Replace(CYContext &context);
264 virtual void Output(CYOutput &out, CYFlags flags) const;
271 CYStatement *statement_;
273 CYLabel(CYIdentifier *name, CYStatement *statement) :
275 statement_(statement)
279 virtual CYStatement *Replace(CYContext &context);
280 virtual void Output(CYOutput &out, CYFlags flags) const;
283 struct CYCStringLess :
284 std::binary_function<const char *, const char *, bool>
286 _finline bool operator ()(const char *lhs, const char *rhs) const {
287 return strcmp(lhs, rhs) < 0;
291 struct CYIdentifierValueLess :
292 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
294 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
295 return CYCStringLess()(lhs->Word(), rhs->Word());
299 enum CYIdentifierFlags {
300 CYIdentifierArgument,
301 CYIdentifierVariable,
307 typedef std::set<const char *, CYCStringLess> CYCStringSet;
308 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
309 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
311 struct CYIdentifierUsage {
312 CYIdentifier *identifier_;
316 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
322 CYIdentifierAddressFlagsMap internal_;
323 CYIdentifierValueSet identifiers_;
325 CYScope(bool transparent, CYContext &context);
327 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
328 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
329 void Merge(CYContext &context, CYIdentifier *identifier);
330 void Close(CYContext &context, CYStatement *&statements);
338 CYProgram(CYStatement *code) :
343 virtual void Replace(CYContext &context);
344 virtual void Output(CYOutput &out) const;
356 CYIdentifierUsageVector rename_;
358 CYNonLocal *nonlocal_;
359 CYNonLocal *nextlocal_;
362 CYContext(CYOptions &options) :
372 virtual ~CYContext() {
375 void ReplaceAll(CYStatement *&statement) {
376 if (statement == NULL)
378 CYStatement *next(statement->next_);
383 if (statement == NULL)
386 statement->SetNext(next);
389 template <typename Type_>
390 void Replace(Type_ *&value) {
391 for (;;) if (value == NULL)
394 Type_ *replace(value->Replace(*this));
395 if (replace != value)
401 void NonLocal(CYStatement *&statements);
402 CYIdentifier *Unique();
406 CYIdentifier *identifier_;
413 CYIdentifier *Target(CYContext &context) {
414 if (identifier_ == NULL)
415 identifier_ = context.Unique();
423 CYIdentifier *identifier_;
430 CYIdentifier *Identifier(CYContext &context) {
432 return next_->Identifier(context);
433 if (identifier_ == NULL)
434 identifier_ = context.Unique();
444 CYBlock(CYStatement *code) :
449 virtual CYStatement *Replace(CYContext &context);
451 virtual void Output(CYOutput &out, CYFlags flags) const;
453 virtual CYStatement *Return();
456 struct CYForInitialiser {
457 virtual ~CYForInitialiser() {
460 virtual CYExpression *Replace(CYContext &context) = 0;
461 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
464 struct CYForInInitialiser {
465 virtual ~CYForInInitialiser() {
468 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
469 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
471 virtual CYExpression *Replace(CYContext &context) = 0;
472 virtual CYAssignment *Assignment(CYContext &context) = 0;
474 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
477 struct CYFunctionParameter;
482 struct CYExpression :
488 virtual int Precedence() const = 0;
490 virtual bool RightHand() const {
494 virtual void ForIn(CYOutput &out, CYFlags flags) const;
495 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
497 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
499 virtual void Output(CYOutput &out) const;
500 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
501 void Output(CYOutput &out, int precedence, CYFlags flags) const;
503 virtual CYExpression *ClassName(CYContext &context, bool object);
504 virtual void ClassName(CYOutput &out, bool object) const;
506 virtual CYExpression *Replace(CYContext &context) = 0;
507 virtual CYAssignment *Assignment(CYContext &context);
509 virtual CYExpression *Primitive(CYContext &context) {
513 virtual CYFunctionParameter *Parameter() const;
515 virtual CYNumber *Number(CYContext &context) {
519 virtual CYString *String(CYContext &context) {
523 virtual const char *Word() const {
528 #define CYAlphabetic(value) \
529 virtual bool Alphabetic() const { \
533 #define CYPrecedence(value) \
534 static const int Precedence_ = value; \
535 virtual int Precedence() const { \
536 return Precedence_; \
539 #define CYRightHand(value) \
540 virtual bool RightHand() const { \
547 CYExpression *expression_;
550 CYCompound(CYExpression *expression, CYExpression *next) :
551 expression_(expression),
554 _assert(expression_ != NULL);
555 _assert(next != NULL);
560 virtual CYExpression *Replace(CYContext &context);
561 void Output(CYOutput &out, CYFlags flags) const;
563 virtual CYFunctionParameter *Parameter() const;
566 struct CYParenthetical :
569 CYExpression *expression_;
571 CYParenthetical(CYExpression *expression) :
572 expression_(expression)
578 virtual CYExpression *Replace(CYContext &context);
579 void Output(CYOutput &out, CYFlags flags) const;
582 struct CYDeclaration;
584 struct CYFunctionParameter :
585 CYNext<CYFunctionParameter>,
588 CYForInInitialiser *initialiser_;
590 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
591 CYNext<CYFunctionParameter>(next),
592 initialiser_(initialiser)
596 void Replace(CYContext &context, CYStatement *&statements);
597 void Output(CYOutput &out) const;
600 struct CYComprehension :
601 CYNext<CYComprehension>,
604 CYComprehension(CYComprehension *next = NULL) :
605 CYNext<CYComprehension>(next)
609 CYComprehension *Modify(CYComprehension *next) {
614 virtual const char *Name() const = 0;
616 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
617 CYFunctionParameter *Parameters(CYContext &context) const;
618 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
619 virtual void Output(CYOutput &out) const = 0;
622 struct CYForInComprehension :
628 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
629 CYComprehension(next),
635 virtual const char *Name() const {
636 return name_->Word();
639 virtual CYFunctionParameter *Parameter(CYContext &context) const;
640 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
641 virtual void Output(CYOutput &out) const;
644 struct CYForOfComprehension :
650 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
651 CYComprehension(next),
657 virtual const char *Name() const {
658 return name_->Word();
661 virtual CYFunctionParameter *Parameter(CYContext &context) const;
662 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
663 virtual void Output(CYOutput &out) const;
666 struct CYIfComprehension :
671 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
672 CYComprehension(next),
677 virtual const char *Name() const {
681 virtual CYFunctionParameter *Parameter(CYContext &context) const;
682 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
683 virtual void Output(CYOutput &out) const;
686 struct CYArrayComprehension :
689 CYExpression *expression_;
690 CYComprehension *comprehensions_;
692 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
693 expression_(expression),
694 comprehensions_(comprehensions)
700 virtual CYExpression *Replace(CYContext &context);
701 virtual void Output(CYOutput &out, CYFlags flags) const;
710 virtual CYExpression *Primitive(CYContext &context) {
718 virtual CYExpression *Replace(CYContext &context);
732 CYRange(uint64_t lo, uint64_t hi) :
737 bool operator [](uint8_t value) const {
738 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
741 void operator()(uint8_t value) {
744 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
748 extern CYRange DigitRange_;
749 extern CYRange WordStartRange_;
750 extern CYRange WordEndRange_;
765 CYString(const char *value) :
771 CYString(const char *value, size_t size) :
777 CYString(const CYWord *word) :
778 value_(word->Word()),
779 size_(strlen(value_))
783 const char *Value() const {
787 virtual const char *Word() const;
789 virtual CYNumber *Number(CYContext &context);
790 virtual CYString *String(CYContext &context);
792 CYString *Concat(CYContext &out, CYString *rhs) const;
793 virtual void Output(CYOutput &out, CYFlags flags) const;
794 virtual void PropertyName(CYOutput &out) const;
803 CYNumber(double value) :
808 double Value() const {
812 virtual CYNumber *Number(CYContext &context);
813 virtual CYString *String(CYContext &context);
815 virtual void Output(CYOutput &out, CYFlags flags) const;
816 virtual void PropertyName(CYOutput &out) const;
824 CYRegEx(const char *value) :
829 const char *Value() const {
833 virtual void Output(CYOutput &out, CYFlags flags) const;
845 virtual CYNumber *Number(CYContext &context);
846 virtual CYString *String(CYContext &context);
848 virtual void Output(CYOutput &out, CYFlags flags) const;
860 virtual CYExpression *Replace(CYContext &context);
861 virtual void Output(CYOutput &out, CYFlags flags) const;
867 virtual bool Value() const = 0;
868 virtual void Output(CYOutput &out, CYFlags flags) const;
880 virtual bool Value() const {
884 virtual CYNumber *Number(CYContext &context);
885 virtual CYString *String(CYContext &context);
897 virtual bool Value() const {
901 virtual CYNumber *Number(CYContext &context);
902 virtual CYString *String(CYContext &context);
910 CYVariable(CYIdentifier *name) :
915 CYVariable(const char *name) :
916 name_(new($pool) CYIdentifier(name))
923 virtual CYExpression *Replace(CYContext &context);
924 virtual void Output(CYOutput &out, CYFlags flags) const;
926 virtual CYFunctionParameter *Parameter() const;
934 CYPrefix(CYExpression *rhs) :
939 virtual bool Alphabetic() const = 0;
940 virtual const char *Operator() const = 0;
944 virtual CYExpression *Replace(CYContext &context);
945 virtual void Output(CYOutput &out, CYFlags flags) const;
954 CYInfix(CYExpression *lhs, CYExpression *rhs) :
960 void SetLeft(CYExpression *lhs) {
964 virtual bool Alphabetic() const = 0;
965 virtual const char *Operator() const = 0;
967 virtual CYExpression *Replace(CYContext &context);
968 virtual void Output(CYOutput &out, CYFlags flags) const;
976 CYPostfix(CYExpression *lhs) :
981 virtual const char *Operator() const = 0;
985 virtual CYExpression *Replace(CYContext &context);
986 virtual void Output(CYOutput &out, CYFlags flags) const;
989 struct CYAssignment :
995 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1001 void SetLeft(CYExpression *lhs) {
1005 virtual const char *Operator() const = 0;
1009 virtual CYExpression *Replace(CYContext &context);
1010 virtual void Output(CYOutput &out, CYFlags flags) const;
1018 CYExpression *value_;
1020 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1021 CYNext<CYArgument>(next),
1027 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1028 CYNext<CYArgument>(next),
1034 CYArgument *Replace(CYContext &context);
1035 void Output(CYOutput &out) const;
1051 CYExpression *case_;
1054 CYClause(CYExpression *_case, CYStatement *code) :
1060 void Replace(CYContext &context);
1061 virtual void Output(CYOutput &out) const;
1068 CYExpression *value_;
1070 CYElement(CYExpression *value, CYElement *next) :
1071 CYNext<CYElement>(next),
1076 void Replace(CYContext &context);
1077 void Output(CYOutput &out) const;
1083 CYElement *elements_;
1085 CYArray(CYElement *elements = NULL) :
1090 virtual CYExpression *Replace(CYContext &context);
1091 virtual void Output(CYOutput &out, CYFlags flags) const;
1098 CYPropertyName *name_;
1099 CYExpression *value_;
1101 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1102 CYNext<CYProperty>(next),
1108 void Replace(CYContext &context);
1109 virtual void Output(CYOutput &out) const;
1112 struct CYDeclaration :
1115 CYIdentifier *identifier_;
1116 CYExpression *initialiser_;
1118 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1119 identifier_(identifier),
1120 initialiser_(initialiser)
1124 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1125 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1127 virtual CYExpression *Replace(CYContext &context);
1129 virtual CYAssignment *Assignment(CYContext &context);
1130 CYVariable *Variable(CYContext &context);
1132 virtual void Output(CYOutput &out, CYFlags flags) const;
1135 struct CYDeclarations :
1136 CYNext<CYDeclarations>,
1139 CYDeclaration *declaration_;
1141 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1142 CYNext<CYDeclarations>(next),
1143 declaration_(declaration)
1147 void Replace(CYContext &context);
1149 CYExpression *Expression(CYContext &context);
1150 CYProperty *Property(CYContext &context);
1151 CYArgument *Argument(CYContext &context);
1152 CYFunctionParameter *Parameter(CYContext &context);
1154 virtual void Output(CYOutput &out) const;
1155 virtual void Output(CYOutput &out, CYFlags flags) const;
1158 struct CYForDeclarations :
1161 CYDeclarations *declarations_;
1163 CYForDeclarations(CYDeclarations *declarations) :
1164 declarations_(declarations)
1168 virtual CYExpression *Replace(CYContext &context);
1169 virtual void Output(CYOutput &out, CYFlags flags) const;
1175 CYDeclarations *declarations_;
1177 CYVar(CYDeclarations *declarations) :
1178 declarations_(declarations)
1182 virtual CYStatement *Replace(CYContext &context);
1183 virtual void Output(CYOutput &out, CYFlags flags) const;
1186 struct CYLetStatement :
1189 CYDeclarations *declarations_;
1192 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1193 declarations_(declarations),
1198 virtual CYStatement *Replace(CYContext &context);
1199 virtual void Output(CYOutput &out, CYFlags flags) const;
1205 CYForInitialiser *initialiser_;
1206 CYExpression *test_;
1207 CYExpression *increment_;
1210 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1211 initialiser_(initialiser),
1213 increment_(increment),
1218 virtual CYStatement *Replace(CYContext &context);
1219 virtual void Output(CYOutput &out, CYFlags flags) const;
1225 CYForInInitialiser *initialiser_;
1229 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1230 initialiser_(initialiser),
1236 virtual CYStatement *Replace(CYContext &context);
1237 virtual void Output(CYOutput &out, CYFlags flags) const;
1243 CYForInInitialiser *initialiser_;
1247 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1248 initialiser_(initialiser),
1254 virtual CYStatement *Replace(CYContext &context);
1255 virtual void Output(CYOutput &out, CYFlags flags) const;
1261 CYProperty *properties_;
1263 CYObject(CYProperty *properties = NULL) :
1264 properties_(properties)
1268 virtual CYExpression *Replace(CYContext &context);
1269 void Output(CYOutput &out, CYFlags flags) const;
1275 CYExpression *object_;
1276 CYExpression *property_;
1278 CYMember(CYExpression *object, CYExpression *property) :
1284 void SetLeft(CYExpression *object) {
1289 struct CYDirectMember :
1292 CYDirectMember(CYExpression *object, CYExpression *property) :
1293 CYMember(object, property)
1300 virtual CYExpression *Replace(CYContext &context);
1301 virtual void Output(CYOutput &out, CYFlags flags) const;
1304 struct CYIndirectMember :
1307 CYIndirectMember(CYExpression *object, CYExpression *property) :
1308 CYMember(object, property)
1315 virtual CYExpression *Replace(CYContext &context);
1316 virtual void Output(CYOutput &out, CYFlags flags) const;
1325 CYExpression *constructor_;
1326 CYArgument *arguments_;
1328 New(CYExpression *constructor, CYArgument *arguments) :
1329 constructor_(constructor),
1330 arguments_(arguments)
1334 virtual int Precedence() const {
1335 return arguments_ == NULL ? 2 : 1;
1340 virtual CYExpression *Replace(CYContext &context);
1341 virtual void Output(CYOutput &out, CYFlags flags) const;
1343 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1351 CYExpression *function_;
1352 CYArgument *arguments_;
1354 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1355 function_(function),
1356 arguments_(arguments)
1363 virtual CYExpression *Replace(CYContext &context);
1364 virtual void Output(CYOutput &out, CYFlags flags) const;
1366 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1371 struct CYRubyBlock :
1374 CYExpression *call_;
1377 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1386 virtual CYExpression *Replace(CYContext &context);
1387 virtual void Output(CYOutput &out, CYFlags flags) const;
1393 CYExpression *test_;
1395 CYStatement *false_;
1397 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1404 virtual CYStatement *Replace(CYContext &context);
1405 virtual void Output(CYOutput &out, CYFlags flags) const;
1407 virtual CYStatement *Return();
1413 CYExpression *test_;
1416 CYDoWhile(CYExpression *test, CYStatement *code) :
1422 virtual CYStatement *Replace(CYContext &context);
1423 virtual void Output(CYOutput &out, CYFlags flags) const;
1429 CYExpression *test_;
1432 CYWhile(CYExpression *test, CYStatement *code) :
1438 virtual CYStatement *Replace(CYContext &context);
1439 virtual void Output(CYOutput &out, CYFlags flags) const;
1442 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1444 CYIdentifier *name_;
1445 CYFunctionParameter *parameters_;
1448 CYNonLocal *nonlocal_;
1452 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1454 parameters_(parameters),
1461 virtual ~CYFunction() {
1464 void Inject(CYContext &context);
1465 virtual void Replace_(CYContext &context, bool outer);
1466 virtual void Output(CYOutput &out, CYFlags flags) const;
1469 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1470 struct CYFunctionExpression :
1474 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1475 CYFunction(name, parameters, code)
1482 virtual CYExpression *Replace(CYContext &context);
1483 virtual void Output(CYOutput &out, CYFlags flags) const;
1486 // XXX: this should derive from CYAnonymousFunction
1491 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1492 CYFunction(NULL, parameters, code)
1499 virtual CYExpression *Replace(CYContext &context);
1500 virtual void Output(CYOutput &out, CYFlags flags) const;
1503 // XXX: this should derive from CYAnonymousFunctionExpression
1505 CYFunctionExpression
1507 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1508 CYFunctionExpression(NULL, parameters, code)
1512 virtual CYExpression *Replace(CYContext &context);
1513 virtual void Output(CYOutput &out, CYFlags flags) const;
1516 // XXX: this should derive from CYNamedFunction
1517 struct CYFunctionStatement :
1521 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1522 CYFunction(name, parameters, code)
1526 virtual CYStatement *Replace(CYContext &context);
1527 virtual void Output(CYOutput &out, CYFlags flags) const;
1533 CYExpression *expression_;
1535 CYExpress(CYExpression *expression) :
1536 expression_(expression)
1538 if (expression_ == NULL)
1542 virtual CYStatement *Replace(CYContext &context);
1543 virtual void Output(CYOutput &out, CYFlags flags) const;
1545 virtual CYStatement *Return();
1551 CYIdentifier *label_;
1553 CYContinue(CYIdentifier *label) :
1558 virtual CYStatement *Replace(CYContext &context);
1559 virtual void Output(CYOutput &out, CYFlags flags) const;
1565 CYIdentifier *label_;
1567 CYBreak(CYIdentifier *label) :
1572 virtual CYStatement *Replace(CYContext &context);
1573 virtual void Output(CYOutput &out, CYFlags flags) const;
1579 CYExpression *value_;
1581 CYReturn(CYExpression *value) :
1586 virtual CYStatement *Replace(CYContext &context);
1587 virtual void Output(CYOutput &out, CYFlags flags) const;
1593 virtual CYStatement *Replace(CYContext &context);
1594 virtual void Output(CYOutput &out, CYFlags flags) const;
1602 CYFinally(CYStatement *code) :
1607 void Replace(CYContext &context);
1608 virtual void Output(CYOutput &out) const;
1611 struct CYTypeSpecifier :
1614 virtual CYExpression *Replace(CYContext &context) = 0;
1617 struct CYTypeError :
1623 virtual CYExpression *Replace(CYContext &context);
1624 virtual void Output(CYOutput &out) const;
1633 virtual CYExpression *Replace(CYContext &context);
1634 virtual void Output(CYOutput &out) const;
1637 struct CYTypeVariable :
1640 CYIdentifier *name_;
1642 CYTypeVariable(CYIdentifier *name) :
1647 CYTypeVariable(const char *name) :
1648 name_(new($pool) CYIdentifier(name))
1652 virtual CYExpression *Replace(CYContext &context);
1653 virtual void Output(CYOutput &out) const;
1656 struct CYTypeUnsigned :
1659 CYTypeSpecifier *specifier_;
1661 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1662 specifier_(specifier)
1666 virtual CYExpression *Replace(CYContext &context);
1667 virtual void Output(CYOutput &out) const;
1670 struct CYTypeSigned :
1673 CYTypeSpecifier *specifier_;
1675 CYTypeSigned(CYTypeSpecifier *specifier) :
1676 specifier_(specifier)
1680 virtual CYExpression *Replace(CYContext &context);
1681 virtual void Output(CYOutput &out) const;
1687 CYTypeSpecifier *specifier_;
1689 CYTypeLong(CYTypeSpecifier *specifier) :
1690 specifier_(specifier)
1694 virtual CYExpression *Replace(CYContext &context);
1695 virtual void Output(CYOutput &out) const;
1698 struct CYTypeShort :
1701 CYTypeSpecifier *specifier_;
1703 CYTypeShort(CYTypeSpecifier *specifier) :
1704 specifier_(specifier)
1708 virtual CYExpression *Replace(CYContext &context);
1709 virtual void Output(CYOutput &out) const;
1712 struct CYTypeFunctionWith;
1714 struct CYTypeModifier :
1715 CYNext<CYTypeModifier>
1717 CYTypeModifier(CYTypeModifier *next) :
1718 CYNext<CYTypeModifier>(next)
1722 virtual int Precedence() const = 0;
1724 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1725 CYExpression *Replace(CYContext &context, CYExpression *type);
1727 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1728 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1730 virtual CYTypeFunctionWith *Function() { return NULL; }
1733 struct CYTypeArrayOf :
1736 CYExpression *size_;
1738 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1739 CYTypeModifier(next),
1746 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1747 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1750 struct CYTypeConstant :
1753 CYTypeConstant(CYTypeModifier *next = NULL) :
1754 CYTypeModifier(next)
1760 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1761 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1764 struct CYTypePointerTo :
1767 CYTypePointerTo(CYTypeModifier *next = NULL) :
1768 CYTypeModifier(next)
1774 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1775 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1778 struct CYTypeVolatile :
1781 CYTypeVolatile(CYTypeModifier *next = NULL) :
1782 CYTypeModifier(next)
1788 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1789 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1792 struct CYTypedIdentifier :
1793 CYNext<CYTypedIdentifier>,
1796 CYLocation location_;
1797 CYIdentifier *identifier_;
1798 CYTypeSpecifier *specifier_;
1799 CYTypeModifier *modifier_;
1801 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1802 location_(location),
1803 identifier_(identifier),
1809 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1811 specifier_(specifier),
1816 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1817 CYSetLast(modifier_) = modifier;
1821 virtual CYExpression *Replace(CYContext &context);
1822 virtual void Output(CYOutput &out) const;
1824 CYTypeFunctionWith *Function();
1827 struct CYEncodedType :
1830 CYTypedIdentifier *typed_;
1832 CYEncodedType(CYTypedIdentifier *typed) :
1839 virtual CYExpression *Replace(CYContext &context);
1840 virtual void Output(CYOutput &out, CYFlags flags) const;
1843 struct CYTypedParameter :
1844 CYNext<CYTypedParameter>,
1847 CYTypedIdentifier *typed_;
1849 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1850 CYNext<CYTypedParameter>(next),
1855 CYArgument *Argument(CYContext &context);
1856 CYFunctionParameter *Parameters(CYContext &context);
1857 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1859 virtual void Output(CYOutput &out) const;
1865 CYTypedIdentifier *typed_;
1866 CYTypedParameter *parameters_;
1869 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1871 parameters_(parameters),
1878 virtual CYExpression *Replace(CYContext &context);
1879 virtual void Output(CYOutput &out, CYFlags flags) const;
1888 CYModule(CYWord *part, CYModule *next = NULL) :
1889 CYNext<CYModule>(next),
1894 CYString *Replace(CYContext &context, const char *separator) const;
1895 void Output(CYOutput &out) const;
1903 CYImport(CYModule *module) :
1908 virtual CYStatement *Replace(CYContext &context);
1909 virtual void Output(CYOutput &out, CYFlags flags) const;
1916 CYTypedIdentifier *typed_;
1918 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1924 virtual CYStatement *Replace(CYContext &context);
1925 virtual void Output(CYOutput &out, CYFlags flags) const;
1928 struct CYTypeDefinition :
1931 CYTypedIdentifier *typed_;
1933 CYTypeDefinition(CYTypedIdentifier *typed) :
1938 virtual CYStatement *Replace(CYContext &context);
1939 virtual void Output(CYOutput &out, CYFlags flags) const;
1942 struct CYTypeBlockWith :
1945 CYTypedParameter *parameters_;
1947 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1948 CYTypeModifier(next),
1949 parameters_(parameters)
1955 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1956 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1959 struct CYTypeFunctionWith :
1962 CYTypedParameter *parameters_;
1964 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1965 CYTypeModifier(next),
1966 parameters_(parameters)
1972 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1973 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1975 virtual CYTypeFunctionWith *Function() { return this; }
1984 CYIdentifier *name_;
1987 Catch(CYIdentifier *name, CYStatement *code) :
1993 void Replace(CYContext &context);
1994 virtual void Output(CYOutput &out) const;
2002 CYFinally *finally_;
2004 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2011 virtual CYStatement *Replace(CYContext &context);
2012 virtual void Output(CYOutput &out, CYFlags flags) const;
2018 CYExpression *value_;
2020 Throw(CYExpression *value = NULL) :
2025 virtual CYStatement *Replace(CYContext &context);
2026 virtual void Output(CYOutput &out, CYFlags flags) const;
2034 CYExpression *scope_;
2037 CYWith(CYExpression *scope, CYStatement *code) :
2043 virtual CYStatement *Replace(CYContext &context);
2044 virtual void Output(CYOutput &out, CYFlags flags) const;
2050 CYExpression *value_;
2053 CYSwitch(CYExpression *value, CYClause *clauses) :
2059 virtual CYStatement *Replace(CYContext &context);
2060 virtual void Output(CYOutput &out, CYFlags flags) const;
2070 virtual CYStatement *Replace(CYContext &context);
2071 virtual void Output(CYOutput &out, CYFlags flags) const;
2074 struct CYCondition :
2077 CYExpression *test_;
2078 CYExpression *true_;
2079 CYExpression *false_;
2081 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2090 virtual CYExpression *Replace(CYContext &context);
2091 virtual void Output(CYOutput &out, CYFlags flags) const;
2094 struct CYAddressOf :
2097 CYAddressOf(CYExpression *rhs) :
2102 virtual const char *Operator() const {
2108 virtual CYExpression *Replace(CYContext &context);
2114 CYIndirect(CYExpression *rhs) :
2119 virtual const char *Operator() const {
2125 virtual CYExpression *Replace(CYContext &context);
2129 virtual CYExpression *Replace(CYContext &context);
2131 #define CYPostfix_(op, name, args...) \
2132 struct CY ## name : \
2135 CY ## name(CYExpression *lhs) : \
2140 virtual const char *Operator() const { \
2145 #define CYPrefix_(alphabetic, op, name, args...) \
2146 struct CY ## name : \
2149 CY ## name(CYExpression *rhs) : \
2154 CYAlphabetic(alphabetic) \
2156 virtual const char *Operator() const { \
2161 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2162 struct CY ## name : \
2165 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2170 CYAlphabetic(alphabetic) \
2171 CYPrecedence(precedence) \
2173 virtual const char *Operator() const { \
2178 #define CYAssignment_(op, name, args...) \
2179 struct CY ## name ## Assign : \
2182 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2183 CYAssignment(lhs, rhs) \
2187 virtual const char *Operator() const { \
2192 CYPostfix_("++", PostIncrement)
2193 CYPostfix_("--", PostDecrement)
2195 CYPrefix_(true, "delete", Delete)
2196 CYPrefix_(true, "void", Void)
2197 CYPrefix_(true, "typeof", TypeOf)
2198 CYPrefix_(false, "++", PreIncrement)
2199 CYPrefix_(false, "--", PreDecrement)
2200 CYPrefix_(false, "+", Affirm)
2201 CYPrefix_(false, "-", Negate)
2202 CYPrefix_(false, "~", BitwiseNot)
2203 CYPrefix_(false, "!", LogicalNot)
2205 CYInfix_(false, 5, "*", Multiply, CYReplace)
2206 CYInfix_(false, 5, "/", Divide)
2207 CYInfix_(false, 5, "%", Modulus)
2208 CYInfix_(false, 6, "+", Add, CYReplace)
2209 CYInfix_(false, 6, "-", Subtract)
2210 CYInfix_(false, 7, "<<", ShiftLeft)
2211 CYInfix_(false, 7, ">>", ShiftRightSigned)
2212 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2213 CYInfix_(false, 8, "<", Less)
2214 CYInfix_(false, 8, ">", Greater)
2215 CYInfix_(false, 8, "<=", LessOrEqual)
2216 CYInfix_(false, 8, ">=", GreaterOrEqual)
2217 CYInfix_(true, 8, "instanceof", InstanceOf)
2218 CYInfix_(true, 8, "in", In)
2219 CYInfix_(false, 9, "==", Equal)
2220 CYInfix_(false, 9, "!=", NotEqual)
2221 CYInfix_(false, 9, "===", Identical)
2222 CYInfix_(false, 9, "!==", NotIdentical)
2223 CYInfix_(false, 10, "&", BitwiseAnd)
2224 CYInfix_(false, 11, "^", BitwiseXOr)
2225 CYInfix_(false, 12, "|", BitwiseOr)
2226 CYInfix_(false, 13, "&&", LogicalAnd)
2227 CYInfix_(false, 14, "||", LogicalOr)
2229 CYAssignment_("=", )
2230 CYAssignment_("*=", Multiply)
2231 CYAssignment_("/=", Divide)
2232 CYAssignment_("%=", Modulus)
2233 CYAssignment_("+=", Add)
2234 CYAssignment_("-=", Subtract)
2235 CYAssignment_("<<=", ShiftLeft)
2236 CYAssignment_(">>=", ShiftRightSigned)
2237 CYAssignment_(">>>=", ShiftRightUnsigned)
2238 CYAssignment_("&=", BitwiseAnd)
2239 CYAssignment_("^=", BitwiseXOr)
2240 CYAssignment_("|=", BitwiseOr)
2242 #endif/*CYCRIPT_PARSER_HPP*/