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