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