]> git.saurik.com Git - cycript.git/blob - Parser.hpp
ac335b587b7dca8f7cb2ae48a2494737741c864c
[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 CYWord,
857 CYTrivial
858 {
859 CYNull() :
860 CYWord("null")
861 {
862 }
863
864 virtual CYNumber *Number(CYContext &context);
865 virtual CYString *String(CYContext &context);
866
867 virtual void Output(CYOutput &out, CYFlags flags) const;
868 };
869
870 struct CYThis :
871 CYWord,
872 CYMagic
873 {
874 CYThis() :
875 CYWord("this")
876 {
877 }
878
879 virtual CYExpression *Replace(CYContext &context);
880 virtual void Output(CYOutput &out, CYFlags flags) const;
881 };
882
883 struct CYBoolean :
884 CYTrivial
885 {
886 virtual bool Value() const = 0;
887 virtual void Output(CYOutput &out, CYFlags flags) const;
888 };
889
890 struct CYFalse :
891 CYWord,
892 CYBoolean
893 {
894 CYFalse() :
895 CYWord("false")
896 {
897 }
898
899 virtual bool Value() const {
900 return false;
901 }
902
903 virtual CYNumber *Number(CYContext &context);
904 virtual CYString *String(CYContext &context);
905 };
906
907 struct CYTrue :
908 CYWord,
909 CYBoolean
910 {
911 CYTrue() :
912 CYWord("true")
913 {
914 }
915
916 virtual bool Value() const {
917 return true;
918 }
919
920 virtual CYNumber *Number(CYContext &context);
921 virtual CYString *String(CYContext &context);
922 };
923
924 struct CYVariable :
925 CYExpression
926 {
927 CYIdentifier *name_;
928
929 CYVariable(CYIdentifier *name) :
930 name_(name)
931 {
932 }
933
934 CYVariable(const char *name) :
935 name_(new($pool) CYIdentifier(name))
936 {
937 }
938
939 CYPrecedence(0)
940 CYRightHand(false)
941
942 virtual CYExpression *Replace(CYContext &context);
943 virtual void Output(CYOutput &out, CYFlags flags) const;
944
945 virtual CYFunctionParameter *Parameter() const;
946 };
947
948 struct CYPrefix :
949 CYExpression
950 {
951 CYExpression *rhs_;
952
953 CYPrefix(CYExpression *rhs) :
954 rhs_(rhs)
955 {
956 }
957
958 virtual bool Alphabetic() const = 0;
959 virtual const char *Operator() const = 0;
960
961 CYPrecedence(4)
962
963 virtual CYExpression *Replace(CYContext &context);
964 virtual void Output(CYOutput &out, CYFlags flags) const;
965 };
966
967 struct CYInfix :
968 CYExpression
969 {
970 CYExpression *lhs_;
971 CYExpression *rhs_;
972
973 CYInfix(CYExpression *lhs, CYExpression *rhs) :
974 lhs_(lhs),
975 rhs_(rhs)
976 {
977 }
978
979 void SetLeft(CYExpression *lhs) {
980 lhs_ = lhs;
981 }
982
983 virtual bool Alphabetic() const = 0;
984 virtual const char *Operator() const = 0;
985
986 virtual CYExpression *Replace(CYContext &context);
987 virtual void Output(CYOutput &out, CYFlags flags) const;
988 };
989
990 struct CYPostfix :
991 CYExpression
992 {
993 CYExpression *lhs_;
994
995 CYPostfix(CYExpression *lhs) :
996 lhs_(lhs)
997 {
998 }
999
1000 virtual const char *Operator() const = 0;
1001
1002 CYPrecedence(3)
1003
1004 virtual CYExpression *Replace(CYContext &context);
1005 virtual void Output(CYOutput &out, CYFlags flags) const;
1006 };
1007
1008 struct CYAssignment :
1009 CYExpression
1010 {
1011 CYExpression *lhs_;
1012 CYExpression *rhs_;
1013
1014 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1015 lhs_(lhs),
1016 rhs_(rhs)
1017 {
1018 }
1019
1020 void SetLeft(CYExpression *lhs) {
1021 lhs_ = lhs;
1022 }
1023
1024 virtual const char *Operator() const = 0;
1025
1026 CYPrecedence(16)
1027
1028 virtual CYExpression *Replace(CYContext &context);
1029 virtual void Output(CYOutput &out, CYFlags flags) const;
1030 };
1031
1032 struct CYArgument :
1033 CYNext<CYArgument>,
1034 CYThing
1035 {
1036 CYWord *name_;
1037 CYExpression *value_;
1038
1039 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1040 CYNext<CYArgument>(next),
1041 name_(NULL),
1042 value_(value)
1043 {
1044 }
1045
1046 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1047 CYNext<CYArgument>(next),
1048 name_(name),
1049 value_(value)
1050 {
1051 }
1052
1053 CYArgument *Replace(CYContext &context);
1054 void Output(CYOutput &out) const;
1055 };
1056
1057 struct CYBlank :
1058 public CYWord
1059 {
1060 CYBlank() :
1061 CYWord("")
1062 {
1063 }
1064 };
1065
1066 struct CYClause :
1067 CYThing,
1068 CYNext<CYClause>
1069 {
1070 CYExpression *case_;
1071 CYStatement *code_;
1072
1073 CYClause(CYExpression *_case, CYStatement *code) :
1074 case_(_case),
1075 code_(code)
1076 {
1077 }
1078
1079 void Replace(CYContext &context);
1080 virtual void Output(CYOutput &out) const;
1081 };
1082
1083 struct CYElement :
1084 CYNext<CYElement>,
1085 CYThing
1086 {
1087 CYExpression *value_;
1088
1089 CYElement(CYExpression *value, CYElement *next) :
1090 CYNext<CYElement>(next),
1091 value_(value)
1092 {
1093 }
1094
1095 void Replace(CYContext &context);
1096 void Output(CYOutput &out) const;
1097 };
1098
1099 struct CYArray :
1100 CYLiteral
1101 {
1102 CYElement *elements_;
1103
1104 CYArray(CYElement *elements = NULL) :
1105 elements_(elements)
1106 {
1107 }
1108
1109 virtual CYExpression *Replace(CYContext &context);
1110 virtual void Output(CYOutput &out, CYFlags flags) const;
1111 };
1112
1113 struct CYProperty :
1114 CYNext<CYProperty>,
1115 CYThing
1116 {
1117 CYPropertyName *name_;
1118 CYExpression *value_;
1119
1120 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1121 CYNext<CYProperty>(next),
1122 name_(name),
1123 value_(value)
1124 {
1125 }
1126
1127 void Replace(CYContext &context);
1128 virtual void Output(CYOutput &out) const;
1129 };
1130
1131 struct CYDeclaration :
1132 CYForInInitialiser
1133 {
1134 CYIdentifier *identifier_;
1135 CYExpression *initialiser_;
1136
1137 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1138 identifier_(identifier),
1139 initialiser_(initialiser)
1140 {
1141 }
1142
1143 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1144 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1145
1146 virtual CYExpression *Replace(CYContext &context);
1147
1148 virtual CYAssignment *Assignment(CYContext &context);
1149 CYVariable *Variable(CYContext &context);
1150
1151 virtual void Output(CYOutput &out, CYFlags flags) const;
1152 };
1153
1154 struct CYDeclarations :
1155 CYNext<CYDeclarations>,
1156 CYThing
1157 {
1158 CYDeclaration *declaration_;
1159
1160 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1161 CYNext<CYDeclarations>(next),
1162 declaration_(declaration)
1163 {
1164 }
1165
1166 void Replace(CYContext &context);
1167
1168 CYExpression *Expression(CYContext &context);
1169 CYProperty *Property(CYContext &context);
1170 CYArgument *Argument(CYContext &context);
1171 CYFunctionParameter *Parameter(CYContext &context);
1172
1173 virtual void Output(CYOutput &out) const;
1174 virtual void Output(CYOutput &out, CYFlags flags) const;
1175 };
1176
1177 struct CYForDeclarations :
1178 CYForInitialiser
1179 {
1180 CYDeclarations *declarations_;
1181
1182 CYForDeclarations(CYDeclarations *declarations) :
1183 declarations_(declarations)
1184 {
1185 }
1186
1187 virtual CYExpression *Replace(CYContext &context);
1188 virtual void Output(CYOutput &out, CYFlags flags) const;
1189 };
1190
1191 struct CYVar :
1192 CYStatement
1193 {
1194 CYDeclarations *declarations_;
1195
1196 CYVar(CYDeclarations *declarations) :
1197 declarations_(declarations)
1198 {
1199 }
1200
1201 CYCompact(None)
1202
1203 virtual CYStatement *Replace(CYContext &context);
1204 virtual void Output(CYOutput &out, CYFlags flags) const;
1205 };
1206
1207 struct CYLetStatement :
1208 CYStatement
1209 {
1210 CYDeclarations *declarations_;
1211 CYStatement *code_;
1212
1213 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1214 declarations_(declarations),
1215 code_(code)
1216 {
1217 }
1218
1219 CYCompact(Long)
1220
1221 virtual CYStatement *Replace(CYContext &context);
1222 virtual void Output(CYOutput &out, CYFlags flags) const;
1223 };
1224
1225 struct CYFor :
1226 CYStatement
1227 {
1228 CYForInitialiser *initialiser_;
1229 CYExpression *test_;
1230 CYExpression *increment_;
1231 CYStatement *code_;
1232
1233 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1234 initialiser_(initialiser),
1235 test_(test),
1236 increment_(increment),
1237 code_(code)
1238 {
1239 }
1240
1241 CYCompact(Long)
1242
1243 virtual CYStatement *Replace(CYContext &context);
1244 virtual void Output(CYOutput &out, CYFlags flags) const;
1245 };
1246
1247 struct CYForIn :
1248 CYStatement
1249 {
1250 CYForInInitialiser *initialiser_;
1251 CYExpression *set_;
1252 CYStatement *code_;
1253
1254 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1255 initialiser_(initialiser),
1256 set_(set),
1257 code_(code)
1258 {
1259 }
1260
1261 CYCompact(Long)
1262
1263 virtual CYStatement *Replace(CYContext &context);
1264 virtual void Output(CYOutput &out, CYFlags flags) const;
1265 };
1266
1267 struct CYForOf :
1268 CYStatement
1269 {
1270 CYForInInitialiser *initialiser_;
1271 CYExpression *set_;
1272 CYStatement *code_;
1273
1274 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1275 initialiser_(initialiser),
1276 set_(set),
1277 code_(code)
1278 {
1279 }
1280
1281 CYCompact(Long)
1282
1283 virtual CYStatement *Replace(CYContext &context);
1284 virtual void Output(CYOutput &out, CYFlags flags) const;
1285 };
1286
1287 struct CYObject :
1288 CYLiteral
1289 {
1290 CYProperty *properties_;
1291
1292 CYObject(CYProperty *properties = NULL) :
1293 properties_(properties)
1294 {
1295 }
1296
1297 virtual CYExpression *Replace(CYContext &context);
1298 void Output(CYOutput &out, CYFlags flags) const;
1299 };
1300
1301 struct CYMember :
1302 CYExpression
1303 {
1304 CYExpression *object_;
1305 CYExpression *property_;
1306
1307 CYMember(CYExpression *object, CYExpression *property) :
1308 object_(object),
1309 property_(property)
1310 {
1311 }
1312
1313 void SetLeft(CYExpression *object) {
1314 object_ = object;
1315 }
1316 };
1317
1318 struct CYDirectMember :
1319 CYMember
1320 {
1321 CYDirectMember(CYExpression *object, CYExpression *property) :
1322 CYMember(object, property)
1323 {
1324 }
1325
1326 CYPrecedence(1)
1327 CYRightHand(false)
1328
1329 virtual CYExpression *Replace(CYContext &context);
1330 virtual void Output(CYOutput &out, CYFlags flags) const;
1331 };
1332
1333 struct CYIndirectMember :
1334 CYMember
1335 {
1336 CYIndirectMember(CYExpression *object, CYExpression *property) :
1337 CYMember(object, property)
1338 {
1339 }
1340
1341 CYPrecedence(1)
1342 CYRightHand(false)
1343
1344 virtual CYExpression *Replace(CYContext &context);
1345 virtual void Output(CYOutput &out, CYFlags flags) const;
1346 };
1347
1348 namespace cy {
1349 namespace Syntax {
1350
1351 struct New :
1352 CYExpression
1353 {
1354 CYExpression *constructor_;
1355 CYArgument *arguments_;
1356
1357 New(CYExpression *constructor, CYArgument *arguments) :
1358 constructor_(constructor),
1359 arguments_(arguments)
1360 {
1361 }
1362
1363 virtual int Precedence() const {
1364 return arguments_ == NULL ? 2 : 1;
1365 }
1366
1367 CYRightHand(false)
1368
1369 virtual CYExpression *Replace(CYContext &context);
1370 virtual void Output(CYOutput &out, CYFlags flags) const;
1371
1372 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1373 };
1374
1375 } }
1376
1377 struct CYCall :
1378 CYExpression
1379 {
1380 CYExpression *function_;
1381 CYArgument *arguments_;
1382
1383 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1384 function_(function),
1385 arguments_(arguments)
1386 {
1387 }
1388
1389 CYPrecedence(1)
1390 CYRightHand(false)
1391
1392 virtual CYExpression *Replace(CYContext &context);
1393 virtual void Output(CYOutput &out, CYFlags flags) const;
1394
1395 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1396 };
1397
1398 struct CYRubyProc;
1399
1400 struct CYRubyBlock :
1401 CYExpression
1402 {
1403 CYExpression *call_;
1404 CYRubyProc *proc_;
1405
1406 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1407 call_(call),
1408 proc_(proc)
1409 {
1410 }
1411
1412 CYPrecedence(1)
1413 CYRightHand(false)
1414
1415 virtual CYExpression *Replace(CYContext &context);
1416 virtual void Output(CYOutput &out, CYFlags flags) const;
1417 };
1418
1419 struct CYIf :
1420 CYStatement
1421 {
1422 CYExpression *test_;
1423 CYStatement *true_;
1424 CYStatement *false_;
1425
1426 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1427 test_(test),
1428 true_(_true),
1429 false_(_false)
1430 {
1431 }
1432
1433 CYCompact(Long)
1434
1435 virtual CYStatement *Replace(CYContext &context);
1436 virtual void Output(CYOutput &out, CYFlags flags) const;
1437
1438 virtual CYStatement *Return();
1439 };
1440
1441 struct CYDoWhile :
1442 CYStatement
1443 {
1444 CYExpression *test_;
1445 CYStatement *code_;
1446
1447 CYDoWhile(CYExpression *test, CYStatement *code) :
1448 test_(test),
1449 code_(code)
1450 {
1451 }
1452
1453 CYCompact(None)
1454
1455 virtual CYStatement *Replace(CYContext &context);
1456 virtual void Output(CYOutput &out, CYFlags flags) const;
1457 };
1458
1459 struct CYWhile :
1460 CYStatement
1461 {
1462 CYExpression *test_;
1463 CYStatement *code_;
1464
1465 CYWhile(CYExpression *test, CYStatement *code) :
1466 test_(test),
1467 code_(code)
1468 {
1469 }
1470
1471 CYCompact(Long)
1472
1473 virtual CYStatement *Replace(CYContext &context);
1474 virtual void Output(CYOutput &out, CYFlags flags) const;
1475 };
1476
1477 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1478 struct CYFunction {
1479 CYIdentifier *name_;
1480 CYFunctionParameter *parameters_;
1481 CYStatement *code_;
1482
1483 CYNonLocal *nonlocal_;
1484 bool implicit_;
1485 CYThisScope this_;
1486
1487 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1488 name_(name),
1489 parameters_(parameters),
1490 code_(code),
1491 nonlocal_(NULL),
1492 implicit_(false)
1493 {
1494 }
1495
1496 void Inject(CYContext &context);
1497 virtual void Replace_(CYContext &context, bool outer);
1498 virtual void Output(CYOutput &out, CYFlags flags) const;
1499 };
1500
1501 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1502 struct CYFunctionExpression :
1503 CYFunction,
1504 CYExpression
1505 {
1506 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1507 CYFunction(name, parameters, code)
1508 {
1509 }
1510
1511 CYPrecedence(0)
1512 CYRightHand(false)
1513
1514 virtual CYExpression *Replace(CYContext &context);
1515 virtual void Output(CYOutput &out, CYFlags flags) const;
1516 };
1517
1518 // XXX: this should derive from CYAnonymousFunction
1519 struct CYFatArrow :
1520 CYFunction,
1521 CYExpression
1522 {
1523 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1524 CYFunction(NULL, parameters, code)
1525 {
1526 }
1527
1528 CYPrecedence(0)
1529 CYRightHand(false)
1530
1531 virtual CYExpression *Replace(CYContext &context);
1532 virtual void Output(CYOutput &out, CYFlags flags) const;
1533 };
1534
1535 // XXX: this should derive from CYAnonymousFunctionExpression
1536 struct CYRubyProc :
1537 CYFunctionExpression
1538 {
1539 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1540 CYFunctionExpression(NULL, parameters, code)
1541 {
1542 }
1543
1544 virtual CYExpression *Replace(CYContext &context);
1545 virtual void Output(CYOutput &out, CYFlags flags) const;
1546 };
1547
1548 // XXX: this should derive from CYNamedFunction
1549 struct CYFunctionStatement :
1550 CYFunction,
1551 CYStatement
1552 {
1553 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1554 CYFunction(name, parameters, code)
1555 {
1556 }
1557
1558 CYCompact(None)
1559
1560 virtual CYStatement *Replace(CYContext &context);
1561 virtual void Output(CYOutput &out, CYFlags flags) const;
1562 };
1563
1564 struct CYExpress :
1565 CYStatement
1566 {
1567 CYExpression *expression_;
1568
1569 CYExpress(CYExpression *expression) :
1570 expression_(expression)
1571 {
1572 if (expression_ == NULL)
1573 throw;
1574 }
1575
1576 CYCompact(None)
1577
1578 virtual CYStatement *Replace(CYContext &context);
1579 virtual void Output(CYOutput &out, CYFlags flags) const;
1580
1581 virtual CYStatement *Return();
1582 };
1583
1584 struct CYContinue :
1585 CYStatement
1586 {
1587 CYIdentifier *label_;
1588
1589 CYContinue(CYIdentifier *label) :
1590 label_(label)
1591 {
1592 }
1593
1594 CYCompact(Short)
1595
1596 virtual CYStatement *Replace(CYContext &context);
1597 virtual void Output(CYOutput &out, CYFlags flags) const;
1598 };
1599
1600 struct CYBreak :
1601 CYStatement
1602 {
1603 CYIdentifier *label_;
1604
1605 CYBreak(CYIdentifier *label) :
1606 label_(label)
1607 {
1608 }
1609
1610 CYCompact(Short)
1611
1612 virtual CYStatement *Replace(CYContext &context);
1613 virtual void Output(CYOutput &out, CYFlags flags) const;
1614 };
1615
1616 struct CYReturn :
1617 CYStatement
1618 {
1619 CYExpression *value_;
1620
1621 CYReturn(CYExpression *value) :
1622 value_(value)
1623 {
1624 }
1625
1626 CYCompact(None)
1627
1628 virtual CYStatement *Replace(CYContext &context);
1629 virtual void Output(CYOutput &out, CYFlags flags) const;
1630 };
1631
1632 struct CYEmpty :
1633 CYStatement
1634 {
1635 CYCompact(Short)
1636
1637 virtual CYStatement *Replace(CYContext &context);
1638 virtual void Output(CYOutput &out, CYFlags flags) const;
1639 };
1640
1641 struct CYFinally :
1642 CYThing
1643 {
1644 CYStatement *code_;
1645
1646 CYFinally(CYStatement *code) :
1647 code_(code)
1648 {
1649 }
1650
1651 void Replace(CYContext &context);
1652 virtual void Output(CYOutput &out) const;
1653 };
1654
1655 struct CYTypeSpecifier :
1656 CYThing
1657 {
1658 virtual CYExpression *Replace(CYContext &context) = 0;
1659 };
1660
1661 struct CYTypeError :
1662 CYTypeSpecifier
1663 {
1664 CYTypeError() {
1665 }
1666
1667 virtual CYExpression *Replace(CYContext &context);
1668 virtual void Output(CYOutput &out) const;
1669 };
1670
1671 struct CYTypeVoid :
1672 CYTypeSpecifier
1673 {
1674 CYTypeVoid() {
1675 }
1676
1677 virtual CYExpression *Replace(CYContext &context);
1678 virtual void Output(CYOutput &out) const;
1679 };
1680
1681 struct CYTypeVariable :
1682 CYTypeSpecifier
1683 {
1684 CYIdentifier *name_;
1685
1686 CYTypeVariable(CYIdentifier *name) :
1687 name_(name)
1688 {
1689 }
1690
1691 CYTypeVariable(const char *name) :
1692 name_(new($pool) CYIdentifier(name))
1693 {
1694 }
1695
1696 virtual CYExpression *Replace(CYContext &context);
1697 virtual void Output(CYOutput &out) const;
1698 };
1699
1700 struct CYTypeUnsigned :
1701 CYTypeSpecifier
1702 {
1703 CYTypeSpecifier *specifier_;
1704
1705 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1706 specifier_(specifier)
1707 {
1708 }
1709
1710 virtual CYExpression *Replace(CYContext &context);
1711 virtual void Output(CYOutput &out) const;
1712 };
1713
1714 struct CYTypeSigned :
1715 CYTypeSpecifier
1716 {
1717 CYTypeSpecifier *specifier_;
1718
1719 CYTypeSigned(CYTypeSpecifier *specifier) :
1720 specifier_(specifier)
1721 {
1722 }
1723
1724 virtual CYExpression *Replace(CYContext &context);
1725 virtual void Output(CYOutput &out) const;
1726 };
1727
1728 struct CYTypeLong :
1729 CYTypeSpecifier
1730 {
1731 CYTypeSpecifier *specifier_;
1732
1733 CYTypeLong(CYTypeSpecifier *specifier) :
1734 specifier_(specifier)
1735 {
1736 }
1737
1738 virtual CYExpression *Replace(CYContext &context);
1739 virtual void Output(CYOutput &out) const;
1740 };
1741
1742 struct CYTypeShort :
1743 CYTypeSpecifier
1744 {
1745 CYTypeSpecifier *specifier_;
1746
1747 CYTypeShort(CYTypeSpecifier *specifier) :
1748 specifier_(specifier)
1749 {
1750 }
1751
1752 virtual CYExpression *Replace(CYContext &context);
1753 virtual void Output(CYOutput &out) const;
1754 };
1755
1756 struct CYTypeFunctionWith;
1757
1758 struct CYTypeModifier :
1759 CYNext<CYTypeModifier>
1760 {
1761 CYTypeModifier(CYTypeModifier *next) :
1762 CYNext<CYTypeModifier>(next)
1763 {
1764 }
1765
1766 virtual int Precedence() const = 0;
1767
1768 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1769 CYExpression *Replace(CYContext &context, CYExpression *type);
1770
1771 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1772 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1773
1774 virtual CYTypeFunctionWith *Function() { return NULL; }
1775 };
1776
1777 struct CYTypeArrayOf :
1778 CYTypeModifier
1779 {
1780 CYExpression *size_;
1781
1782 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1783 CYTypeModifier(next),
1784 size_(size)
1785 {
1786 }
1787
1788 CYPrecedence(1)
1789
1790 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1791 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1792 };
1793
1794 struct CYTypeConstant :
1795 CYTypeModifier
1796 {
1797 CYTypeConstant(CYTypeModifier *next = NULL) :
1798 CYTypeModifier(next)
1799 {
1800 }
1801
1802 CYPrecedence(0)
1803
1804 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1805 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1806 };
1807
1808 struct CYTypePointerTo :
1809 CYTypeModifier
1810 {
1811 CYTypePointerTo(CYTypeModifier *next = NULL) :
1812 CYTypeModifier(next)
1813 {
1814 }
1815
1816 CYPrecedence(0)
1817
1818 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1819 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1820 };
1821
1822 struct CYTypeVolatile :
1823 CYTypeModifier
1824 {
1825 CYTypeVolatile(CYTypeModifier *next = NULL) :
1826 CYTypeModifier(next)
1827 {
1828 }
1829
1830 CYPrecedence(0)
1831
1832 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1833 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1834 };
1835
1836 struct CYTypedIdentifier :
1837 CYNext<CYTypedIdentifier>,
1838 CYThing
1839 {
1840 CYLocation location_;
1841 CYIdentifier *identifier_;
1842 CYTypeSpecifier *specifier_;
1843 CYTypeModifier *modifier_;
1844
1845 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1846 location_(location),
1847 identifier_(identifier),
1848 specifier_(NULL),
1849 modifier_(NULL)
1850 {
1851 }
1852
1853 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1854 identifier_(NULL),
1855 specifier_(specifier),
1856 modifier_(modifier)
1857 {
1858 }
1859
1860 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1861 CYSetLast(modifier_) = modifier;
1862 return this;
1863 }
1864
1865 virtual CYExpression *Replace(CYContext &context);
1866 virtual void Output(CYOutput &out) const;
1867
1868 CYTypeFunctionWith *Function();
1869 };
1870
1871 struct CYEncodedType :
1872 CYExpression
1873 {
1874 CYTypedIdentifier *typed_;
1875
1876 CYEncodedType(CYTypedIdentifier *typed) :
1877 typed_(typed)
1878 {
1879 }
1880
1881 CYPrecedence(1)
1882
1883 virtual CYExpression *Replace(CYContext &context);
1884 virtual void Output(CYOutput &out, CYFlags flags) const;
1885 };
1886
1887 struct CYTypedParameter :
1888 CYNext<CYTypedParameter>,
1889 CYThing
1890 {
1891 CYTypedIdentifier *typed_;
1892
1893 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1894 CYNext<CYTypedParameter>(next),
1895 typed_(typed)
1896 {
1897 }
1898
1899 CYArgument *Argument(CYContext &context);
1900 CYFunctionParameter *Parameters(CYContext &context);
1901 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1902
1903 virtual void Output(CYOutput &out) const;
1904 };
1905
1906 struct CYLambda :
1907 CYExpression
1908 {
1909 CYTypedIdentifier *typed_;
1910 CYTypedParameter *parameters_;
1911 CYStatement *code_;
1912
1913 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1914 typed_(typed),
1915 parameters_(parameters),
1916 code_(code)
1917 {
1918 }
1919
1920 CYPrecedence(1)
1921
1922 virtual CYExpression *Replace(CYContext &context);
1923 virtual void Output(CYOutput &out, CYFlags flags) const;
1924 };
1925
1926 struct CYModule :
1927 CYNext<CYModule>,
1928 CYThing
1929 {
1930 CYWord *part_;
1931
1932 CYModule(CYWord *part, CYModule *next = NULL) :
1933 CYNext<CYModule>(next),
1934 part_(part)
1935 {
1936 }
1937
1938 CYString *Replace(CYContext &context, const char *separator) const;
1939 void Output(CYOutput &out) const;
1940 };
1941
1942 struct CYImport :
1943 CYStatement
1944 {
1945 CYModule *module_;
1946
1947 CYImport(CYModule *module) :
1948 module_(module)
1949 {
1950 }
1951
1952 CYCompact(None)
1953
1954 virtual CYStatement *Replace(CYContext &context);
1955 virtual void Output(CYOutput &out, CYFlags flags) const;
1956 };
1957
1958 struct CYExternal :
1959 CYStatement
1960 {
1961 CYString *abi_;
1962 CYTypedIdentifier *typed_;
1963
1964 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1965 abi_(abi),
1966 typed_(typed)
1967 {
1968 }
1969
1970 CYCompact(None)
1971
1972 virtual CYStatement *Replace(CYContext &context);
1973 virtual void Output(CYOutput &out, CYFlags flags) const;
1974 };
1975
1976 struct CYTypeDefinition :
1977 CYStatement
1978 {
1979 CYTypedIdentifier *typed_;
1980
1981 CYTypeDefinition(CYTypedIdentifier *typed) :
1982 typed_(typed)
1983 {
1984 }
1985
1986 CYCompact(None)
1987
1988 virtual CYStatement *Replace(CYContext &context);
1989 virtual void Output(CYOutput &out, CYFlags flags) const;
1990 };
1991
1992 struct CYTypeBlockWith :
1993 CYTypeModifier
1994 {
1995 CYTypedParameter *parameters_;
1996
1997 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
1998 CYTypeModifier(next),
1999 parameters_(parameters)
2000 {
2001 }
2002
2003 CYPrecedence(0)
2004
2005 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2006 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2007 };
2008
2009 struct CYTypeFunctionWith :
2010 CYTypeModifier
2011 {
2012 CYTypedParameter *parameters_;
2013
2014 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2015 CYTypeModifier(next),
2016 parameters_(parameters)
2017 {
2018 }
2019
2020 CYPrecedence(1)
2021
2022 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2023 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2024
2025 virtual CYTypeFunctionWith *Function() { return this; }
2026 };
2027
2028 namespace cy {
2029 namespace Syntax {
2030
2031 struct Catch :
2032 CYThing
2033 {
2034 CYIdentifier *name_;
2035 CYStatement *code_;
2036
2037 Catch(CYIdentifier *name, CYStatement *code) :
2038 name_(name),
2039 code_(code)
2040 {
2041 }
2042
2043 void Replace(CYContext &context);
2044 virtual void Output(CYOutput &out) const;
2045 };
2046
2047 struct Try :
2048 CYStatement
2049 {
2050 CYStatement *code_;
2051 Catch *catch_;
2052 CYFinally *finally_;
2053
2054 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2055 code_(code),
2056 catch_(_catch),
2057 finally_(finally)
2058 {
2059 }
2060
2061 CYCompact(Short)
2062
2063 virtual CYStatement *Replace(CYContext &context);
2064 virtual void Output(CYOutput &out, CYFlags flags) const;
2065 };
2066
2067 struct Throw :
2068 CYStatement
2069 {
2070 CYExpression *value_;
2071
2072 Throw(CYExpression *value = NULL) :
2073 value_(value)
2074 {
2075 }
2076
2077 CYCompact(None)
2078
2079 virtual CYStatement *Replace(CYContext &context);
2080 virtual void Output(CYOutput &out, CYFlags flags) const;
2081 };
2082
2083 } }
2084
2085 struct CYWith :
2086 CYStatement
2087 {
2088 CYExpression *scope_;
2089 CYStatement *code_;
2090
2091 CYWith(CYExpression *scope, CYStatement *code) :
2092 scope_(scope),
2093 code_(code)
2094 {
2095 }
2096
2097 CYCompact(Long)
2098
2099 virtual CYStatement *Replace(CYContext &context);
2100 virtual void Output(CYOutput &out, CYFlags flags) const;
2101 };
2102
2103 struct CYSwitch :
2104 CYStatement
2105 {
2106 CYExpression *value_;
2107 CYClause *clauses_;
2108
2109 CYSwitch(CYExpression *value, CYClause *clauses) :
2110 value_(value),
2111 clauses_(clauses)
2112 {
2113 }
2114
2115 CYCompact(Long)
2116
2117 virtual CYStatement *Replace(CYContext &context);
2118 virtual void Output(CYOutput &out, CYFlags flags) const;
2119 };
2120
2121 struct CYDebugger :
2122 CYStatement
2123 {
2124 CYDebugger()
2125 {
2126 }
2127
2128 CYCompact(None)
2129
2130 virtual CYStatement *Replace(CYContext &context);
2131 virtual void Output(CYOutput &out, CYFlags flags) const;
2132 };
2133
2134 struct CYCondition :
2135 CYExpression
2136 {
2137 CYExpression *test_;
2138 CYExpression *true_;
2139 CYExpression *false_;
2140
2141 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2142 test_(test),
2143 true_(_true),
2144 false_(_false)
2145 {
2146 }
2147
2148 CYPrecedence(15)
2149
2150 virtual CYExpression *Replace(CYContext &context);
2151 virtual void Output(CYOutput &out, CYFlags flags) const;
2152 };
2153
2154 struct CYAddressOf :
2155 CYPrefix
2156 {
2157 CYAddressOf(CYExpression *rhs) :
2158 CYPrefix(rhs)
2159 {
2160 }
2161
2162 virtual const char *Operator() const {
2163 return "&";
2164 }
2165
2166 CYAlphabetic(false)
2167
2168 virtual CYExpression *Replace(CYContext &context);
2169 };
2170
2171 struct CYIndirect :
2172 CYPrefix
2173 {
2174 CYIndirect(CYExpression *rhs) :
2175 CYPrefix(rhs)
2176 {
2177 }
2178
2179 virtual const char *Operator() const {
2180 return "*";
2181 }
2182
2183 CYAlphabetic(false)
2184
2185 virtual CYExpression *Replace(CYContext &context);
2186 };
2187
2188 #define CYReplace \
2189 virtual CYExpression *Replace(CYContext &context);
2190
2191 #define CYPostfix_(op, name, args...) \
2192 struct CY ## name : \
2193 CYPostfix \
2194 { args \
2195 CY ## name(CYExpression *lhs) : \
2196 CYPostfix(lhs) \
2197 { \
2198 } \
2199 \
2200 virtual const char *Operator() const { \
2201 return op; \
2202 } \
2203 };
2204
2205 #define CYPrefix_(alphabetic, op, name, args...) \
2206 struct CY ## name : \
2207 CYPrefix \
2208 { args \
2209 CY ## name(CYExpression *rhs) : \
2210 CYPrefix(rhs) \
2211 { \
2212 } \
2213 \
2214 CYAlphabetic(alphabetic) \
2215 \
2216 virtual const char *Operator() const { \
2217 return op; \
2218 } \
2219 };
2220
2221 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2222 struct CY ## name : \
2223 CYInfix \
2224 { args \
2225 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2226 CYInfix(lhs, rhs) \
2227 { \
2228 } \
2229 \
2230 CYAlphabetic(alphabetic) \
2231 CYPrecedence(precedence) \
2232 \
2233 virtual const char *Operator() const { \
2234 return op; \
2235 } \
2236 };
2237
2238 #define CYAssignment_(op, name, args...) \
2239 struct CY ## name ## Assign : \
2240 CYAssignment \
2241 { args \
2242 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2243 CYAssignment(lhs, rhs) \
2244 { \
2245 } \
2246 \
2247 virtual const char *Operator() const { \
2248 return op; \
2249 } \
2250 };
2251
2252 CYPostfix_("++", PostIncrement)
2253 CYPostfix_("--", PostDecrement)
2254
2255 CYPrefix_(true, "delete", Delete)
2256 CYPrefix_(true, "void", Void)
2257 CYPrefix_(true, "typeof", TypeOf)
2258 CYPrefix_(false, "++", PreIncrement)
2259 CYPrefix_(false, "--", PreDecrement)
2260 CYPrefix_(false, "+", Affirm)
2261 CYPrefix_(false, "-", Negate)
2262 CYPrefix_(false, "~", BitwiseNot)
2263 CYPrefix_(false, "!", LogicalNot)
2264
2265 CYInfix_(false, 5, "*", Multiply, CYReplace)
2266 CYInfix_(false, 5, "/", Divide)
2267 CYInfix_(false, 5, "%", Modulus)
2268 CYInfix_(false, 6, "+", Add, CYReplace)
2269 CYInfix_(false, 6, "-", Subtract)
2270 CYInfix_(false, 7, "<<", ShiftLeft)
2271 CYInfix_(false, 7, ">>", ShiftRightSigned)
2272 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2273 CYInfix_(false, 8, "<", Less)
2274 CYInfix_(false, 8, ">", Greater)
2275 CYInfix_(false, 8, "<=", LessOrEqual)
2276 CYInfix_(false, 8, ">=", GreaterOrEqual)
2277 CYInfix_(true, 8, "instanceof", InstanceOf)
2278 CYInfix_(true, 8, "in", In)
2279 CYInfix_(false, 9, "==", Equal)
2280 CYInfix_(false, 9, "!=", NotEqual)
2281 CYInfix_(false, 9, "===", Identical)
2282 CYInfix_(false, 9, "!==", NotIdentical)
2283 CYInfix_(false, 10, "&", BitwiseAnd)
2284 CYInfix_(false, 11, "^", BitwiseXOr)
2285 CYInfix_(false, 12, "|", BitwiseOr)
2286 CYInfix_(false, 13, "&&", LogicalAnd)
2287 CYInfix_(false, 14, "||", LogicalOr)
2288
2289 CYAssignment_("=", )
2290 CYAssignment_("*=", Multiply)
2291 CYAssignment_("/=", Divide)
2292 CYAssignment_("%=", Modulus)
2293 CYAssignment_("+=", Add)
2294 CYAssignment_("-=", Subtract)
2295 CYAssignment_("<<=", ShiftLeft)
2296 CYAssignment_(">>=", ShiftRightSigned)
2297 CYAssignment_(">>>=", ShiftRightUnsigned)
2298 CYAssignment_("&=", BitwiseAnd)
2299 CYAssignment_("^=", BitwiseXOr)
2300 CYAssignment_("|=", BitwiseOr)
2301
2302 #endif/*CYCRIPT_PARSER_HPP*/