]> git.saurik.com Git - cycript.git/blob - Syntax.hpp
e3999e3c63e2037e3a272edefd3a622fa7037390
[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 CYLetStatement :
1195 CYStatement
1196 {
1197 CYDeclarations *declarations_;
1198 CYStatement *code_;
1199
1200 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1201 declarations_(declarations),
1202 code_(code)
1203 {
1204 }
1205
1206 CYCompact(Long)
1207
1208 virtual CYStatement *Replace(CYContext &context);
1209 virtual void Output(CYOutput &out, CYFlags flags) const;
1210 };
1211
1212 struct CYBuilder {
1213 CYList<CYDeclarations> declarations_;
1214 CYList<CYStatement> statements_;
1215
1216 operator bool() const {
1217 return statements_ != NULL;
1218 }
1219 };
1220
1221 struct CYProperty :
1222 CYNext<CYProperty>,
1223 CYThing
1224 {
1225 CYPropertyName *name_;
1226
1227 CYProperty(CYPropertyName *name, CYProperty *next = NULL) :
1228 CYNext<CYProperty>(next),
1229 name_(name)
1230 {
1231 }
1232
1233 CYProperty *ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update);
1234 void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool computed);
1235
1236 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name) = 0;
1237
1238 virtual void Replace(CYContext &context) = 0;
1239 virtual void Output(CYOutput &out) const;
1240 };
1241
1242 struct CYPropertyValue :
1243 CYProperty
1244 {
1245 CYExpression *value_;
1246
1247 CYPropertyValue(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1248 CYProperty(name, next),
1249 value_(value)
1250 {
1251 }
1252
1253 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name);
1254 virtual void Replace(CYContext &context);
1255 virtual void Output(CYOutput &out) const;
1256 };
1257
1258 struct CYFor :
1259 CYStatement
1260 {
1261 CYForInitializer *initialiser_;
1262 CYExpression *test_;
1263 CYExpression *increment_;
1264 CYStatement *code_;
1265
1266 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1267 initialiser_(initialiser),
1268 test_(test),
1269 increment_(increment),
1270 code_(code)
1271 {
1272 }
1273
1274 CYCompact(Long)
1275
1276 virtual CYStatement *Replace(CYContext &context);
1277 virtual void Output(CYOutput &out, CYFlags flags) const;
1278 };
1279
1280 struct CYForIn :
1281 CYStatement
1282 {
1283 CYForInInitializer *initialiser_;
1284 CYExpression *set_;
1285 CYStatement *code_;
1286
1287 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1288 initialiser_(initialiser),
1289 set_(set),
1290 code_(code)
1291 {
1292 }
1293
1294 CYCompact(Long)
1295
1296 virtual CYStatement *Replace(CYContext &context);
1297 virtual void Output(CYOutput &out, CYFlags flags) const;
1298 };
1299
1300 struct CYForOf :
1301 CYStatement
1302 {
1303 CYForInInitializer *initialiser_;
1304 CYExpression *set_;
1305 CYStatement *code_;
1306
1307 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1308 initialiser_(initialiser),
1309 set_(set),
1310 code_(code)
1311 {
1312 }
1313
1314 CYCompact(Long)
1315
1316 virtual CYStatement *Replace(CYContext &context);
1317 virtual void Output(CYOutput &out, CYFlags flags) const;
1318 };
1319
1320 struct CYObject :
1321 CYLiteral
1322 {
1323 CYProperty *properties_;
1324
1325 CYObject(CYProperty *properties = NULL) :
1326 properties_(properties)
1327 {
1328 }
1329
1330 virtual CYExpression *Replace(CYContext &context);
1331 void Output(CYOutput &out, CYFlags flags) const;
1332 };
1333
1334 struct CYMember :
1335 CYExpression
1336 {
1337 CYExpression *object_;
1338 CYExpression *property_;
1339
1340 CYMember(CYExpression *object, CYExpression *property) :
1341 object_(object),
1342 property_(property)
1343 {
1344 }
1345
1346 void SetLeft(CYExpression *object) {
1347 object_ = object;
1348 }
1349 };
1350
1351 struct CYDirectMember :
1352 CYMember
1353 {
1354 CYDirectMember(CYExpression *object, CYExpression *property) :
1355 CYMember(object, property)
1356 {
1357 }
1358
1359 CYPrecedence(1)
1360 CYRightHand(false)
1361
1362 virtual CYExpression *Replace(CYContext &context);
1363 virtual void Output(CYOutput &out, CYFlags flags) const;
1364 };
1365
1366 struct CYIndirectMember :
1367 CYMember
1368 {
1369 CYIndirectMember(CYExpression *object, CYExpression *property) :
1370 CYMember(object, property)
1371 {
1372 }
1373
1374 CYPrecedence(1)
1375 CYRightHand(false)
1376
1377 virtual CYExpression *Replace(CYContext &context);
1378 virtual void Output(CYOutput &out, CYFlags flags) const;
1379 };
1380
1381 namespace cy {
1382 namespace Syntax {
1383
1384 struct New :
1385 CYExpression
1386 {
1387 CYExpression *constructor_;
1388 CYArgument *arguments_;
1389
1390 New(CYExpression *constructor, CYArgument *arguments = NULL) :
1391 constructor_(constructor),
1392 arguments_(arguments)
1393 {
1394 }
1395
1396 virtual int Precedence() const {
1397 return arguments_ == NULL ? 2 : 1;
1398 }
1399
1400 CYRightHand(false)
1401
1402 virtual CYExpression *Replace(CYContext &context);
1403 virtual void Output(CYOutput &out, CYFlags flags) const;
1404
1405 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1406 };
1407
1408 } }
1409
1410 struct CYCall :
1411 CYExpression
1412 {
1413 CYExpression *function_;
1414 CYArgument *arguments_;
1415
1416 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1417 function_(function),
1418 arguments_(arguments)
1419 {
1420 }
1421
1422 CYPrecedence(1)
1423 CYRightHand(false)
1424
1425 virtual CYExpression *Replace(CYContext &context);
1426 virtual void Output(CYOutput &out, CYFlags flags) const;
1427
1428 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1429 };
1430
1431 struct CYRubyProc;
1432
1433 struct CYRubyBlock :
1434 CYExpression
1435 {
1436 CYExpression *call_;
1437 CYRubyProc *proc_;
1438
1439 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1440 call_(call),
1441 proc_(proc)
1442 {
1443 }
1444
1445 CYPrecedence(1)
1446 CYRightHand(false)
1447
1448 virtual CYExpression *Replace(CYContext &context);
1449 virtual void Output(CYOutput &out, CYFlags flags) const;
1450
1451 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1452 };
1453
1454 struct CYIf :
1455 CYStatement
1456 {
1457 CYExpression *test_;
1458 CYStatement *true_;
1459 CYStatement *false_;
1460
1461 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1462 test_(test),
1463 true_(_true),
1464 false_(_false)
1465 {
1466 }
1467
1468 CYCompact(Long)
1469
1470 virtual CYStatement *Replace(CYContext &context);
1471 virtual void Output(CYOutput &out, CYFlags flags) const;
1472
1473 virtual CYStatement *Return();
1474 };
1475
1476 struct CYDoWhile :
1477 CYStatement
1478 {
1479 CYExpression *test_;
1480 CYStatement *code_;
1481
1482 CYDoWhile(CYExpression *test, CYStatement *code) :
1483 test_(test),
1484 code_(code)
1485 {
1486 }
1487
1488 CYCompact(None)
1489
1490 virtual CYStatement *Replace(CYContext &context);
1491 virtual void Output(CYOutput &out, CYFlags flags) const;
1492 };
1493
1494 struct CYWhile :
1495 CYStatement
1496 {
1497 CYExpression *test_;
1498 CYStatement *code_;
1499
1500 CYWhile(CYExpression *test, CYStatement *code) :
1501 test_(test),
1502 code_(code)
1503 {
1504 }
1505
1506 CYCompact(Long)
1507
1508 virtual CYStatement *Replace(CYContext &context);
1509 virtual void Output(CYOutput &out, CYFlags flags) const;
1510 };
1511
1512 struct CYFunction {
1513 CYFunctionParameter *parameters_;
1514 CYStatement *code_;
1515
1516 CYNonLocal *nonlocal_;
1517 bool implicit_;
1518 CYThisScope this_;
1519 CYIdentifier *super_;
1520
1521 CYFunction(CYFunctionParameter *parameters, CYStatement *code) :
1522 parameters_(parameters),
1523 code_(code),
1524 nonlocal_(NULL),
1525 implicit_(false),
1526 super_(NULL)
1527 {
1528 }
1529
1530 void Replace(CYContext &context);
1531 void Output(CYOutput &out) const;
1532 };
1533
1534 struct CYFunctionExpression :
1535 CYFunction,
1536 CYExpression
1537 {
1538 CYIdentifier *name_;
1539
1540 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1541 CYFunction(parameters, code),
1542 name_(name)
1543 {
1544 }
1545
1546 CYPrecedence(0)
1547 CYRightHand(false)
1548
1549 virtual CYExpression *Replace(CYContext &context);
1550 virtual void Output(CYOutput &out, CYFlags flags) const;
1551 };
1552
1553 struct CYFatArrow :
1554 CYFunction,
1555 CYExpression
1556 {
1557 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1558 CYFunction(parameters, code)
1559 {
1560 }
1561
1562 CYPrecedence(0)
1563 CYRightHand(false)
1564
1565 virtual CYExpression *Replace(CYContext &context);
1566 virtual void Output(CYOutput &out, CYFlags flags) const;
1567 };
1568
1569 struct CYRubyProc :
1570 CYFunction,
1571 CYExpression
1572 {
1573 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1574 CYFunction(parameters, code)
1575 {
1576 }
1577
1578 CYPrecedence(0)
1579 CYRightHand(false)
1580
1581 virtual CYExpression *Replace(CYContext &context);
1582 virtual void Output(CYOutput &out, CYFlags flags) const;
1583 };
1584
1585 struct CYFunctionStatement :
1586 CYFunction,
1587 CYStatement
1588 {
1589 CYIdentifier *name_;
1590
1591 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1592 CYFunction(parameters, code),
1593 name_(name)
1594 {
1595 }
1596
1597 CYCompact(None)
1598
1599 virtual CYStatement *Replace(CYContext &context);
1600 virtual void Output(CYOutput &out, CYFlags flags) const;
1601 };
1602
1603 struct CYPropertyMethod;
1604
1605 struct CYMethod :
1606 CYFunction,
1607 CYProperty
1608 {
1609 CYMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1610 CYFunction(parameters, code),
1611 CYProperty(name, next)
1612 {
1613 }
1614
1615 virtual CYFunctionExpression *Constructor();
1616
1617 using CYProperty::Replace;
1618 virtual void Replace(CYContext &context);
1619 };
1620
1621 struct CYPropertyGetter :
1622 CYMethod
1623 {
1624 CYPropertyGetter(CYPropertyName *name, CYStatement *code, CYProperty *next = NULL) :
1625 CYMethod(name, NULL, code, next)
1626 {
1627 }
1628
1629 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name);
1630 virtual void Output(CYOutput &out) const;
1631 };
1632
1633 struct CYPropertySetter :
1634 CYMethod
1635 {
1636 CYPropertySetter(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1637 CYMethod(name, parameters, code, next)
1638 {
1639 }
1640
1641 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name);
1642 virtual void Output(CYOutput &out) const;
1643 };
1644
1645 struct CYPropertyMethod :
1646 CYMethod
1647 {
1648 CYPropertyMethod(CYPropertyName *name, CYFunctionParameter *parameters, CYStatement *code, CYProperty *next = NULL) :
1649 CYMethod(name, parameters, code, next)
1650 {
1651 }
1652
1653 virtual CYFunctionExpression *Constructor();
1654
1655 virtual void Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name);
1656 virtual void Output(CYOutput &out) const;
1657 };
1658
1659 struct CYClassTail :
1660 CYThing
1661 {
1662 CYExpression *extends_;
1663
1664 CYFunctionExpression *constructor_;
1665 CYList<CYProperty> instance_;
1666 CYList<CYProperty> static_;
1667
1668 CYClassTail(CYExpression *extends) :
1669 extends_(extends),
1670 constructor_(NULL)
1671 {
1672 }
1673
1674 void Output(CYOutput &out) const;
1675 };
1676
1677 struct CYClassExpression :
1678 CYExpression
1679 {
1680 CYIdentifier *name_;
1681 CYClassTail *tail_;
1682
1683 CYClassExpression(CYIdentifier *name, CYClassTail *tail) :
1684 name_(name),
1685 tail_(tail)
1686 {
1687 }
1688
1689 CYPrecedence(0)
1690 CYRightHand(false)
1691
1692 virtual CYExpression *Replace(CYContext &context);
1693 virtual void Output(CYOutput &out, CYFlags flags) const;
1694 };
1695
1696 struct CYClassStatement :
1697 CYStatement
1698 {
1699 CYIdentifier *name_;
1700 CYClassTail *tail_;
1701
1702 CYClassStatement(CYIdentifier *name, CYClassTail *tail) :
1703 name_(name),
1704 tail_(tail)
1705 {
1706 }
1707
1708 CYCompact(Long)
1709
1710 virtual CYStatement *Replace(CYContext &context);
1711 virtual void Output(CYOutput &out, CYFlags flags) const;
1712 };
1713
1714 struct CYSuperCall :
1715 CYExpression
1716 {
1717 CYArgument *arguments_;
1718
1719 CYSuperCall(CYArgument *arguments) :
1720 arguments_(arguments)
1721 {
1722 }
1723
1724 CYPrecedence(2)
1725 CYRightHand(false)
1726
1727 virtual CYExpression *Replace(CYContext &context);
1728 virtual void Output(CYOutput &out, CYFlags flags) const;
1729 };
1730
1731 struct CYSuperAccess :
1732 CYExpression
1733 {
1734 CYExpression *property_;
1735
1736 CYSuperAccess(CYExpression *property) :
1737 property_(property)
1738 {
1739 }
1740
1741 CYPrecedence(1)
1742 CYRightHand(false)
1743
1744 virtual CYExpression *Replace(CYContext &context);
1745 virtual void Output(CYOutput &out, CYFlags flags) const;
1746 };
1747
1748 struct CYExpress :
1749 CYStatement
1750 {
1751 CYExpression *expression_;
1752
1753 CYExpress(CYExpression *expression) :
1754 expression_(expression)
1755 {
1756 if (expression_ == NULL)
1757 throw;
1758 }
1759
1760 CYCompact(None)
1761
1762 virtual CYStatement *Replace(CYContext &context);
1763 virtual void Output(CYOutput &out, CYFlags flags) const;
1764
1765 virtual CYStatement *Return();
1766 };
1767
1768 struct CYContinue :
1769 CYStatement
1770 {
1771 CYIdentifier *label_;
1772
1773 CYContinue(CYIdentifier *label) :
1774 label_(label)
1775 {
1776 }
1777
1778 CYCompact(Short)
1779
1780 virtual CYStatement *Replace(CYContext &context);
1781 virtual void Output(CYOutput &out, CYFlags flags) const;
1782 };
1783
1784 struct CYBreak :
1785 CYStatement
1786 {
1787 CYIdentifier *label_;
1788
1789 CYBreak(CYIdentifier *label) :
1790 label_(label)
1791 {
1792 }
1793
1794 CYCompact(Short)
1795
1796 virtual CYStatement *Replace(CYContext &context);
1797 virtual void Output(CYOutput &out, CYFlags flags) const;
1798 };
1799
1800 struct CYReturn :
1801 CYStatement
1802 {
1803 CYExpression *value_;
1804
1805 CYReturn(CYExpression *value) :
1806 value_(value)
1807 {
1808 }
1809
1810 CYCompact(None)
1811
1812 virtual CYStatement *Replace(CYContext &context);
1813 virtual void Output(CYOutput &out, CYFlags flags) const;
1814 };
1815
1816 struct CYYieldGenerator :
1817 CYExpression
1818 {
1819 CYExpression *value_;
1820
1821 CYYieldGenerator(CYExpression *value) :
1822 value_(value)
1823 {
1824 }
1825
1826 CYPrecedence(0)
1827
1828 virtual CYExpression *Replace(CYContext &context);
1829 virtual void Output(CYOutput &out, CYFlags flags) const;
1830 };
1831
1832 struct CYYieldValue :
1833 CYExpression
1834 {
1835 CYExpression *value_;
1836
1837 CYYieldValue(CYExpression *value) :
1838 value_(value)
1839 {
1840 }
1841
1842 CYPrecedence(0)
1843
1844 virtual CYExpression *Replace(CYContext &context);
1845 virtual void Output(CYOutput &out, CYFlags flags) const;
1846 };
1847
1848 struct CYEmpty :
1849 CYStatement
1850 {
1851 CYCompact(Short)
1852
1853 virtual CYStatement *Replace(CYContext &context);
1854 virtual void Output(CYOutput &out, CYFlags flags) const;
1855 };
1856
1857 struct CYFinally :
1858 CYThing
1859 {
1860 CYStatement *code_;
1861
1862 CYFinally(CYStatement *code) :
1863 code_(code)
1864 {
1865 }
1866
1867 void Replace(CYContext &context);
1868 virtual void Output(CYOutput &out) const;
1869 };
1870
1871 struct CYTypeSpecifier :
1872 CYThing
1873 {
1874 virtual CYExpression *Replace(CYContext &context) = 0;
1875 };
1876
1877 struct CYTypeError :
1878 CYTypeSpecifier
1879 {
1880 CYTypeError() {
1881 }
1882
1883 virtual CYExpression *Replace(CYContext &context);
1884 virtual void Output(CYOutput &out) const;
1885 };
1886
1887 struct CYTypeVoid :
1888 CYTypeSpecifier
1889 {
1890 CYTypeVoid() {
1891 }
1892
1893 virtual CYExpression *Replace(CYContext &context);
1894 virtual void Output(CYOutput &out) const;
1895 };
1896
1897 struct CYTypeVariable :
1898 CYTypeSpecifier
1899 {
1900 CYIdentifier *name_;
1901
1902 CYTypeVariable(CYIdentifier *name) :
1903 name_(name)
1904 {
1905 }
1906
1907 CYTypeVariable(const char *name) :
1908 name_(new($pool) CYIdentifier(name))
1909 {
1910 }
1911
1912 virtual CYExpression *Replace(CYContext &context);
1913 virtual void Output(CYOutput &out) const;
1914 };
1915
1916 struct CYTypeUnsigned :
1917 CYTypeSpecifier
1918 {
1919 CYTypeSpecifier *specifier_;
1920
1921 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1922 specifier_(specifier)
1923 {
1924 }
1925
1926 virtual CYExpression *Replace(CYContext &context);
1927 virtual void Output(CYOutput &out) const;
1928 };
1929
1930 struct CYTypeSigned :
1931 CYTypeSpecifier
1932 {
1933 CYTypeSpecifier *specifier_;
1934
1935 CYTypeSigned(CYTypeSpecifier *specifier) :
1936 specifier_(specifier)
1937 {
1938 }
1939
1940 virtual CYExpression *Replace(CYContext &context);
1941 virtual void Output(CYOutput &out) const;
1942 };
1943
1944 struct CYTypeLong :
1945 CYTypeSpecifier
1946 {
1947 CYTypeSpecifier *specifier_;
1948
1949 CYTypeLong(CYTypeSpecifier *specifier) :
1950 specifier_(specifier)
1951 {
1952 }
1953
1954 virtual CYExpression *Replace(CYContext &context);
1955 virtual void Output(CYOutput &out) const;
1956 };
1957
1958 struct CYTypeShort :
1959 CYTypeSpecifier
1960 {
1961 CYTypeSpecifier *specifier_;
1962
1963 CYTypeShort(CYTypeSpecifier *specifier) :
1964 specifier_(specifier)
1965 {
1966 }
1967
1968 virtual CYExpression *Replace(CYContext &context);
1969 virtual void Output(CYOutput &out) const;
1970 };
1971
1972 struct CYTypeFunctionWith;
1973
1974 struct CYTypeModifier :
1975 CYNext<CYTypeModifier>
1976 {
1977 CYTypeModifier(CYTypeModifier *next) :
1978 CYNext<CYTypeModifier>(next)
1979 {
1980 }
1981
1982 virtual int Precedence() const = 0;
1983
1984 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1985 CYExpression *Replace(CYContext &context, CYExpression *type);
1986
1987 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1988 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1989
1990 virtual CYTypeFunctionWith *Function() { return NULL; }
1991 };
1992
1993 struct CYTypeArrayOf :
1994 CYTypeModifier
1995 {
1996 CYExpression *size_;
1997
1998 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1999 CYTypeModifier(next),
2000 size_(size)
2001 {
2002 }
2003
2004 CYPrecedence(1)
2005
2006 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2007 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2008 };
2009
2010 struct CYTypeConstant :
2011 CYTypeModifier
2012 {
2013 CYTypeConstant(CYTypeModifier *next = NULL) :
2014 CYTypeModifier(next)
2015 {
2016 }
2017
2018 CYPrecedence(0)
2019
2020 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2021 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2022 };
2023
2024 struct CYTypePointerTo :
2025 CYTypeModifier
2026 {
2027 CYTypePointerTo(CYTypeModifier *next = NULL) :
2028 CYTypeModifier(next)
2029 {
2030 }
2031
2032 CYPrecedence(0)
2033
2034 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2035 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2036 };
2037
2038 struct CYTypeVolatile :
2039 CYTypeModifier
2040 {
2041 CYTypeVolatile(CYTypeModifier *next = NULL) :
2042 CYTypeModifier(next)
2043 {
2044 }
2045
2046 CYPrecedence(0)
2047
2048 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2049 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2050 };
2051
2052 struct CYTypedIdentifier :
2053 CYNext<CYTypedIdentifier>,
2054 CYThing
2055 {
2056 CYLocation location_;
2057 CYIdentifier *identifier_;
2058 CYTypeSpecifier *specifier_;
2059 CYTypeModifier *modifier_;
2060
2061 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
2062 location_(location),
2063 identifier_(identifier),
2064 specifier_(NULL),
2065 modifier_(NULL)
2066 {
2067 }
2068
2069 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
2070 identifier_(NULL),
2071 specifier_(specifier),
2072 modifier_(modifier)
2073 {
2074 }
2075
2076 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
2077 CYSetLast(modifier_) = modifier;
2078 return this;
2079 }
2080
2081 virtual CYExpression *Replace(CYContext &context);
2082 virtual void Output(CYOutput &out) const;
2083
2084 CYTypeFunctionWith *Function();
2085 };
2086
2087 struct CYEncodedType :
2088 CYExpression
2089 {
2090 CYTypedIdentifier *typed_;
2091
2092 CYEncodedType(CYTypedIdentifier *typed) :
2093 typed_(typed)
2094 {
2095 }
2096
2097 CYPrecedence(1)
2098
2099 virtual CYExpression *Replace(CYContext &context);
2100 virtual void Output(CYOutput &out, CYFlags flags) const;
2101 };
2102
2103 struct CYTypedParameter :
2104 CYNext<CYTypedParameter>,
2105 CYThing
2106 {
2107 CYTypedIdentifier *typed_;
2108
2109 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
2110 CYNext<CYTypedParameter>(next),
2111 typed_(typed)
2112 {
2113 }
2114
2115 CYArgument *Argument(CYContext &context);
2116 CYFunctionParameter *Parameters(CYContext &context);
2117 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
2118
2119 virtual void Output(CYOutput &out) const;
2120 };
2121
2122 struct CYLambda :
2123 CYExpression
2124 {
2125 CYTypedIdentifier *typed_;
2126 CYTypedParameter *parameters_;
2127 CYStatement *code_;
2128
2129 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
2130 typed_(typed),
2131 parameters_(parameters),
2132 code_(code)
2133 {
2134 }
2135
2136 CYPrecedence(1)
2137
2138 virtual CYExpression *Replace(CYContext &context);
2139 virtual void Output(CYOutput &out, CYFlags flags) const;
2140 };
2141
2142 struct CYModule :
2143 CYNext<CYModule>,
2144 CYThing
2145 {
2146 CYWord *part_;
2147
2148 CYModule(CYWord *part, CYModule *next = NULL) :
2149 CYNext<CYModule>(next),
2150 part_(part)
2151 {
2152 }
2153
2154 CYString *Replace(CYContext &context, const char *separator) const;
2155 void Output(CYOutput &out) const;
2156 };
2157
2158 struct CYImport :
2159 CYStatement
2160 {
2161 CYModule *module_;
2162
2163 CYImport(CYModule *module) :
2164 module_(module)
2165 {
2166 }
2167
2168 CYCompact(None)
2169
2170 virtual CYStatement *Replace(CYContext &context);
2171 virtual void Output(CYOutput &out, CYFlags flags) const;
2172 };
2173
2174 struct CYExternal :
2175 CYStatement
2176 {
2177 CYString *abi_;
2178 CYTypedIdentifier *typed_;
2179
2180 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2181 abi_(abi),
2182 typed_(typed)
2183 {
2184 }
2185
2186 CYCompact(None)
2187
2188 virtual CYStatement *Replace(CYContext &context);
2189 virtual void Output(CYOutput &out, CYFlags flags) const;
2190 };
2191
2192 struct CYTypeDefinition :
2193 CYStatement
2194 {
2195 CYTypedIdentifier *typed_;
2196
2197 CYTypeDefinition(CYTypedIdentifier *typed) :
2198 typed_(typed)
2199 {
2200 }
2201
2202 CYCompact(None)
2203
2204 virtual CYStatement *Replace(CYContext &context);
2205 virtual void Output(CYOutput &out, CYFlags flags) const;
2206 };
2207
2208 struct CYTypeBlockWith :
2209 CYTypeModifier
2210 {
2211 CYTypedParameter *parameters_;
2212
2213 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2214 CYTypeModifier(next),
2215 parameters_(parameters)
2216 {
2217 }
2218
2219 CYPrecedence(0)
2220
2221 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2222 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2223 };
2224
2225 struct CYTypeFunctionWith :
2226 CYTypeModifier
2227 {
2228 CYTypedParameter *parameters_;
2229
2230 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2231 CYTypeModifier(next),
2232 parameters_(parameters)
2233 {
2234 }
2235
2236 CYPrecedence(1)
2237
2238 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2239 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2240
2241 virtual CYTypeFunctionWith *Function() { return this; }
2242 };
2243
2244 namespace cy {
2245 namespace Syntax {
2246
2247 struct Catch :
2248 CYThing
2249 {
2250 CYIdentifier *name_;
2251 CYStatement *code_;
2252
2253 Catch(CYIdentifier *name, CYStatement *code) :
2254 name_(name),
2255 code_(code)
2256 {
2257 }
2258
2259 void Replace(CYContext &context);
2260 virtual void Output(CYOutput &out) const;
2261 };
2262
2263 struct Try :
2264 CYStatement
2265 {
2266 CYStatement *code_;
2267 Catch *catch_;
2268 CYFinally *finally_;
2269
2270 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2271 code_(code),
2272 catch_(_catch),
2273 finally_(finally)
2274 {
2275 }
2276
2277 CYCompact(Short)
2278
2279 virtual CYStatement *Replace(CYContext &context);
2280 virtual void Output(CYOutput &out, CYFlags flags) const;
2281 };
2282
2283 struct Throw :
2284 CYStatement
2285 {
2286 CYExpression *value_;
2287
2288 Throw(CYExpression *value = NULL) :
2289 value_(value)
2290 {
2291 }
2292
2293 CYCompact(None)
2294
2295 virtual CYStatement *Replace(CYContext &context);
2296 virtual void Output(CYOutput &out, CYFlags flags) const;
2297 };
2298
2299 } }
2300
2301 struct CYWith :
2302 CYStatement
2303 {
2304 CYExpression *scope_;
2305 CYStatement *code_;
2306
2307 CYWith(CYExpression *scope, CYStatement *code) :
2308 scope_(scope),
2309 code_(code)
2310 {
2311 }
2312
2313 CYCompact(Long)
2314
2315 virtual CYStatement *Replace(CYContext &context);
2316 virtual void Output(CYOutput &out, CYFlags flags) const;
2317 };
2318
2319 struct CYSwitch :
2320 CYStatement
2321 {
2322 CYExpression *value_;
2323 CYClause *clauses_;
2324
2325 CYSwitch(CYExpression *value, CYClause *clauses) :
2326 value_(value),
2327 clauses_(clauses)
2328 {
2329 }
2330
2331 CYCompact(Long)
2332
2333 virtual CYStatement *Replace(CYContext &context);
2334 virtual void Output(CYOutput &out, CYFlags flags) const;
2335 };
2336
2337 struct CYDebugger :
2338 CYStatement
2339 {
2340 CYDebugger()
2341 {
2342 }
2343
2344 CYCompact(None)
2345
2346 virtual CYStatement *Replace(CYContext &context);
2347 virtual void Output(CYOutput &out, CYFlags flags) const;
2348 };
2349
2350 struct CYCondition :
2351 CYExpression
2352 {
2353 CYExpression *test_;
2354 CYExpression *true_;
2355 CYExpression *false_;
2356
2357 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2358 test_(test),
2359 true_(_true),
2360 false_(_false)
2361 {
2362 }
2363
2364 CYPrecedence(15)
2365
2366 virtual CYExpression *Replace(CYContext &context);
2367 virtual void Output(CYOutput &out, CYFlags flags) const;
2368 };
2369
2370 struct CYAddressOf :
2371 CYPrefix
2372 {
2373 CYAddressOf(CYExpression *rhs) :
2374 CYPrefix(rhs)
2375 {
2376 }
2377
2378 virtual const char *Operator() const {
2379 return "&";
2380 }
2381
2382 CYAlphabetic(false)
2383
2384 virtual CYExpression *Replace(CYContext &context);
2385 };
2386
2387 struct CYIndirect :
2388 CYPrefix
2389 {
2390 CYIndirect(CYExpression *rhs) :
2391 CYPrefix(rhs)
2392 {
2393 }
2394
2395 virtual const char *Operator() const {
2396 return "*";
2397 }
2398
2399 CYAlphabetic(false)
2400
2401 virtual CYExpression *Replace(CYContext &context);
2402 };
2403
2404 #define CYReplace \
2405 virtual CYExpression *Replace(CYContext &context);
2406
2407 #define CYPostfix_(op, name, args...) \
2408 struct CY ## name : \
2409 CYPostfix \
2410 { args \
2411 CY ## name(CYExpression *lhs) : \
2412 CYPostfix(lhs) \
2413 { \
2414 } \
2415 \
2416 virtual const char *Operator() const { \
2417 return op; \
2418 } \
2419 };
2420
2421 #define CYPrefix_(alphabetic, op, name, args...) \
2422 struct CY ## name : \
2423 CYPrefix \
2424 { args \
2425 CY ## name(CYExpression *rhs) : \
2426 CYPrefix(rhs) \
2427 { \
2428 } \
2429 \
2430 CYAlphabetic(alphabetic) \
2431 \
2432 virtual const char *Operator() const { \
2433 return op; \
2434 } \
2435 };
2436
2437 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2438 struct CY ## name : \
2439 CYInfix \
2440 { args \
2441 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2442 CYInfix(lhs, rhs) \
2443 { \
2444 } \
2445 \
2446 CYAlphabetic(alphabetic) \
2447 CYPrecedence(precedence) \
2448 \
2449 virtual const char *Operator() const { \
2450 return op; \
2451 } \
2452 };
2453
2454 #define CYAssignment_(op, name, args...) \
2455 struct CY ## name ## Assign : \
2456 CYAssignment \
2457 { args \
2458 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2459 CYAssignment(lhs, rhs) \
2460 { \
2461 } \
2462 \
2463 virtual const char *Operator() const { \
2464 return op; \
2465 } \
2466 };
2467
2468 CYPostfix_("++", PostIncrement)
2469 CYPostfix_("--", PostDecrement)
2470
2471 CYPrefix_(true, "delete", Delete)
2472 CYPrefix_(true, "void", Void)
2473 CYPrefix_(true, "typeof", TypeOf)
2474 CYPrefix_(false, "++", PreIncrement)
2475 CYPrefix_(false, "--", PreDecrement)
2476 CYPrefix_(false, "+", Affirm)
2477 CYPrefix_(false, "-", Negate)
2478 CYPrefix_(false, "~", BitwiseNot)
2479 CYPrefix_(false, "!", LogicalNot)
2480
2481 CYInfix_(false, 5, "*", Multiply, CYReplace)
2482 CYInfix_(false, 5, "/", Divide)
2483 CYInfix_(false, 5, "%", Modulus)
2484 CYInfix_(false, 6, "+", Add, CYReplace)
2485 CYInfix_(false, 6, "-", Subtract)
2486 CYInfix_(false, 7, "<<", ShiftLeft)
2487 CYInfix_(false, 7, ">>", ShiftRightSigned)
2488 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2489 CYInfix_(false, 8, "<", Less)
2490 CYInfix_(false, 8, ">", Greater)
2491 CYInfix_(false, 8, "<=", LessOrEqual)
2492 CYInfix_(false, 8, ">=", GreaterOrEqual)
2493 CYInfix_(true, 8, "instanceof", InstanceOf)
2494 CYInfix_(true, 8, "in", In)
2495 CYInfix_(false, 9, "==", Equal)
2496 CYInfix_(false, 9, "!=", NotEqual)
2497 CYInfix_(false, 9, "===", Identical)
2498 CYInfix_(false, 9, "!==", NotIdentical)
2499 CYInfix_(false, 10, "&", BitwiseAnd)
2500 CYInfix_(false, 11, "^", BitwiseXOr)
2501 CYInfix_(false, 12, "|", BitwiseOr)
2502 CYInfix_(false, 13, "&&", LogicalAnd)
2503 CYInfix_(false, 14, "||", LogicalOr)
2504
2505 CYAssignment_("=", )
2506 CYAssignment_("*=", Multiply)
2507 CYAssignment_("/=", Divide)
2508 CYAssignment_("%=", Modulus)
2509 CYAssignment_("+=", Add)
2510 CYAssignment_("-=", Subtract)
2511 CYAssignment_("<<=", ShiftLeft)
2512 CYAssignment_(">>=", ShiftRightSigned)
2513 CYAssignment_(">>>=", ShiftRightUnsigned)
2514 CYAssignment_("&=", BitwiseAnd)
2515 CYAssignment_("^=", BitwiseXOr)
2516 CYAssignment_("|=", BitwiseOr)
2517
2518 #endif/*CYCRIPT_PARSER_HPP*/