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