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