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