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