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