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