]> git.saurik.com Git - cycript.git/blob - Parser.hpp
Use OS X (not Simulator) build for x86_64 on OS X.
[cycript.git] / Parser.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_PARSER_HPP
23 #define CYCRIPT_PARSER_HPP
24
25 #include <iostream>
26
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include <set>
31
32 #include <cstdio>
33 #include <cstdlib>
34
35 #include "List.hpp"
36 #include "Pooling.hpp"
37 #include "Options.hpp"
38
39 struct CYContext;
40
41 struct CYThing {
42 virtual ~CYThing() {
43 }
44
45 virtual void Output(struct CYOutput &out) const = 0;
46 };
47
48 struct CYOutput {
49 std::ostream &out_;
50 CYOptions &options_;
51 bool pretty_;
52 unsigned indent_;
53 bool right_;
54
55 enum {
56 NoMode,
57 NoLetter,
58 NoPlus,
59 NoHyphen,
60 Terminated
61 } mode_;
62
63 CYOutput(std::ostream &out, CYOptions &options) :
64 out_(out),
65 options_(options),
66 pretty_(false),
67 indent_(0),
68 right_(false),
69 mode_(NoMode)
70 {
71 }
72
73 void Check(char value);
74 void Terminate();
75
76 CYOutput &operator <<(char rhs);
77 CYOutput &operator <<(const char *rhs);
78
79 _finline CYOutput &operator <<(const CYThing *rhs) {
80 if (rhs != NULL)
81 rhs->Output(*this);
82 return *this;
83 }
84
85 _finline CYOutput &operator <<(const CYThing &rhs) {
86 rhs.Output(*this);
87 return *this;
88 }
89 };
90
91 struct CYPropertyName {
92 virtual void PropertyName(CYOutput &out) const = 0;
93
94 virtual ~CYPropertyName() {
95 }
96 };
97
98 struct CYExpression;
99 struct CYAssignment;
100
101 enum CYNeeded {
102 CYNever = -1,
103 CYSometimes = 0,
104 CYAlways = 1,
105 };
106
107 enum CYFlags {
108 CYNoFlags = 0,
109 CYNoBrace = (1 << 0),
110 CYNoFunction = (1 << 1),
111 CYNoIn = (1 << 2),
112 CYNoCall = (1 << 3),
113 CYNoRightHand = (1 << 4),
114 CYNoDangle = (1 << 5),
115 CYNoInteger = (1 << 6),
116 CYNoBF = (CYNoBrace | CYNoFunction),
117 };
118
119 _finline CYFlags operator ~(CYFlags rhs) {
120 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
121 }
122
123 _finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
124 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
125 }
126
127 _finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
128 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
129 }
130
131 _finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
132 return lhs = lhs | rhs;
133 }
134
135 _finline CYFlags CYLeft(CYFlags flags) {
136 return flags & ~(CYNoDangle | CYNoInteger);
137 }
138
139 _finline CYFlags CYRight(CYFlags flags) {
140 return flags & ~CYNoBF;
141 }
142
143 _finline CYFlags CYCenter(CYFlags flags) {
144 return CYLeft(CYRight(flags));
145 }
146
147 struct CYStatement :
148 CYNext<CYStatement>
149 {
150 virtual ~CYStatement() {
151 }
152
153 void Single(CYOutput &out, CYFlags flags) const;
154 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
155
156 virtual CYStatement *Replace(CYContext &context) = 0;
157
158 private:
159 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
160 };
161
162 struct CYStatements {
163 CYStatement *first_;
164 CYStatement *last_;
165
166 CYStatements() :
167 first_(NULL),
168 last_(NULL)
169 {
170 }
171
172 operator CYStatement *() const {
173 return first_;
174 }
175
176 CYStatements &operator ->*(CYStatement *next) {
177 if (next != NULL)
178 if (first_ == NULL) {
179 first_ = next;
180 last_ = next;
181 } else for (;; last_ = last_->next_)
182 if (last_->next_ == NULL) {
183 last_->next_ = next;
184 last_ = next;
185 break;
186 }
187 return *this;
188 }
189 };
190
191 struct CYClassName {
192 virtual ~CYClassName() {
193 }
194
195 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
196 virtual void ClassName(CYOutput &out, bool object) const = 0;
197 };
198
199 struct CYWord :
200 CYThing,
201 CYPropertyName,
202 CYClassName
203 {
204 const char *word_;
205
206 CYWord(const char *word) :
207 word_(word)
208 {
209 }
210
211 void Set(const char *value) {
212 word_ = value;
213 }
214
215 virtual const char *Word() const;
216 virtual void Output(CYOutput &out) const;
217
218 virtual CYExpression *ClassName(CYContext &context, bool object);
219 virtual void ClassName(CYOutput &out, bool object) const;
220 virtual void PropertyName(CYOutput &out) const;
221 };
222
223 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
224 lhs << &rhs << '=';
225 return lhs << rhs.Word();
226 }
227
228 struct CYIdentifier :
229 CYNext<CYIdentifier>,
230 CYWord
231 {
232 CYIdentifier *replace_;
233 size_t offset_;
234 size_t usage_;
235
236 CYIdentifier(const char *word) :
237 CYWord(word),
238 replace_(NULL),
239 offset_(0),
240 usage_(0)
241 {
242 }
243
244 virtual const char *Word() const;
245 CYIdentifier *Replace(CYContext &context);
246 };
247
248 struct CYComment :
249 CYStatement
250 {
251 const char *value_;
252
253 CYComment(const char *value) :
254 value_(value)
255 {
256 }
257
258 virtual CYStatement *Replace(CYContext &context);
259 virtual void Output(CYOutput &out, CYFlags flags) const;
260 };
261
262 struct CYLabel :
263 CYStatement
264 {
265 CYIdentifier *name_;
266 CYStatement *statement_;
267
268 CYLabel(CYIdentifier *name, CYStatement *statement) :
269 name_(name),
270 statement_(statement)
271 {
272 }
273
274 virtual CYStatement *Replace(CYContext &context);
275 virtual void Output(CYOutput &out, CYFlags flags) const;
276 };
277
278 struct CYCStringLess :
279 std::binary_function<const char *, const char *, bool>
280 {
281 _finline bool operator ()(const char *lhs, const char *rhs) const {
282 return strcmp(lhs, rhs) < 0;
283 }
284 };
285
286 struct CYIdentifierValueLess :
287 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
288 {
289 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
290 return CYCStringLess()(lhs->Word(), rhs->Word());
291 }
292 };
293
294 enum CYIdentifierFlags {
295 CYIdentifierArgument,
296 CYIdentifierVariable,
297 CYIdentifierOther,
298 CYIdentifierMagic,
299 CYIdentifierCatch,
300 };
301
302 typedef std::set<const char *, CYCStringLess> CYCStringSet;
303 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
304 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
305
306 struct CYIdentifierUsage {
307 CYIdentifier *identifier_;
308 size_t usage_;
309 };
310
311 typedef std::vector<CYIdentifierUsage> CYIdentifierUsageVector;
312
313 struct CYScope {
314 bool transparent_;
315
316 CYContext &context_;
317 CYStatement *&statements_;
318
319 CYScope *parent_;
320
321 CYIdentifierAddressFlagsMap internal_;
322 CYIdentifierValueSet identifiers_;
323
324 CYScope(bool transparent, CYContext &context, CYStatement *&statements);
325 virtual ~CYScope();
326
327 void Close();
328
329 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
330 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
331 void Merge(CYContext &context, CYIdentifier *identifier);
332 void Scope(CYContext &context, CYStatement *&statements);
333 };
334
335 struct CYProgram :
336 CYThing
337 {
338 CYStatement *statements_;
339
340 CYProgram(CYStatement *statements) :
341 statements_(statements)
342 {
343 }
344
345 virtual void Replace(CYContext &context);
346 virtual void Output(CYOutput &out) const;
347 };
348
349 struct CYNonLocal;
350 struct CYThisScope;
351
352 struct CYContext {
353 CYOptions &options_;
354
355 CYScope *scope_;
356 CYThisScope *this_;
357
358 CYIdentifierUsageVector rename_;
359
360 CYNonLocal *nonlocal_;
361 CYNonLocal *nextlocal_;
362 unsigned unique_;
363
364 CYContext(CYOptions &options) :
365 options_(options),
366 scope_(NULL),
367 this_(NULL),
368 nonlocal_(NULL),
369 nextlocal_(NULL),
370 unique_(0)
371 {
372 }
373
374 virtual ~CYContext() {
375 }
376
377 template <typename Type_>
378 void ReplaceAll(Type_ *&values) {
379 Type_ **last(&values);
380 CYForEach (next, values) {
381 Replace(*last = next);
382 if (*last != NULL)
383 last = &(*last)->next_;
384 }
385 }
386
387 template <typename Type_>
388 void Replace(Type_ *&value) {
389 for (;;) if (value == NULL)
390 break;
391 else {
392 Type_ *replace(value->Replace(*this));
393 if (replace != value)
394 value = replace;
395 else break;
396 }
397 }
398
399 void NonLocal(CYStatement *&statements);
400 CYIdentifier *Unique();
401 };
402
403 struct CYNonLocal {
404 CYIdentifier *identifier_;
405
406 CYNonLocal() :
407 identifier_(NULL)
408 {
409 }
410
411 CYIdentifier *Target(CYContext &context) {
412 if (identifier_ == NULL)
413 identifier_ = context.Unique();
414 return identifier_;
415 }
416 };
417
418 struct CYThisScope :
419 CYNext<CYThisScope>
420 {
421 CYIdentifier *identifier_;
422
423 CYThisScope() :
424 identifier_(NULL)
425 {
426 }
427
428 CYIdentifier *Identifier(CYContext &context) {
429 if (next_ != NULL)
430 return next_->Identifier(context);
431 if (identifier_ == NULL)
432 identifier_ = context.Unique();
433 return identifier_;
434 }
435 };
436
437 struct CYBlock :
438 CYStatement,
439 CYThing
440 {
441 CYStatement *statements_;
442
443 CYBlock(CYStatement *statements) :
444 statements_(statements)
445 {
446 }
447
448 operator CYStatement *() const {
449 return statements_;
450 }
451
452 void AddPrev(CYStatement *statement) {
453 CYSetLast(statement) = statements_;
454 statements_ = statement;
455 }
456
457 virtual CYStatement *Replace(CYContext &context);
458
459 virtual void Output(CYOutput &out) const;
460 virtual void Output(CYOutput &out, CYFlags flags) const;
461 };
462
463 struct CYForInitialiser {
464 virtual ~CYForInitialiser() {
465 }
466
467 virtual CYExpression *Replace(CYContext &context) = 0;
468 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
469 };
470
471 struct CYForInInitialiser {
472 virtual ~CYForInInitialiser() {
473 }
474
475 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
476 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
477
478 virtual CYExpression *Replace(CYContext &context) = 0;
479 virtual CYAssignment *Assignment(CYContext &context) = 0;
480
481 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
482 };
483
484 struct CYNumber;
485 struct CYString;
486
487 struct CYExpression :
488 CYForInitialiser,
489 CYForInInitialiser,
490 CYClassName,
491 CYThing
492 {
493 virtual int Precedence() const = 0;
494
495 virtual bool RightHand() const {
496 return true;
497 }
498
499 virtual void ForIn(CYOutput &out, CYFlags flags) const;
500 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
501
502 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
503
504 virtual void Output(CYOutput &out) const;
505 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
506 void Output(CYOutput &out, int precedence, CYFlags flags) const;
507
508 virtual CYExpression *ClassName(CYContext &context, bool object);
509 virtual void ClassName(CYOutput &out, bool object) const;
510
511 virtual CYExpression *Replace(CYContext &context) = 0;
512 virtual CYAssignment *Assignment(CYContext &context);
513
514 virtual CYExpression *Primitive(CYContext &context) {
515 return NULL;
516 }
517
518 virtual CYNumber *Number(CYContext &context) {
519 return NULL;
520 }
521
522 virtual CYString *String(CYContext &context) {
523 return NULL;
524 }
525
526 virtual const char *Word() const {
527 return NULL;
528 }
529 };
530
531 #define CYAlphabetic(value) \
532 virtual bool Alphabetic() const { \
533 return value; \
534 }
535
536 #define CYPrecedence(value) \
537 static const int Precedence_ = value; \
538 virtual int Precedence() const { \
539 return Precedence_; \
540 }
541
542 #define CYRightHand(value) \
543 virtual bool RightHand() const { \
544 return value; \
545 }
546
547 struct CYCompound :
548 CYExpression
549 {
550 CYExpression *expression_;
551 CYExpression *next_;
552
553 CYCompound(CYExpression *expression, CYExpression *next = NULL) :
554 expression_(expression),
555 next_(next)
556 {
557 if (expression_ == NULL)
558 throw;
559 _assert(expression_ != NULL);
560 }
561
562 CYPrecedence(17)
563
564 virtual CYExpression *Replace(CYContext &context);
565 void Output(CYOutput &out, CYFlags flags) const;
566
567 virtual CYExpression *Primitive(CYContext &context);
568 };
569
570 struct CYDeclaration;
571
572 struct CYFunctionParameter :
573 CYNext<CYFunctionParameter>,
574 CYThing
575 {
576 CYForInInitialiser *initialiser_;
577
578 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
579 CYNext<CYFunctionParameter>(next),
580 initialiser_(initialiser)
581 {
582 }
583
584 void Replace(CYContext &context, CYBlock &code);
585 void Output(CYOutput &out) const;
586 };
587
588 struct CYComprehension :
589 CYNext<CYComprehension>,
590 CYThing
591 {
592 CYComprehension(CYComprehension *next = NULL) :
593 CYNext<CYComprehension>(next)
594 {
595 }
596
597 CYComprehension *Modify(CYComprehension *next) {
598 next_ = next;
599 return this;
600 }
601
602 virtual const char *Name() const = 0;
603
604 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
605 CYFunctionParameter *Parameters(CYContext &context) const;
606 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
607 virtual void Output(CYOutput &out) const = 0;
608 };
609
610 struct CYForInComprehension :
611 CYComprehension
612 {
613 CYIdentifier *name_;
614 CYExpression *set_;
615
616 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
617 CYComprehension(next),
618 name_(name),
619 set_(set)
620 {
621 }
622
623 virtual const char *Name() const {
624 return name_->Word();
625 }
626
627 virtual CYFunctionParameter *Parameter(CYContext &context) const;
628 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
629 virtual void Output(CYOutput &out) const;
630 };
631
632 struct CYForOfComprehension :
633 CYComprehension
634 {
635 CYIdentifier *name_;
636 CYExpression *set_;
637
638 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
639 CYComprehension(next),
640 name_(name),
641 set_(set)
642 {
643 }
644
645 virtual const char *Name() const {
646 return name_->Word();
647 }
648
649 virtual CYFunctionParameter *Parameter(CYContext &context) const;
650 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
651 virtual void Output(CYOutput &out) const;
652 };
653
654 struct CYIfComprehension :
655 CYComprehension
656 {
657 CYExpression *test_;
658
659 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
660 CYComprehension(next),
661 test_(test)
662 {
663 }
664
665 virtual const char *Name() const {
666 return NULL;
667 }
668
669 virtual CYFunctionParameter *Parameter(CYContext &context) const;
670 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
671 virtual void Output(CYOutput &out) const;
672 };
673
674 struct CYArrayComprehension :
675 CYExpression
676 {
677 CYExpression *expression_;
678 CYComprehension *comprehensions_;
679
680 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
681 expression_(expression),
682 comprehensions_(comprehensions)
683 {
684 }
685
686 CYPrecedence(0)
687
688 virtual CYExpression *Replace(CYContext &context);
689 virtual void Output(CYOutput &out, CYFlags flags) const;
690 };
691
692 struct CYLiteral :
693 CYExpression
694 {
695 CYPrecedence(0)
696 CYRightHand(false)
697
698 virtual CYExpression *Primitive(CYContext &context) {
699 return this;
700 }
701 };
702
703 struct CYTrivial :
704 CYLiteral
705 {
706 virtual CYExpression *Replace(CYContext &context);
707 };
708
709 struct CYMagic :
710 CYExpression
711 {
712 CYPrecedence(0)
713 CYRightHand(false)
714 };
715
716 struct CYRange {
717 uint64_t lo_;
718 uint64_t hi_;
719
720 CYRange(uint64_t lo, uint64_t hi) :
721 lo_(lo), hi_(hi)
722 {
723 }
724
725 bool operator [](uint8_t value) const {
726 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
727 }
728
729 void operator()(uint8_t value) {
730 if (value >> 7)
731 return;
732 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
733 }
734 };
735
736 extern CYRange DigitRange_;
737 extern CYRange WordStartRange_;
738 extern CYRange WordEndRange_;
739
740 struct CYString :
741 CYTrivial,
742 CYPropertyName
743 {
744 const char *value_;
745 size_t size_;
746
747 CYString() :
748 value_(NULL),
749 size_(0)
750 {
751 }
752
753 CYString(const char *value) :
754 value_(value),
755 size_(strlen(value))
756 {
757 }
758
759 CYString(const char *value, size_t size) :
760 value_(value),
761 size_(size)
762 {
763 }
764
765 CYString(const CYWord *word) :
766 value_(word->Word()),
767 size_(strlen(value_))
768 {
769 }
770
771 const char *Value() const {
772 return value_;
773 }
774
775 virtual const char *Word() const;
776
777 virtual CYNumber *Number(CYContext &context);
778 virtual CYString *String(CYContext &context);
779
780 CYString *Concat(CYContext &out, CYString *rhs) const;
781 virtual void Output(CYOutput &out, CYFlags flags) const;
782 virtual void PropertyName(CYOutput &out) const;
783 };
784
785 struct CYNumber :
786 CYTrivial,
787 CYPropertyName
788 {
789 double value_;
790
791 CYNumber(double value) :
792 value_(value)
793 {
794 }
795
796 double Value() const {
797 return value_;
798 }
799
800 virtual CYNumber *Number(CYContext &context);
801 virtual CYString *String(CYContext &context);
802
803 virtual void Output(CYOutput &out, CYFlags flags) const;
804 virtual void PropertyName(CYOutput &out) const;
805 };
806
807 struct CYRegEx :
808 CYTrivial
809 {
810 const char *value_;
811
812 CYRegEx(const char *value) :
813 value_(value)
814 {
815 }
816
817 const char *Value() const {
818 return value_;
819 }
820
821 virtual void Output(CYOutput &out, CYFlags flags) const;
822 };
823
824 struct CYNull :
825 CYWord,
826 CYTrivial
827 {
828 CYNull() :
829 CYWord("null")
830 {
831 }
832
833 virtual CYNumber *Number(CYContext &context);
834 virtual CYString *String(CYContext &context);
835
836 virtual void Output(CYOutput &out, CYFlags flags) const;
837 };
838
839 struct CYThis :
840 CYWord,
841 CYMagic
842 {
843 CYThis() :
844 CYWord("this")
845 {
846 }
847
848 virtual CYExpression *Replace(CYContext &context);
849 virtual void Output(CYOutput &out, CYFlags flags) const;
850 };
851
852 struct CYBoolean :
853 CYTrivial
854 {
855 virtual bool Value() const = 0;
856 virtual void Output(CYOutput &out, CYFlags flags) const;
857 };
858
859 struct CYFalse :
860 CYWord,
861 CYBoolean
862 {
863 CYFalse() :
864 CYWord("false")
865 {
866 }
867
868 virtual bool Value() const {
869 return false;
870 }
871
872 virtual CYNumber *Number(CYContext &context);
873 virtual CYString *String(CYContext &context);
874 };
875
876 struct CYTrue :
877 CYWord,
878 CYBoolean
879 {
880 CYTrue() :
881 CYWord("true")
882 {
883 }
884
885 virtual bool Value() const {
886 return true;
887 }
888
889 virtual CYNumber *Number(CYContext &context);
890 virtual CYString *String(CYContext &context);
891 };
892
893 struct CYVariable :
894 CYExpression
895 {
896 CYIdentifier *name_;
897
898 CYVariable(CYIdentifier *name) :
899 name_(name)
900 {
901 }
902
903 CYVariable(const char *name) :
904 name_(new($pool) CYIdentifier(name))
905 {
906 }
907
908 CYPrecedence(0)
909 CYRightHand(false)
910
911 virtual CYExpression *Replace(CYContext &context);
912 virtual void Output(CYOutput &out, CYFlags flags) const;
913 };
914
915 struct CYPrefix :
916 CYExpression
917 {
918 CYExpression *rhs_;
919
920 CYPrefix(CYExpression *rhs) :
921 rhs_(rhs)
922 {
923 }
924
925 virtual bool Alphabetic() const = 0;
926 virtual const char *Operator() const = 0;
927
928 CYPrecedence(4)
929
930 virtual CYExpression *Replace(CYContext &context);
931 virtual void Output(CYOutput &out, CYFlags flags) const;
932 };
933
934 struct CYInfix :
935 CYExpression
936 {
937 CYExpression *lhs_;
938 CYExpression *rhs_;
939
940 CYInfix(CYExpression *lhs, CYExpression *rhs) :
941 lhs_(lhs),
942 rhs_(rhs)
943 {
944 }
945
946 void SetLeft(CYExpression *lhs) {
947 lhs_ = lhs;
948 }
949
950 virtual bool Alphabetic() const = 0;
951 virtual const char *Operator() const = 0;
952
953 virtual CYExpression *Replace(CYContext &context);
954 virtual void Output(CYOutput &out, CYFlags flags) const;
955 };
956
957 struct CYPostfix :
958 CYExpression
959 {
960 CYExpression *lhs_;
961
962 CYPostfix(CYExpression *lhs) :
963 lhs_(lhs)
964 {
965 }
966
967 virtual const char *Operator() const = 0;
968
969 CYPrecedence(3)
970
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
973 };
974
975 struct CYAssignment :
976 CYExpression
977 {
978 CYExpression *lhs_;
979 CYExpression *rhs_;
980
981 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
982 lhs_(lhs),
983 rhs_(rhs)
984 {
985 }
986
987 void SetLeft(CYExpression *lhs) {
988 lhs_ = lhs;
989 }
990
991 virtual const char *Operator() const = 0;
992
993 CYPrecedence(16)
994
995 virtual CYExpression *Replace(CYContext &context);
996 virtual void Output(CYOutput &out, CYFlags flags) const;
997 };
998
999 struct CYArgument :
1000 CYNext<CYArgument>,
1001 CYThing
1002 {
1003 CYWord *name_;
1004 CYExpression *value_;
1005
1006 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1007 CYNext<CYArgument>(next),
1008 name_(NULL),
1009 value_(value)
1010 {
1011 }
1012
1013 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1014 CYNext<CYArgument>(next),
1015 name_(name),
1016 value_(value)
1017 {
1018 }
1019
1020 CYArgument *Replace(CYContext &context);
1021 void Output(CYOutput &out) const;
1022 };
1023
1024 struct CYBlank :
1025 public CYWord
1026 {
1027 CYBlank() :
1028 CYWord("")
1029 {
1030 }
1031 };
1032
1033 struct CYClause :
1034 CYThing,
1035 CYNext<CYClause>
1036 {
1037 CYExpression *case_;
1038 CYStatement *statements_;
1039
1040 CYClause(CYExpression *_case, CYStatement *statements) :
1041 case_(_case),
1042 statements_(statements)
1043 {
1044 }
1045
1046 void Replace(CYContext &context);
1047 virtual void Output(CYOutput &out) const;
1048 };
1049
1050 struct CYElement :
1051 CYNext<CYElement>,
1052 CYThing
1053 {
1054 CYExpression *value_;
1055
1056 CYElement(CYExpression *value, CYElement *next) :
1057 CYNext<CYElement>(next),
1058 value_(value)
1059 {
1060 }
1061
1062 void Replace(CYContext &context);
1063 void Output(CYOutput &out) const;
1064 };
1065
1066 struct CYArray :
1067 CYLiteral
1068 {
1069 CYElement *elements_;
1070
1071 CYArray(CYElement *elements = NULL) :
1072 elements_(elements)
1073 {
1074 }
1075
1076 virtual CYExpression *Replace(CYContext &context);
1077 virtual void Output(CYOutput &out, CYFlags flags) const;
1078 };
1079
1080 struct CYProperty :
1081 CYNext<CYProperty>,
1082 CYThing
1083 {
1084 CYPropertyName *name_;
1085 CYExpression *value_;
1086
1087 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1088 CYNext<CYProperty>(next),
1089 name_(name),
1090 value_(value)
1091 {
1092 }
1093
1094 void Replace(CYContext &context);
1095 virtual void Output(CYOutput &out) const;
1096 };
1097
1098 struct CYDeclaration :
1099 CYForInInitialiser
1100 {
1101 CYIdentifier *identifier_;
1102 CYExpression *initialiser_;
1103
1104 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1105 identifier_(identifier),
1106 initialiser_(initialiser)
1107 {
1108 }
1109
1110 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1111 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1112
1113 virtual CYExpression *Replace(CYContext &context);
1114
1115 virtual CYAssignment *Assignment(CYContext &context);
1116 CYVariable *Variable(CYContext &context);
1117
1118 virtual void Output(CYOutput &out, CYFlags flags) const;
1119 };
1120
1121 struct CYDeclarations :
1122 CYNext<CYDeclarations>,
1123 CYThing
1124 {
1125 CYDeclaration *declaration_;
1126
1127 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1128 CYNext<CYDeclarations>(next),
1129 declaration_(declaration)
1130 {
1131 }
1132
1133 void Replace(CYContext &context);
1134
1135 CYCompound *Compound(CYContext &context);
1136 CYProperty *Property(CYContext &context);
1137 CYArgument *Argument(CYContext &context);
1138 CYFunctionParameter *Parameter(CYContext &context);
1139
1140 virtual void Output(CYOutput &out) const;
1141 virtual void Output(CYOutput &out, CYFlags flags) const;
1142 };
1143
1144 struct CYForDeclarations :
1145 CYForInitialiser
1146 {
1147 CYDeclarations *declarations_;
1148
1149 CYForDeclarations(CYDeclarations *declarations) :
1150 declarations_(declarations)
1151 {
1152 }
1153
1154 virtual CYCompound *Replace(CYContext &context);
1155 virtual void Output(CYOutput &out, CYFlags flags) const;
1156 };
1157
1158 struct CYVar :
1159 CYStatement
1160 {
1161 CYDeclarations *declarations_;
1162
1163 CYVar(CYDeclarations *declarations) :
1164 declarations_(declarations)
1165 {
1166 }
1167
1168 virtual CYStatement *Replace(CYContext &context);
1169 virtual void Output(CYOutput &out, CYFlags flags) const;
1170 };
1171
1172 struct CYLetStatement :
1173 CYStatement
1174 {
1175 CYDeclarations *declarations_;
1176 CYStatement *code_;
1177
1178 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1179 declarations_(declarations),
1180 code_(code)
1181 {
1182 }
1183
1184 virtual CYStatement *Replace(CYContext &context);
1185 virtual void Output(CYOutput &out, CYFlags flags) const;
1186 };
1187
1188 struct CYFor :
1189 CYStatement
1190 {
1191 CYForInitialiser *initialiser_;
1192 CYExpression *test_;
1193 CYExpression *increment_;
1194 CYStatement *code_;
1195
1196 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1197 initialiser_(initialiser),
1198 test_(test),
1199 increment_(increment),
1200 code_(code)
1201 {
1202 }
1203
1204 virtual CYStatement *Replace(CYContext &context);
1205 virtual void Output(CYOutput &out, CYFlags flags) const;
1206 };
1207
1208 struct CYForIn :
1209 CYStatement
1210 {
1211 CYForInInitialiser *initialiser_;
1212 CYExpression *set_;
1213 CYStatement *code_;
1214
1215 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1216 initialiser_(initialiser),
1217 set_(set),
1218 code_(code)
1219 {
1220 }
1221
1222 virtual CYStatement *Replace(CYContext &context);
1223 virtual void Output(CYOutput &out, CYFlags flags) const;
1224 };
1225
1226 struct CYForOf :
1227 CYStatement
1228 {
1229 CYForInInitialiser *initialiser_;
1230 CYExpression *set_;
1231 CYStatement *code_;
1232
1233 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1234 initialiser_(initialiser),
1235 set_(set),
1236 code_(code)
1237 {
1238 }
1239
1240 virtual CYStatement *Replace(CYContext &context);
1241 virtual void Output(CYOutput &out, CYFlags flags) const;
1242 };
1243
1244 struct CYObject :
1245 CYLiteral
1246 {
1247 CYProperty *properties_;
1248
1249 CYObject(CYProperty *properties = NULL) :
1250 properties_(properties)
1251 {
1252 }
1253
1254 virtual CYExpression *Replace(CYContext &context);
1255 void Output(CYOutput &out, CYFlags flags) const;
1256 };
1257
1258 struct CYMember :
1259 CYExpression
1260 {
1261 CYExpression *object_;
1262 CYExpression *property_;
1263
1264 CYMember(CYExpression *object, CYExpression *property) :
1265 object_(object),
1266 property_(property)
1267 {
1268 }
1269
1270 void SetLeft(CYExpression *object) {
1271 object_ = object;
1272 }
1273 };
1274
1275 struct CYDirectMember :
1276 CYMember
1277 {
1278 CYDirectMember(CYExpression *object, CYExpression *property) :
1279 CYMember(object, property)
1280 {
1281 }
1282
1283 CYPrecedence(1)
1284 CYRightHand(false)
1285
1286 virtual CYExpression *Replace(CYContext &context);
1287 virtual void Output(CYOutput &out, CYFlags flags) const;
1288 };
1289
1290 struct CYIndirectMember :
1291 CYMember
1292 {
1293 CYIndirectMember(CYExpression *object, CYExpression *property) :
1294 CYMember(object, property)
1295 {
1296 }
1297
1298 CYPrecedence(1)
1299 CYRightHand(false)
1300
1301 virtual CYExpression *Replace(CYContext &context);
1302 virtual void Output(CYOutput &out, CYFlags flags) const;
1303 };
1304
1305 namespace cy {
1306 namespace Syntax {
1307
1308 struct New :
1309 CYExpression
1310 {
1311 CYExpression *constructor_;
1312 CYArgument *arguments_;
1313
1314 New(CYExpression *constructor, CYArgument *arguments) :
1315 constructor_(constructor),
1316 arguments_(arguments)
1317 {
1318 }
1319
1320 virtual int Precedence() const {
1321 return arguments_ == NULL ? 2 : 1;
1322 }
1323
1324 CYRightHand(false)
1325
1326 virtual CYExpression *Replace(CYContext &context);
1327 virtual void Output(CYOutput &out, CYFlags flags) const;
1328
1329 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1330 };
1331
1332 } }
1333
1334 struct CYCall :
1335 CYExpression
1336 {
1337 CYExpression *function_;
1338 CYArgument *arguments_;
1339
1340 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1341 function_(function),
1342 arguments_(arguments)
1343 {
1344 }
1345
1346 CYPrecedence(1)
1347 CYRightHand(false)
1348
1349 virtual CYExpression *Replace(CYContext &context);
1350 virtual void Output(CYOutput &out, CYFlags flags) const;
1351
1352 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1353 };
1354
1355 struct CYRubyProc;
1356
1357 struct CYRubyBlock :
1358 CYExpression
1359 {
1360 CYExpression *call_;
1361 CYRubyProc *proc_;
1362
1363 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1364 call_(call),
1365 proc_(proc)
1366 {
1367 }
1368
1369 CYPrecedence(1)
1370 CYRightHand(false)
1371
1372 virtual CYExpression *Replace(CYContext &context);
1373 virtual void Output(CYOutput &out, CYFlags flags) const;
1374 };
1375
1376 struct CYIf :
1377 CYStatement
1378 {
1379 CYExpression *test_;
1380 CYStatement *true_;
1381 CYStatement *false_;
1382
1383 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1384 test_(test),
1385 true_(_true),
1386 false_(_false)
1387 {
1388 }
1389
1390 virtual CYStatement *Replace(CYContext &context);
1391 virtual void Output(CYOutput &out, CYFlags flags) const;
1392 };
1393
1394 struct CYDoWhile :
1395 CYStatement
1396 {
1397 CYExpression *test_;
1398 CYStatement *code_;
1399
1400 CYDoWhile(CYExpression *test, CYStatement *code) :
1401 test_(test),
1402 code_(code)
1403 {
1404 }
1405
1406 virtual CYStatement *Replace(CYContext &context);
1407 virtual void Output(CYOutput &out, CYFlags flags) const;
1408 };
1409
1410 struct CYWhile :
1411 CYStatement
1412 {
1413 CYExpression *test_;
1414 CYStatement *code_;
1415
1416 CYWhile(CYExpression *test, CYStatement *code) :
1417 test_(test),
1418 code_(code)
1419 {
1420 }
1421
1422 virtual CYStatement *Replace(CYContext &context);
1423 virtual void Output(CYOutput &out, CYFlags flags) const;
1424 };
1425
1426 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1427 struct CYFunction {
1428 CYIdentifier *name_;
1429 CYFunctionParameter *parameters_;
1430 CYBlock code_;
1431
1432 CYNonLocal *nonlocal_;
1433 CYThisScope this_;
1434
1435 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1436 name_(name),
1437 parameters_(parameters),
1438 code_(statements),
1439 nonlocal_(NULL)
1440 {
1441 }
1442
1443 virtual ~CYFunction() {
1444 }
1445
1446 void Inject(CYContext &context);
1447 virtual void Replace_(CYContext &context, bool outer);
1448 virtual void Output(CYOutput &out, CYFlags flags) const;
1449 };
1450
1451 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1452 struct CYFunctionExpression :
1453 CYFunction,
1454 CYExpression
1455 {
1456 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1457 CYFunction(name, parameters, statements)
1458 {
1459 }
1460
1461 CYPrecedence(0)
1462 CYRightHand(false)
1463
1464 virtual CYExpression *Replace(CYContext &context);
1465 virtual void Output(CYOutput &out, CYFlags flags) const;
1466 };
1467
1468 // XXX: this should derive from CYAnonymousFunction
1469 struct CYFatArrow :
1470 CYFunction,
1471 CYExpression
1472 {
1473 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1474 CYFunction(NULL, parameters, statements)
1475 {
1476 }
1477
1478 CYPrecedence(0)
1479 CYRightHand(false)
1480
1481 virtual CYExpression *Replace(CYContext &context);
1482 virtual void Output(CYOutput &out, CYFlags flags) const;
1483 };
1484
1485 // XXX: this should derive from CYAnonymousFunctionExpression
1486 struct CYRubyProc :
1487 CYFunctionExpression
1488 {
1489 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1490 CYFunctionExpression(NULL, parameters, statements)
1491 {
1492 }
1493
1494 virtual CYExpression *Replace(CYContext &context);
1495 virtual void Output(CYOutput &out, CYFlags flags) const;
1496 };
1497
1498 // XXX: this should derive from CYNamedFunction
1499 struct CYFunctionStatement :
1500 CYFunction,
1501 CYStatement
1502 {
1503 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1504 CYFunction(name, parameters, statements)
1505 {
1506 }
1507
1508 virtual CYStatement *Replace(CYContext &context);
1509 virtual void Output(CYOutput &out, CYFlags flags) const;
1510 };
1511
1512 struct CYExpress :
1513 CYStatement
1514 {
1515 CYExpression *expression_;
1516
1517 CYExpress(CYExpression *expression) :
1518 expression_(expression)
1519 {
1520 if (expression_ == NULL)
1521 throw;
1522 }
1523
1524 virtual CYStatement *Replace(CYContext &context);
1525 virtual void Output(CYOutput &out, CYFlags flags) const;
1526 };
1527
1528 struct CYContinue :
1529 CYStatement
1530 {
1531 CYIdentifier *label_;
1532
1533 CYContinue(CYIdentifier *label) :
1534 label_(label)
1535 {
1536 }
1537
1538 virtual CYStatement *Replace(CYContext &context);
1539 virtual void Output(CYOutput &out, CYFlags flags) const;
1540 };
1541
1542 struct CYBreak :
1543 CYStatement
1544 {
1545 CYIdentifier *label_;
1546
1547 CYBreak(CYIdentifier *label) :
1548 label_(label)
1549 {
1550 }
1551
1552 virtual CYStatement *Replace(CYContext &context);
1553 virtual void Output(CYOutput &out, CYFlags flags) const;
1554 };
1555
1556 struct CYReturn :
1557 CYStatement
1558 {
1559 CYExpression *value_;
1560
1561 CYReturn(CYExpression *value) :
1562 value_(value)
1563 {
1564 }
1565
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1568 };
1569
1570 struct CYEmpty :
1571 CYStatement
1572 {
1573 virtual CYStatement *Replace(CYContext &context);
1574 virtual void Output(CYOutput &out, CYFlags flags) const;
1575 };
1576
1577 struct CYFinally :
1578 CYThing
1579 {
1580 CYBlock code_;
1581
1582 CYFinally(CYStatement *statements) :
1583 code_(statements)
1584 {
1585 }
1586
1587 void Replace(CYContext &context);
1588 virtual void Output(CYOutput &out) const;
1589 };
1590
1591 struct CYTypeSpecifier :
1592 CYThing
1593 {
1594 virtual CYExpression *Replace(CYContext &context) = 0;
1595 };
1596
1597 struct CYTypeError :
1598 CYTypeSpecifier
1599 {
1600 CYTypeError() {
1601 }
1602
1603 virtual CYExpression *Replace(CYContext &context);
1604 virtual void Output(CYOutput &out) const;
1605 };
1606
1607 struct CYTypeVoid :
1608 CYTypeSpecifier
1609 {
1610 CYTypeVoid() {
1611 }
1612
1613 virtual CYExpression *Replace(CYContext &context);
1614 virtual void Output(CYOutput &out) const;
1615 };
1616
1617 struct CYTypeVariable :
1618 CYTypeSpecifier
1619 {
1620 CYIdentifier *name_;
1621
1622 CYTypeVariable(CYIdentifier *name) :
1623 name_(name)
1624 {
1625 }
1626
1627 CYTypeVariable(const char *name) :
1628 name_(new($pool) CYIdentifier(name))
1629 {
1630 }
1631
1632 virtual CYExpression *Replace(CYContext &context);
1633 virtual void Output(CYOutput &out) const;
1634 };
1635
1636 struct CYTypeUnsigned :
1637 CYTypeSpecifier
1638 {
1639 CYTypeSpecifier *specifier_;
1640
1641 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1642 specifier_(specifier)
1643 {
1644 }
1645
1646 virtual CYExpression *Replace(CYContext &context);
1647 virtual void Output(CYOutput &out) const;
1648 };
1649
1650 struct CYTypeSigned :
1651 CYTypeSpecifier
1652 {
1653 CYTypeSpecifier *specifier_;
1654
1655 CYTypeSigned(CYTypeSpecifier *specifier) :
1656 specifier_(specifier)
1657 {
1658 }
1659
1660 virtual CYExpression *Replace(CYContext &context);
1661 virtual void Output(CYOutput &out) const;
1662 };
1663
1664 struct CYTypeLong :
1665 CYTypeSpecifier
1666 {
1667 CYTypeSpecifier *specifier_;
1668
1669 CYTypeLong(CYTypeSpecifier *specifier) :
1670 specifier_(specifier)
1671 {
1672 }
1673
1674 virtual CYExpression *Replace(CYContext &context);
1675 virtual void Output(CYOutput &out) const;
1676 };
1677
1678 struct CYTypeShort :
1679 CYTypeSpecifier
1680 {
1681 CYTypeSpecifier *specifier_;
1682
1683 CYTypeShort(CYTypeSpecifier *specifier) :
1684 specifier_(specifier)
1685 {
1686 }
1687
1688 virtual CYExpression *Replace(CYContext &context);
1689 virtual void Output(CYOutput &out) const;
1690 };
1691
1692 struct CYTypeModifier :
1693 CYNext<CYTypeModifier>
1694 {
1695 CYTypeModifier(CYTypeModifier *next) :
1696 CYNext<CYTypeModifier>(next)
1697 {
1698 }
1699
1700 virtual int Precedence() const = 0;
1701
1702 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1703 CYExpression *Replace(CYContext &context, CYExpression *type);
1704
1705 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1706 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1707 };
1708
1709 struct CYTypeArrayOf :
1710 CYTypeModifier
1711 {
1712 CYExpression *size_;
1713
1714 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1715 CYTypeModifier(next),
1716 size_(size)
1717 {
1718 }
1719
1720 CYPrecedence(1)
1721
1722 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1723 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1724 };
1725
1726 struct CYTypeConstant :
1727 CYTypeModifier
1728 {
1729 CYTypeConstant(CYTypeModifier *next = NULL) :
1730 CYTypeModifier(next)
1731 {
1732 }
1733
1734 CYPrecedence(0)
1735
1736 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1737 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1738 };
1739
1740 struct CYTypePointerTo :
1741 CYTypeModifier
1742 {
1743 CYTypePointerTo(CYTypeModifier *next = NULL) :
1744 CYTypeModifier(next)
1745 {
1746 }
1747
1748 CYPrecedence(0)
1749
1750 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1751 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1752 };
1753
1754 struct CYTypeVolatile :
1755 CYTypeModifier
1756 {
1757 CYTypeVolatile(CYTypeModifier *next = NULL) :
1758 CYTypeModifier(next)
1759 {
1760 }
1761
1762 CYPrecedence(0)
1763
1764 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1765 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1766 };
1767
1768 struct CYTypedIdentifier :
1769 CYNext<CYTypedIdentifier>,
1770 CYThing
1771 {
1772 CYIdentifier *identifier_;
1773 CYTypeSpecifier *specifier_;
1774 CYTypeModifier *modifier_;
1775
1776 CYTypedIdentifier(CYIdentifier *identifier = NULL) :
1777 identifier_(identifier),
1778 specifier_(NULL),
1779 modifier_(NULL)
1780 {
1781 }
1782
1783 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1784 identifier_(NULL),
1785 specifier_(specifier),
1786 modifier_(modifier)
1787 {
1788 }
1789
1790 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1791 CYSetLast(modifier_) = modifier;
1792 return this;
1793 }
1794
1795 virtual CYExpression *Replace(CYContext &context);
1796 virtual void Output(CYOutput &out) const;
1797 };
1798
1799 struct CYEncodedType :
1800 CYExpression
1801 {
1802 CYTypedIdentifier *typed_;
1803
1804 CYEncodedType(CYTypedIdentifier *typed) :
1805 typed_(typed)
1806 {
1807 }
1808
1809 CYPrecedence(1)
1810
1811 virtual CYExpression *Replace(CYContext &context);
1812 virtual void Output(CYOutput &out, CYFlags flags) const;
1813 };
1814
1815 struct CYTypedParameter :
1816 CYNext<CYTypedParameter>,
1817 CYThing
1818 {
1819 CYTypedIdentifier *typed_;
1820
1821 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1822 CYNext<CYTypedParameter>(next),
1823 typed_(typed)
1824 {
1825 }
1826
1827 CYArgument *Argument(CYContext &context);
1828 CYFunctionParameter *Parameters(CYContext &context);
1829 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1830
1831 virtual void Output(CYOutput &out) const;
1832 };
1833
1834 struct CYLambda :
1835 CYExpression
1836 {
1837 CYTypedIdentifier *typed_;
1838 CYTypedParameter *parameters_;
1839 CYStatement *statements_;
1840
1841 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1842 typed_(typed),
1843 parameters_(parameters),
1844 statements_(statements)
1845 {
1846 }
1847
1848 CYPrecedence(1)
1849
1850 virtual CYExpression *Replace(CYContext &context);
1851 virtual void Output(CYOutput &out, CYFlags flags) const;
1852 };
1853
1854 struct CYTypeDefinition :
1855 CYStatement
1856 {
1857 CYTypedIdentifier *typed_;
1858
1859 CYTypeDefinition(CYTypedIdentifier *typed) :
1860 typed_(typed)
1861 {
1862 }
1863
1864 virtual CYStatement *Replace(CYContext &context);
1865 virtual void Output(CYOutput &out, CYFlags flags) const;
1866 };
1867
1868 struct CYTypeBlockWith :
1869 CYTypeModifier
1870 {
1871 CYTypedParameter *parameters_;
1872
1873 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1874 CYTypeModifier(next),
1875 parameters_(parameters)
1876 {
1877 }
1878
1879 CYPrecedence(0)
1880
1881 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1882 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1883 };
1884
1885 struct CYTypeFunctionWith :
1886 CYTypeModifier
1887 {
1888 CYTypedParameter *parameters_;
1889
1890 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1891 CYTypeModifier(next),
1892 parameters_(parameters)
1893 {
1894 }
1895
1896 CYPrecedence(1)
1897
1898 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1899 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1900 };
1901
1902 namespace cy {
1903 namespace Syntax {
1904
1905 struct Catch :
1906 CYThing
1907 {
1908 CYIdentifier *name_;
1909 CYBlock code_;
1910
1911 Catch(CYIdentifier *name, CYStatement *statements) :
1912 name_(name),
1913 code_(statements)
1914 {
1915 }
1916
1917 void Replace(CYContext &context);
1918 virtual void Output(CYOutput &out) const;
1919 };
1920
1921 struct Try :
1922 CYStatement
1923 {
1924 CYBlock code_;
1925 Catch *catch_;
1926 CYFinally *finally_;
1927
1928 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1929 code_(statements),
1930 catch_(_catch),
1931 finally_(finally)
1932 {
1933 }
1934
1935 virtual CYStatement *Replace(CYContext &context);
1936 virtual void Output(CYOutput &out, CYFlags flags) const;
1937 };
1938
1939 struct Throw :
1940 CYStatement
1941 {
1942 CYExpression *value_;
1943
1944 Throw(CYExpression *value = NULL) :
1945 value_(value)
1946 {
1947 }
1948
1949 virtual CYStatement *Replace(CYContext &context);
1950 virtual void Output(CYOutput &out, CYFlags flags) const;
1951 };
1952
1953 } }
1954
1955 struct CYWith :
1956 CYStatement
1957 {
1958 CYExpression *scope_;
1959 CYStatement *code_;
1960
1961 CYWith(CYExpression *scope, CYStatement *code) :
1962 scope_(scope),
1963 code_(code)
1964 {
1965 }
1966
1967 virtual CYStatement *Replace(CYContext &context);
1968 virtual void Output(CYOutput &out, CYFlags flags) const;
1969 };
1970
1971 struct CYSwitch :
1972 CYStatement
1973 {
1974 CYExpression *value_;
1975 CYClause *clauses_;
1976
1977 CYSwitch(CYExpression *value, CYClause *clauses) :
1978 value_(value),
1979 clauses_(clauses)
1980 {
1981 }
1982
1983 virtual CYStatement *Replace(CYContext &context);
1984 virtual void Output(CYOutput &out, CYFlags flags) const;
1985 };
1986
1987 struct CYDebugger :
1988 CYStatement
1989 {
1990 CYDebugger()
1991 {
1992 }
1993
1994 virtual CYStatement *Replace(CYContext &context);
1995 virtual void Output(CYOutput &out, CYFlags flags) const;
1996 };
1997
1998 struct CYCondition :
1999 CYExpression
2000 {
2001 CYExpression *test_;
2002 CYExpression *true_;
2003 CYExpression *false_;
2004
2005 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2006 test_(test),
2007 true_(_true),
2008 false_(_false)
2009 {
2010 }
2011
2012 CYPrecedence(15)
2013
2014 virtual CYExpression *Replace(CYContext &context);
2015 virtual void Output(CYOutput &out, CYFlags flags) const;
2016 };
2017
2018 struct CYAddressOf :
2019 CYPrefix
2020 {
2021 CYAddressOf(CYExpression *rhs) :
2022 CYPrefix(rhs)
2023 {
2024 }
2025
2026 virtual const char *Operator() const {
2027 return "&";
2028 }
2029
2030 CYAlphabetic(false)
2031
2032 virtual CYExpression *Replace(CYContext &context);
2033 };
2034
2035 struct CYIndirect :
2036 CYPrefix
2037 {
2038 CYIndirect(CYExpression *rhs) :
2039 CYPrefix(rhs)
2040 {
2041 }
2042
2043 virtual const char *Operator() const {
2044 return "*";
2045 }
2046
2047 CYAlphabetic(false)
2048
2049 virtual CYExpression *Replace(CYContext &context);
2050 };
2051
2052 #define CYReplace \
2053 virtual CYExpression *Replace(CYContext &context);
2054
2055 #define CYPostfix_(op, name, args...) \
2056 struct CY ## name : \
2057 CYPostfix \
2058 { args \
2059 CY ## name(CYExpression *lhs) : \
2060 CYPostfix(lhs) \
2061 { \
2062 } \
2063 \
2064 virtual const char *Operator() const { \
2065 return op; \
2066 } \
2067 };
2068
2069 #define CYPrefix_(alphabetic, op, name, args...) \
2070 struct CY ## name : \
2071 CYPrefix \
2072 { args \
2073 CY ## name(CYExpression *rhs) : \
2074 CYPrefix(rhs) \
2075 { \
2076 } \
2077 \
2078 CYAlphabetic(alphabetic) \
2079 \
2080 virtual const char *Operator() const { \
2081 return op; \
2082 } \
2083 };
2084
2085 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2086 struct CY ## name : \
2087 CYInfix \
2088 { args \
2089 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2090 CYInfix(lhs, rhs) \
2091 { \
2092 } \
2093 \
2094 CYAlphabetic(alphabetic) \
2095 CYPrecedence(precedence) \
2096 \
2097 virtual const char *Operator() const { \
2098 return op; \
2099 } \
2100 };
2101
2102 #define CYAssignment_(op, name, args...) \
2103 struct CY ## name ## Assign : \
2104 CYAssignment \
2105 { args \
2106 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2107 CYAssignment(lhs, rhs) \
2108 { \
2109 } \
2110 \
2111 virtual const char *Operator() const { \
2112 return op; \
2113 } \
2114 };
2115
2116 CYPostfix_("++", PostIncrement)
2117 CYPostfix_("--", PostDecrement)
2118
2119 CYPrefix_(true, "delete", Delete)
2120 CYPrefix_(true, "void", Void)
2121 CYPrefix_(true, "typeof", TypeOf)
2122 CYPrefix_(false, "++", PreIncrement)
2123 CYPrefix_(false, "--", PreDecrement)
2124 CYPrefix_(false, "+", Affirm)
2125 CYPrefix_(false, "-", Negate)
2126 CYPrefix_(false, "~", BitwiseNot)
2127 CYPrefix_(false, "!", LogicalNot)
2128
2129 CYInfix_(false, 5, "*", Multiply, CYReplace)
2130 CYInfix_(false, 5, "/", Divide)
2131 CYInfix_(false, 5, "%", Modulus)
2132 CYInfix_(false, 6, "+", Add, CYReplace)
2133 CYInfix_(false, 6, "-", Subtract)
2134 CYInfix_(false, 7, "<<", ShiftLeft)
2135 CYInfix_(false, 7, ">>", ShiftRightSigned)
2136 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2137 CYInfix_(false, 8, "<", Less)
2138 CYInfix_(false, 8, ">", Greater)
2139 CYInfix_(false, 8, "<=", LessOrEqual)
2140 CYInfix_(false, 8, ">=", GreaterOrEqual)
2141 CYInfix_(true, 8, "instanceof", InstanceOf)
2142 CYInfix_(true, 8, "in", In)
2143 CYInfix_(false, 9, "==", Equal)
2144 CYInfix_(false, 9, "!=", NotEqual)
2145 CYInfix_(false, 9, "===", Identical)
2146 CYInfix_(false, 9, "!==", NotIdentical)
2147 CYInfix_(false, 10, "&", BitwiseAnd)
2148 CYInfix_(false, 11, "^", BitwiseXOr)
2149 CYInfix_(false, 12, "|", BitwiseOr)
2150 CYInfix_(false, 13, "&&", LogicalAnd)
2151 CYInfix_(false, 14, "||", LogicalOr)
2152
2153 CYAssignment_("=", )
2154 CYAssignment_("*=", Multiply)
2155 CYAssignment_("/=", Divide)
2156 CYAssignment_("%=", Modulus)
2157 CYAssignment_("+=", Add)
2158 CYAssignment_("-=", Subtract)
2159 CYAssignment_("<<=", ShiftLeft)
2160 CYAssignment_(">>=", ShiftRightSigned)
2161 CYAssignment_(">>>=", ShiftRightUnsigned)
2162 CYAssignment_("&=", BitwiseAnd)
2163 CYAssignment_("^=", BitwiseXOr)
2164 CYAssignment_("|=", BitwiseOr)
2165
2166 #endif/*CYCRIPT_PARSER_HPP*/