]> git.saurik.com Git - cycript.git/blob - Parser.hpp
Fix crash when trying to use specifier without int.
[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 class CYStream :
464 public std::istream
465 {
466 private:
467 class CYBuffer :
468 public std::streambuf
469 {
470 public:
471 CYBuffer(const char *start, const char *end) {
472 setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
473 }
474 } buffer_;
475
476 public:
477 CYStream(const char *start, const char *end) :
478 std::istream(&buffer_),
479 buffer_(start, end)
480 {
481 }
482 };
483
484 struct CYForInitialiser {
485 virtual ~CYForInitialiser() {
486 }
487
488 virtual CYExpression *Replace(CYContext &context) = 0;
489 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
490 };
491
492 struct CYForInInitialiser {
493 virtual ~CYForInInitialiser() {
494 }
495
496 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
497 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
498
499 virtual CYExpression *Replace(CYContext &context) = 0;
500 virtual CYAssignment *Assignment(CYContext &context) = 0;
501
502 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
503 };
504
505 struct CYNumber;
506 struct CYString;
507
508 struct CYExpression :
509 CYNext<CYExpression>,
510 CYForInitialiser,
511 CYForInInitialiser,
512 CYClassName,
513 CYThing
514 {
515 virtual int Precedence() const = 0;
516
517 virtual bool RightHand() const {
518 return true;
519 }
520
521 virtual void ForIn(CYOutput &out, CYFlags flags) const;
522 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
523
524 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
525
526 virtual void Output(CYOutput &out) const;
527 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
528 void Output(CYOutput &out, int precedence, CYFlags flags) const;
529
530 virtual CYExpression *ClassName(CYContext &context, bool object);
531 virtual void ClassName(CYOutput &out, bool object) const;
532
533 virtual CYExpression *Replace(CYContext &context) = 0;
534 virtual CYAssignment *Assignment(CYContext &context);
535
536 virtual CYExpression *Primitive(CYContext &context) {
537 return this;
538 }
539
540 virtual CYNumber *Number(CYContext &context) {
541 return NULL;
542 }
543
544 virtual CYString *String(CYContext &context) {
545 return NULL;
546 }
547
548 virtual const char *Word() const {
549 return NULL;
550 }
551 };
552
553 #define CYAlphabetic(value) \
554 virtual bool Alphabetic() const { \
555 return value; \
556 }
557
558 #define CYPrecedence(value) \
559 static const int Precedence_ = value; \
560 virtual int Precedence() const { \
561 return Precedence_; \
562 }
563
564 #define CYRightHand(value) \
565 virtual bool RightHand() const { \
566 return value; \
567 }
568
569 struct CYCompound :
570 CYExpression
571 {
572 CYExpression *expressions_;
573
574 CYCompound(CYExpression *expressions = NULL) :
575 expressions_(expressions)
576 {
577 }
578
579 void AddPrev(CYExpression *expression) {
580 CYSetLast(expression) = expressions_;
581 expressions_ = expression;
582 }
583
584 CYPrecedence(17)
585
586 virtual CYExpression *Replace(CYContext &context);
587 void Output(CYOutput &out, CYFlags flags) const;
588
589 virtual CYExpression *Primitive(CYContext &context);
590 };
591
592 struct CYDeclaration;
593
594 struct CYFunctionParameter :
595 CYNext<CYFunctionParameter>,
596 CYThing
597 {
598 CYForInInitialiser *initialiser_;
599
600 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
601 CYNext<CYFunctionParameter>(next),
602 initialiser_(initialiser)
603 {
604 }
605
606 void Replace(CYContext &context, CYBlock &code);
607 void Output(CYOutput &out) const;
608 };
609
610 struct CYComprehension :
611 CYNext<CYComprehension>,
612 CYThing
613 {
614 CYComprehension(CYComprehension *next = NULL) :
615 CYNext<CYComprehension>(next)
616 {
617 }
618
619 CYComprehension *Modify(CYComprehension *next) {
620 next_ = next;
621 return this;
622 }
623
624 virtual const char *Name() const = 0;
625
626 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
627 CYFunctionParameter *Parameters(CYContext &context) const;
628 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
629 virtual void Output(CYOutput &out) const = 0;
630 };
631
632 struct CYForInComprehension :
633 CYComprehension
634 {
635 CYIdentifier *name_;
636 CYExpression *set_;
637
638 CYForInComprehension(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 CYForOfComprehension :
655 CYComprehension
656 {
657 CYIdentifier *name_;
658 CYExpression *set_;
659
660 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
661 CYComprehension(next),
662 name_(name),
663 set_(set)
664 {
665 }
666
667 virtual const char *Name() const {
668 return name_->Word();
669 }
670
671 virtual CYFunctionParameter *Parameter(CYContext &context) const;
672 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
673 virtual void Output(CYOutput &out) const;
674 };
675
676 struct CYIfComprehension :
677 CYComprehension
678 {
679 CYExpression *test_;
680
681 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
682 CYComprehension(next),
683 test_(test)
684 {
685 }
686
687 virtual const char *Name() const {
688 return NULL;
689 }
690
691 virtual CYFunctionParameter *Parameter(CYContext &context) const;
692 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
693 virtual void Output(CYOutput &out) const;
694 };
695
696 struct CYArrayComprehension :
697 CYExpression
698 {
699 CYExpression *expression_;
700 CYComprehension *comprehensions_;
701
702 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
703 expression_(expression),
704 comprehensions_(comprehensions)
705 {
706 }
707
708 CYPrecedence(0)
709
710 virtual CYExpression *Replace(CYContext &context);
711 virtual void Output(CYOutput &out, CYFlags flags) const;
712 };
713
714 struct CYLiteral :
715 CYExpression
716 {
717 CYPrecedence(0)
718 CYRightHand(false)
719 };
720
721 struct CYTrivial :
722 CYLiteral
723 {
724 virtual CYExpression *Replace(CYContext &context);
725 };
726
727 struct CYMagic :
728 CYExpression
729 {
730 CYPrecedence(0)
731 CYRightHand(false)
732 };
733
734 struct CYRange {
735 uint64_t lo_;
736 uint64_t hi_;
737
738 CYRange(uint64_t lo, uint64_t hi) :
739 lo_(lo), hi_(hi)
740 {
741 }
742
743 bool operator [](uint8_t value) const {
744 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
745 }
746
747 void operator()(uint8_t value) {
748 if (value >> 7)
749 return;
750 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
751 }
752 };
753
754 extern CYRange DigitRange_;
755 extern CYRange WordStartRange_;
756 extern CYRange WordEndRange_;
757
758 struct CYString :
759 CYTrivial,
760 CYPropertyName
761 {
762 const char *value_;
763 size_t size_;
764
765 CYString() :
766 value_(NULL),
767 size_(0)
768 {
769 }
770
771 CYString(const char *value) :
772 value_(value),
773 size_(strlen(value))
774 {
775 }
776
777 CYString(const char *value, size_t size) :
778 value_(value),
779 size_(size)
780 {
781 }
782
783 CYString(const CYWord *word) :
784 value_(word->Word()),
785 size_(strlen(value_))
786 {
787 }
788
789 const char *Value() const {
790 return value_;
791 }
792
793 virtual const char *Word() const;
794
795 virtual CYNumber *Number(CYContext &context);
796 virtual CYString *String(CYContext &context);
797
798 CYString *Concat(CYContext &out, CYString *rhs) const;
799 virtual void Output(CYOutput &out, CYFlags flags) const;
800 virtual void PropertyName(CYOutput &out) const;
801 };
802
803 struct CYNumber :
804 CYTrivial,
805 CYPropertyName
806 {
807 double value_;
808
809 CYNumber(double value) :
810 value_(value)
811 {
812 }
813
814 double Value() const {
815 return value_;
816 }
817
818 virtual CYNumber *Number(CYContext &context);
819 virtual CYString *String(CYContext &context);
820
821 virtual void Output(CYOutput &out, CYFlags flags) const;
822 virtual void PropertyName(CYOutput &out) const;
823 };
824
825 struct CYRegEx :
826 CYTrivial
827 {
828 const char *value_;
829
830 CYRegEx(const char *value) :
831 value_(value)
832 {
833 }
834
835 const char *Value() const {
836 return value_;
837 }
838
839 virtual void Output(CYOutput &out, CYFlags flags) const;
840 };
841
842 struct CYNull :
843 CYWord,
844 CYTrivial
845 {
846 CYNull() :
847 CYWord("null")
848 {
849 }
850
851 virtual CYNumber *Number(CYContext &context);
852 virtual CYString *String(CYContext &context);
853
854 virtual void Output(CYOutput &out, CYFlags flags) const;
855 };
856
857 struct CYThis :
858 CYWord,
859 CYMagic
860 {
861 CYThis() :
862 CYWord("this")
863 {
864 }
865
866 virtual CYExpression *Replace(CYContext &context);
867 virtual void Output(CYOutput &out, CYFlags flags) const;
868 };
869
870 struct CYBoolean :
871 CYTrivial
872 {
873 virtual bool Value() const = 0;
874 virtual void Output(CYOutput &out, CYFlags flags) const;
875 };
876
877 struct CYFalse :
878 CYWord,
879 CYBoolean
880 {
881 CYFalse() :
882 CYWord("false")
883 {
884 }
885
886 virtual bool Value() const {
887 return false;
888 }
889
890 virtual CYNumber *Number(CYContext &context);
891 virtual CYString *String(CYContext &context);
892 };
893
894 struct CYTrue :
895 CYWord,
896 CYBoolean
897 {
898 CYTrue() :
899 CYWord("true")
900 {
901 }
902
903 virtual bool Value() const {
904 return true;
905 }
906
907 virtual CYNumber *Number(CYContext &context);
908 virtual CYString *String(CYContext &context);
909 };
910
911 struct CYVariable :
912 CYExpression
913 {
914 CYIdentifier *name_;
915
916 CYVariable(CYIdentifier *name) :
917 name_(name)
918 {
919 }
920
921 CYVariable(const char *name) :
922 name_(new($pool) CYIdentifier(name))
923 {
924 }
925
926 CYPrecedence(0)
927 CYRightHand(false)
928
929 virtual CYExpression *Replace(CYContext &context);
930 virtual void Output(CYOutput &out, CYFlags flags) const;
931 };
932
933 struct CYPrefix :
934 CYExpression
935 {
936 CYExpression *rhs_;
937
938 CYPrefix(CYExpression *rhs) :
939 rhs_(rhs)
940 {
941 }
942
943 virtual bool Alphabetic() const = 0;
944 virtual const char *Operator() const = 0;
945
946 CYPrecedence(4)
947
948 virtual CYExpression *Replace(CYContext &context);
949 virtual void Output(CYOutput &out, CYFlags flags) const;
950 };
951
952 struct CYInfix :
953 CYExpression
954 {
955 CYExpression *lhs_;
956 CYExpression *rhs_;
957
958 CYInfix(CYExpression *lhs, CYExpression *rhs) :
959 lhs_(lhs),
960 rhs_(rhs)
961 {
962 }
963
964 void SetLeft(CYExpression *lhs) {
965 lhs_ = lhs;
966 }
967
968 virtual bool Alphabetic() const = 0;
969 virtual const char *Operator() const = 0;
970
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
973 };
974
975 struct CYPostfix :
976 CYExpression
977 {
978 CYExpression *lhs_;
979
980 CYPostfix(CYExpression *lhs) :
981 lhs_(lhs)
982 {
983 }
984
985 virtual const char *Operator() const = 0;
986
987 CYPrecedence(3)
988
989 virtual CYExpression *Replace(CYContext &context);
990 virtual void Output(CYOutput &out, CYFlags flags) const;
991 };
992
993 struct CYAssignment :
994 CYExpression
995 {
996 CYExpression *lhs_;
997 CYExpression *rhs_;
998
999 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1000 lhs_(lhs),
1001 rhs_(rhs)
1002 {
1003 }
1004
1005 void SetLeft(CYExpression *lhs) {
1006 lhs_ = lhs;
1007 }
1008
1009 virtual const char *Operator() const = 0;
1010
1011 CYPrecedence(16)
1012
1013 virtual CYExpression *Replace(CYContext &context);
1014 virtual void Output(CYOutput &out, CYFlags flags) const;
1015 };
1016
1017 struct CYArgument :
1018 CYNext<CYArgument>,
1019 CYThing
1020 {
1021 CYWord *name_;
1022 CYExpression *value_;
1023
1024 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1025 CYNext<CYArgument>(next),
1026 name_(NULL),
1027 value_(value)
1028 {
1029 }
1030
1031 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1032 CYNext<CYArgument>(next),
1033 name_(name),
1034 value_(value)
1035 {
1036 }
1037
1038 CYArgument *Replace(CYContext &context);
1039 void Output(CYOutput &out) const;
1040 };
1041
1042 struct CYBlank :
1043 public CYWord
1044 {
1045 CYBlank() :
1046 CYWord("")
1047 {
1048 }
1049 };
1050
1051 struct CYClause :
1052 CYThing,
1053 CYNext<CYClause>
1054 {
1055 CYExpression *case_;
1056 CYStatement *statements_;
1057
1058 CYClause(CYExpression *_case, CYStatement *statements) :
1059 case_(_case),
1060 statements_(statements)
1061 {
1062 }
1063
1064 void Replace(CYContext &context);
1065 virtual void Output(CYOutput &out) const;
1066 };
1067
1068 struct CYElement :
1069 CYNext<CYElement>,
1070 CYThing
1071 {
1072 CYExpression *value_;
1073
1074 CYElement(CYExpression *value, CYElement *next) :
1075 CYNext<CYElement>(next),
1076 value_(value)
1077 {
1078 }
1079
1080 void Replace(CYContext &context);
1081 void Output(CYOutput &out) const;
1082 };
1083
1084 struct CYArray :
1085 CYLiteral
1086 {
1087 CYElement *elements_;
1088
1089 CYArray(CYElement *elements = NULL) :
1090 elements_(elements)
1091 {
1092 }
1093
1094 virtual CYExpression *Replace(CYContext &context);
1095 virtual void Output(CYOutput &out, CYFlags flags) const;
1096 };
1097
1098 struct CYProperty :
1099 CYNext<CYProperty>,
1100 CYThing
1101 {
1102 CYPropertyName *name_;
1103 CYExpression *value_;
1104
1105 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1106 CYNext<CYProperty>(next),
1107 name_(name),
1108 value_(value)
1109 {
1110 }
1111
1112 void Replace(CYContext &context);
1113 virtual void Output(CYOutput &out) const;
1114 };
1115
1116 struct CYDeclaration :
1117 CYForInInitialiser
1118 {
1119 CYIdentifier *identifier_;
1120 CYExpression *initialiser_;
1121
1122 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1123 identifier_(identifier),
1124 initialiser_(initialiser)
1125 {
1126 }
1127
1128 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1129 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1130
1131 virtual CYExpression *Replace(CYContext &context);
1132
1133 virtual CYAssignment *Assignment(CYContext &context);
1134 CYVariable *Variable(CYContext &context);
1135
1136 virtual void Output(CYOutput &out, CYFlags flags) const;
1137 };
1138
1139 struct CYDeclarations :
1140 CYNext<CYDeclarations>,
1141 CYThing
1142 {
1143 CYDeclaration *declaration_;
1144
1145 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1146 CYNext<CYDeclarations>(next),
1147 declaration_(declaration)
1148 {
1149 }
1150
1151 void Replace(CYContext &context);
1152
1153 CYCompound *Compound(CYContext &context);
1154 CYProperty *Property(CYContext &context);
1155 CYArgument *Argument(CYContext &context);
1156 CYFunctionParameter *Parameter(CYContext &context);
1157
1158 virtual void Output(CYOutput &out) const;
1159 virtual void Output(CYOutput &out, CYFlags flags) const;
1160 };
1161
1162 struct CYForDeclarations :
1163 CYForInitialiser
1164 {
1165 CYDeclarations *declarations_;
1166
1167 CYForDeclarations(CYDeclarations *declarations) :
1168 declarations_(declarations)
1169 {
1170 }
1171
1172 virtual CYCompound *Replace(CYContext &context);
1173 virtual void Output(CYOutput &out, CYFlags flags) const;
1174 };
1175
1176 struct CYVar :
1177 CYStatement
1178 {
1179 CYDeclarations *declarations_;
1180
1181 CYVar(CYDeclarations *declarations) :
1182 declarations_(declarations)
1183 {
1184 }
1185
1186 virtual CYStatement *Replace(CYContext &context);
1187 virtual void Output(CYOutput &out, CYFlags flags) const;
1188 };
1189
1190 struct CYLetStatement :
1191 CYStatement
1192 {
1193 CYDeclarations *declarations_;
1194 CYStatement *code_;
1195
1196 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1197 declarations_(declarations),
1198 code_(code)
1199 {
1200 }
1201
1202 virtual CYStatement *Replace(CYContext &context);
1203 virtual void Output(CYOutput &out, CYFlags flags) const;
1204 };
1205
1206 struct CYFor :
1207 CYStatement
1208 {
1209 CYForInitialiser *initialiser_;
1210 CYExpression *test_;
1211 CYExpression *increment_;
1212 CYStatement *code_;
1213
1214 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1215 initialiser_(initialiser),
1216 test_(test),
1217 increment_(increment),
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 CYForIn :
1227 CYStatement
1228 {
1229 CYForInInitialiser *initialiser_;
1230 CYExpression *set_;
1231 CYStatement *code_;
1232
1233 CYForIn(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 CYForOf :
1245 CYStatement
1246 {
1247 CYForInInitialiser *initialiser_;
1248 CYExpression *set_;
1249 CYStatement *code_;
1250
1251 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1252 initialiser_(initialiser),
1253 set_(set),
1254 code_(code)
1255 {
1256 }
1257
1258 virtual CYStatement *Replace(CYContext &context);
1259 virtual void Output(CYOutput &out, CYFlags flags) const;
1260 };
1261
1262 struct CYObject :
1263 CYLiteral
1264 {
1265 CYProperty *properties_;
1266
1267 CYObject(CYProperty *properties = NULL) :
1268 properties_(properties)
1269 {
1270 }
1271
1272 virtual CYExpression *Replace(CYContext &context);
1273 void Output(CYOutput &out, CYFlags flags) const;
1274 };
1275
1276 struct CYMember :
1277 CYExpression
1278 {
1279 CYExpression *object_;
1280 CYExpression *property_;
1281
1282 CYMember(CYExpression *object, CYExpression *property) :
1283 object_(object),
1284 property_(property)
1285 {
1286 }
1287
1288 void SetLeft(CYExpression *object) {
1289 object_ = object;
1290 }
1291 };
1292
1293 struct CYDirectMember :
1294 CYMember
1295 {
1296 CYDirectMember(CYExpression *object, CYExpression *property) :
1297 CYMember(object, property)
1298 {
1299 }
1300
1301 CYPrecedence(1)
1302 CYRightHand(false)
1303
1304 virtual CYExpression *Replace(CYContext &context);
1305 virtual void Output(CYOutput &out, CYFlags flags) const;
1306 };
1307
1308 struct CYIndirectMember :
1309 CYMember
1310 {
1311 CYIndirectMember(CYExpression *object, CYExpression *property) :
1312 CYMember(object, property)
1313 {
1314 }
1315
1316 CYPrecedence(1)
1317 CYRightHand(false)
1318
1319 virtual CYExpression *Replace(CYContext &context);
1320 virtual void Output(CYOutput &out, CYFlags flags) const;
1321 };
1322
1323 namespace cy {
1324 namespace Syntax {
1325
1326 struct New :
1327 CYExpression
1328 {
1329 CYExpression *constructor_;
1330 CYArgument *arguments_;
1331
1332 New(CYExpression *constructor, CYArgument *arguments) :
1333 constructor_(constructor),
1334 arguments_(arguments)
1335 {
1336 }
1337
1338 virtual int Precedence() const {
1339 return arguments_ == NULL ? 2 : 1;
1340 }
1341
1342 CYRightHand(false)
1343
1344 virtual CYExpression *Replace(CYContext &context);
1345 virtual void Output(CYOutput &out, CYFlags flags) const;
1346
1347 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1348 };
1349
1350 } }
1351
1352 struct CYCall :
1353 CYExpression
1354 {
1355 CYExpression *function_;
1356 CYArgument *arguments_;
1357
1358 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1359 function_(function),
1360 arguments_(arguments)
1361 {
1362 }
1363
1364 CYPrecedence(1)
1365 CYRightHand(false)
1366
1367 virtual CYExpression *Replace(CYContext &context);
1368 virtual void Output(CYOutput &out, CYFlags flags) const;
1369
1370 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1371 };
1372
1373 struct CYRubyProc;
1374
1375 struct CYRubyBlock :
1376 CYExpression
1377 {
1378 CYExpression *call_;
1379 CYRubyProc *proc_;
1380
1381 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1382 call_(call),
1383 proc_(proc)
1384 {
1385 }
1386
1387 CYPrecedence(1)
1388 CYRightHand(false)
1389
1390 virtual CYExpression *Replace(CYContext &context);
1391 virtual void Output(CYOutput &out, CYFlags flags) const;
1392 };
1393
1394 struct CYIf :
1395 CYStatement
1396 {
1397 CYExpression *test_;
1398 CYStatement *true_;
1399 CYStatement *false_;
1400
1401 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1402 test_(test),
1403 true_(_true),
1404 false_(_false)
1405 {
1406 }
1407
1408 virtual CYStatement *Replace(CYContext &context);
1409 virtual void Output(CYOutput &out, CYFlags flags) const;
1410 };
1411
1412 struct CYDoWhile :
1413 CYStatement
1414 {
1415 CYExpression *test_;
1416 CYStatement *code_;
1417
1418 CYDoWhile(CYExpression *test, CYStatement *code) :
1419 test_(test),
1420 code_(code)
1421 {
1422 }
1423
1424 virtual CYStatement *Replace(CYContext &context);
1425 virtual void Output(CYOutput &out, CYFlags flags) const;
1426 };
1427
1428 struct CYWhile :
1429 CYStatement
1430 {
1431 CYExpression *test_;
1432 CYStatement *code_;
1433
1434 CYWhile(CYExpression *test, CYStatement *code) :
1435 test_(test),
1436 code_(code)
1437 {
1438 }
1439
1440 virtual CYStatement *Replace(CYContext &context);
1441 virtual void Output(CYOutput &out, CYFlags flags) const;
1442 };
1443
1444 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1445 struct CYFunction {
1446 CYIdentifier *name_;
1447 CYFunctionParameter *parameters_;
1448 CYBlock code_;
1449
1450 CYNonLocal *nonlocal_;
1451 CYThisScope this_;
1452
1453 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1454 name_(name),
1455 parameters_(parameters),
1456 code_(statements),
1457 nonlocal_(NULL)
1458 {
1459 }
1460
1461 virtual ~CYFunction() {
1462 }
1463
1464 void Inject(CYContext &context);
1465 virtual void Replace_(CYContext &context, bool outer);
1466 virtual void Output(CYOutput &out, CYFlags flags) const;
1467 };
1468
1469 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1470 struct CYFunctionExpression :
1471 CYFunction,
1472 CYExpression
1473 {
1474 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1475 CYFunction(name, parameters, statements)
1476 {
1477 }
1478
1479 CYPrecedence(0)
1480 CYRightHand(false)
1481
1482 virtual CYExpression *Replace(CYContext &context);
1483 virtual void Output(CYOutput &out, CYFlags flags) const;
1484 };
1485
1486 // XXX: this should derive from CYAnonymousFunction
1487 struct CYFatArrow :
1488 CYFunction,
1489 CYExpression
1490 {
1491 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1492 CYFunction(NULL, parameters, statements)
1493 {
1494 }
1495
1496 CYPrecedence(0)
1497 CYRightHand(false)
1498
1499 virtual CYExpression *Replace(CYContext &context);
1500 virtual void Output(CYOutput &out, CYFlags flags) const;
1501 };
1502
1503 // XXX: this should derive from CYAnonymousFunctionExpression
1504 struct CYRubyProc :
1505 CYFunctionExpression
1506 {
1507 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1508 CYFunctionExpression(NULL, parameters, statements)
1509 {
1510 }
1511
1512 virtual CYExpression *Replace(CYContext &context);
1513 virtual void Output(CYOutput &out, CYFlags flags) const;
1514 };
1515
1516 // XXX: this should derive from CYNamedFunction
1517 struct CYFunctionStatement :
1518 CYFunction,
1519 CYStatement
1520 {
1521 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1522 CYFunction(name, parameters, statements)
1523 {
1524 }
1525
1526 virtual CYStatement *Replace(CYContext &context);
1527 virtual void Output(CYOutput &out, CYFlags flags) const;
1528 };
1529
1530 struct CYExpress :
1531 CYStatement
1532 {
1533 CYExpression *expression_;
1534
1535 CYExpress(CYExpression *expression) :
1536 expression_(expression)
1537 {
1538 if (expression == NULL)
1539 throw;
1540 }
1541
1542 virtual CYStatement *Replace(CYContext &context);
1543 virtual void Output(CYOutput &out, CYFlags flags) const;
1544 };
1545
1546 struct CYContinue :
1547 CYStatement
1548 {
1549 CYIdentifier *label_;
1550
1551 CYContinue(CYIdentifier *label) :
1552 label_(label)
1553 {
1554 }
1555
1556 virtual CYStatement *Replace(CYContext &context);
1557 virtual void Output(CYOutput &out, CYFlags flags) const;
1558 };
1559
1560 struct CYBreak :
1561 CYStatement
1562 {
1563 CYIdentifier *label_;
1564
1565 CYBreak(CYIdentifier *label) :
1566 label_(label)
1567 {
1568 }
1569
1570 virtual CYStatement *Replace(CYContext &context);
1571 virtual void Output(CYOutput &out, CYFlags flags) const;
1572 };
1573
1574 struct CYReturn :
1575 CYStatement
1576 {
1577 CYExpression *value_;
1578
1579 CYReturn(CYExpression *value) :
1580 value_(value)
1581 {
1582 }
1583
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1586 };
1587
1588 struct CYEmpty :
1589 CYStatement
1590 {
1591 virtual CYStatement *Replace(CYContext &context);
1592 virtual void Output(CYOutput &out, CYFlags flags) const;
1593 };
1594
1595 struct CYFinally :
1596 CYThing
1597 {
1598 CYBlock code_;
1599
1600 CYFinally(CYStatement *statements) :
1601 code_(statements)
1602 {
1603 }
1604
1605 void Replace(CYContext &context);
1606 virtual void Output(CYOutput &out) const;
1607 };
1608
1609 struct CYTypeSpecifier :
1610 CYThing
1611 {
1612 virtual CYExpression *Replace(CYContext &context) = 0;
1613 };
1614
1615 struct CYTypeVoid :
1616 CYTypeSpecifier
1617 {
1618 CYTypeVoid() {
1619 }
1620
1621 virtual CYExpression *Replace(CYContext &context);
1622 virtual void Output(CYOutput &out) const;
1623 };
1624
1625 struct CYTypeVariable :
1626 CYTypeSpecifier
1627 {
1628 CYIdentifier *name_;
1629
1630 CYTypeVariable(CYIdentifier *name) :
1631 name_(name)
1632 {
1633 }
1634
1635 CYTypeVariable(const char *name) :
1636 name_(new($pool) CYIdentifier(name))
1637 {
1638 }
1639
1640 virtual CYExpression *Replace(CYContext &context);
1641 virtual void Output(CYOutput &out) const;
1642 };
1643
1644 struct CYTypeUnsigned :
1645 CYTypeSpecifier
1646 {
1647 CYTypeSpecifier *specifier_;
1648
1649 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1650 specifier_(specifier)
1651 {
1652 }
1653
1654 virtual CYExpression *Replace(CYContext &context);
1655 virtual void Output(CYOutput &out) const;
1656 };
1657
1658 struct CYTypeSigned :
1659 CYTypeSpecifier
1660 {
1661 CYTypeSpecifier *specifier_;
1662
1663 CYTypeSigned(CYTypeSpecifier *specifier) :
1664 specifier_(specifier)
1665 {
1666 }
1667
1668 virtual CYExpression *Replace(CYContext &context);
1669 virtual void Output(CYOutput &out) const;
1670 };
1671
1672 struct CYTypeLong :
1673 CYTypeSpecifier
1674 {
1675 CYTypeSpecifier *specifier_;
1676
1677 CYTypeLong(CYTypeSpecifier *specifier) :
1678 specifier_(specifier)
1679 {
1680 }
1681
1682 virtual CYExpression *Replace(CYContext &context);
1683 virtual void Output(CYOutput &out) const;
1684 };
1685
1686 struct CYTypeShort :
1687 CYTypeSpecifier
1688 {
1689 CYTypeSpecifier *specifier_;
1690
1691 CYTypeShort(CYTypeSpecifier *specifier) :
1692 specifier_(specifier)
1693 {
1694 }
1695
1696 virtual CYExpression *Replace(CYContext &context);
1697 virtual void Output(CYOutput &out) const;
1698 };
1699
1700 struct CYTypeModifier :
1701 CYNext<CYTypeModifier>
1702 {
1703 CYTypeModifier(CYTypeModifier *next) :
1704 CYNext<CYTypeModifier>(next)
1705 {
1706 }
1707
1708 virtual int Precedence() const = 0;
1709
1710 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1711 CYExpression *Replace(CYContext &context, CYExpression *type);
1712
1713 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1714 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1715 };
1716
1717 struct CYTypeArrayOf :
1718 CYTypeModifier
1719 {
1720 CYExpression *size_;
1721
1722 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1723 CYTypeModifier(next),
1724 size_(size)
1725 {
1726 }
1727
1728 CYPrecedence(1)
1729
1730 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1731 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1732 };
1733
1734 struct CYTypeConstant :
1735 CYTypeModifier
1736 {
1737 CYTypeConstant(CYTypeModifier *next = NULL) :
1738 CYTypeModifier(next)
1739 {
1740 }
1741
1742 CYPrecedence(0)
1743
1744 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1745 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1746 };
1747
1748 struct CYTypePointerTo :
1749 CYTypeModifier
1750 {
1751 CYTypePointerTo(CYTypeModifier *next = NULL) :
1752 CYTypeModifier(next)
1753 {
1754 }
1755
1756 CYPrecedence(0)
1757
1758 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1759 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1760 };
1761
1762 struct CYTypeVolatile :
1763 CYTypeModifier
1764 {
1765 CYTypeVolatile(CYTypeModifier *next = NULL) :
1766 CYTypeModifier(next)
1767 {
1768 }
1769
1770 CYPrecedence(0)
1771
1772 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1773 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1774 };
1775
1776 struct CYTypedIdentifier :
1777 CYNext<CYTypedIdentifier>,
1778 CYThing
1779 {
1780 CYIdentifier *identifier_;
1781 CYTypeSpecifier *specifier_;
1782 CYTypeModifier *modifier_;
1783
1784 CYTypedIdentifier(CYIdentifier *identifier = NULL) :
1785 identifier_(identifier),
1786 specifier_(NULL),
1787 modifier_(NULL)
1788 {
1789 }
1790
1791 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1792 identifier_(NULL),
1793 specifier_(specifier),
1794 modifier_(modifier)
1795 {
1796 }
1797
1798 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1799 CYSetLast(modifier_) = modifier;
1800 return this;
1801 }
1802
1803 virtual CYExpression *Replace(CYContext &context);
1804 virtual void Output(CYOutput &out) const;
1805 };
1806
1807 struct CYEncodedType :
1808 CYExpression
1809 {
1810 CYTypedIdentifier *typed_;
1811
1812 CYEncodedType(CYTypedIdentifier *typed) :
1813 typed_(typed)
1814 {
1815 }
1816
1817 CYPrecedence(1)
1818
1819 virtual CYExpression *Replace(CYContext &context);
1820 virtual void Output(CYOutput &out, CYFlags flags) const;
1821 };
1822
1823 struct CYTypedParameter :
1824 CYNext<CYTypedParameter>,
1825 CYThing
1826 {
1827 CYTypedIdentifier *typed_;
1828
1829 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1830 CYNext<CYTypedParameter>(next),
1831 typed_(typed)
1832 {
1833 }
1834
1835 CYArgument *Argument(CYContext &context);
1836 CYFunctionParameter *Parameters(CYContext &context);
1837 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1838
1839 virtual void Output(CYOutput &out) const;
1840 };
1841
1842 struct CYLambda :
1843 CYExpression
1844 {
1845 CYTypedIdentifier *typed_;
1846 CYTypedParameter *parameters_;
1847 CYStatement *statements_;
1848
1849 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *statements) :
1850 typed_(typed),
1851 parameters_(parameters),
1852 statements_(statements)
1853 {
1854 }
1855
1856 CYPrecedence(1)
1857
1858 virtual CYExpression *Replace(CYContext &context);
1859 virtual void Output(CYOutput &out, CYFlags flags) const;
1860 };
1861
1862 struct CYTypeDefinition :
1863 CYStatement
1864 {
1865 CYTypedIdentifier *typed_;
1866
1867 CYTypeDefinition(CYTypedIdentifier *typed) :
1868 typed_(typed)
1869 {
1870 }
1871
1872 virtual CYStatement *Replace(CYContext &context);
1873 virtual void Output(CYOutput &out, CYFlags flags) const;
1874 };
1875
1876 struct CYTypeBlockWith :
1877 CYTypeModifier
1878 {
1879 CYTypedParameter *parameters_;
1880
1881 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1882 CYTypeModifier(next),
1883 parameters_(parameters)
1884 {
1885 }
1886
1887 CYPrecedence(0)
1888
1889 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1890 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1891 };
1892
1893 struct CYTypeFunctionWith :
1894 CYTypeModifier
1895 {
1896 CYTypedParameter *parameters_;
1897
1898 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1899 CYTypeModifier(next),
1900 parameters_(parameters)
1901 {
1902 }
1903
1904 CYPrecedence(1)
1905
1906 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1907 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1908 };
1909
1910 namespace cy {
1911 namespace Syntax {
1912
1913 struct Catch :
1914 CYThing
1915 {
1916 CYIdentifier *name_;
1917 CYBlock code_;
1918
1919 Catch(CYIdentifier *name, CYStatement *statements) :
1920 name_(name),
1921 code_(statements)
1922 {
1923 }
1924
1925 void Replace(CYContext &context);
1926 virtual void Output(CYOutput &out) const;
1927 };
1928
1929 struct Try :
1930 CYStatement
1931 {
1932 CYBlock code_;
1933 Catch *catch_;
1934 CYFinally *finally_;
1935
1936 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1937 code_(statements),
1938 catch_(_catch),
1939 finally_(finally)
1940 {
1941 }
1942
1943 virtual CYStatement *Replace(CYContext &context);
1944 virtual void Output(CYOutput &out, CYFlags flags) const;
1945 };
1946
1947 struct Throw :
1948 CYStatement
1949 {
1950 CYExpression *value_;
1951
1952 Throw(CYExpression *value = NULL) :
1953 value_(value)
1954 {
1955 }
1956
1957 virtual CYStatement *Replace(CYContext &context);
1958 virtual void Output(CYOutput &out, CYFlags flags) const;
1959 };
1960
1961 } }
1962
1963 struct CYWith :
1964 CYStatement
1965 {
1966 CYExpression *scope_;
1967 CYStatement *code_;
1968
1969 CYWith(CYExpression *scope, CYStatement *code) :
1970 scope_(scope),
1971 code_(code)
1972 {
1973 }
1974
1975 virtual CYStatement *Replace(CYContext &context);
1976 virtual void Output(CYOutput &out, CYFlags flags) const;
1977 };
1978
1979 struct CYSwitch :
1980 CYStatement
1981 {
1982 CYExpression *value_;
1983 CYClause *clauses_;
1984
1985 CYSwitch(CYExpression *value, CYClause *clauses) :
1986 value_(value),
1987 clauses_(clauses)
1988 {
1989 }
1990
1991 virtual CYStatement *Replace(CYContext &context);
1992 virtual void Output(CYOutput &out, CYFlags flags) const;
1993 };
1994
1995 struct CYDebugger :
1996 CYStatement
1997 {
1998 CYDebugger()
1999 {
2000 }
2001
2002 virtual CYStatement *Replace(CYContext &context);
2003 virtual void Output(CYOutput &out, CYFlags flags) const;
2004 };
2005
2006 struct CYCondition :
2007 CYExpression
2008 {
2009 CYExpression *test_;
2010 CYExpression *true_;
2011 CYExpression *false_;
2012
2013 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2014 test_(test),
2015 true_(_true),
2016 false_(_false)
2017 {
2018 }
2019
2020 CYPrecedence(15)
2021
2022 virtual CYExpression *Replace(CYContext &context);
2023 virtual void Output(CYOutput &out, CYFlags flags) const;
2024 };
2025
2026 struct CYAddressOf :
2027 CYPrefix
2028 {
2029 CYAddressOf(CYExpression *rhs) :
2030 CYPrefix(rhs)
2031 {
2032 }
2033
2034 virtual const char *Operator() const {
2035 return "&";
2036 }
2037
2038 CYAlphabetic(false)
2039
2040 virtual CYExpression *Replace(CYContext &context);
2041 };
2042
2043 struct CYIndirect :
2044 CYPrefix
2045 {
2046 CYIndirect(CYExpression *rhs) :
2047 CYPrefix(rhs)
2048 {
2049 }
2050
2051 virtual const char *Operator() const {
2052 return "*";
2053 }
2054
2055 CYAlphabetic(false)
2056
2057 virtual CYExpression *Replace(CYContext &context);
2058 };
2059
2060 #define CYReplace \
2061 virtual CYExpression *Replace(CYContext &context);
2062
2063 #define CYPostfix_(op, name, args...) \
2064 struct CY ## name : \
2065 CYPostfix \
2066 { args \
2067 CY ## name(CYExpression *lhs) : \
2068 CYPostfix(lhs) \
2069 { \
2070 } \
2071 \
2072 virtual const char *Operator() const { \
2073 return op; \
2074 } \
2075 };
2076
2077 #define CYPrefix_(alphabetic, op, name, args...) \
2078 struct CY ## name : \
2079 CYPrefix \
2080 { args \
2081 CY ## name(CYExpression *rhs) : \
2082 CYPrefix(rhs) \
2083 { \
2084 } \
2085 \
2086 CYAlphabetic(alphabetic) \
2087 \
2088 virtual const char *Operator() const { \
2089 return op; \
2090 } \
2091 };
2092
2093 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2094 struct CY ## name : \
2095 CYInfix \
2096 { args \
2097 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2098 CYInfix(lhs, rhs) \
2099 { \
2100 } \
2101 \
2102 CYAlphabetic(alphabetic) \
2103 CYPrecedence(precedence) \
2104 \
2105 virtual const char *Operator() const { \
2106 return op; \
2107 } \
2108 };
2109
2110 #define CYAssignment_(op, name, args...) \
2111 struct CY ## name ## Assign : \
2112 CYAssignment \
2113 { args \
2114 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2115 CYAssignment(lhs, rhs) \
2116 { \
2117 } \
2118 \
2119 virtual const char *Operator() const { \
2120 return op; \
2121 } \
2122 };
2123
2124 CYPostfix_("++", PostIncrement)
2125 CYPostfix_("--", PostDecrement)
2126
2127 CYPrefix_(true, "delete", Delete)
2128 CYPrefix_(true, "void", Void)
2129 CYPrefix_(true, "typeof", TypeOf)
2130 CYPrefix_(false, "++", PreIncrement)
2131 CYPrefix_(false, "--", PreDecrement)
2132 CYPrefix_(false, "+", Affirm)
2133 CYPrefix_(false, "-", Negate)
2134 CYPrefix_(false, "~", BitwiseNot)
2135 CYPrefix_(false, "!", LogicalNot)
2136
2137 CYInfix_(false, 5, "*", Multiply, CYReplace)
2138 CYInfix_(false, 5, "/", Divide)
2139 CYInfix_(false, 5, "%", Modulus)
2140 CYInfix_(false, 6, "+", Add, CYReplace)
2141 CYInfix_(false, 6, "-", Subtract)
2142 CYInfix_(false, 7, "<<", ShiftLeft)
2143 CYInfix_(false, 7, ">>", ShiftRightSigned)
2144 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2145 CYInfix_(false, 8, "<", Less)
2146 CYInfix_(false, 8, ">", Greater)
2147 CYInfix_(false, 8, "<=", LessOrEqual)
2148 CYInfix_(false, 8, ">=", GreaterOrEqual)
2149 CYInfix_(true, 8, "instanceof", InstanceOf)
2150 CYInfix_(true, 8, "in", In)
2151 CYInfix_(false, 9, "==", Equal)
2152 CYInfix_(false, 9, "!=", NotEqual)
2153 CYInfix_(false, 9, "===", Identical)
2154 CYInfix_(false, 9, "!==", NotIdentical)
2155 CYInfix_(false, 10, "&", BitwiseAnd)
2156 CYInfix_(false, 11, "^", BitwiseXOr)
2157 CYInfix_(false, 12, "|", BitwiseOr)
2158 CYInfix_(false, 13, "&&", LogicalAnd)
2159 CYInfix_(false, 14, "||", LogicalOr)
2160
2161 CYAssignment_("=", )
2162 CYAssignment_("*=", Multiply)
2163 CYAssignment_("/=", Divide)
2164 CYAssignment_("%=", Modulus)
2165 CYAssignment_("+=", Add)
2166 CYAssignment_("-=", Subtract)
2167 CYAssignment_("<<=", ShiftLeft)
2168 CYAssignment_(">>=", ShiftRightSigned)
2169 CYAssignment_(">>>=", ShiftRightUnsigned)
2170 CYAssignment_("&=", BitwiseAnd)
2171 CYAssignment_("^=", BitwiseXOr)
2172 CYAssignment_("|=", BitwiseOr)
2173
2174 #endif/*CYCRIPT_PARSER_HPP*/