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
35 #include "Location.hpp"
36 #include "Pooling.hpp"
37 #include "Options.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);
280 CYComment(const char *value) :
287 virtual CYStatement *Replace(CYContext &context);
288 virtual void Output(CYOutput &out, CYFlags flags) const;
295 CYStatement *statement_;
297 CYLabel(CYIdentifier *name, CYStatement *statement) :
299 statement_(statement)
305 virtual CYStatement *Replace(CYContext &context);
306 virtual void Output(CYOutput &out, CYFlags flags) const;
309 struct CYCStringLess :
310 std::binary_function<const char *, const char *, bool>
312 _finline bool operator ()(const char *lhs, const char *rhs) const {
313 return strcmp(lhs, rhs) < 0;
317 struct CYIdentifierValueLess :
318 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
320 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
321 return CYCStringLess()(lhs->Word(), rhs->Word());
325 enum CYIdentifierFlags {
326 CYIdentifierArgument,
327 CYIdentifierVariable,
333 typedef std::set<const char *, CYCStringLess> CYCStringSet;
334 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
335 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
337 struct CYIdentifierUsage {
338 CYIdentifier *identifier_;
342 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
348 CYIdentifierAddressFlagsMap internal_;
349 CYIdentifierValueSet identifiers_;
351 CYScope(bool transparent, CYContext &context);
353 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
354 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
355 void Merge(CYContext &context, CYIdentifier *identifier);
356 void Close(CYContext &context, CYStatement *&statements);
364 CYProgram(CYStatement *code) :
369 virtual void Replace(CYContext &context);
370 virtual void Output(CYOutput &out) const;
382 CYIdentifierUsageVector rename_;
384 CYNonLocal *nonlocal_;
385 CYNonLocal *nextlocal_;
388 CYContext(CYOptions &options) :
398 void ReplaceAll(CYStatement *&statement) {
399 if (statement == NULL)
401 CYStatement *next(statement->next_);
406 if (statement == NULL)
409 statement->SetNext(next);
412 template <typename Type_>
413 void Replace(Type_ *&value) {
414 for (;;) if (value == NULL)
417 Type_ *replace(value->Replace(*this));
418 if (replace != value)
424 void NonLocal(CYStatement *&statements);
425 CYIdentifier *Unique();
429 CYIdentifier *identifier_;
436 CYIdentifier *Target(CYContext &context) {
437 if (identifier_ == NULL)
438 identifier_ = context.Unique();
446 CYIdentifier *identifier_;
453 CYIdentifier *Identifier(CYContext &context) {
455 return next_->Identifier(context);
456 if (identifier_ == NULL)
457 identifier_ = context.Unique();
467 CYBlock(CYStatement *code) :
474 virtual CYStatement *Replace(CYContext &context);
476 virtual void Output(CYOutput &out, CYFlags flags) const;
478 virtual CYStatement *Return();
481 struct CYForInitialiser {
482 virtual CYExpression *Replace(CYContext &context) = 0;
483 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
486 struct CYForInInitialiser {
487 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
488 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
490 virtual CYExpression *Replace(CYContext &context) = 0;
491 virtual CYAssignment *Assignment(CYContext &context) = 0;
493 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
496 struct CYFunctionParameter;
501 struct CYExpression :
507 virtual int Precedence() const = 0;
509 virtual bool RightHand() const {
513 virtual void ForIn(CYOutput &out, CYFlags flags) const;
514 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
516 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
518 virtual void Output(CYOutput &out) const;
519 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
520 void Output(CYOutput &out, int precedence, CYFlags flags) const;
522 virtual CYExpression *ClassName(CYContext &context, bool object);
523 virtual void ClassName(CYOutput &out, bool object) const;
525 virtual CYExpression *Replace(CYContext &context) = 0;
526 virtual CYAssignment *Assignment(CYContext &context);
528 virtual CYExpression *Primitive(CYContext &context) {
532 virtual CYFunctionParameter *Parameter() const;
534 virtual CYNumber *Number(CYContext &context) {
538 virtual CYString *String(CYContext &context) {
542 virtual const char *Word() const {
547 #define CYAlphabetic(value) \
548 virtual bool Alphabetic() const { \
552 #define CYPrecedence(value) \
553 static const int Precedence_ = value; \
554 virtual int Precedence() const { \
555 return Precedence_; \
558 #define CYRightHand(value) \
559 virtual bool RightHand() const { \
566 CYExpression *expression_;
569 CYCompound(CYExpression *expression, CYExpression *next) :
570 expression_(expression),
573 _assert(expression_ != NULL);
574 _assert(next != NULL);
579 virtual CYExpression *Replace(CYContext &context);
580 void Output(CYOutput &out, CYFlags flags) const;
582 virtual CYFunctionParameter *Parameter() const;
585 struct CYParenthetical :
588 CYExpression *expression_;
590 CYParenthetical(CYExpression *expression) :
591 expression_(expression)
597 virtual CYExpression *Replace(CYContext &context);
598 void Output(CYOutput &out, CYFlags flags) const;
601 struct CYDeclaration;
603 struct CYFunctionParameter :
604 CYNext<CYFunctionParameter>,
607 CYForInInitialiser *initialiser_;
609 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
610 CYNext<CYFunctionParameter>(next),
611 initialiser_(initialiser)
615 void Replace(CYContext &context, CYStatement *&statements);
616 void Output(CYOutput &out) const;
619 struct CYComprehension :
620 CYNext<CYComprehension>,
623 CYComprehension(CYComprehension *next = NULL) :
624 CYNext<CYComprehension>(next)
628 CYComprehension *Modify(CYComprehension *next) {
633 virtual const char *Name() const = 0;
635 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
636 CYFunctionParameter *Parameters(CYContext &context) const;
637 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
638 virtual void Output(CYOutput &out) const = 0;
641 struct CYForInComprehension :
647 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
648 CYComprehension(next),
654 virtual const char *Name() const {
655 return name_->Word();
658 virtual CYFunctionParameter *Parameter(CYContext &context) const;
659 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
660 virtual void Output(CYOutput &out) const;
663 struct CYForOfComprehension :
669 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
670 CYComprehension(next),
676 virtual const char *Name() const {
677 return name_->Word();
680 virtual CYFunctionParameter *Parameter(CYContext &context) const;
681 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
682 virtual void Output(CYOutput &out) const;
685 struct CYIfComprehension :
690 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
691 CYComprehension(next),
696 virtual const char *Name() const {
700 virtual CYFunctionParameter *Parameter(CYContext &context) const;
701 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
702 virtual void Output(CYOutput &out) const;
705 struct CYArrayComprehension :
708 CYExpression *expression_;
709 CYComprehension *comprehensions_;
711 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
712 expression_(expression),
713 comprehensions_(comprehensions)
719 virtual CYExpression *Replace(CYContext &context);
720 virtual void Output(CYOutput &out, CYFlags flags) const;
729 virtual CYExpression *Primitive(CYContext &context) {
737 virtual CYExpression *Replace(CYContext &context);
751 CYRange(uint64_t lo, uint64_t hi) :
756 bool operator [](uint8_t value) const {
757 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
760 void operator()(uint8_t value) {
763 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
767 extern CYRange DigitRange_;
768 extern CYRange WordStartRange_;
769 extern CYRange WordEndRange_;
784 CYString(const char *value) :
790 CYString(const char *value, size_t size) :
796 CYString(const CYWord *word) :
797 value_(word->Word()),
798 size_(strlen(value_))
802 const char *Value() const {
806 virtual const char *Word() const;
808 virtual CYNumber *Number(CYContext &context);
809 virtual CYString *String(CYContext &context);
811 CYString *Concat(CYContext &out, CYString *rhs) const;
812 virtual void Output(CYOutput &out, CYFlags flags) const;
813 virtual void PropertyName(CYOutput &out) const;
822 CYNumber(double value) :
827 double Value() const {
831 virtual CYNumber *Number(CYContext &context);
832 virtual CYString *String(CYContext &context);
834 virtual void Output(CYOutput &out, CYFlags flags) const;
835 virtual void PropertyName(CYOutput &out) const;
843 CYRegEx(const char *value) :
848 const char *Value() const {
852 virtual void Output(CYOutput &out, CYFlags flags) const;
858 virtual CYNumber *Number(CYContext &context);
859 virtual CYString *String(CYContext &context);
861 virtual void Output(CYOutput &out, CYFlags flags) const;
867 virtual CYExpression *Replace(CYContext &context);
868 virtual void Output(CYOutput &out, CYFlags flags) const;
874 virtual bool Value() const = 0;
875 virtual void Output(CYOutput &out, CYFlags flags) const;
881 virtual bool Value() const {
885 virtual CYNumber *Number(CYContext &context);
886 virtual CYString *String(CYContext &context);
892 virtual bool Value() const {
896 virtual CYNumber *Number(CYContext &context);
897 virtual CYString *String(CYContext &context);
905 CYVariable(CYIdentifier *name) :
910 CYVariable(const char *name) :
911 name_(new($pool) CYIdentifier(name))
918 virtual CYExpression *Replace(CYContext &context);
919 virtual void Output(CYOutput &out, CYFlags flags) const;
921 virtual CYFunctionParameter *Parameter() const;
929 CYPrefix(CYExpression *rhs) :
934 virtual bool Alphabetic() const = 0;
935 virtual const char *Operator() const = 0;
939 virtual CYExpression *Replace(CYContext &context);
940 virtual void Output(CYOutput &out, CYFlags flags) const;
949 CYInfix(CYExpression *lhs, CYExpression *rhs) :
955 void SetLeft(CYExpression *lhs) {
959 virtual bool Alphabetic() const = 0;
960 virtual const char *Operator() const = 0;
962 virtual CYExpression *Replace(CYContext &context);
963 virtual void Output(CYOutput &out, CYFlags flags) const;
971 CYPostfix(CYExpression *lhs) :
976 virtual const char *Operator() const = 0;
980 virtual CYExpression *Replace(CYContext &context);
981 virtual void Output(CYOutput &out, CYFlags flags) const;
984 struct CYAssignment :
990 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
996 void SetLeft(CYExpression *lhs) {
1000 virtual const char *Operator() const = 0;
1004 virtual CYExpression *Replace(CYContext &context);
1005 virtual void Output(CYOutput &out, CYFlags flags) const;
1013 CYExpression *value_;
1015 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1016 CYNext<CYArgument>(next),
1022 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1023 CYNext<CYArgument>(next),
1029 CYArgument *Replace(CYContext &context);
1030 void Output(CYOutput &out) const;
1037 CYExpression *case_;
1040 CYClause(CYExpression *_case, CYStatement *code) :
1046 void Replace(CYContext &context);
1047 virtual void Output(CYOutput &out) const;
1054 CYExpression *value_;
1056 CYElement(CYExpression *value, CYElement *next) :
1057 CYNext<CYElement>(next),
1062 void Replace(CYContext &context);
1063 void Output(CYOutput &out) const;
1069 CYElement *elements_;
1071 CYArray(CYElement *elements = NULL) :
1076 virtual CYExpression *Replace(CYContext &context);
1077 virtual void Output(CYOutput &out, CYFlags flags) const;
1084 CYPropertyName *name_;
1085 CYExpression *value_;
1087 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1088 CYNext<CYProperty>(next),
1094 void Replace(CYContext &context);
1095 virtual void Output(CYOutput &out) const;
1098 struct CYDeclaration :
1101 CYIdentifier *identifier_;
1102 CYExpression *initialiser_;
1104 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1105 identifier_(identifier),
1106 initialiser_(initialiser)
1110 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1111 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1113 virtual CYExpression *Replace(CYContext &context);
1115 virtual CYAssignment *Assignment(CYContext &context);
1116 CYVariable *Variable(CYContext &context);
1118 virtual void Output(CYOutput &out, CYFlags flags) const;
1121 struct CYDeclarations :
1122 CYNext<CYDeclarations>,
1125 CYDeclaration *declaration_;
1127 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1128 CYNext<CYDeclarations>(next),
1129 declaration_(declaration)
1133 void Replace(CYContext &context);
1135 CYExpression *Expression(CYContext &context);
1136 CYProperty *Property(CYContext &context);
1137 CYArgument *Argument(CYContext &context);
1138 CYFunctionParameter *Parameter(CYContext &context);
1140 virtual void Output(CYOutput &out) const;
1141 virtual void Output(CYOutput &out, CYFlags flags) const;
1144 struct CYForDeclarations :
1147 CYDeclarations *declarations_;
1149 CYForDeclarations(CYDeclarations *declarations) :
1150 declarations_(declarations)
1154 virtual CYExpression *Replace(CYContext &context);
1155 virtual void Output(CYOutput &out, CYFlags flags) const;
1161 CYDeclarations *declarations_;
1163 CYVar(CYDeclarations *declarations) :
1164 declarations_(declarations)
1170 virtual CYStatement *Replace(CYContext &context);
1171 virtual void Output(CYOutput &out, CYFlags flags) const;
1174 struct CYLetStatement :
1177 CYDeclarations *declarations_;
1180 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1181 declarations_(declarations),
1188 virtual CYStatement *Replace(CYContext &context);
1189 virtual void Output(CYOutput &out, CYFlags flags) const;
1195 CYForInitialiser *initialiser_;
1196 CYExpression *test_;
1197 CYExpression *increment_;
1200 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1201 initialiser_(initialiser),
1203 increment_(increment),
1210 virtual CYStatement *Replace(CYContext &context);
1211 virtual void Output(CYOutput &out, CYFlags flags) const;
1217 CYForInInitialiser *initialiser_;
1221 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1222 initialiser_(initialiser),
1230 virtual CYStatement *Replace(CYContext &context);
1231 virtual void Output(CYOutput &out, CYFlags flags) const;
1237 CYForInInitialiser *initialiser_;
1241 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1242 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) :
1402 virtual CYStatement *Replace(CYContext &context);
1403 virtual void Output(CYOutput &out, CYFlags flags) const;
1405 virtual CYStatement *Return();
1411 CYExpression *test_;
1414 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) :
1440 virtual CYStatement *Replace(CYContext &context);
1441 virtual void Output(CYOutput &out, CYFlags flags) const;
1444 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1446 CYIdentifier *name_;
1447 CYFunctionParameter *parameters_;
1450 CYNonLocal *nonlocal_;
1454 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1456 parameters_(parameters),
1463 void Inject(CYContext &context);
1464 virtual void Replace_(CYContext &context, bool outer);
1465 virtual void Output(CYOutput &out, CYFlags flags) const;
1468 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1469 struct CYFunctionExpression :
1473 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1474 CYFunction(name, parameters, code)
1481 virtual CYExpression *Replace(CYContext &context);
1482 virtual void Output(CYOutput &out, CYFlags flags) const;
1485 // XXX: this should derive from CYAnonymousFunction
1490 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1491 CYFunction(NULL, parameters, code)
1498 virtual CYExpression *Replace(CYContext &context);
1499 virtual void Output(CYOutput &out, CYFlags flags) const;
1502 // XXX: this should derive from CYAnonymousFunctionExpression
1504 CYFunctionExpression
1506 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1507 CYFunctionExpression(NULL, parameters, code)
1511 virtual CYExpression *Replace(CYContext &context);
1512 virtual void Output(CYOutput &out, CYFlags flags) const;
1515 // XXX: this should derive from CYNamedFunction
1516 struct CYFunctionStatement :
1520 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1521 CYFunction(name, parameters, code)
1527 virtual CYStatement *Replace(CYContext &context);
1528 virtual void Output(CYOutput &out, CYFlags flags) const;
1534 CYExpression *expression_;
1536 CYExpress(CYExpression *expression) :
1537 expression_(expression)
1539 if (expression_ == NULL)
1545 virtual CYStatement *Replace(CYContext &context);
1546 virtual void Output(CYOutput &out, CYFlags flags) const;
1548 virtual CYStatement *Return();
1554 CYIdentifier *label_;
1556 CYContinue(CYIdentifier *label) :
1563 virtual CYStatement *Replace(CYContext &context);
1564 virtual void Output(CYOutput &out, CYFlags flags) const;
1570 CYIdentifier *label_;
1572 CYBreak(CYIdentifier *label) :
1579 virtual CYStatement *Replace(CYContext &context);
1580 virtual void Output(CYOutput &out, CYFlags flags) const;
1586 CYExpression *value_;
1588 CYReturn(CYExpression *value) :
1595 virtual CYStatement *Replace(CYContext &context);
1596 virtual void Output(CYOutput &out, CYFlags flags) const;
1604 virtual CYStatement *Replace(CYContext &context);
1605 virtual void Output(CYOutput &out, CYFlags flags) const;
1613 CYFinally(CYStatement *code) :
1618 void Replace(CYContext &context);
1619 virtual void Output(CYOutput &out) const;
1622 struct CYTypeSpecifier :
1625 virtual CYExpression *Replace(CYContext &context) = 0;
1628 struct CYTypeError :
1634 virtual CYExpression *Replace(CYContext &context);
1635 virtual void Output(CYOutput &out) const;
1644 virtual CYExpression *Replace(CYContext &context);
1645 virtual void Output(CYOutput &out) const;
1648 struct CYTypeVariable :
1651 CYIdentifier *name_;
1653 CYTypeVariable(CYIdentifier *name) :
1658 CYTypeVariable(const char *name) :
1659 name_(new($pool) CYIdentifier(name))
1663 virtual CYExpression *Replace(CYContext &context);
1664 virtual void Output(CYOutput &out) const;
1667 struct CYTypeUnsigned :
1670 CYTypeSpecifier *specifier_;
1672 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1673 specifier_(specifier)
1677 virtual CYExpression *Replace(CYContext &context);
1678 virtual void Output(CYOutput &out) const;
1681 struct CYTypeSigned :
1684 CYTypeSpecifier *specifier_;
1686 CYTypeSigned(CYTypeSpecifier *specifier) :
1687 specifier_(specifier)
1691 virtual CYExpression *Replace(CYContext &context);
1692 virtual void Output(CYOutput &out) const;
1698 CYTypeSpecifier *specifier_;
1700 CYTypeLong(CYTypeSpecifier *specifier) :
1701 specifier_(specifier)
1705 virtual CYExpression *Replace(CYContext &context);
1706 virtual void Output(CYOutput &out) const;
1709 struct CYTypeShort :
1712 CYTypeSpecifier *specifier_;
1714 CYTypeShort(CYTypeSpecifier *specifier) :
1715 specifier_(specifier)
1719 virtual CYExpression *Replace(CYContext &context);
1720 virtual void Output(CYOutput &out) const;
1723 struct CYTypeFunctionWith;
1725 struct CYTypeModifier :
1726 CYNext<CYTypeModifier>
1728 CYTypeModifier(CYTypeModifier *next) :
1729 CYNext<CYTypeModifier>(next)
1733 virtual int Precedence() const = 0;
1735 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1736 CYExpression *Replace(CYContext &context, CYExpression *type);
1738 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1739 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1741 virtual CYTypeFunctionWith *Function() { return NULL; }
1744 struct CYTypeArrayOf :
1747 CYExpression *size_;
1749 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1750 CYTypeModifier(next),
1757 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1758 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1761 struct CYTypeConstant :
1764 CYTypeConstant(CYTypeModifier *next = NULL) :
1765 CYTypeModifier(next)
1771 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1772 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1775 struct CYTypePointerTo :
1778 CYTypePointerTo(CYTypeModifier *next = NULL) :
1779 CYTypeModifier(next)
1785 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1786 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1789 struct CYTypeVolatile :
1792 CYTypeVolatile(CYTypeModifier *next = NULL) :
1793 CYTypeModifier(next)
1799 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1800 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1803 struct CYTypedIdentifier :
1804 CYNext<CYTypedIdentifier>,
1807 CYLocation location_;
1808 CYIdentifier *identifier_;
1809 CYTypeSpecifier *specifier_;
1810 CYTypeModifier *modifier_;
1812 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1813 location_(location),
1814 identifier_(identifier),
1820 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1822 specifier_(specifier),
1827 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1828 CYSetLast(modifier_) = modifier;
1832 virtual CYExpression *Replace(CYContext &context);
1833 virtual void Output(CYOutput &out) const;
1835 CYTypeFunctionWith *Function();
1838 struct CYEncodedType :
1841 CYTypedIdentifier *typed_;
1843 CYEncodedType(CYTypedIdentifier *typed) :
1850 virtual CYExpression *Replace(CYContext &context);
1851 virtual void Output(CYOutput &out, CYFlags flags) const;
1854 struct CYTypedParameter :
1855 CYNext<CYTypedParameter>,
1858 CYTypedIdentifier *typed_;
1860 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1861 CYNext<CYTypedParameter>(next),
1866 CYArgument *Argument(CYContext &context);
1867 CYFunctionParameter *Parameters(CYContext &context);
1868 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1870 virtual void Output(CYOutput &out) const;
1876 CYTypedIdentifier *typed_;
1877 CYTypedParameter *parameters_;
1880 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1882 parameters_(parameters),
1889 virtual CYExpression *Replace(CYContext &context);
1890 virtual void Output(CYOutput &out, CYFlags flags) const;
1899 CYModule(CYWord *part, CYModule *next = NULL) :
1900 CYNext<CYModule>(next),
1905 CYString *Replace(CYContext &context, const char *separator) const;
1906 void Output(CYOutput &out) const;
1914 CYImport(CYModule *module) :
1921 virtual CYStatement *Replace(CYContext &context);
1922 virtual void Output(CYOutput &out, CYFlags flags) const;
1929 CYTypedIdentifier *typed_;
1931 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1939 virtual CYStatement *Replace(CYContext &context);
1940 virtual void Output(CYOutput &out, CYFlags flags) const;
1943 struct CYTypeDefinition :
1946 CYTypedIdentifier *typed_;
1948 CYTypeDefinition(CYTypedIdentifier *typed) :
1955 virtual CYStatement *Replace(CYContext &context);
1956 virtual void Output(CYOutput &out, CYFlags flags) const;
1959 struct CYTypeBlockWith :
1962 CYTypedParameter *parameters_;
1964 CYTypeBlockWith(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;
1976 struct CYTypeFunctionWith :
1979 CYTypedParameter *parameters_;
1981 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1982 CYTypeModifier(next),
1983 parameters_(parameters)
1989 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1990 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1992 virtual CYTypeFunctionWith *Function() { return this; }
2001 CYIdentifier *name_;
2004 Catch(CYIdentifier *name, CYStatement *code) :
2010 void Replace(CYContext &context);
2011 virtual void Output(CYOutput &out) const;
2019 CYFinally *finally_;
2021 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2030 virtual CYStatement *Replace(CYContext &context);
2031 virtual void Output(CYOutput &out, CYFlags flags) const;
2037 CYExpression *value_;
2039 Throw(CYExpression *value = NULL) :
2046 virtual CYStatement *Replace(CYContext &context);
2047 virtual void Output(CYOutput &out, CYFlags flags) const;
2055 CYExpression *scope_;
2058 CYWith(CYExpression *scope, CYStatement *code) :
2066 virtual CYStatement *Replace(CYContext &context);
2067 virtual void Output(CYOutput &out, CYFlags flags) const;
2073 CYExpression *value_;
2076 CYSwitch(CYExpression *value, CYClause *clauses) :
2084 virtual CYStatement *Replace(CYContext &context);
2085 virtual void Output(CYOutput &out, CYFlags flags) const;
2097 virtual CYStatement *Replace(CYContext &context);
2098 virtual void Output(CYOutput &out, CYFlags flags) const;
2101 struct CYCondition :
2104 CYExpression *test_;
2105 CYExpression *true_;
2106 CYExpression *false_;
2108 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2117 virtual CYExpression *Replace(CYContext &context);
2118 virtual void Output(CYOutput &out, CYFlags flags) const;
2121 struct CYAddressOf :
2124 CYAddressOf(CYExpression *rhs) :
2129 virtual const char *Operator() const {
2135 virtual CYExpression *Replace(CYContext &context);
2141 CYIndirect(CYExpression *rhs) :
2146 virtual const char *Operator() const {
2152 virtual CYExpression *Replace(CYContext &context);
2156 virtual CYExpression *Replace(CYContext &context);
2158 #define CYPostfix_(op, name, args...) \
2159 struct CY ## name : \
2162 CY ## name(CYExpression *lhs) : \
2167 virtual const char *Operator() const { \
2172 #define CYPrefix_(alphabetic, op, name, args...) \
2173 struct CY ## name : \
2176 CY ## name(CYExpression *rhs) : \
2181 CYAlphabetic(alphabetic) \
2183 virtual const char *Operator() const { \
2188 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2189 struct CY ## name : \
2192 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2197 CYAlphabetic(alphabetic) \
2198 CYPrecedence(precedence) \
2200 virtual const char *Operator() const { \
2205 #define CYAssignment_(op, name, args...) \
2206 struct CY ## name ## Assign : \
2209 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2210 CYAssignment(lhs, rhs) \
2214 virtual const char *Operator() const { \
2219 CYPostfix_("++", PostIncrement)
2220 CYPostfix_("--", PostDecrement)
2222 CYPrefix_(true, "delete", Delete)
2223 CYPrefix_(true, "void", Void)
2224 CYPrefix_(true, "typeof", TypeOf)
2225 CYPrefix_(false, "++", PreIncrement)
2226 CYPrefix_(false, "--", PreDecrement)
2227 CYPrefix_(false, "+", Affirm)
2228 CYPrefix_(false, "-", Negate)
2229 CYPrefix_(false, "~", BitwiseNot)
2230 CYPrefix_(false, "!", LogicalNot)
2232 CYInfix_(false, 5, "*", Multiply, CYReplace)
2233 CYInfix_(false, 5, "/", Divide)
2234 CYInfix_(false, 5, "%", Modulus)
2235 CYInfix_(false, 6, "+", Add, CYReplace)
2236 CYInfix_(false, 6, "-", Subtract)
2237 CYInfix_(false, 7, "<<", ShiftLeft)
2238 CYInfix_(false, 7, ">>", ShiftRightSigned)
2239 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2240 CYInfix_(false, 8, "<", Less)
2241 CYInfix_(false, 8, ">", Greater)
2242 CYInfix_(false, 8, "<=", LessOrEqual)
2243 CYInfix_(false, 8, ">=", GreaterOrEqual)
2244 CYInfix_(true, 8, "instanceof", InstanceOf)
2245 CYInfix_(true, 8, "in", In)
2246 CYInfix_(false, 9, "==", Equal)
2247 CYInfix_(false, 9, "!=", NotEqual)
2248 CYInfix_(false, 9, "===", Identical)
2249 CYInfix_(false, 9, "!==", NotIdentical)
2250 CYInfix_(false, 10, "&", BitwiseAnd)
2251 CYInfix_(false, 11, "^", BitwiseXOr)
2252 CYInfix_(false, 12, "|", BitwiseOr)
2253 CYInfix_(false, 13, "&&", LogicalAnd)
2254 CYInfix_(false, 14, "||", LogicalOr)
2256 CYAssignment_("=", )
2257 CYAssignment_("*=", Multiply)
2258 CYAssignment_("/=", Divide)
2259 CYAssignment_("%=", Modulus)
2260 CYAssignment_("+=", Add)
2261 CYAssignment_("-=", Subtract)
2262 CYAssignment_("<<=", ShiftLeft)
2263 CYAssignment_(">>=", ShiftRightSigned)
2264 CYAssignment_(">>>=", ShiftRightUnsigned)
2265 CYAssignment_("&=", BitwiseAnd)
2266 CYAssignment_("^=", BitwiseXOr)
2267 CYAssignment_("|=", BitwiseOr)
2269 #endif/*CYCRIPT_PARSER_HPP*/