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