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