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