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