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