1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 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
35 #include "location.hh"
38 #include "Pooling.hpp"
39 #include "Options.hpp"
47 virtual void Output(struct CYOutput &out) const = 0;
65 CYOutput(std::ostream &out, CYOptions &options) :
75 void Check(char value);
78 CYOutput &operator <<(char rhs);
79 CYOutput &operator <<(const char *rhs);
81 _finline CYOutput &operator <<(const CYThing *rhs) {
87 _finline CYOutput &operator <<(const CYThing &rhs) {
93 struct CYPropertyName {
94 virtual void PropertyName(CYOutput &out) const = 0;
96 virtual ~CYPropertyName() {
111 CYNoBrace = (1 << 0),
112 CYNoFunction = (1 << 1),
115 CYNoRightHand = (1 << 4),
116 CYNoDangle = (1 << 5),
117 CYNoInteger = (1 << 6),
118 CYNoBF = (CYNoBrace | CYNoFunction),
121 _finline CYFlags operator ~(CYFlags rhs) {
122 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
125 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
126 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
129 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
130 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
133 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
134 return lhs = lhs | rhs;
137 _finline CYFlags CYLeft(CYFlags flags) {
138 return flags & ~(CYNoDangle | CYNoInteger);
141 _finline CYFlags CYRight(CYFlags flags) {
142 return flags & ~CYNoBF;
145 _finline CYFlags CYCenter(CYFlags flags) {
146 return CYLeft(CYRight(flags));
152 virtual ~CYStatement() {
155 void Single(CYOutput &out, CYFlags flags) const;
156 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
158 virtual CYStatement *Replace(CYContext &context) = 0;
161 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
164 struct CYStatements {
174 operator CYStatement *() const {
178 CYStatements &operator ->*(CYStatement *next) {
180 if (first_ == NULL) {
183 } else for (;; last_ = last_->next_)
184 if (last_->next_ == NULL) {
194 virtual ~CYClassName() {
197 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
198 virtual void ClassName(CYOutput &out, bool object) const = 0;
208 CYWord(const char *word) :
213 void Set(const char *value) {
217 virtual const char *Word() const;
218 virtual void Output(CYOutput &out) const;
220 virtual CYExpression *ClassName(CYContext &context, bool object);
221 virtual void ClassName(CYOutput &out, bool object) const;
222 virtual void PropertyName(CYOutput &out) const;
225 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
227 return lhs << rhs.Word();
230 struct CYIdentifier :
231 CYNext<CYIdentifier>,
234 CYIdentifier *replace_;
238 CYIdentifier(const char *word) :
246 virtual const char *Word() const;
247 CYIdentifier *Replace(CYContext &context);
255 CYComment(const char *value) :
260 virtual CYStatement *Replace(CYContext &context);
261 virtual void Output(CYOutput &out, CYFlags flags) const;
268 CYStatement *statement_;
270 CYLabel(CYIdentifier *name, CYStatement *statement) :
272 statement_(statement)
276 virtual CYStatement *Replace(CYContext &context);
277 virtual void Output(CYOutput &out, CYFlags flags) const;
280 struct CYCStringLess :
281 std::binary_function<const char *, const char *, bool>
283 _finline bool operator ()(const char *lhs, const char *rhs) const {
284 return strcmp(lhs, rhs) < 0;
288 struct CYIdentifierValueLess :
289 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
291 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
292 return CYCStringLess()(lhs->Word(), rhs->Word());
296 enum CYIdentifierFlags {
297 CYIdentifierArgument,
298 CYIdentifierVariable,
304 typedef std::set<const char *, CYCStringLess> CYCStringSet;
305 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
306 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
308 struct CYIdentifierUsage {
309 CYIdentifier *identifier_;
313 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
319 CYStatement *&statements_;
323 CYIdentifierAddressFlagsMap internal_;
324 CYIdentifierValueSet identifiers_;
326 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
331 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
332 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
333 void Merge(CYContext &context, CYIdentifier *identifier);
334 void Scope(CYContext &context, CYStatement *&statements);
340 CYStatement *statements_;
342 CYProgram(CYStatement *statements) :
343 statements_(statements)
347 virtual void Replace(CYContext &context);
348 virtual void Output(CYOutput &out) const;
357 CYIdentifierUsageVector rename_;
359 CYNonLocal *nonlocal_;
360 CYNonLocal *nextlocal_;
363 CYContext(CYOptions &options) :
372 virtual ~CYContext() {
375 template <typename Type_>
376 void ReplaceAll(Type_ *&values) {
377 Type_ **last(&values);
378 CYForEach (next, values) {
379 Replace(*last = next);
381 last = &(*last)->next_;
385 template <typename Type_>
386 void Replace(Type_ *&value) {
387 for (;;) if (value == NULL)
390 Type_ *replace(value->Replace(*this));
391 if (replace != value)
397 void NonLocal(CYStatement *&statements);
398 CYIdentifier *Unique();
402 CYIdentifier *identifier_;
409 CYIdentifier *Target(CYContext &context) {
410 if (identifier_ == NULL)
411 identifier_ = context.Unique();
420 CYStatement *statements_;
422 CYBlock(CYStatement *statements) :
423 statements_(statements)
427 operator CYStatement *() const {
431 void AddPrev(CYStatement *statement) {
432 CYSetLast(statement, statements_);
433 statements_ = statement;
436 virtual CYStatement *Replace(CYContext &context);
438 virtual void Output(CYOutput &out) const;
439 virtual void Output(CYOutput &out, CYFlags flags) const;
465 std::string filename_;
469 cy::location location_;
470 std::string message_;
473 typedef std::vector<Error> Errors;
481 CYExpression *context_;
483 Context(CYExpression *context) :
488 typedef std::vector<CYWord *> Words;
492 typedef std::vector<Context> Contexts;
495 CYExpression *context_;
507 void ScannerDestroy();
510 CYDriver(const std::string &filename = "");
513 Condition GetCondition();
514 void SetCondition(Condition condition);
516 void PushCondition(Condition condition);
519 void Warning(const cy::location &location, const char *message);
522 struct CYForInitialiser {
523 virtual ~CYForInitialiser() {
526 virtual CYExpression *Replace(CYContext &context) = 0;
527 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
530 struct CYForInInitialiser {
531 virtual ~CYForInInitialiser() {
534 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
535 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
537 virtual CYExpression *Replace(CYContext &context) = 0;
538 virtual CYAssignment *Assignment(CYContext &context) = 0;
540 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
546 struct CYExpression :
547 CYNext<CYExpression>,
553 virtual unsigned Precedence() const = 0;
555 virtual bool RightHand() const {
559 virtual void ForIn(CYOutput &out, CYFlags flags) const;
560 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
562 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
564 virtual void Output(CYOutput &out) const;
565 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
566 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
568 virtual CYExpression *ClassName(CYContext &context, bool object);
569 virtual void ClassName(CYOutput &out, bool object) const;
571 virtual CYExpression *Replace(CYContext &context) = 0;
572 virtual CYAssignment *Assignment(CYContext &context);
574 virtual CYExpression *Primitive(CYContext &context) {
578 virtual CYNumber *Number(CYContext &context) {
582 virtual CYString *String(CYContext &context) {
586 virtual const char *Word() const {
591 #define CYAlphabetic(value) \
592 virtual bool Alphabetic() const { \
596 #define CYPrecedence(value) \
597 static const unsigned Precedence_ = value; \
598 virtual unsigned Precedence() const { \
599 return Precedence_; \
602 #define CYRightHand(value) \
603 virtual bool RightHand() const { \
610 CYExpression *expressions_;
612 CYCompound(CYExpression *expressions = NULL) :
613 expressions_(expressions)
617 void AddPrev(CYExpression *expression) {
618 CYSetLast(expression, expressions_);
619 expressions_ = expression;
624 virtual CYExpression *Replace(CYContext &context);
625 void Output(CYOutput &out, CYFlags flags) const;
628 struct CYDeclaration;
630 struct CYFunctionParameter :
631 CYNext<CYFunctionParameter>,
634 CYForInInitialiser *initialiser_;
636 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
637 CYNext<CYFunctionParameter>(next),
638 initialiser_(initialiser)
642 void Replace(CYContext &context, CYBlock &code);
643 void Output(CYOutput &out) const;
646 struct CYComprehension :
647 CYNext<CYComprehension>,
650 virtual const char *Name() const = 0;
652 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
653 CYFunctionParameter *Parameters(CYContext &context) const;
654 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
655 virtual void Output(CYOutput &out) const = 0;
658 struct CYForInComprehension :
664 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
670 virtual const char *Name() const {
671 return name_->Word();
674 virtual CYFunctionParameter *Parameter(CYContext &context) const;
675 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
676 virtual void Output(CYOutput &out) const;
679 struct CYForEachInComprehension :
685 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
691 virtual const char *Name() const {
692 return name_->Word();
695 virtual CYFunctionParameter *Parameter(CYContext &context) const;
696 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
697 virtual void Output(CYOutput &out) const;
700 struct CYIfComprehension :
705 CYIfComprehension(CYExpression *test) :
710 virtual const char *Name() const {
714 virtual CYFunctionParameter *Parameter(CYContext &context) const;
715 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
716 virtual void Output(CYOutput &out) const;
719 struct CYArrayComprehension :
722 CYExpression *expression_;
723 CYComprehension *comprehensions_;
725 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
726 expression_(expression),
727 comprehensions_(comprehensions)
733 virtual CYExpression *Replace(CYContext &context);
734 virtual void Output(CYOutput &out, CYFlags flags) const;
747 virtual CYExpression *Replace(CYContext &context);
761 CYRange(uint64_t lo, uint64_t hi) :
766 bool operator [](uint8_t value) const {
767 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
770 void operator()(uint8_t value) {
773 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
777 extern CYRange DigitRange_;
778 extern CYRange WordStartRange_;
779 extern CYRange WordEndRange_;
794 CYString(const char *value) :
800 CYString(const char *value, size_t size) :
806 CYString(const CYWord *word) :
807 value_(word->Word()),
808 size_(strlen(value_))
812 const char *Value() const {
816 virtual const char *Word() const;
818 virtual CYNumber *Number(CYContext &context);
819 virtual CYString *String(CYContext &context);
821 CYString *Concat(CYContext &out, CYString *rhs) const;
822 virtual void Output(CYOutput &out, CYFlags flags) const;
823 virtual void PropertyName(CYOutput &out) const;
832 CYNumber(double value) :
837 double Value() const {
841 virtual CYNumber *Number(CYContext &context);
842 virtual CYString *String(CYContext &context);
844 virtual void Output(CYOutput &out, CYFlags flags) const;
845 virtual void PropertyName(CYOutput &out) const;
853 CYRegEx(const char *value) :
858 const char *Value() const {
862 virtual void Output(CYOutput &out, CYFlags flags) const;
874 virtual CYNumber *Number(CYContext &context);
875 virtual CYString *String(CYContext &context);
877 virtual void Output(CYOutput &out, CYFlags flags) const;
889 virtual CYExpression *Replace(CYContext &context);
890 virtual void Output(CYOutput &out, CYFlags flags) const;
896 virtual bool Value() const = 0;
897 virtual void Output(CYOutput &out, CYFlags flags) const;
909 virtual bool Value() const {
913 virtual CYNumber *Number(CYContext &context);
914 virtual CYString *String(CYContext &context);
926 virtual bool Value() const {
930 virtual CYNumber *Number(CYContext &context);
931 virtual CYString *String(CYContext &context);
939 CYVariable(CYIdentifier *name) :
944 CYVariable(const char *name) :
945 name_(new($pool) CYIdentifier(name))
952 virtual CYExpression *Replace(CYContext &context);
953 virtual void Output(CYOutput &out, CYFlags flags) const;
961 CYPrefix(CYExpression *rhs) :
966 virtual bool Alphabetic() const = 0;
967 virtual const char *Operator() const = 0;
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
981 CYInfix(CYExpression *lhs, CYExpression *rhs) :
987 void SetLeft(CYExpression *lhs) {
991 virtual bool Alphabetic() const = 0;
992 virtual const char *Operator() const = 0;
994 virtual CYExpression *Replace(CYContext &context);
995 virtual void Output(CYOutput &out, CYFlags flags) const;
1003 CYPostfix(CYExpression *lhs) :
1008 virtual const char *Operator() const = 0;
1012 virtual CYExpression *Replace(CYContext &context);
1013 virtual void Output(CYOutput &out, CYFlags flags) const;
1016 struct CYAssignment :
1022 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1028 void SetLeft(CYExpression *lhs) {
1032 virtual const char *Operator() const = 0;
1036 virtual CYExpression *Replace(CYContext &context);
1037 virtual void Output(CYOutput &out, CYFlags flags) const;
1045 CYExpression *value_;
1047 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1048 CYNext<CYArgument>(next),
1054 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1055 CYNext<CYArgument>(next),
1061 CYArgument *Replace(CYContext &context);
1062 void Output(CYOutput &out) const;
1078 CYExpression *case_;
1079 CYStatement *statements_;
1081 CYClause(CYExpression *_case, CYStatement *statements) :
1083 statements_(statements)
1087 void Replace(CYContext &context);
1088 virtual void Output(CYOutput &out) const;
1095 CYExpression *value_;
1097 CYElement(CYExpression *value, CYElement *next) :
1098 CYNext<CYElement>(next),
1103 void Replace(CYContext &context);
1104 void Output(CYOutput &out) const;
1110 CYElement *elements_;
1112 CYArray(CYElement *elements = NULL) :
1117 virtual CYExpression *Replace(CYContext &context);
1118 virtual void Output(CYOutput &out, CYFlags flags) const;
1125 CYPropertyName *name_;
1126 CYExpression *value_;
1128 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1129 CYNext<CYProperty>(next),
1135 void Replace(CYContext &context);
1136 virtual void Output(CYOutput &out) const;
1139 struct CYDeclaration :
1142 CYIdentifier *identifier_;
1143 CYExpression *initialiser_;
1145 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1146 identifier_(identifier),
1147 initialiser_(initialiser)
1151 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1152 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1154 virtual CYExpression *Replace(CYContext &context);
1156 virtual CYAssignment *Assignment(CYContext &context);
1157 CYVariable *Variable(CYContext &context);
1159 virtual void Output(CYOutput &out, CYFlags flags) const;
1162 struct CYDeclarations :
1163 CYNext<CYDeclarations>,
1166 CYDeclaration *declaration_;
1168 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1169 CYNext<CYDeclarations>(next),
1170 declaration_(declaration)
1174 void Replace(CYContext &context);
1176 CYCompound *Compound(CYContext &context);
1177 CYProperty *Property(CYContext &context);
1178 CYArgument *Argument(CYContext &context);
1179 CYFunctionParameter *Parameter(CYContext &context);
1181 virtual void Output(CYOutput &out) const;
1182 virtual void Output(CYOutput &out, CYFlags flags) const;
1185 struct CYForDeclarations :
1188 CYDeclarations *declarations_;
1190 CYForDeclarations(CYDeclarations *declarations) :
1191 declarations_(declarations)
1195 virtual CYCompound *Replace(CYContext &context);
1196 virtual void Output(CYOutput &out, CYFlags flags) const;
1202 CYDeclarations *declarations_;
1204 CYVar(CYDeclarations *declarations) :
1205 declarations_(declarations)
1209 virtual CYStatement *Replace(CYContext &context);
1210 virtual void Output(CYOutput &out, CYFlags flags) const;
1213 struct CYLetStatement :
1216 CYDeclarations *declarations_;
1219 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1220 declarations_(declarations),
1225 virtual CYStatement *Replace(CYContext &context);
1226 virtual void Output(CYOutput &out, CYFlags flags) const;
1232 CYForInitialiser *initialiser_;
1233 CYExpression *test_;
1234 CYExpression *increment_;
1237 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1238 initialiser_(initialiser),
1240 increment_(increment),
1245 virtual CYStatement *Replace(CYContext &context);
1246 virtual void Output(CYOutput &out, CYFlags flags) const;
1252 CYForInInitialiser *initialiser_;
1256 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1257 initialiser_(initialiser),
1263 virtual CYStatement *Replace(CYContext &context);
1264 virtual void Output(CYOutput &out, CYFlags flags) const;
1267 struct CYForEachIn :
1270 CYForInInitialiser *initialiser_;
1274 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1275 initialiser_(initialiser),
1281 virtual CYStatement *Replace(CYContext &context);
1282 virtual void Output(CYOutput &out, CYFlags flags) const;
1288 CYProperty *properties_;
1290 CYObject(CYProperty *properties = NULL) :
1291 properties_(properties)
1295 virtual CYExpression *Replace(CYContext &context);
1296 void Output(CYOutput &out, CYFlags flags) const;
1302 CYExpression *object_;
1303 CYExpression *property_;
1305 CYMember(CYExpression *object, CYExpression *property) :
1311 void SetLeft(CYExpression *object) {
1316 struct CYDirectMember :
1319 CYDirectMember(CYExpression *object, CYExpression *property) :
1320 CYMember(object, property)
1327 virtual CYExpression *Replace(CYContext &context);
1328 virtual void Output(CYOutput &out, CYFlags flags) const;
1331 struct CYIndirectMember :
1334 CYIndirectMember(CYExpression *object, CYExpression *property) :
1335 CYMember(object, property)
1342 virtual CYExpression *Replace(CYContext &context);
1343 virtual void Output(CYOutput &out, CYFlags flags) const;
1352 CYExpression *constructor_;
1353 CYArgument *arguments_;
1355 New(CYExpression *constructor, CYArgument *arguments) :
1356 constructor_(constructor),
1357 arguments_(arguments)
1361 virtual unsigned Precedence() const {
1362 return arguments_ == NULL ? 2 : 1;
1367 virtual CYExpression *Replace(CYContext &context);
1368 virtual void Output(CYOutput &out, CYFlags flags) const;
1370 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1378 CYExpression *function_;
1379 CYArgument *arguments_;
1381 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1382 function_(function),
1383 arguments_(arguments)
1390 virtual CYExpression *Replace(CYContext &context);
1391 virtual void Output(CYOutput &out, CYFlags flags) const;
1393 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1398 struct CYRubyBlock :
1401 CYExpression *call_;
1404 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1413 virtual CYExpression *Replace(CYContext &context);
1414 virtual void Output(CYOutput &out, CYFlags flags) const;
1420 CYExpression *test_;
1422 CYStatement *false_;
1424 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1431 virtual CYStatement *Replace(CYContext &context);
1432 virtual void Output(CYOutput &out, CYFlags flags) const;
1438 CYExpression *test_;
1441 CYDoWhile(CYExpression *test, CYStatement *code) :
1447 virtual CYStatement *Replace(CYContext &context);
1448 virtual void Output(CYOutput &out, CYFlags flags) const;
1454 CYExpression *test_;
1457 CYWhile(CYExpression *test, CYStatement *code) :
1463 virtual CYStatement *Replace(CYContext &context);
1464 virtual void Output(CYOutput &out, CYFlags flags) const;
1467 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1469 CYIdentifier *name_;
1470 CYFunctionParameter *parameters_;
1472 CYNonLocal *nonlocal_;
1474 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1476 parameters_(parameters),
1482 virtual ~CYFunction() {
1485 void Inject(CYContext &context);
1486 virtual void Replace_(CYContext &context, bool outer);
1487 virtual void Output(CYOutput &out, CYFlags flags) const;
1490 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1491 struct CYFunctionExpression :
1495 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1496 CYFunction(name, parameters, statements)
1503 virtual CYExpression *Replace(CYContext &context);
1504 virtual void Output(CYOutput &out, CYFlags flags) const;
1507 // XXX: this should derive from CYAnonymousFunctionExpression
1509 CYFunctionExpression
1511 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1512 CYFunctionExpression(NULL, parameters, statements)
1516 virtual CYExpression *Replace(CYContext &context);
1517 virtual void Output(CYOutput &out, CYFlags flags) const;
1520 // XXX: this should derive from CYNamedFunction
1521 struct CYFunctionStatement :
1525 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1526 CYFunction(name, parameters, statements)
1530 virtual CYStatement *Replace(CYContext &context);
1531 virtual void Output(CYOutput &out, CYFlags flags) const;
1537 CYExpression *expression_;
1539 CYExpress(CYExpression *expression) :
1540 expression_(expression)
1542 if (expression == NULL)
1546 virtual CYStatement *Replace(CYContext &context);
1547 virtual void Output(CYOutput &out, CYFlags flags) const;
1553 CYIdentifier *label_;
1555 CYContinue(CYIdentifier *label) :
1560 virtual CYStatement *Replace(CYContext &context);
1561 virtual void Output(CYOutput &out, CYFlags flags) const;
1567 CYIdentifier *label_;
1569 CYBreak(CYIdentifier *label) :
1574 virtual CYStatement *Replace(CYContext &context);
1575 virtual void Output(CYOutput &out, CYFlags flags) const;
1581 CYExpression *value_;
1583 CYReturn(CYExpression *value) :
1588 virtual CYStatement *Replace(CYContext &context);
1589 virtual void Output(CYOutput &out, CYFlags flags) const;
1595 virtual CYStatement *Replace(CYContext &context);
1596 virtual void Output(CYOutput &out, CYFlags flags) const;
1604 CYFinally(CYStatement *statements) :
1609 void Replace(CYContext &context);
1610 virtual void Output(CYOutput &out) const;
1619 CYIdentifier *name_;
1622 Catch(CYIdentifier *name, CYStatement *statements) :
1628 void Replace(CYContext &context);
1629 virtual void Output(CYOutput &out) const;
1637 CYFinally *finally_;
1639 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1646 virtual CYStatement *Replace(CYContext &context);
1647 virtual void Output(CYOutput &out, CYFlags flags) const;
1653 CYExpression *value_;
1655 Throw(CYExpression *value = NULL) :
1660 virtual CYStatement *Replace(CYContext &context);
1661 virtual void Output(CYOutput &out, CYFlags flags) const;
1669 CYExpression *scope_;
1672 CYWith(CYExpression *scope, CYStatement *code) :
1678 virtual CYStatement *Replace(CYContext &context);
1679 virtual void Output(CYOutput &out, CYFlags flags) const;
1685 CYExpression *value_;
1688 CYSwitch(CYExpression *value, CYClause *clauses) :
1694 virtual CYStatement *Replace(CYContext &context);
1695 virtual void Output(CYOutput &out, CYFlags flags) const;
1705 virtual CYStatement *Replace(CYContext &context);
1706 virtual void Output(CYOutput &out, CYFlags flags) const;
1709 struct CYCondition :
1712 CYExpression *test_;
1713 CYExpression *true_;
1714 CYExpression *false_;
1716 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1725 virtual CYExpression *Replace(CYContext &context);
1726 virtual void Output(CYOutput &out, CYFlags flags) const;
1729 struct CYAddressOf :
1732 CYAddressOf(CYExpression *rhs) :
1737 virtual const char *Operator() const {
1743 virtual CYExpression *Replace(CYContext &context);
1749 CYIndirect(CYExpression *rhs) :
1754 virtual const char *Operator() const {
1760 virtual CYExpression *Replace(CYContext &context);
1764 virtual CYExpression *Replace(CYContext &context);
1766 #define CYPostfix_(op, name, args...) \
1767 struct CY ## name : \
1770 CY ## name(CYExpression *lhs) : \
1775 virtual const char *Operator() const { \
1780 #define CYPrefix_(alphabetic, op, name, args...) \
1781 struct CY ## name : \
1784 CY ## name(CYExpression *rhs) : \
1789 CYAlphabetic(alphabetic) \
1791 virtual const char *Operator() const { \
1796 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1797 struct CY ## name : \
1800 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1805 CYAlphabetic(alphabetic) \
1806 CYPrecedence(precedence) \
1808 virtual const char *Operator() const { \
1813 #define CYAssignment_(op, name, args...) \
1814 struct CY ## name ## Assign : \
1817 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1818 CYAssignment(lhs, rhs) \
1822 virtual const char *Operator() const { \
1827 CYPostfix_("++", PostIncrement)
1828 CYPostfix_("--", PostDecrement)
1830 CYPrefix_(true, "delete", Delete)
1831 CYPrefix_(true, "void", Void)
1832 CYPrefix_(true, "typeof", TypeOf)
1833 CYPrefix_(false, "++", PreIncrement)
1834 CYPrefix_(false, "--", PreDecrement)
1835 CYPrefix_(false, "+", Affirm)
1836 CYPrefix_(false, "-", Negate)
1837 CYPrefix_(false, "~", BitwiseNot)
1838 CYPrefix_(false, "!", LogicalNot)
1840 CYInfix_(false, 5, "*", Multiply)
1841 CYInfix_(false, 5, "/", Divide)
1842 CYInfix_(false, 5, "%", Modulus)
1843 CYInfix_(false, 6, "+", Add, CYReplace)
1844 CYInfix_(false, 6, "-", Subtract)
1845 CYInfix_(false, 7, "<<", ShiftLeft)
1846 CYInfix_(false, 7, ">>", ShiftRightSigned)
1847 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1848 CYInfix_(false, 8, "<", Less)
1849 CYInfix_(false, 8, ">", Greater)
1850 CYInfix_(false, 8, "<=", LessOrEqual)
1851 CYInfix_(false, 8, ">=", GreaterOrEqual)
1852 CYInfix_(true, 8, "instanceof", InstanceOf)
1853 CYInfix_(true, 8, "in", In)
1854 CYInfix_(false, 9, "==", Equal)
1855 CYInfix_(false, 9, "!=", NotEqual)
1856 CYInfix_(false, 9, "===", Identical)
1857 CYInfix_(false, 9, "!==", NotIdentical)
1858 CYInfix_(false, 10, "&", BitwiseAnd)
1859 CYInfix_(false, 11, "^", BitwiseXOr)
1860 CYInfix_(false, 12, "|", BitwiseOr)
1861 CYInfix_(false, 13, "&&", LogicalAnd)
1862 CYInfix_(false, 14, "||", LogicalOr)
1864 CYAssignment_("=", )
1865 CYAssignment_("*=", Multiply)
1866 CYAssignment_("/=", Divide)
1867 CYAssignment_("%=", Modulus)
1868 CYAssignment_("+=", Add)
1869 CYAssignment_("-=", Subtract)
1870 CYAssignment_("<<=", ShiftLeft)
1871 CYAssignment_(">>=", ShiftRightSigned)
1872 CYAssignment_(">>>=", ShiftRightUnsigned)
1873 CYAssignment_("&=", BitwiseAnd)
1874 CYAssignment_("^=", BitwiseXOr)
1875 CYAssignment_("|=", BitwiseOr)
1877 #endif/*CYCRIPT_PARSER_HPP*/