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;
162 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
165 struct CYStatements {
175 operator CYStatement *() const {
179 CYStatements &operator ->*(CYStatement *next) {
181 if (first_ == NULL) {
184 } else for (;; last_ = last_->next_)
185 if (last_->next_ == NULL) {
195 virtual ~CYClassName() {
198 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
199 virtual void ClassName(CYOutput &out, bool object) const = 0;
209 CYWord(const char *word) :
214 void Set(const char *value) {
218 virtual const char *Word() const;
219 virtual void Output(CYOutput &out) const;
221 virtual CYExpression *ClassName(CYContext &context, bool object);
222 virtual void ClassName(CYOutput &out, bool object) const;
223 virtual void PropertyName(CYOutput &out) const;
226 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
228 return lhs << rhs.Word();
231 struct CYIdentifier :
232 CYNext<CYIdentifier>,
235 CYIdentifier *replace_;
239 CYIdentifier(const char *word) :
247 virtual const char *Word() const;
248 CYIdentifier *Replace(CYContext &context);
256 CYComment(const char *value) :
261 virtual CYStatement *Replace(CYContext &context);
262 virtual void Output(CYOutput &out, CYFlags flags) const;
269 CYStatement *statement_;
271 CYLabel(CYIdentifier *name, CYStatement *statement) :
273 statement_(statement)
277 virtual CYStatement *Replace(CYContext &context);
278 virtual void Output(CYOutput &out, CYFlags flags) const;
281 struct CYCStringLess :
282 std::binary_function<const char *, const char *, bool>
284 _finline bool operator ()(const char *lhs, const char *rhs) const {
285 return strcmp(lhs, rhs) < 0;
289 struct CYIdentifierValueLess :
290 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
292 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
293 return CYCStringLess()(lhs->Word(), rhs->Word());
297 enum CYIdentifierFlags {
298 CYIdentifierArgument,
299 CYIdentifierVariable,
305 typedef std::set<const char *, CYCStringLess> CYCStringSet;
306 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
307 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
309 struct CYIdentifierUsage {
310 CYIdentifier *identifier_;
314 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
320 CYIdentifierAddressFlagsMap internal_;
321 CYIdentifierValueSet identifiers_;
323 CYScope(bool transparent, CYContext &context);
325 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
326 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
327 void Merge(CYContext &context, CYIdentifier *identifier);
328 void Close(CYContext &context, CYStatement *&statements);
336 CYProgram(CYStatement *code) :
341 virtual void Replace(CYContext &context);
342 virtual void Output(CYOutput &out) const;
354 CYIdentifierUsageVector rename_;
356 CYNonLocal *nonlocal_;
357 CYNonLocal *nextlocal_;
360 CYContext(CYOptions &options) :
370 virtual ~CYContext() {
373 void ReplaceAll(CYStatement *&statement) {
374 if (statement == NULL)
376 CYStatement *next(statement->next_);
381 if (statement == NULL)
384 statement->SetNext(next);
387 template <typename Type_>
388 void Replace(Type_ *&value) {
389 for (;;) if (value == NULL)
392 Type_ *replace(value->Replace(*this));
393 if (replace != value)
399 void NonLocal(CYStatement *&statements);
400 CYIdentifier *Unique();
404 CYIdentifier *identifier_;
411 CYIdentifier *Target(CYContext &context) {
412 if (identifier_ == NULL)
413 identifier_ = context.Unique();
421 CYIdentifier *identifier_;
428 CYIdentifier *Identifier(CYContext &context) {
430 return next_->Identifier(context);
431 if (identifier_ == NULL)
432 identifier_ = context.Unique();
442 CYBlock(CYStatement *code) :
447 virtual CYStatement *Replace(CYContext &context);
449 virtual void Output(CYOutput &out, CYFlags flags) const;
452 struct CYForInitialiser {
453 virtual ~CYForInitialiser() {
456 virtual CYExpression *Replace(CYContext &context) = 0;
457 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
460 struct CYForInInitialiser {
461 virtual ~CYForInInitialiser() {
464 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
465 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
467 virtual CYExpression *Replace(CYContext &context) = 0;
468 virtual CYAssignment *Assignment(CYContext &context) = 0;
470 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
473 struct CYFunctionParameter;
478 struct CYExpression :
484 virtual int Precedence() const = 0;
486 virtual bool RightHand() const {
490 virtual void ForIn(CYOutput &out, CYFlags flags) const;
491 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
493 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
495 virtual void Output(CYOutput &out) const;
496 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
497 void Output(CYOutput &out, int precedence, CYFlags flags) const;
499 virtual CYExpression *ClassName(CYContext &context, bool object);
500 virtual void ClassName(CYOutput &out, bool object) const;
502 virtual CYExpression *Replace(CYContext &context) = 0;
503 virtual CYAssignment *Assignment(CYContext &context);
505 virtual CYExpression *Primitive(CYContext &context) {
509 virtual CYFunctionParameter *Parameter() const;
511 virtual CYNumber *Number(CYContext &context) {
515 virtual CYString *String(CYContext &context) {
519 virtual const char *Word() const {
524 #define CYAlphabetic(value) \
525 virtual bool Alphabetic() const { \
529 #define CYPrecedence(value) \
530 static const int Precedence_ = value; \
531 virtual int Precedence() const { \
532 return Precedence_; \
535 #define CYRightHand(value) \
536 virtual bool RightHand() const { \
543 CYExpression *expression_;
546 CYCompound(CYExpression *expression, CYExpression *next) :
547 expression_(expression),
550 _assert(expression_ != NULL);
551 _assert(next != NULL);
556 virtual CYExpression *Replace(CYContext &context);
557 void Output(CYOutput &out, CYFlags flags) const;
559 virtual CYFunctionParameter *Parameter() const;
562 struct CYParenthetical :
565 CYExpression *expression_;
567 CYParenthetical(CYExpression *expression) :
568 expression_(expression)
574 virtual CYExpression *Replace(CYContext &context);
575 void Output(CYOutput &out, CYFlags flags) const;
578 struct CYDeclaration;
580 struct CYFunctionParameter :
581 CYNext<CYFunctionParameter>,
584 CYForInInitialiser *initialiser_;
586 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
587 CYNext<CYFunctionParameter>(next),
588 initialiser_(initialiser)
592 void Replace(CYContext &context, CYStatement *&statements);
593 void Output(CYOutput &out) const;
596 struct CYComprehension :
597 CYNext<CYComprehension>,
600 CYComprehension(CYComprehension *next = NULL) :
601 CYNext<CYComprehension>(next)
605 CYComprehension *Modify(CYComprehension *next) {
610 virtual const char *Name() const = 0;
612 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
613 CYFunctionParameter *Parameters(CYContext &context) const;
614 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
615 virtual void Output(CYOutput &out) const = 0;
618 struct CYForInComprehension :
624 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
625 CYComprehension(next),
631 virtual const char *Name() const {
632 return name_->Word();
635 virtual CYFunctionParameter *Parameter(CYContext &context) const;
636 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
637 virtual void Output(CYOutput &out) const;
640 struct CYForOfComprehension :
646 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
647 CYComprehension(next),
653 virtual const char *Name() const {
654 return name_->Word();
657 virtual CYFunctionParameter *Parameter(CYContext &context) const;
658 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
659 virtual void Output(CYOutput &out) const;
662 struct CYIfComprehension :
667 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
668 CYComprehension(next),
673 virtual const char *Name() const {
677 virtual CYFunctionParameter *Parameter(CYContext &context) const;
678 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
679 virtual void Output(CYOutput &out) const;
682 struct CYArrayComprehension :
685 CYExpression *expression_;
686 CYComprehension *comprehensions_;
688 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
689 expression_(expression),
690 comprehensions_(comprehensions)
696 virtual CYExpression *Replace(CYContext &context);
697 virtual void Output(CYOutput &out, CYFlags flags) const;
706 virtual CYExpression *Primitive(CYContext &context) {
714 virtual CYExpression *Replace(CYContext &context);
728 CYRange(uint64_t lo, uint64_t hi) :
733 bool operator [](uint8_t value) const {
734 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
737 void operator()(uint8_t value) {
740 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
744 extern CYRange DigitRange_;
745 extern CYRange WordStartRange_;
746 extern CYRange WordEndRange_;
761 CYString(const char *value) :
767 CYString(const char *value, size_t size) :
773 CYString(const CYWord *word) :
774 value_(word->Word()),
775 size_(strlen(value_))
779 const char *Value() const {
783 virtual const char *Word() const;
785 virtual CYNumber *Number(CYContext &context);
786 virtual CYString *String(CYContext &context);
788 CYString *Concat(CYContext &out, CYString *rhs) const;
789 virtual void Output(CYOutput &out, CYFlags flags) const;
790 virtual void PropertyName(CYOutput &out) const;
799 CYNumber(double value) :
804 double Value() const {
808 virtual CYNumber *Number(CYContext &context);
809 virtual CYString *String(CYContext &context);
811 virtual void Output(CYOutput &out, CYFlags flags) const;
812 virtual void PropertyName(CYOutput &out) const;
820 CYRegEx(const char *value) :
825 const char *Value() const {
829 virtual void Output(CYOutput &out, CYFlags flags) const;
841 virtual CYNumber *Number(CYContext &context);
842 virtual CYString *String(CYContext &context);
844 virtual void Output(CYOutput &out, CYFlags flags) const;
856 virtual CYExpression *Replace(CYContext &context);
857 virtual void Output(CYOutput &out, CYFlags flags) const;
863 virtual bool Value() const = 0;
864 virtual void Output(CYOutput &out, CYFlags flags) const;
876 virtual bool Value() const {
880 virtual CYNumber *Number(CYContext &context);
881 virtual CYString *String(CYContext &context);
893 virtual bool Value() const {
897 virtual CYNumber *Number(CYContext &context);
898 virtual CYString *String(CYContext &context);
906 CYVariable(CYIdentifier *name) :
911 CYVariable(const char *name) :
912 name_(new($pool) CYIdentifier(name))
919 virtual CYExpression *Replace(CYContext &context);
920 virtual void Output(CYOutput &out, CYFlags flags) const;
922 virtual CYFunctionParameter *Parameter() const;
930 CYPrefix(CYExpression *rhs) :
935 virtual bool Alphabetic() const = 0;
936 virtual const char *Operator() const = 0;
940 virtual CYExpression *Replace(CYContext &context);
941 virtual void Output(CYOutput &out, CYFlags flags) const;
950 CYInfix(CYExpression *lhs, CYExpression *rhs) :
956 void SetLeft(CYExpression *lhs) {
960 virtual bool Alphabetic() const = 0;
961 virtual const char *Operator() const = 0;
963 virtual CYExpression *Replace(CYContext &context);
964 virtual void Output(CYOutput &out, CYFlags flags) const;
972 CYPostfix(CYExpression *lhs) :
977 virtual const char *Operator() const = 0;
981 virtual CYExpression *Replace(CYContext &context);
982 virtual void Output(CYOutput &out, CYFlags flags) const;
985 struct CYAssignment :
991 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
997 void SetLeft(CYExpression *lhs) {
1001 virtual const char *Operator() const = 0;
1005 virtual CYExpression *Replace(CYContext &context);
1006 virtual void Output(CYOutput &out, CYFlags flags) const;
1014 CYExpression *value_;
1016 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1017 CYNext<CYArgument>(next),
1023 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1024 CYNext<CYArgument>(next),
1030 CYArgument *Replace(CYContext &context);
1031 void Output(CYOutput &out) const;
1047 CYExpression *case_;
1050 CYClause(CYExpression *_case, CYStatement *code) :
1056 void Replace(CYContext &context);
1057 virtual void Output(CYOutput &out) const;
1064 CYExpression *value_;
1066 CYElement(CYExpression *value, CYElement *next) :
1067 CYNext<CYElement>(next),
1072 void Replace(CYContext &context);
1073 void Output(CYOutput &out) const;
1079 CYElement *elements_;
1081 CYArray(CYElement *elements = NULL) :
1086 virtual CYExpression *Replace(CYContext &context);
1087 virtual void Output(CYOutput &out, CYFlags flags) const;
1094 CYPropertyName *name_;
1095 CYExpression *value_;
1097 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1098 CYNext<CYProperty>(next),
1104 void Replace(CYContext &context);
1105 virtual void Output(CYOutput &out) const;
1108 struct CYDeclaration :
1111 CYIdentifier *identifier_;
1112 CYExpression *initialiser_;
1114 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1115 identifier_(identifier),
1116 initialiser_(initialiser)
1120 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1121 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1123 virtual CYExpression *Replace(CYContext &context);
1125 virtual CYAssignment *Assignment(CYContext &context);
1126 CYVariable *Variable(CYContext &context);
1128 virtual void Output(CYOutput &out, CYFlags flags) const;
1131 struct CYDeclarations :
1132 CYNext<CYDeclarations>,
1135 CYDeclaration *declaration_;
1137 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1138 CYNext<CYDeclarations>(next),
1139 declaration_(declaration)
1143 void Replace(CYContext &context);
1145 CYExpression *Expression(CYContext &context);
1146 CYProperty *Property(CYContext &context);
1147 CYArgument *Argument(CYContext &context);
1148 CYFunctionParameter *Parameter(CYContext &context);
1150 virtual void Output(CYOutput &out) const;
1151 virtual void Output(CYOutput &out, CYFlags flags) const;
1154 struct CYForDeclarations :
1157 CYDeclarations *declarations_;
1159 CYForDeclarations(CYDeclarations *declarations) :
1160 declarations_(declarations)
1164 virtual CYExpression *Replace(CYContext &context);
1165 virtual void Output(CYOutput &out, CYFlags flags) const;
1171 CYDeclarations *declarations_;
1173 CYVar(CYDeclarations *declarations) :
1174 declarations_(declarations)
1178 virtual CYStatement *Replace(CYContext &context);
1179 virtual void Output(CYOutput &out, CYFlags flags) const;
1182 struct CYLetStatement :
1185 CYDeclarations *declarations_;
1188 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1189 declarations_(declarations),
1194 virtual CYStatement *Replace(CYContext &context);
1195 virtual void Output(CYOutput &out, CYFlags flags) const;
1201 CYForInitialiser *initialiser_;
1202 CYExpression *test_;
1203 CYExpression *increment_;
1206 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1207 initialiser_(initialiser),
1209 increment_(increment),
1214 virtual CYStatement *Replace(CYContext &context);
1215 virtual void Output(CYOutput &out, CYFlags flags) const;
1221 CYForInInitialiser *initialiser_;
1225 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1226 initialiser_(initialiser),
1232 virtual CYStatement *Replace(CYContext &context);
1233 virtual void Output(CYOutput &out, CYFlags flags) const;
1239 CYForInInitialiser *initialiser_;
1243 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1244 initialiser_(initialiser),
1250 virtual CYStatement *Replace(CYContext &context);
1251 virtual void Output(CYOutput &out, CYFlags flags) const;
1257 CYProperty *properties_;
1259 CYObject(CYProperty *properties = NULL) :
1260 properties_(properties)
1264 virtual CYExpression *Replace(CYContext &context);
1265 void Output(CYOutput &out, CYFlags flags) const;
1271 CYExpression *object_;
1272 CYExpression *property_;
1274 CYMember(CYExpression *object, CYExpression *property) :
1280 void SetLeft(CYExpression *object) {
1285 struct CYDirectMember :
1288 CYDirectMember(CYExpression *object, CYExpression *property) :
1289 CYMember(object, property)
1296 virtual CYExpression *Replace(CYContext &context);
1297 virtual void Output(CYOutput &out, CYFlags flags) const;
1300 struct CYIndirectMember :
1303 CYIndirectMember(CYExpression *object, CYExpression *property) :
1304 CYMember(object, property)
1311 virtual CYExpression *Replace(CYContext &context);
1312 virtual void Output(CYOutput &out, CYFlags flags) const;
1321 CYExpression *constructor_;
1322 CYArgument *arguments_;
1324 New(CYExpression *constructor, CYArgument *arguments) :
1325 constructor_(constructor),
1326 arguments_(arguments)
1330 virtual int Precedence() const {
1331 return arguments_ == NULL ? 2 : 1;
1336 virtual CYExpression *Replace(CYContext &context);
1337 virtual void Output(CYOutput &out, CYFlags flags) const;
1339 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1347 CYExpression *function_;
1348 CYArgument *arguments_;
1350 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1351 function_(function),
1352 arguments_(arguments)
1359 virtual CYExpression *Replace(CYContext &context);
1360 virtual void Output(CYOutput &out, CYFlags flags) const;
1362 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1367 struct CYRubyBlock :
1370 CYExpression *call_;
1373 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1382 virtual CYExpression *Replace(CYContext &context);
1383 virtual void Output(CYOutput &out, CYFlags flags) const;
1389 CYExpression *test_;
1391 CYStatement *false_;
1393 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1400 virtual CYStatement *Replace(CYContext &context);
1401 virtual void Output(CYOutput &out, CYFlags flags) const;
1407 CYExpression *test_;
1410 CYDoWhile(CYExpression *test, CYStatement *code) :
1416 virtual CYStatement *Replace(CYContext &context);
1417 virtual void Output(CYOutput &out, CYFlags flags) const;
1423 CYExpression *test_;
1426 CYWhile(CYExpression *test, CYStatement *code) :
1432 virtual CYStatement *Replace(CYContext &context);
1433 virtual void Output(CYOutput &out, CYFlags flags) const;
1436 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1438 CYIdentifier *name_;
1439 CYFunctionParameter *parameters_;
1442 CYNonLocal *nonlocal_;
1445 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1447 parameters_(parameters),
1453 virtual ~CYFunction() {
1456 void Inject(CYContext &context);
1457 virtual void Replace_(CYContext &context, bool outer);
1458 virtual void Output(CYOutput &out, CYFlags flags) const;
1461 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1462 struct CYFunctionExpression :
1466 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1467 CYFunction(name, parameters, code)
1474 virtual CYExpression *Replace(CYContext &context);
1475 virtual void Output(CYOutput &out, CYFlags flags) const;
1478 // XXX: this should derive from CYAnonymousFunction
1483 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1484 CYFunction(NULL, parameters, code)
1491 virtual CYExpression *Replace(CYContext &context);
1492 virtual void Output(CYOutput &out, CYFlags flags) const;
1495 // XXX: this should derive from CYAnonymousFunctionExpression
1497 CYFunctionExpression
1499 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1500 CYFunctionExpression(NULL, parameters, code)
1504 virtual CYExpression *Replace(CYContext &context);
1505 virtual void Output(CYOutput &out, CYFlags flags) const;
1508 // XXX: this should derive from CYNamedFunction
1509 struct CYFunctionStatement :
1513 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1514 CYFunction(name, parameters, code)
1518 virtual CYStatement *Replace(CYContext &context);
1519 virtual void Output(CYOutput &out, CYFlags flags) const;
1525 CYExpression *expression_;
1527 CYExpress(CYExpression *expression) :
1528 expression_(expression)
1530 if (expression_ == NULL)
1534 virtual CYStatement *Replace(CYContext &context);
1535 virtual void Output(CYOutput &out, CYFlags flags) const;
1541 CYIdentifier *label_;
1543 CYContinue(CYIdentifier *label) :
1548 virtual CYStatement *Replace(CYContext &context);
1549 virtual void Output(CYOutput &out, CYFlags flags) const;
1555 CYIdentifier *label_;
1557 CYBreak(CYIdentifier *label) :
1562 virtual CYStatement *Replace(CYContext &context);
1563 virtual void Output(CYOutput &out, CYFlags flags) const;
1569 CYExpression *value_;
1571 CYReturn(CYExpression *value) :
1576 virtual CYStatement *Replace(CYContext &context);
1577 virtual void Output(CYOutput &out, CYFlags flags) const;
1583 virtual CYStatement *Replace(CYContext &context);
1584 virtual void Output(CYOutput &out, CYFlags flags) const;
1592 CYFinally(CYStatement *code) :
1597 void Replace(CYContext &context);
1598 virtual void Output(CYOutput &out) const;
1601 struct CYTypeSpecifier :
1604 virtual CYExpression *Replace(CYContext &context) = 0;
1607 struct CYTypeError :
1613 virtual CYExpression *Replace(CYContext &context);
1614 virtual void Output(CYOutput &out) const;
1623 virtual CYExpression *Replace(CYContext &context);
1624 virtual void Output(CYOutput &out) const;
1627 struct CYTypeVariable :
1630 CYIdentifier *name_;
1632 CYTypeVariable(CYIdentifier *name) :
1637 CYTypeVariable(const char *name) :
1638 name_(new($pool) CYIdentifier(name))
1642 virtual CYExpression *Replace(CYContext &context);
1643 virtual void Output(CYOutput &out) const;
1646 struct CYTypeUnsigned :
1649 CYTypeSpecifier *specifier_;
1651 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1652 specifier_(specifier)
1656 virtual CYExpression *Replace(CYContext &context);
1657 virtual void Output(CYOutput &out) const;
1660 struct CYTypeSigned :
1663 CYTypeSpecifier *specifier_;
1665 CYTypeSigned(CYTypeSpecifier *specifier) :
1666 specifier_(specifier)
1670 virtual CYExpression *Replace(CYContext &context);
1671 virtual void Output(CYOutput &out) const;
1677 CYTypeSpecifier *specifier_;
1679 CYTypeLong(CYTypeSpecifier *specifier) :
1680 specifier_(specifier)
1684 virtual CYExpression *Replace(CYContext &context);
1685 virtual void Output(CYOutput &out) const;
1688 struct CYTypeShort :
1691 CYTypeSpecifier *specifier_;
1693 CYTypeShort(CYTypeSpecifier *specifier) :
1694 specifier_(specifier)
1698 virtual CYExpression *Replace(CYContext &context);
1699 virtual void Output(CYOutput &out) const;
1702 struct CYTypeFunctionWith;
1704 struct CYTypeModifier :
1705 CYNext<CYTypeModifier>
1707 CYTypeModifier(CYTypeModifier *next) :
1708 CYNext<CYTypeModifier>(next)
1712 virtual int Precedence() const = 0;
1714 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1715 CYExpression *Replace(CYContext &context, CYExpression *type);
1717 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1718 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1720 virtual CYTypeFunctionWith *Function() { return NULL; }
1723 struct CYTypeArrayOf :
1726 CYExpression *size_;
1728 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1729 CYTypeModifier(next),
1736 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1737 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1740 struct CYTypeConstant :
1743 CYTypeConstant(CYTypeModifier *next = NULL) :
1744 CYTypeModifier(next)
1750 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1751 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1754 struct CYTypePointerTo :
1757 CYTypePointerTo(CYTypeModifier *next = NULL) :
1758 CYTypeModifier(next)
1764 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1765 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1768 struct CYTypeVolatile :
1771 CYTypeVolatile(CYTypeModifier *next = NULL) :
1772 CYTypeModifier(next)
1778 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1779 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1782 struct CYTypedIdentifier :
1783 CYNext<CYTypedIdentifier>,
1786 CYLocation location_;
1787 CYIdentifier *identifier_;
1788 CYTypeSpecifier *specifier_;
1789 CYTypeModifier *modifier_;
1791 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1792 location_(location),
1793 identifier_(identifier),
1799 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1801 specifier_(specifier),
1806 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1807 CYSetLast(modifier_) = modifier;
1811 virtual CYExpression *Replace(CYContext &context);
1812 virtual void Output(CYOutput &out) const;
1814 CYTypeFunctionWith *Function();
1817 struct CYEncodedType :
1820 CYTypedIdentifier *typed_;
1822 CYEncodedType(CYTypedIdentifier *typed) :
1829 virtual CYExpression *Replace(CYContext &context);
1830 virtual void Output(CYOutput &out, CYFlags flags) const;
1833 struct CYTypedParameter :
1834 CYNext<CYTypedParameter>,
1837 CYTypedIdentifier *typed_;
1839 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1840 CYNext<CYTypedParameter>(next),
1845 CYArgument *Argument(CYContext &context);
1846 CYFunctionParameter *Parameters(CYContext &context);
1847 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1849 virtual void Output(CYOutput &out) const;
1855 CYTypedIdentifier *typed_;
1856 CYTypedParameter *parameters_;
1859 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1861 parameters_(parameters),
1868 virtual CYExpression *Replace(CYContext &context);
1869 virtual void Output(CYOutput &out, CYFlags flags) const;
1878 CYModule(CYWord *part, CYModule *next = NULL) :
1879 CYNext<CYModule>(next),
1884 CYString *Replace(CYContext &context, const char *separator) const;
1885 void Output(CYOutput &out) const;
1893 CYImport(CYModule *module) :
1898 virtual CYStatement *Replace(CYContext &context);
1899 virtual void Output(CYOutput &out, CYFlags flags) const;
1906 CYTypedIdentifier *typed_;
1908 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1914 virtual CYStatement *Replace(CYContext &context);
1915 virtual void Output(CYOutput &out, CYFlags flags) const;
1918 struct CYTypeDefinition :
1921 CYTypedIdentifier *typed_;
1923 CYTypeDefinition(CYTypedIdentifier *typed) :
1928 virtual CYStatement *Replace(CYContext &context);
1929 virtual void Output(CYOutput &out, CYFlags flags) const;
1932 struct CYTypeBlockWith :
1935 CYTypedParameter *parameters_;
1937 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1938 CYTypeModifier(next),
1939 parameters_(parameters)
1945 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1946 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1949 struct CYTypeFunctionWith :
1952 CYTypedParameter *parameters_;
1954 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1955 CYTypeModifier(next),
1956 parameters_(parameters)
1962 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1963 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1965 virtual CYTypeFunctionWith *Function() { return this; }
1974 CYIdentifier *name_;
1977 Catch(CYIdentifier *name, CYStatement *code) :
1983 void Replace(CYContext &context);
1984 virtual void Output(CYOutput &out) const;
1992 CYFinally *finally_;
1994 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2001 virtual CYStatement *Replace(CYContext &context);
2002 virtual void Output(CYOutput &out, CYFlags flags) const;
2008 CYExpression *value_;
2010 Throw(CYExpression *value = NULL) :
2015 virtual CYStatement *Replace(CYContext &context);
2016 virtual void Output(CYOutput &out, CYFlags flags) const;
2024 CYExpression *scope_;
2027 CYWith(CYExpression *scope, CYStatement *code) :
2033 virtual CYStatement *Replace(CYContext &context);
2034 virtual void Output(CYOutput &out, CYFlags flags) const;
2040 CYExpression *value_;
2043 CYSwitch(CYExpression *value, CYClause *clauses) :
2049 virtual CYStatement *Replace(CYContext &context);
2050 virtual void Output(CYOutput &out, CYFlags flags) const;
2060 virtual CYStatement *Replace(CYContext &context);
2061 virtual void Output(CYOutput &out, CYFlags flags) const;
2064 struct CYCondition :
2067 CYExpression *test_;
2068 CYExpression *true_;
2069 CYExpression *false_;
2071 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2080 virtual CYExpression *Replace(CYContext &context);
2081 virtual void Output(CYOutput &out, CYFlags flags) const;
2084 struct CYAddressOf :
2087 CYAddressOf(CYExpression *rhs) :
2092 virtual const char *Operator() const {
2098 virtual CYExpression *Replace(CYContext &context);
2104 CYIndirect(CYExpression *rhs) :
2109 virtual const char *Operator() const {
2115 virtual CYExpression *Replace(CYContext &context);
2119 virtual CYExpression *Replace(CYContext &context);
2121 #define CYPostfix_(op, name, args...) \
2122 struct CY ## name : \
2125 CY ## name(CYExpression *lhs) : \
2130 virtual const char *Operator() const { \
2135 #define CYPrefix_(alphabetic, op, name, args...) \
2136 struct CY ## name : \
2139 CY ## name(CYExpression *rhs) : \
2144 CYAlphabetic(alphabetic) \
2146 virtual const char *Operator() const { \
2151 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2152 struct CY ## name : \
2155 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2160 CYAlphabetic(alphabetic) \
2161 CYPrecedence(precedence) \
2163 virtual const char *Operator() const { \
2168 #define CYAssignment_(op, name, args...) \
2169 struct CY ## name ## Assign : \
2172 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2173 CYAssignment(lhs, rhs) \
2177 virtual const char *Operator() const { \
2182 CYPostfix_("++", PostIncrement)
2183 CYPostfix_("--", PostDecrement)
2185 CYPrefix_(true, "delete", Delete)
2186 CYPrefix_(true, "void", Void)
2187 CYPrefix_(true, "typeof", TypeOf)
2188 CYPrefix_(false, "++", PreIncrement)
2189 CYPrefix_(false, "--", PreDecrement)
2190 CYPrefix_(false, "+", Affirm)
2191 CYPrefix_(false, "-", Negate)
2192 CYPrefix_(false, "~", BitwiseNot)
2193 CYPrefix_(false, "!", LogicalNot)
2195 CYInfix_(false, 5, "*", Multiply, CYReplace)
2196 CYInfix_(false, 5, "/", Divide)
2197 CYInfix_(false, 5, "%", Modulus)
2198 CYInfix_(false, 6, "+", Add, CYReplace)
2199 CYInfix_(false, 6, "-", Subtract)
2200 CYInfix_(false, 7, "<<", ShiftLeft)
2201 CYInfix_(false, 7, ">>", ShiftRightSigned)
2202 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2203 CYInfix_(false, 8, "<", Less)
2204 CYInfix_(false, 8, ">", Greater)
2205 CYInfix_(false, 8, "<=", LessOrEqual)
2206 CYInfix_(false, 8, ">=", GreaterOrEqual)
2207 CYInfix_(true, 8, "instanceof", InstanceOf)
2208 CYInfix_(true, 8, "in", In)
2209 CYInfix_(false, 9, "==", Equal)
2210 CYInfix_(false, 9, "!=", NotEqual)
2211 CYInfix_(false, 9, "===", Identical)
2212 CYInfix_(false, 9, "!==", NotIdentical)
2213 CYInfix_(false, 10, "&", BitwiseAnd)
2214 CYInfix_(false, 11, "^", BitwiseXOr)
2215 CYInfix_(false, 12, "|", BitwiseOr)
2216 CYInfix_(false, 13, "&&", LogicalAnd)
2217 CYInfix_(false, 14, "||", LogicalOr)
2219 CYAssignment_("=", )
2220 CYAssignment_("*=", Multiply)
2221 CYAssignment_("/=", Divide)
2222 CYAssignment_("%=", Modulus)
2223 CYAssignment_("+=", Add)
2224 CYAssignment_("-=", Subtract)
2225 CYAssignment_("<<=", ShiftLeft)
2226 CYAssignment_(">>=", ShiftRightSigned)
2227 CYAssignment_(">>>=", ShiftRightUnsigned)
2228 CYAssignment_("&=", BitwiseAnd)
2229 CYAssignment_("^=", BitwiseXOr)
2230 CYAssignment_("|=", BitwiseOr)
2232 #endif/*CYCRIPT_PARSER_HPP*/