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"
45 virtual void Output(struct CYOutput &out) const = 0;
66 CYOutput(std::streambuf &out, CYOptions &options) :
77 void Check(char value);
80 _finline void operator ()(char value) {
81 _assert(out_.sputc(value) != EOF);
89 _finline void operator ()(const char *data, std::streamsize size) {
90 _assert(out_.sputn(data, size) == size);
92 position_.columns(size);
95 _finline void operator ()(const char *data) {
96 return operator ()(data, strlen(data));
99 CYOutput &operator <<(char rhs);
100 CYOutput &operator <<(const char *rhs);
102 _finline CYOutput &operator <<(const CYThing *rhs) {
108 _finline CYOutput &operator <<(const CYThing &rhs) {
114 struct CYPropertyName {
115 virtual void PropertyName(CYOutput &out) const = 0;
117 virtual ~CYPropertyName() {
132 CYNoBrace = (1 << 0),
133 CYNoFunction = (1 << 1),
136 CYNoRightHand = (1 << 4),
137 CYNoDangle = (1 << 5),
138 CYNoInteger = (1 << 6),
139 CYNoBF = (CYNoBrace | CYNoFunction),
142 _finline CYFlags operator ~(CYFlags rhs) {
143 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
146 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
147 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
150 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
151 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
154 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
155 return lhs = lhs | rhs;
158 _finline CYFlags CYLeft(CYFlags flags) {
159 return flags & ~(CYNoDangle | CYNoInteger);
162 _finline CYFlags CYRight(CYFlags flags) {
163 return flags & ~CYNoBF;
166 _finline CYFlags CYCenter(CYFlags flags) {
167 return CYLeft(CYRight(flags));
176 #define CYCompact(type) \
177 virtual CYCompactType Compact() const { \
178 return CYCompact ## type; \
185 virtual ~CYStatement() {
188 void Single(CYOutput &out, CYFlags flags, CYCompactType request) const;
189 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
190 virtual void Output(CYOutput &out) const;
192 virtual CYStatement *Replace(CYContext &context) = 0;
194 virtual CYCompactType Compact() const = 0;
195 virtual CYStatement *Return();
198 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
201 struct CYStatements {
211 operator CYStatement *() const {
215 CYStatements &operator ->*(CYStatement *next) {
217 if (first_ == NULL) {
220 } else for (;; last_ = last_->next_)
221 if (last_->next_ == NULL) {
231 virtual ~CYClassName() {
234 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
235 virtual void ClassName(CYOutput &out, bool object) const = 0;
245 CYWord(const char *word) :
250 void Set(const char *value) {
254 virtual const char *Word() const;
255 virtual void Output(CYOutput &out) const;
257 virtual CYExpression *ClassName(CYContext &context, bool object);
258 virtual void ClassName(CYOutput &out, bool object) const;
259 virtual void PropertyName(CYOutput &out) const;
262 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
264 return lhs << rhs.Word();
267 struct CYIdentifier :
268 CYNext<CYIdentifier>,
271 CYIdentifier *replace_;
275 CYIdentifier(const char *word) :
283 virtual const char *Word() const;
284 CYIdentifier *Replace(CYContext &context);
292 CYComment(const char *value) :
299 virtual CYStatement *Replace(CYContext &context);
300 virtual void Output(CYOutput &out, CYFlags flags) const;
307 CYStatement *statement_;
309 CYLabel(CYIdentifier *name, CYStatement *statement) :
311 statement_(statement)
317 virtual CYStatement *Replace(CYContext &context);
318 virtual void Output(CYOutput &out, CYFlags flags) const;
321 struct CYCStringLess :
322 std::binary_function<const char *, const char *, bool>
324 _finline bool operator ()(const char *lhs, const char *rhs) const {
325 return strcmp(lhs, rhs) < 0;
329 struct CYIdentifierValueLess :
330 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
332 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
333 return CYCStringLess()(lhs->Word(), rhs->Word());
337 enum CYIdentifierFlags {
338 CYIdentifierArgument,
339 CYIdentifierVariable,
345 typedef std::set<const char *, CYCStringLess> CYCStringSet;
346 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
347 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
349 struct CYIdentifierUsage {
350 CYIdentifier *identifier_;
354 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
360 CYIdentifierAddressFlagsMap internal_;
361 CYIdentifierValueSet identifiers_;
363 CYScope(bool transparent, CYContext &context);
365 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
366 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
367 void Merge(CYContext &context, CYIdentifier *identifier);
368 void Close(CYContext &context, CYStatement *&statements);
376 CYProgram(CYStatement *code) :
381 virtual void Replace(CYContext &context);
382 virtual void Output(CYOutput &out) const;
394 CYIdentifierUsageVector rename_;
396 CYNonLocal *nonlocal_;
397 CYNonLocal *nextlocal_;
400 CYContext(CYOptions &options) :
410 virtual ~CYContext() {
413 void ReplaceAll(CYStatement *&statement) {
414 if (statement == NULL)
416 CYStatement *next(statement->next_);
421 if (statement == NULL)
424 statement->SetNext(next);
427 template <typename Type_>
428 void Replace(Type_ *&value) {
429 for (;;) if (value == NULL)
432 Type_ *replace(value->Replace(*this));
433 if (replace != value)
439 void NonLocal(CYStatement *&statements);
440 CYIdentifier *Unique();
444 CYIdentifier *identifier_;
451 CYIdentifier *Target(CYContext &context) {
452 if (identifier_ == NULL)
453 identifier_ = context.Unique();
461 CYIdentifier *identifier_;
468 CYIdentifier *Identifier(CYContext &context) {
470 return next_->Identifier(context);
471 if (identifier_ == NULL)
472 identifier_ = context.Unique();
482 CYBlock(CYStatement *code) :
489 virtual CYStatement *Replace(CYContext &context);
491 virtual void Output(CYOutput &out, CYFlags flags) const;
493 virtual CYStatement *Return();
496 struct CYForInitialiser {
497 virtual ~CYForInitialiser() {
500 virtual CYExpression *Replace(CYContext &context) = 0;
501 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
504 struct CYForInInitialiser {
505 virtual ~CYForInInitialiser() {
508 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
509 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
511 virtual CYExpression *Replace(CYContext &context) = 0;
512 virtual CYAssignment *Assignment(CYContext &context) = 0;
514 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
517 struct CYFunctionParameter;
522 struct CYExpression :
528 virtual int Precedence() const = 0;
530 virtual bool RightHand() const {
534 virtual void ForIn(CYOutput &out, CYFlags flags) const;
535 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
537 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
539 virtual void Output(CYOutput &out) const;
540 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
541 void Output(CYOutput &out, int precedence, CYFlags flags) const;
543 virtual CYExpression *ClassName(CYContext &context, bool object);
544 virtual void ClassName(CYOutput &out, bool object) const;
546 virtual CYExpression *Replace(CYContext &context) = 0;
547 virtual CYAssignment *Assignment(CYContext &context);
549 virtual CYExpression *Primitive(CYContext &context) {
553 virtual CYFunctionParameter *Parameter() const;
555 virtual CYNumber *Number(CYContext &context) {
559 virtual CYString *String(CYContext &context) {
563 virtual const char *Word() const {
568 #define CYAlphabetic(value) \
569 virtual bool Alphabetic() const { \
573 #define CYPrecedence(value) \
574 static const int Precedence_ = value; \
575 virtual int Precedence() const { \
576 return Precedence_; \
579 #define CYRightHand(value) \
580 virtual bool RightHand() const { \
587 CYExpression *expression_;
590 CYCompound(CYExpression *expression, CYExpression *next) :
591 expression_(expression),
594 _assert(expression_ != NULL);
595 _assert(next != NULL);
600 virtual CYExpression *Replace(CYContext &context);
601 void Output(CYOutput &out, CYFlags flags) const;
603 virtual CYFunctionParameter *Parameter() const;
606 struct CYParenthetical :
609 CYExpression *expression_;
611 CYParenthetical(CYExpression *expression) :
612 expression_(expression)
618 virtual CYExpression *Replace(CYContext &context);
619 void Output(CYOutput &out, CYFlags flags) const;
622 struct CYDeclaration;
624 struct CYFunctionParameter :
625 CYNext<CYFunctionParameter>,
628 CYForInInitialiser *initialiser_;
630 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
631 CYNext<CYFunctionParameter>(next),
632 initialiser_(initialiser)
636 void Replace(CYContext &context, CYStatement *&statements);
637 void Output(CYOutput &out) const;
640 struct CYComprehension :
641 CYNext<CYComprehension>,
644 CYComprehension(CYComprehension *next = NULL) :
645 CYNext<CYComprehension>(next)
649 CYComprehension *Modify(CYComprehension *next) {
654 virtual const char *Name() const = 0;
656 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
657 CYFunctionParameter *Parameters(CYContext &context) const;
658 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
659 virtual void Output(CYOutput &out) const = 0;
662 struct CYForInComprehension :
668 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
669 CYComprehension(next),
675 virtual const char *Name() const {
676 return name_->Word();
679 virtual CYFunctionParameter *Parameter(CYContext &context) const;
680 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
681 virtual void Output(CYOutput &out) const;
684 struct CYForOfComprehension :
690 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
691 CYComprehension(next),
697 virtual const char *Name() const {
698 return name_->Word();
701 virtual CYFunctionParameter *Parameter(CYContext &context) const;
702 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
703 virtual void Output(CYOutput &out) const;
706 struct CYIfComprehension :
711 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
712 CYComprehension(next),
717 virtual const char *Name() const {
721 virtual CYFunctionParameter *Parameter(CYContext &context) const;
722 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
723 virtual void Output(CYOutput &out) const;
726 struct CYArrayComprehension :
729 CYExpression *expression_;
730 CYComprehension *comprehensions_;
732 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
733 expression_(expression),
734 comprehensions_(comprehensions)
740 virtual CYExpression *Replace(CYContext &context);
741 virtual void Output(CYOutput &out, CYFlags flags) const;
750 virtual CYExpression *Primitive(CYContext &context) {
758 virtual CYExpression *Replace(CYContext &context);
772 CYRange(uint64_t lo, uint64_t hi) :
777 bool operator [](uint8_t value) const {
778 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
781 void operator()(uint8_t value) {
784 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
788 extern CYRange DigitRange_;
789 extern CYRange WordStartRange_;
790 extern CYRange WordEndRange_;
805 CYString(const char *value) :
811 CYString(const char *value, size_t size) :
817 CYString(const CYWord *word) :
818 value_(word->Word()),
819 size_(strlen(value_))
823 const char *Value() const {
827 virtual const char *Word() const;
829 virtual CYNumber *Number(CYContext &context);
830 virtual CYString *String(CYContext &context);
832 CYString *Concat(CYContext &out, CYString *rhs) const;
833 virtual void Output(CYOutput &out, CYFlags flags) const;
834 virtual void PropertyName(CYOutput &out) const;
843 CYNumber(double value) :
848 double Value() const {
852 virtual CYNumber *Number(CYContext &context);
853 virtual CYString *String(CYContext &context);
855 virtual void Output(CYOutput &out, CYFlags flags) const;
856 virtual void PropertyName(CYOutput &out) const;
864 CYRegEx(const char *value) :
869 const char *Value() const {
873 virtual void Output(CYOutput &out, CYFlags flags) const;
885 virtual CYNumber *Number(CYContext &context);
886 virtual CYString *String(CYContext &context);
888 virtual void Output(CYOutput &out, CYFlags flags) const;
900 virtual CYExpression *Replace(CYContext &context);
901 virtual void Output(CYOutput &out, CYFlags flags) const;
907 virtual bool Value() const = 0;
908 virtual void Output(CYOutput &out, CYFlags flags) const;
920 virtual bool Value() const {
924 virtual CYNumber *Number(CYContext &context);
925 virtual CYString *String(CYContext &context);
937 virtual bool Value() const {
941 virtual CYNumber *Number(CYContext &context);
942 virtual CYString *String(CYContext &context);
950 CYVariable(CYIdentifier *name) :
955 CYVariable(const char *name) :
956 name_(new($pool) CYIdentifier(name))
963 virtual CYExpression *Replace(CYContext &context);
964 virtual void Output(CYOutput &out, CYFlags flags) const;
966 virtual CYFunctionParameter *Parameter() const;
974 CYPrefix(CYExpression *rhs) :
979 virtual bool Alphabetic() const = 0;
980 virtual const char *Operator() const = 0;
984 virtual CYExpression *Replace(CYContext &context);
985 virtual void Output(CYOutput &out, CYFlags flags) const;
994 CYInfix(CYExpression *lhs, CYExpression *rhs) :
1000 void SetLeft(CYExpression *lhs) {
1004 virtual bool Alphabetic() const = 0;
1005 virtual const char *Operator() const = 0;
1007 virtual CYExpression *Replace(CYContext &context);
1008 virtual void Output(CYOutput &out, CYFlags flags) const;
1016 CYPostfix(CYExpression *lhs) :
1021 virtual const char *Operator() const = 0;
1025 virtual CYExpression *Replace(CYContext &context);
1026 virtual void Output(CYOutput &out, CYFlags flags) const;
1029 struct CYAssignment :
1035 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1041 void SetLeft(CYExpression *lhs) {
1045 virtual const char *Operator() const = 0;
1049 virtual CYExpression *Replace(CYContext &context);
1050 virtual void Output(CYOutput &out, CYFlags flags) const;
1058 CYExpression *value_;
1060 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1061 CYNext<CYArgument>(next),
1067 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1068 CYNext<CYArgument>(next),
1074 CYArgument *Replace(CYContext &context);
1075 void Output(CYOutput &out) const;
1091 CYExpression *case_;
1094 CYClause(CYExpression *_case, CYStatement *code) :
1100 void Replace(CYContext &context);
1101 virtual void Output(CYOutput &out) const;
1108 CYExpression *value_;
1110 CYElement(CYExpression *value, CYElement *next) :
1111 CYNext<CYElement>(next),
1116 void Replace(CYContext &context);
1117 void Output(CYOutput &out) const;
1123 CYElement *elements_;
1125 CYArray(CYElement *elements = NULL) :
1130 virtual CYExpression *Replace(CYContext &context);
1131 virtual void Output(CYOutput &out, CYFlags flags) const;
1138 CYPropertyName *name_;
1139 CYExpression *value_;
1141 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1142 CYNext<CYProperty>(next),
1148 void Replace(CYContext &context);
1149 virtual void Output(CYOutput &out) const;
1152 struct CYDeclaration :
1155 CYIdentifier *identifier_;
1156 CYExpression *initialiser_;
1158 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1159 identifier_(identifier),
1160 initialiser_(initialiser)
1164 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1165 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1167 virtual CYExpression *Replace(CYContext &context);
1169 virtual CYAssignment *Assignment(CYContext &context);
1170 CYVariable *Variable(CYContext &context);
1172 virtual void Output(CYOutput &out, CYFlags flags) const;
1175 struct CYDeclarations :
1176 CYNext<CYDeclarations>,
1179 CYDeclaration *declaration_;
1181 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1182 CYNext<CYDeclarations>(next),
1183 declaration_(declaration)
1187 void Replace(CYContext &context);
1189 CYExpression *Expression(CYContext &context);
1190 CYProperty *Property(CYContext &context);
1191 CYArgument *Argument(CYContext &context);
1192 CYFunctionParameter *Parameter(CYContext &context);
1194 virtual void Output(CYOutput &out) const;
1195 virtual void Output(CYOutput &out, CYFlags flags) const;
1198 struct CYForDeclarations :
1201 CYDeclarations *declarations_;
1203 CYForDeclarations(CYDeclarations *declarations) :
1204 declarations_(declarations)
1208 virtual CYExpression *Replace(CYContext &context);
1209 virtual void Output(CYOutput &out, CYFlags flags) const;
1215 CYDeclarations *declarations_;
1217 CYVar(CYDeclarations *declarations) :
1218 declarations_(declarations)
1224 virtual CYStatement *Replace(CYContext &context);
1225 virtual void Output(CYOutput &out, CYFlags flags) const;
1228 struct CYLetStatement :
1231 CYDeclarations *declarations_;
1234 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1235 declarations_(declarations),
1242 virtual CYStatement *Replace(CYContext &context);
1243 virtual void Output(CYOutput &out, CYFlags flags) const;
1249 CYForInitialiser *initialiser_;
1250 CYExpression *test_;
1251 CYExpression *increment_;
1254 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1255 initialiser_(initialiser),
1257 increment_(increment),
1264 virtual CYStatement *Replace(CYContext &context);
1265 virtual void Output(CYOutput &out, CYFlags flags) const;
1271 CYForInInitialiser *initialiser_;
1275 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1276 initialiser_(initialiser),
1284 virtual CYStatement *Replace(CYContext &context);
1285 virtual void Output(CYOutput &out, CYFlags flags) const;
1291 CYForInInitialiser *initialiser_;
1295 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1296 initialiser_(initialiser),
1304 virtual CYStatement *Replace(CYContext &context);
1305 virtual void Output(CYOutput &out, CYFlags flags) const;
1311 CYProperty *properties_;
1313 CYObject(CYProperty *properties = NULL) :
1314 properties_(properties)
1318 virtual CYExpression *Replace(CYContext &context);
1319 void Output(CYOutput &out, CYFlags flags) const;
1325 CYExpression *object_;
1326 CYExpression *property_;
1328 CYMember(CYExpression *object, CYExpression *property) :
1334 void SetLeft(CYExpression *object) {
1339 struct CYDirectMember :
1342 CYDirectMember(CYExpression *object, CYExpression *property) :
1343 CYMember(object, property)
1350 virtual CYExpression *Replace(CYContext &context);
1351 virtual void Output(CYOutput &out, CYFlags flags) const;
1354 struct CYIndirectMember :
1357 CYIndirectMember(CYExpression *object, CYExpression *property) :
1358 CYMember(object, property)
1365 virtual CYExpression *Replace(CYContext &context);
1366 virtual void Output(CYOutput &out, CYFlags flags) const;
1375 CYExpression *constructor_;
1376 CYArgument *arguments_;
1378 New(CYExpression *constructor, CYArgument *arguments) :
1379 constructor_(constructor),
1380 arguments_(arguments)
1384 virtual int Precedence() const {
1385 return arguments_ == NULL ? 2 : 1;
1390 virtual CYExpression *Replace(CYContext &context);
1391 virtual void Output(CYOutput &out, CYFlags flags) const;
1393 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1401 CYExpression *function_;
1402 CYArgument *arguments_;
1404 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1405 function_(function),
1406 arguments_(arguments)
1413 virtual CYExpression *Replace(CYContext &context);
1414 virtual void Output(CYOutput &out, CYFlags flags) const;
1416 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1421 struct CYRubyBlock :
1424 CYExpression *call_;
1427 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1436 virtual CYExpression *Replace(CYContext &context);
1437 virtual void Output(CYOutput &out, CYFlags flags) const;
1443 CYExpression *test_;
1445 CYStatement *false_;
1447 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1456 virtual CYStatement *Replace(CYContext &context);
1457 virtual void Output(CYOutput &out, CYFlags flags) const;
1459 virtual CYStatement *Return();
1465 CYExpression *test_;
1468 CYDoWhile(CYExpression *test, CYStatement *code) :
1476 virtual CYStatement *Replace(CYContext &context);
1477 virtual void Output(CYOutput &out, CYFlags flags) const;
1483 CYExpression *test_;
1486 CYWhile(CYExpression *test, CYStatement *code) :
1494 virtual CYStatement *Replace(CYContext &context);
1495 virtual void Output(CYOutput &out, CYFlags flags) const;
1498 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1500 CYIdentifier *name_;
1501 CYFunctionParameter *parameters_;
1504 CYNonLocal *nonlocal_;
1508 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1510 parameters_(parameters),
1517 virtual ~CYFunction() {
1520 void Inject(CYContext &context);
1521 virtual void Replace_(CYContext &context, bool outer);
1522 virtual void Output(CYOutput &out, CYFlags flags) const;
1525 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1526 struct CYFunctionExpression :
1530 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1531 CYFunction(name, parameters, code)
1538 virtual CYExpression *Replace(CYContext &context);
1539 virtual void Output(CYOutput &out, CYFlags flags) const;
1542 // XXX: this should derive from CYAnonymousFunction
1547 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1548 CYFunction(NULL, parameters, code)
1555 virtual CYExpression *Replace(CYContext &context);
1556 virtual void Output(CYOutput &out, CYFlags flags) const;
1559 // XXX: this should derive from CYAnonymousFunctionExpression
1561 CYFunctionExpression
1563 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1564 CYFunctionExpression(NULL, parameters, code)
1568 virtual CYExpression *Replace(CYContext &context);
1569 virtual void Output(CYOutput &out, CYFlags flags) const;
1572 // XXX: this should derive from CYNamedFunction
1573 struct CYFunctionStatement :
1577 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1578 CYFunction(name, parameters, code)
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1591 CYExpression *expression_;
1593 CYExpress(CYExpression *expression) :
1594 expression_(expression)
1596 if (expression_ == NULL)
1602 virtual CYStatement *Replace(CYContext &context);
1603 virtual void Output(CYOutput &out, CYFlags flags) const;
1605 virtual CYStatement *Return();
1611 CYIdentifier *label_;
1613 CYContinue(CYIdentifier *label) :
1620 virtual CYStatement *Replace(CYContext &context);
1621 virtual void Output(CYOutput &out, CYFlags flags) const;
1627 CYIdentifier *label_;
1629 CYBreak(CYIdentifier *label) :
1636 virtual CYStatement *Replace(CYContext &context);
1637 virtual void Output(CYOutput &out, CYFlags flags) const;
1643 CYExpression *value_;
1645 CYReturn(CYExpression *value) :
1652 virtual CYStatement *Replace(CYContext &context);
1653 virtual void Output(CYOutput &out, CYFlags flags) const;
1661 virtual CYStatement *Replace(CYContext &context);
1662 virtual void Output(CYOutput &out, CYFlags flags) const;
1670 CYFinally(CYStatement *code) :
1675 void Replace(CYContext &context);
1676 virtual void Output(CYOutput &out) const;
1679 struct CYTypeSpecifier :
1682 virtual CYExpression *Replace(CYContext &context) = 0;
1685 struct CYTypeError :
1691 virtual CYExpression *Replace(CYContext &context);
1692 virtual void Output(CYOutput &out) const;
1701 virtual CYExpression *Replace(CYContext &context);
1702 virtual void Output(CYOutput &out) const;
1705 struct CYTypeVariable :
1708 CYIdentifier *name_;
1710 CYTypeVariable(CYIdentifier *name) :
1715 CYTypeVariable(const char *name) :
1716 name_(new($pool) CYIdentifier(name))
1720 virtual CYExpression *Replace(CYContext &context);
1721 virtual void Output(CYOutput &out) const;
1724 struct CYTypeUnsigned :
1727 CYTypeSpecifier *specifier_;
1729 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1730 specifier_(specifier)
1734 virtual CYExpression *Replace(CYContext &context);
1735 virtual void Output(CYOutput &out) const;
1738 struct CYTypeSigned :
1741 CYTypeSpecifier *specifier_;
1743 CYTypeSigned(CYTypeSpecifier *specifier) :
1744 specifier_(specifier)
1748 virtual CYExpression *Replace(CYContext &context);
1749 virtual void Output(CYOutput &out) const;
1755 CYTypeSpecifier *specifier_;
1757 CYTypeLong(CYTypeSpecifier *specifier) :
1758 specifier_(specifier)
1762 virtual CYExpression *Replace(CYContext &context);
1763 virtual void Output(CYOutput &out) const;
1766 struct CYTypeShort :
1769 CYTypeSpecifier *specifier_;
1771 CYTypeShort(CYTypeSpecifier *specifier) :
1772 specifier_(specifier)
1776 virtual CYExpression *Replace(CYContext &context);
1777 virtual void Output(CYOutput &out) const;
1780 struct CYTypeFunctionWith;
1782 struct CYTypeModifier :
1783 CYNext<CYTypeModifier>
1785 CYTypeModifier(CYTypeModifier *next) :
1786 CYNext<CYTypeModifier>(next)
1790 virtual int Precedence() const = 0;
1792 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1793 CYExpression *Replace(CYContext &context, CYExpression *type);
1795 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1796 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1798 virtual CYTypeFunctionWith *Function() { return NULL; }
1801 struct CYTypeArrayOf :
1804 CYExpression *size_;
1806 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1807 CYTypeModifier(next),
1814 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1815 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1818 struct CYTypeConstant :
1821 CYTypeConstant(CYTypeModifier *next = NULL) :
1822 CYTypeModifier(next)
1828 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1829 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1832 struct CYTypePointerTo :
1835 CYTypePointerTo(CYTypeModifier *next = NULL) :
1836 CYTypeModifier(next)
1842 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1843 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1846 struct CYTypeVolatile :
1849 CYTypeVolatile(CYTypeModifier *next = NULL) :
1850 CYTypeModifier(next)
1856 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1857 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1860 struct CYTypedIdentifier :
1861 CYNext<CYTypedIdentifier>,
1864 CYLocation location_;
1865 CYIdentifier *identifier_;
1866 CYTypeSpecifier *specifier_;
1867 CYTypeModifier *modifier_;
1869 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1870 location_(location),
1871 identifier_(identifier),
1877 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1879 specifier_(specifier),
1884 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1885 CYSetLast(modifier_) = modifier;
1889 virtual CYExpression *Replace(CYContext &context);
1890 virtual void Output(CYOutput &out) const;
1892 CYTypeFunctionWith *Function();
1895 struct CYEncodedType :
1898 CYTypedIdentifier *typed_;
1900 CYEncodedType(CYTypedIdentifier *typed) :
1907 virtual CYExpression *Replace(CYContext &context);
1908 virtual void Output(CYOutput &out, CYFlags flags) const;
1911 struct CYTypedParameter :
1912 CYNext<CYTypedParameter>,
1915 CYTypedIdentifier *typed_;
1917 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1918 CYNext<CYTypedParameter>(next),
1923 CYArgument *Argument(CYContext &context);
1924 CYFunctionParameter *Parameters(CYContext &context);
1925 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1927 virtual void Output(CYOutput &out) const;
1933 CYTypedIdentifier *typed_;
1934 CYTypedParameter *parameters_;
1937 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1939 parameters_(parameters),
1946 virtual CYExpression *Replace(CYContext &context);
1947 virtual void Output(CYOutput &out, CYFlags flags) const;
1956 CYModule(CYWord *part, CYModule *next = NULL) :
1957 CYNext<CYModule>(next),
1962 CYString *Replace(CYContext &context, const char *separator) const;
1963 void Output(CYOutput &out) const;
1971 CYImport(CYModule *module) :
1978 virtual CYStatement *Replace(CYContext &context);
1979 virtual void Output(CYOutput &out, CYFlags flags) const;
1986 CYTypedIdentifier *typed_;
1988 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1996 virtual CYStatement *Replace(CYContext &context);
1997 virtual void Output(CYOutput &out, CYFlags flags) const;
2000 struct CYTypeDefinition :
2003 CYTypedIdentifier *typed_;
2005 CYTypeDefinition(CYTypedIdentifier *typed) :
2012 virtual CYStatement *Replace(CYContext &context);
2013 virtual void Output(CYOutput &out, CYFlags flags) const;
2016 struct CYTypeBlockWith :
2019 CYTypedParameter *parameters_;
2021 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2022 CYTypeModifier(next),
2023 parameters_(parameters)
2029 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2030 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2033 struct CYTypeFunctionWith :
2036 CYTypedParameter *parameters_;
2038 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2039 CYTypeModifier(next),
2040 parameters_(parameters)
2046 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2047 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2049 virtual CYTypeFunctionWith *Function() { return this; }
2058 CYIdentifier *name_;
2061 Catch(CYIdentifier *name, CYStatement *code) :
2067 void Replace(CYContext &context);
2068 virtual void Output(CYOutput &out) const;
2076 CYFinally *finally_;
2078 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2087 virtual CYStatement *Replace(CYContext &context);
2088 virtual void Output(CYOutput &out, CYFlags flags) const;
2094 CYExpression *value_;
2096 Throw(CYExpression *value = NULL) :
2103 virtual CYStatement *Replace(CYContext &context);
2104 virtual void Output(CYOutput &out, CYFlags flags) const;
2112 CYExpression *scope_;
2115 CYWith(CYExpression *scope, CYStatement *code) :
2123 virtual CYStatement *Replace(CYContext &context);
2124 virtual void Output(CYOutput &out, CYFlags flags) const;
2130 CYExpression *value_;
2133 CYSwitch(CYExpression *value, CYClause *clauses) :
2141 virtual CYStatement *Replace(CYContext &context);
2142 virtual void Output(CYOutput &out, CYFlags flags) const;
2154 virtual CYStatement *Replace(CYContext &context);
2155 virtual void Output(CYOutput &out, CYFlags flags) const;
2158 struct CYCondition :
2161 CYExpression *test_;
2162 CYExpression *true_;
2163 CYExpression *false_;
2165 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2174 virtual CYExpression *Replace(CYContext &context);
2175 virtual void Output(CYOutput &out, CYFlags flags) const;
2178 struct CYAddressOf :
2181 CYAddressOf(CYExpression *rhs) :
2186 virtual const char *Operator() const {
2192 virtual CYExpression *Replace(CYContext &context);
2198 CYIndirect(CYExpression *rhs) :
2203 virtual const char *Operator() const {
2209 virtual CYExpression *Replace(CYContext &context);
2213 virtual CYExpression *Replace(CYContext &context);
2215 #define CYPostfix_(op, name, args...) \
2216 struct CY ## name : \
2219 CY ## name(CYExpression *lhs) : \
2224 virtual const char *Operator() const { \
2229 #define CYPrefix_(alphabetic, op, name, args...) \
2230 struct CY ## name : \
2233 CY ## name(CYExpression *rhs) : \
2238 CYAlphabetic(alphabetic) \
2240 virtual const char *Operator() const { \
2245 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2246 struct CY ## name : \
2249 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2254 CYAlphabetic(alphabetic) \
2255 CYPrecedence(precedence) \
2257 virtual const char *Operator() const { \
2262 #define CYAssignment_(op, name, args...) \
2263 struct CY ## name ## Assign : \
2266 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2267 CYAssignment(lhs, rhs) \
2271 virtual const char *Operator() const { \
2276 CYPostfix_("++", PostIncrement)
2277 CYPostfix_("--", PostDecrement)
2279 CYPrefix_(true, "delete", Delete)
2280 CYPrefix_(true, "void", Void)
2281 CYPrefix_(true, "typeof", TypeOf)
2282 CYPrefix_(false, "++", PreIncrement)
2283 CYPrefix_(false, "--", PreDecrement)
2284 CYPrefix_(false, "+", Affirm)
2285 CYPrefix_(false, "-", Negate)
2286 CYPrefix_(false, "~", BitwiseNot)
2287 CYPrefix_(false, "!", LogicalNot)
2289 CYInfix_(false, 5, "*", Multiply, CYReplace)
2290 CYInfix_(false, 5, "/", Divide)
2291 CYInfix_(false, 5, "%", Modulus)
2292 CYInfix_(false, 6, "+", Add, CYReplace)
2293 CYInfix_(false, 6, "-", Subtract)
2294 CYInfix_(false, 7, "<<", ShiftLeft)
2295 CYInfix_(false, 7, ">>", ShiftRightSigned)
2296 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2297 CYInfix_(false, 8, "<", Less)
2298 CYInfix_(false, 8, ">", Greater)
2299 CYInfix_(false, 8, "<=", LessOrEqual)
2300 CYInfix_(false, 8, ">=", GreaterOrEqual)
2301 CYInfix_(true, 8, "instanceof", InstanceOf)
2302 CYInfix_(true, 8, "in", In)
2303 CYInfix_(false, 9, "==", Equal)
2304 CYInfix_(false, 9, "!=", NotEqual)
2305 CYInfix_(false, 9, "===", Identical)
2306 CYInfix_(false, 9, "!==", NotIdentical)
2307 CYInfix_(false, 10, "&", BitwiseAnd)
2308 CYInfix_(false, 11, "^", BitwiseXOr)
2309 CYInfix_(false, 12, "|", BitwiseOr)
2310 CYInfix_(false, 13, "&&", LogicalAnd)
2311 CYInfix_(false, 14, "||", LogicalOr)
2313 CYAssignment_("=", )
2314 CYAssignment_("*=", Multiply)
2315 CYAssignment_("/=", Divide)
2316 CYAssignment_("%=", Modulus)
2317 CYAssignment_("+=", Add)
2318 CYAssignment_("-=", Subtract)
2319 CYAssignment_("<<=", ShiftLeft)
2320 CYAssignment_(">>=", ShiftRightSigned)
2321 CYAssignment_(">>>=", ShiftRightUnsigned)
2322 CYAssignment_("&=", BitwiseAnd)
2323 CYAssignment_("^=", BitwiseXOr)
2324 CYAssignment_("|=", BitwiseOr)
2326 #endif/*CYCRIPT_PARSER_HPP*/