1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
36 #include "location.hh"
39 #include "Pooling.hpp"
40 #include "Options.hpp"
48 virtual void Output(struct CYOutput &out) const = 0;
66 CYOutput(std::ostream &out, CYOptions &options) :
76 void Check(char value);
79 CYOutput &operator <<(char rhs);
80 CYOutput &operator <<(const char *rhs);
82 _finline CYOutput &operator <<(const CYThing *rhs) {
88 _finline CYOutput &operator <<(const CYThing &rhs) {
94 struct CYPropertyName {
95 virtual void PropertyName(CYOutput &out) const = 0;
97 virtual ~CYPropertyName() {
112 CYNoBrace = (1 << 0),
113 CYNoFunction = (1 << 1),
116 CYNoRightHand = (1 << 4),
117 CYNoDangle = (1 << 5),
118 CYNoInteger = (1 << 6),
119 CYNoBF = (CYNoBrace | CYNoFunction),
122 _finline CYFlags operator ~(CYFlags rhs) {
123 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
126 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
127 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
130 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
131 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
134 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
135 return lhs = lhs | rhs;
138 _finline CYFlags CYLeft(CYFlags flags) {
139 return flags & ~(CYNoDangle | CYNoInteger);
142 _finline CYFlags CYRight(CYFlags flags) {
143 return flags & ~CYNoBF;
146 _finline CYFlags CYCenter(CYFlags flags) {
147 return CYLeft(CYRight(flags));
153 virtual ~CYStatement() {
156 void Single(CYOutput &out, CYFlags flags) const;
157 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) 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 CYStatement *&statements_;
324 CYIdentifierAddressFlagsMap internal_;
325 CYIdentifierValueSet identifiers_;
327 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
332 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
333 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
334 void Merge(CYContext &context, CYIdentifier *identifier);
335 void Scope(CYContext &context, CYStatement *&statements);
341 CYStatement *statements_;
343 CYProgram(CYStatement *statements) :
344 statements_(statements)
348 virtual void Replace(CYContext &context);
349 virtual void Output(CYOutput &out) const;
361 CYIdentifierUsageVector rename_;
363 CYNonLocal *nonlocal_;
364 CYNonLocal *nextlocal_;
367 CYContext(CYOptions &options) :
377 virtual ~CYContext() {
380 template <typename Type_>
381 void ReplaceAll(Type_ *&values) {
382 Type_ **last(&values);
383 CYForEach (next, values) {
384 Replace(*last = next);
386 last = &(*last)->next_;
390 template <typename Type_>
391 void Replace(Type_ *&value) {
392 for (;;) if (value == NULL)
395 Type_ *replace(value->Replace(*this));
396 if (replace != value)
402 void NonLocal(CYStatement *&statements);
403 CYIdentifier *Unique();
407 CYIdentifier *identifier_;
414 CYIdentifier *Target(CYContext &context) {
415 if (identifier_ == NULL)
416 identifier_ = context.Unique();
424 CYIdentifier *identifier_;
431 CYIdentifier *Identifier(CYContext &context) {
433 return next_->Identifier(context);
434 if (identifier_ == NULL)
435 identifier_ = context.Unique();
444 CYStatement *statements_;
446 CYBlock(CYStatement *statements) :
447 statements_(statements)
451 operator CYStatement *() const {
455 void AddPrev(CYStatement *statement) {
456 CYSetLast(statement) = statements_;
457 statements_ = statement;
460 virtual CYStatement *Replace(CYContext &context);
462 virtual void Output(CYOutput &out) const;
463 virtual void Output(CYOutput &out, CYFlags flags) const;
477 public std::streambuf
480 CYBuffer(const char *start, const char *end) {
481 setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
486 CYStream(const char *start, const char *end) :
487 std::istream(&buffer_),
498 std::stack<bool> in_;
501 bool AtImplementation;
516 std::string filename_;
520 cy::location location_;
521 std::string message_;
524 typedef std::vector<Error> Errors;
532 CYExpression *context_;
534 Context(CYExpression *context) :
539 typedef std::vector<CYWord *> Words;
543 typedef std::vector<Context> Contexts;
546 CYExpression *context_;
558 void ScannerDestroy();
561 CYDriver(std::istream &data, const std::string &filename = "");
564 Condition GetCondition();
565 void SetCondition(Condition condition);
567 void PushCondition(Condition condition);
570 void Warning(const cy::location &location, const char *message);
573 struct CYForInitialiser {
574 virtual ~CYForInitialiser() {
577 virtual CYExpression *Replace(CYContext &context) = 0;
578 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
581 struct CYForInInitialiser {
582 virtual ~CYForInInitialiser() {
585 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
586 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
588 virtual CYExpression *Replace(CYContext &context) = 0;
589 virtual CYAssignment *Assignment(CYContext &context) = 0;
591 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
597 struct CYExpression :
598 CYNext<CYExpression>,
604 virtual unsigned Precedence() const = 0;
606 virtual bool RightHand() const {
610 virtual void ForIn(CYOutput &out, CYFlags flags) const;
611 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
613 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
615 virtual void Output(CYOutput &out) const;
616 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
617 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
619 virtual CYExpression *ClassName(CYContext &context, bool object);
620 virtual void ClassName(CYOutput &out, bool object) const;
622 virtual CYExpression *Replace(CYContext &context) = 0;
623 virtual CYAssignment *Assignment(CYContext &context);
625 virtual CYExpression *Primitive(CYContext &context) {
629 virtual CYNumber *Number(CYContext &context) {
633 virtual CYString *String(CYContext &context) {
637 virtual const char *Word() const {
642 #define CYAlphabetic(value) \
643 virtual bool Alphabetic() const { \
647 #define CYPrecedence(value) \
648 static const unsigned Precedence_ = value; \
649 virtual unsigned Precedence() const { \
650 return Precedence_; \
653 #define CYRightHand(value) \
654 virtual bool RightHand() const { \
661 CYExpression *expressions_;
663 CYCompound(CYExpression *expressions = NULL) :
664 expressions_(expressions)
668 void AddPrev(CYExpression *expression) {
669 CYSetLast(expression) = expressions_;
670 expressions_ = expression;
675 virtual CYExpression *Replace(CYContext &context);
676 void Output(CYOutput &out, CYFlags flags) const;
678 virtual CYExpression *Primitive(CYContext &context);
681 struct CYDeclaration;
683 struct CYFunctionParameter :
684 CYNext<CYFunctionParameter>,
687 CYForInInitialiser *initialiser_;
689 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
690 CYNext<CYFunctionParameter>(next),
691 initialiser_(initialiser)
695 void Replace(CYContext &context, CYBlock &code);
696 void Output(CYOutput &out) const;
699 struct CYComprehension :
700 CYNext<CYComprehension>,
703 CYComprehension(CYComprehension *next = NULL) :
704 CYNext<CYComprehension>(next)
708 virtual const char *Name() const = 0;
710 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
711 CYFunctionParameter *Parameters(CYContext &context) const;
712 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
713 virtual void Output(CYOutput &out) const = 0;
716 struct CYForInComprehension :
722 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
723 CYComprehension(next),
729 virtual const char *Name() const {
730 return name_->Word();
733 virtual CYFunctionParameter *Parameter(CYContext &context) const;
734 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
735 virtual void Output(CYOutput &out) const;
738 struct CYForOfComprehension :
744 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
745 CYComprehension(next),
751 virtual const char *Name() const {
752 return name_->Word();
755 virtual CYFunctionParameter *Parameter(CYContext &context) const;
756 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
757 virtual void Output(CYOutput &out) const;
760 struct CYIfComprehension :
765 CYIfComprehension(CYExpression *test) :
770 virtual const char *Name() const {
774 virtual CYFunctionParameter *Parameter(CYContext &context) const;
775 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
776 virtual void Output(CYOutput &out) const;
779 struct CYArrayComprehension :
782 CYExpression *expression_;
783 CYComprehension *comprehensions_;
785 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
786 expression_(expression),
787 comprehensions_(comprehensions)
793 virtual CYExpression *Replace(CYContext &context);
794 virtual void Output(CYOutput &out, CYFlags flags) const;
807 virtual CYExpression *Replace(CYContext &context);
821 CYRange(uint64_t lo, uint64_t hi) :
826 bool operator [](uint8_t value) const {
827 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
830 void operator()(uint8_t value) {
833 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
837 extern CYRange DigitRange_;
838 extern CYRange WordStartRange_;
839 extern CYRange WordEndRange_;
854 CYString(const char *value) :
860 CYString(const char *value, size_t size) :
866 CYString(const CYWord *word) :
867 value_(word->Word()),
868 size_(strlen(value_))
872 const char *Value() const {
876 virtual const char *Word() const;
878 virtual CYNumber *Number(CYContext &context);
879 virtual CYString *String(CYContext &context);
881 CYString *Concat(CYContext &out, CYString *rhs) const;
882 virtual void Output(CYOutput &out, CYFlags flags) const;
883 virtual void PropertyName(CYOutput &out) const;
892 CYNumber(double value) :
897 double Value() const {
901 virtual CYNumber *Number(CYContext &context);
902 virtual CYString *String(CYContext &context);
904 virtual void Output(CYOutput &out, CYFlags flags) const;
905 virtual void PropertyName(CYOutput &out) const;
913 CYRegEx(const char *value) :
918 const char *Value() const {
922 virtual void Output(CYOutput &out, CYFlags flags) const;
934 virtual CYNumber *Number(CYContext &context);
935 virtual CYString *String(CYContext &context);
937 virtual void Output(CYOutput &out, CYFlags flags) const;
949 virtual CYExpression *Replace(CYContext &context);
950 virtual void Output(CYOutput &out, CYFlags flags) const;
956 virtual bool Value() const = 0;
957 virtual void Output(CYOutput &out, CYFlags flags) const;
969 virtual bool Value() const {
973 virtual CYNumber *Number(CYContext &context);
974 virtual CYString *String(CYContext &context);
986 virtual bool Value() const {
990 virtual CYNumber *Number(CYContext &context);
991 virtual CYString *String(CYContext &context);
999 CYVariable(CYIdentifier *name) :
1004 CYVariable(const char *name) :
1005 name_(new($pool) CYIdentifier(name))
1012 virtual CYExpression *Replace(CYContext &context);
1013 virtual void Output(CYOutput &out, CYFlags flags) const;
1021 CYPrefix(CYExpression *rhs) :
1026 virtual bool Alphabetic() const = 0;
1027 virtual const char *Operator() const = 0;
1031 virtual CYExpression *Replace(CYContext &context);
1032 virtual void Output(CYOutput &out, CYFlags flags) const;
1041 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1047 void SetLeft(CYExpression *lhs) {
1051 virtual bool Alphabetic() const = 0;
1052 virtual const char *Operator() const = 0;
1054 virtual CYExpression *Replace(CYContext &context);
1055 virtual void Output(CYOutput &out, CYFlags flags) const;
1063 CYPostfix(CYExpression *lhs) :
1068 virtual const char *Operator() const = 0;
1072 virtual CYExpression *Replace(CYContext &context);
1073 virtual void Output(CYOutput &out, CYFlags flags) const;
1076 struct CYAssignment :
1082 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1088 void SetLeft(CYExpression *lhs) {
1092 virtual const char *Operator() const = 0;
1096 virtual CYExpression *Replace(CYContext &context);
1097 virtual void Output(CYOutput &out, CYFlags flags) const;
1105 CYExpression *value_;
1107 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1108 CYNext<CYArgument>(next),
1114 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1115 CYNext<CYArgument>(next),
1121 CYArgument *Replace(CYContext &context);
1122 void Output(CYOutput &out) const;
1138 CYExpression *case_;
1139 CYStatement *statements_;
1141 CYClause(CYExpression *_case, CYStatement *statements) :
1143 statements_(statements)
1147 void Replace(CYContext &context);
1148 virtual void Output(CYOutput &out) const;
1155 CYExpression *value_;
1157 CYElement(CYExpression *value, CYElement *next) :
1158 CYNext<CYElement>(next),
1163 void Replace(CYContext &context);
1164 void Output(CYOutput &out) const;
1170 CYElement *elements_;
1172 CYArray(CYElement *elements = NULL) :
1177 virtual CYExpression *Replace(CYContext &context);
1178 virtual void Output(CYOutput &out, CYFlags flags) const;
1185 CYPropertyName *name_;
1186 CYExpression *value_;
1188 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1189 CYNext<CYProperty>(next),
1195 void Replace(CYContext &context);
1196 virtual void Output(CYOutput &out) const;
1199 struct CYDeclaration :
1202 CYIdentifier *identifier_;
1203 CYExpression *initialiser_;
1205 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1206 identifier_(identifier),
1207 initialiser_(initialiser)
1211 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1212 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1214 virtual CYExpression *Replace(CYContext &context);
1216 virtual CYAssignment *Assignment(CYContext &context);
1217 CYVariable *Variable(CYContext &context);
1219 virtual void Output(CYOutput &out, CYFlags flags) const;
1222 struct CYDeclarations :
1223 CYNext<CYDeclarations>,
1226 CYDeclaration *declaration_;
1228 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1229 CYNext<CYDeclarations>(next),
1230 declaration_(declaration)
1234 void Replace(CYContext &context);
1236 CYCompound *Compound(CYContext &context);
1237 CYProperty *Property(CYContext &context);
1238 CYArgument *Argument(CYContext &context);
1239 CYFunctionParameter *Parameter(CYContext &context);
1241 virtual void Output(CYOutput &out) const;
1242 virtual void Output(CYOutput &out, CYFlags flags) const;
1245 struct CYForDeclarations :
1248 CYDeclarations *declarations_;
1250 CYForDeclarations(CYDeclarations *declarations) :
1251 declarations_(declarations)
1255 virtual CYCompound *Replace(CYContext &context);
1256 virtual void Output(CYOutput &out, CYFlags flags) const;
1262 CYDeclarations *declarations_;
1264 CYVar(CYDeclarations *declarations) :
1265 declarations_(declarations)
1269 virtual CYStatement *Replace(CYContext &context);
1270 virtual void Output(CYOutput &out, CYFlags flags) const;
1273 struct CYLetStatement :
1276 CYDeclarations *declarations_;
1279 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1280 declarations_(declarations),
1285 virtual CYStatement *Replace(CYContext &context);
1286 virtual void Output(CYOutput &out, CYFlags flags) const;
1292 CYForInitialiser *initialiser_;
1293 CYExpression *test_;
1294 CYExpression *increment_;
1297 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1298 initialiser_(initialiser),
1300 increment_(increment),
1305 virtual CYStatement *Replace(CYContext &context);
1306 virtual void Output(CYOutput &out, CYFlags flags) const;
1312 CYForInInitialiser *initialiser_;
1316 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1317 initialiser_(initialiser),
1323 virtual CYStatement *Replace(CYContext &context);
1324 virtual void Output(CYOutput &out, CYFlags flags) const;
1330 CYForInInitialiser *initialiser_;
1334 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1335 initialiser_(initialiser),
1341 virtual CYStatement *Replace(CYContext &context);
1342 virtual void Output(CYOutput &out, CYFlags flags) const;
1348 CYProperty *properties_;
1350 CYObject(CYProperty *properties = NULL) :
1351 properties_(properties)
1355 virtual CYExpression *Replace(CYContext &context);
1356 void Output(CYOutput &out, CYFlags flags) const;
1362 CYExpression *object_;
1363 CYExpression *property_;
1365 CYMember(CYExpression *object, CYExpression *property) :
1371 void SetLeft(CYExpression *object) {
1376 struct CYDirectMember :
1379 CYDirectMember(CYExpression *object, CYExpression *property) :
1380 CYMember(object, property)
1387 virtual CYExpression *Replace(CYContext &context);
1388 virtual void Output(CYOutput &out, CYFlags flags) const;
1391 struct CYIndirectMember :
1394 CYIndirectMember(CYExpression *object, CYExpression *property) :
1395 CYMember(object, property)
1402 virtual CYExpression *Replace(CYContext &context);
1403 virtual void Output(CYOutput &out, CYFlags flags) const;
1412 CYExpression *constructor_;
1413 CYArgument *arguments_;
1415 New(CYExpression *constructor, CYArgument *arguments) :
1416 constructor_(constructor),
1417 arguments_(arguments)
1421 virtual unsigned Precedence() const {
1422 return arguments_ == NULL ? 2 : 1;
1427 virtual CYExpression *Replace(CYContext &context);
1428 virtual void Output(CYOutput &out, CYFlags flags) const;
1430 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1438 CYExpression *function_;
1439 CYArgument *arguments_;
1441 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1442 function_(function),
1443 arguments_(arguments)
1450 virtual CYExpression *Replace(CYContext &context);
1451 virtual void Output(CYOutput &out, CYFlags flags) const;
1453 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1458 struct CYRubyBlock :
1461 CYExpression *call_;
1464 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1473 virtual CYExpression *Replace(CYContext &context);
1474 virtual void Output(CYOutput &out, CYFlags flags) const;
1480 CYExpression *test_;
1482 CYStatement *false_;
1484 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1491 virtual CYStatement *Replace(CYContext &context);
1492 virtual void Output(CYOutput &out, CYFlags flags) const;
1498 CYExpression *test_;
1501 CYDoWhile(CYExpression *test, CYStatement *code) :
1507 virtual CYStatement *Replace(CYContext &context);
1508 virtual void Output(CYOutput &out, CYFlags flags) const;
1514 CYExpression *test_;
1517 CYWhile(CYExpression *test, CYStatement *code) :
1523 virtual CYStatement *Replace(CYContext &context);
1524 virtual void Output(CYOutput &out, CYFlags flags) const;
1527 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1529 CYIdentifier *name_;
1530 CYFunctionParameter *parameters_;
1533 CYNonLocal *nonlocal_;
1536 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1538 parameters_(parameters),
1544 virtual ~CYFunction() {
1547 void Inject(CYContext &context);
1548 virtual void Replace_(CYContext &context, bool outer);
1549 virtual void Output(CYOutput &out, CYFlags flags) const;
1552 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1553 struct CYFunctionExpression :
1557 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1558 CYFunction(name, parameters, statements)
1565 virtual CYExpression *Replace(CYContext &context);
1566 virtual void Output(CYOutput &out, CYFlags flags) const;
1569 // XXX: this should derive from CYAnonymousFunction
1574 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1575 CYFunction(NULL, parameters, statements)
1582 virtual CYExpression *Replace(CYContext &context);
1583 virtual void Output(CYOutput &out, CYFlags flags) const;
1586 // XXX: this should derive from CYAnonymousFunctionExpression
1588 CYFunctionExpression
1590 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1591 CYFunctionExpression(NULL, parameters, statements)
1595 virtual CYExpression *Replace(CYContext &context);
1596 virtual void Output(CYOutput &out, CYFlags flags) const;
1599 // XXX: this should derive from CYNamedFunction
1600 struct CYFunctionStatement :
1604 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1605 CYFunction(name, parameters, statements)
1609 virtual CYStatement *Replace(CYContext &context);
1610 virtual void Output(CYOutput &out, CYFlags flags) const;
1616 CYExpression *expression_;
1618 CYExpress(CYExpression *expression) :
1619 expression_(expression)
1621 if (expression == NULL)
1625 virtual CYStatement *Replace(CYContext &context);
1626 virtual void Output(CYOutput &out, CYFlags flags) const;
1632 CYIdentifier *label_;
1634 CYContinue(CYIdentifier *label) :
1639 virtual CYStatement *Replace(CYContext &context);
1640 virtual void Output(CYOutput &out, CYFlags flags) const;
1646 CYIdentifier *label_;
1648 CYBreak(CYIdentifier *label) :
1653 virtual CYStatement *Replace(CYContext &context);
1654 virtual void Output(CYOutput &out, CYFlags flags) const;
1660 CYExpression *value_;
1662 CYReturn(CYExpression *value) :
1667 virtual CYStatement *Replace(CYContext &context);
1668 virtual void Output(CYOutput &out, CYFlags flags) const;
1674 virtual CYStatement *Replace(CYContext &context);
1675 virtual void Output(CYOutput &out, CYFlags flags) const;
1683 CYFinally(CYStatement *statements) :
1688 void Replace(CYContext &context);
1689 virtual void Output(CYOutput &out) const;
1698 CYIdentifier *name_;
1701 Catch(CYIdentifier *name, CYStatement *statements) :
1707 void Replace(CYContext &context);
1708 virtual void Output(CYOutput &out) const;
1716 CYFinally *finally_;
1718 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1725 virtual CYStatement *Replace(CYContext &context);
1726 virtual void Output(CYOutput &out, CYFlags flags) const;
1732 CYExpression *value_;
1734 Throw(CYExpression *value = NULL) :
1739 virtual CYStatement *Replace(CYContext &context);
1740 virtual void Output(CYOutput &out, CYFlags flags) const;
1748 CYExpression *scope_;
1751 CYWith(CYExpression *scope, CYStatement *code) :
1757 virtual CYStatement *Replace(CYContext &context);
1758 virtual void Output(CYOutput &out, CYFlags flags) const;
1764 CYExpression *value_;
1767 CYSwitch(CYExpression *value, CYClause *clauses) :
1773 virtual CYStatement *Replace(CYContext &context);
1774 virtual void Output(CYOutput &out, CYFlags flags) const;
1784 virtual CYStatement *Replace(CYContext &context);
1785 virtual void Output(CYOutput &out, CYFlags flags) const;
1788 struct CYCondition :
1791 CYExpression *test_;
1792 CYExpression *true_;
1793 CYExpression *false_;
1795 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1804 virtual CYExpression *Replace(CYContext &context);
1805 virtual void Output(CYOutput &out, CYFlags flags) const;
1808 struct CYAddressOf :
1811 CYAddressOf(CYExpression *rhs) :
1816 virtual const char *Operator() const {
1822 virtual CYExpression *Replace(CYContext &context);
1828 CYIndirect(CYExpression *rhs) :
1833 virtual const char *Operator() const {
1839 virtual CYExpression *Replace(CYContext &context);
1843 virtual CYExpression *Replace(CYContext &context);
1845 #define CYPostfix_(op, name, args...) \
1846 struct CY ## name : \
1849 CY ## name(CYExpression *lhs) : \
1854 virtual const char *Operator() const { \
1859 #define CYPrefix_(alphabetic, op, name, args...) \
1860 struct CY ## name : \
1863 CY ## name(CYExpression *rhs) : \
1868 CYAlphabetic(alphabetic) \
1870 virtual const char *Operator() const { \
1875 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1876 struct CY ## name : \
1879 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1884 CYAlphabetic(alphabetic) \
1885 CYPrecedence(precedence) \
1887 virtual const char *Operator() const { \
1892 #define CYAssignment_(op, name, args...) \
1893 struct CY ## name ## Assign : \
1896 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1897 CYAssignment(lhs, rhs) \
1901 virtual const char *Operator() const { \
1906 CYPostfix_("++", PostIncrement)
1907 CYPostfix_("--", PostDecrement)
1909 CYPrefix_(true, "delete", Delete)
1910 CYPrefix_(true, "void", Void)
1911 CYPrefix_(true, "typeof", TypeOf)
1912 CYPrefix_(false, "++", PreIncrement)
1913 CYPrefix_(false, "--", PreDecrement)
1914 CYPrefix_(false, "+", Affirm)
1915 CYPrefix_(false, "-", Negate)
1916 CYPrefix_(false, "~", BitwiseNot)
1917 CYPrefix_(false, "!", LogicalNot)
1919 CYInfix_(false, 5, "*", Multiply)
1920 CYInfix_(false, 5, "/", Divide)
1921 CYInfix_(false, 5, "%", Modulus)
1922 CYInfix_(false, 6, "+", Add, CYReplace)
1923 CYInfix_(false, 6, "-", Subtract)
1924 CYInfix_(false, 7, "<<", ShiftLeft)
1925 CYInfix_(false, 7, ">>", ShiftRightSigned)
1926 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1927 CYInfix_(false, 8, "<", Less)
1928 CYInfix_(false, 8, ">", Greater)
1929 CYInfix_(false, 8, "<=", LessOrEqual)
1930 CYInfix_(false, 8, ">=", GreaterOrEqual)
1931 CYInfix_(true, 8, "instanceof", InstanceOf)
1932 CYInfix_(true, 8, "in", In)
1933 CYInfix_(false, 9, "==", Equal)
1934 CYInfix_(false, 9, "!=", NotEqual)
1935 CYInfix_(false, 9, "===", Identical)
1936 CYInfix_(false, 9, "!==", NotIdentical)
1937 CYInfix_(false, 10, "&", BitwiseAnd)
1938 CYInfix_(false, 11, "^", BitwiseXOr)
1939 CYInfix_(false, 12, "|", BitwiseOr)
1940 CYInfix_(false, 13, "&&", LogicalAnd)
1941 CYInfix_(false, 14, "||", LogicalOr)
1943 CYAssignment_("=", )
1944 CYAssignment_("*=", Multiply)
1945 CYAssignment_("/=", Divide)
1946 CYAssignment_("%=", Modulus)
1947 CYAssignment_("+=", Add)
1948 CYAssignment_("-=", Subtract)
1949 CYAssignment_("<<=", ShiftLeft)
1950 CYAssignment_(">>=", ShiftRightSigned)
1951 CYAssignment_(">>>=", ShiftRightUnsigned)
1952 CYAssignment_("&=", BitwiseAnd)
1953 CYAssignment_("^=", BitwiseXOr)
1954 CYAssignment_("|=", BitwiseOr)
1956 #endif/*CYCRIPT_PARSER_HPP*/