]> git.saurik.com Git - cycript.git/blob - Syntax.hpp
Avoid crashing when the user types a mismatched }.
[cycript.git] / Syntax.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_SYNTAX_HPP
23 #define CYCRIPT_SYNTAX_HPP
24
25 #include <cstdio>
26 #include <cstdlib>
27
28 #include <streambuf>
29 #include <string>
30 #include <vector>
31 #include <map>
32 #include <set>
33
34 #include "List.hpp"
35 #include "Location.hpp"
36 #include "Options.hpp"
37 #include "Pooling.hpp"
38
39 struct CYContext;
40
41 struct CYThing {
42 virtual void Output(struct CYOutput &out) const = 0;
43 };
44
45 struct CYOutput {
46 std::streambuf &out_;
47 CYPosition position_;
48
49 CYOptions &options_;
50 bool pretty_;
51 unsigned indent_;
52 unsigned recent_;
53 bool right_;
54
55 enum {
56 NoMode,
57 NoLetter,
58 NoPlus,
59 NoHyphen,
60 Terminated
61 } mode_;
62
63 CYOutput(std::streambuf &out, CYOptions &options) :
64 out_(out),
65 options_(options),
66 pretty_(false),
67 indent_(0),
68 recent_(0),
69 right_(false),
70 mode_(NoMode)
71 {
72 }
73
74 void Check(char value);
75 void Terminate();
76
77 _finline void operator ()(char value) {
78 _assert(out_.sputc(value) != EOF);
79 recent_ = indent_;
80 if (value == '\n')
81 position_.lines(1);
82 else
83 position_.columns(1);
84 }
85
86 _finline void operator ()(const char *data, std::streamsize size) {
87 _assert(out_.sputn(data, size) == size);
88 recent_ = indent_;
89 position_.columns(size);
90 }
91
92 _finline void operator ()(const char *data) {
93 return operator ()(data, strlen(data));
94 }
95
96 CYOutput &operator <<(char rhs);
97 CYOutput &operator <<(const char *rhs);
98
99 _finline CYOutput &operator <<(const CYThing *rhs) {
100 if (rhs != NULL)
101 rhs->Output(*this);
102 return *this;
103 }
104
105 _finline CYOutput &operator <<(const CYThing &rhs) {
106 rhs.Output(*this);
107 return *this;
108 }
109 };
110
111 struct 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 CYFunctionParameter *Parameter(CYContext &context) const = 0;
618 CYFunctionParameter *Parameters(CYContext &context) const;
619 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
620 virtual void Output(CYOutput &out) const = 0;
621 };
622
623 struct CYForInComprehension :
624 CYComprehension
625 {
626 CYDeclaration *declaration_;
627 CYExpression *set_;
628
629 CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
630 CYComprehension(next),
631 declaration_(declaration),
632 set_(set)
633 {
634 }
635
636 virtual CYFunctionParameter *Parameter(CYContext &context) const;
637 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
638 virtual void Output(CYOutput &out) const;
639 };
640
641 struct CYForOfComprehension :
642 CYComprehension
643 {
644 CYDeclaration *declaration_;
645 CYExpression *set_;
646
647 CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) :
648 CYComprehension(next),
649 declaration_(declaration),
650 set_(set)
651 {
652 }
653
654 virtual CYFunctionParameter *Parameter(CYContext &context) const;
655 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
656 virtual void Output(CYOutput &out) const;
657 };
658
659 struct CYIfComprehension :
660 CYComprehension
661 {
662 CYExpression *test_;
663
664 CYIfComprehension(CYExpression *test, CYComprehension *next = NULL) :
665 CYComprehension(next),
666 test_(test)
667 {
668 }
669
670 virtual CYFunctionParameter *Parameter(CYContext &context) const;
671 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
672 virtual void Output(CYOutput &out) const;
673 };
674
675 struct CYArrayComprehension :
676 CYExpression
677 {
678 CYExpression *expression_;
679 CYComprehension *comprehensions_;
680
681 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
682 expression_(expression),
683 comprehensions_(comprehensions)
684 {
685 }
686
687 CYPrecedence(0)
688
689 virtual CYExpression *Replace(CYContext &context);
690 virtual void Output(CYOutput &out, CYFlags flags) const;
691 };
692
693 struct CYLiteral :
694 CYExpression
695 {
696 CYPrecedence(0)
697 CYRightHand(false)
698
699 virtual CYExpression *Primitive(CYContext &context) {
700 return this;
701 }
702 };
703
704 struct CYTrivial :
705 CYLiteral
706 {
707 virtual CYExpression *Replace(CYContext &context);
708 };
709
710 struct CYMagic :
711 CYExpression
712 {
713 CYPrecedence(0)
714 CYRightHand(false)
715 };
716
717 struct CYRange {
718 uint64_t lo_;
719 uint64_t hi_;
720
721 CYRange(uint64_t lo, uint64_t hi) :
722 lo_(lo), hi_(hi)
723 {
724 }
725
726 bool operator [](uint8_t value) const {
727 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
728 }
729
730 void operator()(uint8_t value) {
731 if (value >> 7)
732 return;
733 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
734 }
735 };
736
737 extern CYRange DigitRange_;
738 extern CYRange WordStartRange_;
739 extern CYRange WordEndRange_;
740
741 struct CYString :
742 CYTrivial,
743 CYPropertyName
744 {
745 const char *value_;
746 size_t size_;
747
748 CYString() :
749 value_(NULL),
750 size_(0)
751 {
752 }
753
754 CYString(const char *value) :
755 value_(value),
756 size_(strlen(value))
757 {
758 }
759
760 CYString(const char *value, size_t size) :
761 value_(value),
762 size_(size)
763 {
764 }
765
766 CYString(const CYWord *word) :
767 value_(word->Word()),
768 size_(strlen(value_))
769 {
770 }
771
772 const char *Value() const {
773 return value_;
774 }
775
776 virtual const char *Word() const;
777
778 virtual CYNumber *Number(CYContext &context);
779 virtual CYString *String(CYContext &context);
780
781 CYString *Concat(CYContext &out, CYString *rhs) const;
782 virtual void Output(CYOutput &out, CYFlags flags) const;
783 virtual void PropertyName(CYOutput &out) const;
784 };
785
786 struct CYElementValue;
787
788 struct CYSpan :
789 CYNext<CYSpan>
790 {
791 CYExpression *expression_;
792 CYString *string_;
793
794 CYSpan(CYExpression *expression, CYString *string, CYSpan *next) :
795 CYNext<CYSpan>(next),
796 expression_(expression),
797 string_(string)
798 {
799 }
800
801 CYElementValue *Replace(CYContext &context);
802 };
803
804 struct CYTemplate :
805 CYExpression
806 {
807 CYString *string_;
808 CYSpan *spans_;
809
810 CYTemplate(CYString *string, CYSpan *spans) :
811 string_(string),
812 spans_(spans)
813 {
814 }
815
816 CYPrecedence(0)
817 CYRightHand(false)
818
819 virtual CYExpression *Replace(CYContext &context);
820 virtual void Output(CYOutput &out, CYFlags flags) const;
821 };
822
823 struct CYNumber :
824 CYTrivial,
825 CYPropertyName
826 {
827 double value_;
828
829 CYNumber(double value) :
830 value_(value)
831 {
832 }
833
834 double Value() const {
835 return value_;
836 }
837
838 virtual CYNumber *Number(CYContext &context);
839 virtual CYString *String(CYContext &context);
840
841 virtual void Output(CYOutput &out, CYFlags flags) const;
842 virtual void PropertyName(CYOutput &out) const;
843 };
844
845 struct CYRegEx :
846 CYTrivial
847 {
848 const char *value_;
849 size_t size_;
850
851 CYRegEx(const char *value, size_t size) :
852 value_(value),
853 size_(size)
854 {
855 }
856
857 const char *Value() const {
858 return value_;
859 }
860
861 virtual void Output(CYOutput &out, CYFlags flags) const;
862 };
863
864 struct CYNull :
865 CYTrivial
866 {
867 virtual CYNumber *Number(CYContext &context);
868 virtual CYString *String(CYContext &context);
869
870 virtual void Output(CYOutput &out, CYFlags flags) const;
871 };
872
873 struct CYThis :
874 CYMagic
875 {
876 virtual CYExpression *Replace(CYContext &context);
877 virtual void Output(CYOutput &out, CYFlags flags) const;
878 };
879
880 struct CYBoolean :
881 CYTrivial
882 {
883 virtual bool Value() const = 0;
884 virtual void Output(CYOutput &out, CYFlags flags) const;
885 };
886
887 struct CYFalse :
888 CYBoolean
889 {
890 virtual bool Value() const {
891 return false;
892 }
893
894 virtual CYNumber *Number(CYContext &context);
895 virtual CYString *String(CYContext &context);
896 };
897
898 struct CYTrue :
899 CYBoolean
900 {
901 virtual bool Value() const {
902 return true;
903 }
904
905 virtual CYNumber *Number(CYContext &context);
906 virtual CYString *String(CYContext &context);
907 };
908
909 struct CYVariable :
910 CYExpression
911 {
912 CYIdentifier *name_;
913
914 CYVariable(CYIdentifier *name) :
915 name_(name)
916 {
917 }
918
919 CYVariable(const char *name) :
920 name_(new($pool) CYIdentifier(name))
921 {
922 }
923
924 CYPrecedence(0)
925 CYRightHand(false)
926
927 virtual CYExpression *Replace(CYContext &context);
928 virtual void Output(CYOutput &out, CYFlags flags) const;
929
930 virtual CYFunctionParameter *Parameter() const;
931 };
932
933 struct CYPrefix :
934 CYExpression
935 {
936 CYExpression *rhs_;
937
938 CYPrefix(CYExpression *rhs) :
939 rhs_(rhs)
940 {
941 }
942
943 virtual bool Alphabetic() const = 0;
944 virtual const char *Operator() const = 0;
945
946 CYPrecedence(4)
947
948 virtual CYExpression *Replace(CYContext &context);
949 virtual void Output(CYOutput &out, CYFlags flags) const;
950 };
951
952 struct CYInfix :
953 CYExpression
954 {
955 CYExpression *lhs_;
956 CYExpression *rhs_;
957
958 CYInfix(CYExpression *lhs, CYExpression *rhs) :
959 lhs_(lhs),
960 rhs_(rhs)
961 {
962 }
963
964 void SetLeft(CYExpression *lhs) {
965 lhs_ = lhs;
966 }
967
968 virtual bool Alphabetic() const = 0;
969 virtual const char *Operator() const = 0;
970
971 virtual CYExpression *Replace(CYContext &context);
972 virtual void Output(CYOutput &out, CYFlags flags) const;
973 };
974
975 struct CYPostfix :
976 CYExpression
977 {
978 CYExpression *lhs_;
979
980 CYPostfix(CYExpression *lhs) :
981 lhs_(lhs)
982 {
983 }
984
985 virtual const char *Operator() const = 0;
986
987 CYPrecedence(3)
988
989 virtual CYExpression *Replace(CYContext &context);
990 virtual void Output(CYOutput &out, CYFlags flags) const;
991 };
992
993 struct CYAssignment :
994 CYExpression
995 {
996 CYExpression *lhs_;
997 CYExpression *rhs_;
998
999 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
1000 lhs_(lhs),
1001 rhs_(rhs)
1002 {
1003 }
1004
1005 void SetLeft(CYExpression *lhs) {
1006 lhs_ = lhs;
1007 }
1008
1009 virtual const char *Operator() const = 0;
1010
1011 CYPrecedence(16)
1012
1013 virtual CYExpression *Replace(CYContext &context);
1014 virtual void Output(CYOutput &out, CYFlags flags) const;
1015 };
1016
1017 struct CYArgument :
1018 CYNext<CYArgument>,
1019 CYThing
1020 {
1021 CYWord *name_;
1022 CYExpression *value_;
1023
1024 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1025 CYNext<CYArgument>(next),
1026 name_(NULL),
1027 value_(value)
1028 {
1029 }
1030
1031 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
1032 CYNext<CYArgument>(next),
1033 name_(name),
1034 value_(value)
1035 {
1036 }
1037
1038 CYArgument *Replace(CYContext &context);
1039 void Output(CYOutput &out) const;
1040 };
1041
1042 struct CYClause :
1043 CYThing,
1044 CYNext<CYClause>
1045 {
1046 CYExpression *case_;
1047 CYStatement *code_;
1048
1049 CYClause(CYExpression *_case, CYStatement *code) :
1050 case_(_case),
1051 code_(code)
1052 {
1053 }
1054
1055 void Replace(CYContext &context);
1056 virtual void Output(CYOutput &out) const;
1057 };
1058
1059 struct CYElement :
1060 CYThing
1061 {
1062 virtual bool Elision() const = 0;
1063
1064 virtual void Replace(CYContext &context) = 0;
1065 };
1066
1067 struct CYElementValue :
1068 CYNext<CYElement>,
1069 CYElement
1070 {
1071 CYExpression *value_;
1072
1073 CYElementValue(CYExpression *value, CYElement *next) :
1074 CYNext<CYElement>(next),
1075 value_(value)
1076 {
1077 }
1078
1079 virtual bool Elision() const {
1080 return value_ == NULL;
1081 }
1082
1083 virtual void Replace(CYContext &context);
1084 virtual void Output(CYOutput &out) const;
1085 };
1086
1087 struct CYElementSpread :
1088 CYElement
1089 {
1090 CYExpression *value_;
1091
1092 CYElementSpread(CYExpression *value) :
1093 value_(value)
1094 {
1095 }
1096
1097 virtual bool Elision() const {
1098 return false;
1099 }
1100
1101 virtual void Replace(CYContext &context);
1102 virtual void Output(CYOutput &out) const;
1103 };
1104
1105 struct CYArray :
1106 CYLiteral
1107 {
1108 CYElement *elements_;
1109
1110 CYArray(CYElement *elements = NULL) :
1111 elements_(elements)
1112 {
1113 }
1114
1115 virtual CYExpression *Replace(CYContext &context);
1116 virtual void Output(CYOutput &out, CYFlags flags) const;
1117 };
1118
1119 struct CYProperty :
1120 CYNext<CYProperty>,
1121 CYThing
1122 {
1123 CYPropertyName *name_;
1124 CYExpression *value_;
1125
1126 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1127 CYNext<CYProperty>(next),
1128 name_(name),
1129 value_(value)
1130 {
1131 }
1132
1133 void Replace(CYContext &context);
1134 virtual void Output(CYOutput &out) const;
1135 };
1136
1137 struct CYDeclaration :
1138 CYForInInitializer
1139 {
1140 CYIdentifier *identifier_;
1141 CYExpression *initialiser_;
1142
1143 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1144 identifier_(identifier),
1145 initialiser_(initialiser)
1146 {
1147 }
1148
1149 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1150 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
1151
1152 virtual CYExpression *Replace(CYContext &context);
1153
1154 virtual CYAssignment *Assignment(CYContext &context);
1155 CYVariable *Variable(CYContext &context);
1156
1157 virtual void Output(CYOutput &out, CYFlags flags) const;
1158 };
1159
1160 struct CYDeclarations :
1161 CYNext<CYDeclarations>,
1162 CYThing
1163 {
1164 CYDeclaration *declaration_;
1165
1166 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1167 CYNext<CYDeclarations>(next),
1168 declaration_(declaration)
1169 {
1170 }
1171
1172 void Replace(CYContext &context);
1173
1174 CYExpression *Expression(CYContext &context);
1175 CYProperty *Property(CYContext &context);
1176 CYArgument *Argument(CYContext &context);
1177 CYFunctionParameter *Parameter(CYContext &context);
1178
1179 virtual void Output(CYOutput &out) const;
1180 virtual void Output(CYOutput &out, CYFlags flags) const;
1181 };
1182
1183 struct CYForDeclarations :
1184 CYForInitializer
1185 {
1186 CYDeclarations *declarations_;
1187
1188 CYForDeclarations(CYDeclarations *declarations) :
1189 declarations_(declarations)
1190 {
1191 }
1192
1193 virtual CYExpression *Replace(CYContext &context);
1194 virtual void Output(CYOutput &out, CYFlags flags) const;
1195 };
1196
1197 struct CYVar :
1198 CYStatement
1199 {
1200 CYDeclarations *declarations_;
1201
1202 CYVar(CYDeclarations *declarations) :
1203 declarations_(declarations)
1204 {
1205 }
1206
1207 CYCompact(None)
1208
1209 virtual CYStatement *Replace(CYContext &context);
1210 virtual void Output(CYOutput &out, CYFlags flags) const;
1211 };
1212
1213 struct CYLetStatement :
1214 CYStatement
1215 {
1216 CYDeclarations *declarations_;
1217 CYStatement *code_;
1218
1219 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
1220 declarations_(declarations),
1221 code_(code)
1222 {
1223 }
1224
1225 CYCompact(Long)
1226
1227 virtual CYStatement *Replace(CYContext &context);
1228 virtual void Output(CYOutput &out, CYFlags flags) const;
1229 };
1230
1231 struct CYFor :
1232 CYStatement
1233 {
1234 CYForInitializer *initialiser_;
1235 CYExpression *test_;
1236 CYExpression *increment_;
1237 CYStatement *code_;
1238
1239 CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1240 initialiser_(initialiser),
1241 test_(test),
1242 increment_(increment),
1243 code_(code)
1244 {
1245 }
1246
1247 CYCompact(Long)
1248
1249 virtual CYStatement *Replace(CYContext &context);
1250 virtual void Output(CYOutput &out, CYFlags flags) const;
1251 };
1252
1253 struct CYForIn :
1254 CYStatement
1255 {
1256 CYForInInitializer *initialiser_;
1257 CYExpression *set_;
1258 CYStatement *code_;
1259
1260 CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1261 initialiser_(initialiser),
1262 set_(set),
1263 code_(code)
1264 {
1265 }
1266
1267 CYCompact(Long)
1268
1269 virtual CYStatement *Replace(CYContext &context);
1270 virtual void Output(CYOutput &out, CYFlags flags) const;
1271 };
1272
1273 struct CYForOf :
1274 CYStatement
1275 {
1276 CYForInInitializer *initialiser_;
1277 CYExpression *set_;
1278 CYStatement *code_;
1279
1280 CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) :
1281 initialiser_(initialiser),
1282 set_(set),
1283 code_(code)
1284 {
1285 }
1286
1287 CYCompact(Long)
1288
1289 virtual CYStatement *Replace(CYContext &context);
1290 virtual void Output(CYOutput &out, CYFlags flags) const;
1291 };
1292
1293 struct CYObject :
1294 CYLiteral
1295 {
1296 CYProperty *properties_;
1297
1298 CYObject(CYProperty *properties = NULL) :
1299 properties_(properties)
1300 {
1301 }
1302
1303 virtual CYExpression *Replace(CYContext &context);
1304 void Output(CYOutput &out, CYFlags flags) const;
1305 };
1306
1307 struct CYMember :
1308 CYExpression
1309 {
1310 CYExpression *object_;
1311 CYExpression *property_;
1312
1313 CYMember(CYExpression *object, CYExpression *property) :
1314 object_(object),
1315 property_(property)
1316 {
1317 }
1318
1319 void SetLeft(CYExpression *object) {
1320 object_ = object;
1321 }
1322 };
1323
1324 struct CYDirectMember :
1325 CYMember
1326 {
1327 CYDirectMember(CYExpression *object, CYExpression *property) :
1328 CYMember(object, property)
1329 {
1330 }
1331
1332 CYPrecedence(1)
1333 CYRightHand(false)
1334
1335 virtual CYExpression *Replace(CYContext &context);
1336 virtual void Output(CYOutput &out, CYFlags flags) const;
1337 };
1338
1339 struct CYIndirectMember :
1340 CYMember
1341 {
1342 CYIndirectMember(CYExpression *object, CYExpression *property) :
1343 CYMember(object, property)
1344 {
1345 }
1346
1347 CYPrecedence(1)
1348 CYRightHand(false)
1349
1350 virtual CYExpression *Replace(CYContext &context);
1351 virtual void Output(CYOutput &out, CYFlags flags) const;
1352 };
1353
1354 namespace cy {
1355 namespace Syntax {
1356
1357 struct New :
1358 CYExpression
1359 {
1360 CYExpression *constructor_;
1361 CYArgument *arguments_;
1362
1363 New(CYExpression *constructor, CYArgument *arguments) :
1364 constructor_(constructor),
1365 arguments_(arguments)
1366 {
1367 }
1368
1369 virtual int Precedence() const {
1370 return arguments_ == NULL ? 2 : 1;
1371 }
1372
1373 CYRightHand(false)
1374
1375 virtual CYExpression *Replace(CYContext &context);
1376 virtual void Output(CYOutput &out, CYFlags flags) const;
1377
1378 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1379 };
1380
1381 } }
1382
1383 struct CYCall :
1384 CYExpression
1385 {
1386 CYExpression *function_;
1387 CYArgument *arguments_;
1388
1389 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1390 function_(function),
1391 arguments_(arguments)
1392 {
1393 }
1394
1395 CYPrecedence(1)
1396 CYRightHand(false)
1397
1398 virtual CYExpression *Replace(CYContext &context);
1399 virtual void Output(CYOutput &out, CYFlags flags) const;
1400
1401 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1402 };
1403
1404 struct CYRubyProc;
1405
1406 struct CYRubyBlock :
1407 CYExpression
1408 {
1409 CYExpression *call_;
1410 CYRubyProc *proc_;
1411
1412 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1413 call_(call),
1414 proc_(proc)
1415 {
1416 }
1417
1418 CYPrecedence(1)
1419 CYRightHand(false)
1420
1421 virtual CYExpression *Replace(CYContext &context);
1422 virtual void Output(CYOutput &out, CYFlags flags) const;
1423 };
1424
1425 struct CYIf :
1426 CYStatement
1427 {
1428 CYExpression *test_;
1429 CYStatement *true_;
1430 CYStatement *false_;
1431
1432 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1433 test_(test),
1434 true_(_true),
1435 false_(_false)
1436 {
1437 }
1438
1439 CYCompact(Long)
1440
1441 virtual CYStatement *Replace(CYContext &context);
1442 virtual void Output(CYOutput &out, CYFlags flags) const;
1443
1444 virtual CYStatement *Return();
1445 };
1446
1447 struct CYDoWhile :
1448 CYStatement
1449 {
1450 CYExpression *test_;
1451 CYStatement *code_;
1452
1453 CYDoWhile(CYExpression *test, CYStatement *code) :
1454 test_(test),
1455 code_(code)
1456 {
1457 }
1458
1459 CYCompact(None)
1460
1461 virtual CYStatement *Replace(CYContext &context);
1462 virtual void Output(CYOutput &out, CYFlags flags) const;
1463 };
1464
1465 struct CYWhile :
1466 CYStatement
1467 {
1468 CYExpression *test_;
1469 CYStatement *code_;
1470
1471 CYWhile(CYExpression *test, CYStatement *code) :
1472 test_(test),
1473 code_(code)
1474 {
1475 }
1476
1477 CYCompact(Long)
1478
1479 virtual CYStatement *Replace(CYContext &context);
1480 virtual void Output(CYOutput &out, CYFlags flags) const;
1481 };
1482
1483 // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
1484 struct CYFunction {
1485 CYIdentifier *name_;
1486 CYFunctionParameter *parameters_;
1487 CYStatement *code_;
1488
1489 CYNonLocal *nonlocal_;
1490 bool implicit_;
1491 CYThisScope this_;
1492
1493 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1494 name_(name),
1495 parameters_(parameters),
1496 code_(code),
1497 nonlocal_(NULL),
1498 implicit_(false)
1499 {
1500 }
1501
1502 void Inject(CYContext &context);
1503 virtual void Replace_(CYContext &context, bool outer);
1504 virtual void Output(CYOutput &out, CYFlags flags) const;
1505 };
1506
1507 // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
1508 struct CYFunctionExpression :
1509 CYFunction,
1510 CYExpression
1511 {
1512 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1513 CYFunction(name, parameters, code)
1514 {
1515 }
1516
1517 CYPrecedence(0)
1518 CYRightHand(false)
1519
1520 virtual CYExpression *Replace(CYContext &context);
1521 virtual void Output(CYOutput &out, CYFlags flags) const;
1522 };
1523
1524 // XXX: this should derive from CYAnonymousFunction
1525 struct CYFatArrow :
1526 CYFunction,
1527 CYExpression
1528 {
1529 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1530 CYFunction(NULL, parameters, code)
1531 {
1532 }
1533
1534 CYPrecedence(0)
1535 CYRightHand(false)
1536
1537 virtual CYExpression *Replace(CYContext &context);
1538 virtual void Output(CYOutput &out, CYFlags flags) const;
1539 };
1540
1541 // XXX: this should derive from CYAnonymousFunctionExpression
1542 struct CYRubyProc :
1543 CYFunctionExpression
1544 {
1545 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1546 CYFunctionExpression(NULL, parameters, code)
1547 {
1548 }
1549
1550 virtual CYExpression *Replace(CYContext &context);
1551 virtual void Output(CYOutput &out, CYFlags flags) const;
1552 };
1553
1554 // XXX: this should derive from CYNamedFunction
1555 struct CYFunctionStatement :
1556 CYFunction,
1557 CYStatement
1558 {
1559 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1560 CYFunction(name, parameters, code)
1561 {
1562 }
1563
1564 CYCompact(None)
1565
1566 virtual CYStatement *Replace(CYContext &context);
1567 virtual void Output(CYOutput &out, CYFlags flags) const;
1568 };
1569
1570 struct CYExpress :
1571 CYStatement
1572 {
1573 CYExpression *expression_;
1574
1575 CYExpress(CYExpression *expression) :
1576 expression_(expression)
1577 {
1578 if (expression_ == NULL)
1579 throw;
1580 }
1581
1582 CYCompact(None)
1583
1584 virtual CYStatement *Replace(CYContext &context);
1585 virtual void Output(CYOutput &out, CYFlags flags) const;
1586
1587 virtual CYStatement *Return();
1588 };
1589
1590 struct CYContinue :
1591 CYStatement
1592 {
1593 CYIdentifier *label_;
1594
1595 CYContinue(CYIdentifier *label) :
1596 label_(label)
1597 {
1598 }
1599
1600 CYCompact(Short)
1601
1602 virtual CYStatement *Replace(CYContext &context);
1603 virtual void Output(CYOutput &out, CYFlags flags) const;
1604 };
1605
1606 struct CYBreak :
1607 CYStatement
1608 {
1609 CYIdentifier *label_;
1610
1611 CYBreak(CYIdentifier *label) :
1612 label_(label)
1613 {
1614 }
1615
1616 CYCompact(Short)
1617
1618 virtual CYStatement *Replace(CYContext &context);
1619 virtual void Output(CYOutput &out, CYFlags flags) const;
1620 };
1621
1622 struct CYReturn :
1623 CYStatement
1624 {
1625 CYExpression *value_;
1626
1627 CYReturn(CYExpression *value) :
1628 value_(value)
1629 {
1630 }
1631
1632 CYCompact(None)
1633
1634 virtual CYStatement *Replace(CYContext &context);
1635 virtual void Output(CYOutput &out, CYFlags flags) const;
1636 };
1637
1638 struct CYYieldGenerator :
1639 CYExpression
1640 {
1641 CYExpression *value_;
1642
1643 CYYieldGenerator(CYExpression *value) :
1644 value_(value)
1645 {
1646 }
1647
1648 CYPrecedence(0)
1649
1650 virtual CYExpression *Replace(CYContext &context);
1651 virtual void Output(CYOutput &out, CYFlags flags) const;
1652 };
1653
1654 struct CYYieldValue :
1655 CYExpression
1656 {
1657 CYExpression *value_;
1658
1659 CYYieldValue(CYExpression *value) :
1660 value_(value)
1661 {
1662 }
1663
1664 CYPrecedence(0)
1665
1666 virtual CYExpression *Replace(CYContext &context);
1667 virtual void Output(CYOutput &out, CYFlags flags) const;
1668 };
1669
1670 struct CYEmpty :
1671 CYStatement
1672 {
1673 CYCompact(Short)
1674
1675 virtual CYStatement *Replace(CYContext &context);
1676 virtual void Output(CYOutput &out, CYFlags flags) const;
1677 };
1678
1679 struct CYFinally :
1680 CYThing
1681 {
1682 CYStatement *code_;
1683
1684 CYFinally(CYStatement *code) :
1685 code_(code)
1686 {
1687 }
1688
1689 void Replace(CYContext &context);
1690 virtual void Output(CYOutput &out) const;
1691 };
1692
1693 struct CYTypeSpecifier :
1694 CYThing
1695 {
1696 virtual CYExpression *Replace(CYContext &context) = 0;
1697 };
1698
1699 struct CYTypeError :
1700 CYTypeSpecifier
1701 {
1702 CYTypeError() {
1703 }
1704
1705 virtual CYExpression *Replace(CYContext &context);
1706 virtual void Output(CYOutput &out) const;
1707 };
1708
1709 struct CYTypeVoid :
1710 CYTypeSpecifier
1711 {
1712 CYTypeVoid() {
1713 }
1714
1715 virtual CYExpression *Replace(CYContext &context);
1716 virtual void Output(CYOutput &out) const;
1717 };
1718
1719 struct CYTypeVariable :
1720 CYTypeSpecifier
1721 {
1722 CYIdentifier *name_;
1723
1724 CYTypeVariable(CYIdentifier *name) :
1725 name_(name)
1726 {
1727 }
1728
1729 CYTypeVariable(const char *name) :
1730 name_(new($pool) CYIdentifier(name))
1731 {
1732 }
1733
1734 virtual CYExpression *Replace(CYContext &context);
1735 virtual void Output(CYOutput &out) const;
1736 };
1737
1738 struct CYTypeUnsigned :
1739 CYTypeSpecifier
1740 {
1741 CYTypeSpecifier *specifier_;
1742
1743 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1744 specifier_(specifier)
1745 {
1746 }
1747
1748 virtual CYExpression *Replace(CYContext &context);
1749 virtual void Output(CYOutput &out) const;
1750 };
1751
1752 struct CYTypeSigned :
1753 CYTypeSpecifier
1754 {
1755 CYTypeSpecifier *specifier_;
1756
1757 CYTypeSigned(CYTypeSpecifier *specifier) :
1758 specifier_(specifier)
1759 {
1760 }
1761
1762 virtual CYExpression *Replace(CYContext &context);
1763 virtual void Output(CYOutput &out) const;
1764 };
1765
1766 struct CYTypeLong :
1767 CYTypeSpecifier
1768 {
1769 CYTypeSpecifier *specifier_;
1770
1771 CYTypeLong(CYTypeSpecifier *specifier) :
1772 specifier_(specifier)
1773 {
1774 }
1775
1776 virtual CYExpression *Replace(CYContext &context);
1777 virtual void Output(CYOutput &out) const;
1778 };
1779
1780 struct CYTypeShort :
1781 CYTypeSpecifier
1782 {
1783 CYTypeSpecifier *specifier_;
1784
1785 CYTypeShort(CYTypeSpecifier *specifier) :
1786 specifier_(specifier)
1787 {
1788 }
1789
1790 virtual CYExpression *Replace(CYContext &context);
1791 virtual void Output(CYOutput &out) const;
1792 };
1793
1794 struct CYTypeFunctionWith;
1795
1796 struct CYTypeModifier :
1797 CYNext<CYTypeModifier>
1798 {
1799 CYTypeModifier(CYTypeModifier *next) :
1800 CYNext<CYTypeModifier>(next)
1801 {
1802 }
1803
1804 virtual int Precedence() const = 0;
1805
1806 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1807 CYExpression *Replace(CYContext &context, CYExpression *type);
1808
1809 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1810 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1811
1812 virtual CYTypeFunctionWith *Function() { return NULL; }
1813 };
1814
1815 struct CYTypeArrayOf :
1816 CYTypeModifier
1817 {
1818 CYExpression *size_;
1819
1820 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1821 CYTypeModifier(next),
1822 size_(size)
1823 {
1824 }
1825
1826 CYPrecedence(1)
1827
1828 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1829 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1830 };
1831
1832 struct CYTypeConstant :
1833 CYTypeModifier
1834 {
1835 CYTypeConstant(CYTypeModifier *next = NULL) :
1836 CYTypeModifier(next)
1837 {
1838 }
1839
1840 CYPrecedence(0)
1841
1842 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1843 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1844 };
1845
1846 struct CYTypePointerTo :
1847 CYTypeModifier
1848 {
1849 CYTypePointerTo(CYTypeModifier *next = NULL) :
1850 CYTypeModifier(next)
1851 {
1852 }
1853
1854 CYPrecedence(0)
1855
1856 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1857 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1858 };
1859
1860 struct CYTypeVolatile :
1861 CYTypeModifier
1862 {
1863 CYTypeVolatile(CYTypeModifier *next = NULL) :
1864 CYTypeModifier(next)
1865 {
1866 }
1867
1868 CYPrecedence(0)
1869
1870 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1871 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1872 };
1873
1874 struct CYTypedIdentifier :
1875 CYNext<CYTypedIdentifier>,
1876 CYThing
1877 {
1878 CYLocation location_;
1879 CYIdentifier *identifier_;
1880 CYTypeSpecifier *specifier_;
1881 CYTypeModifier *modifier_;
1882
1883 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1884 location_(location),
1885 identifier_(identifier),
1886 specifier_(NULL),
1887 modifier_(NULL)
1888 {
1889 }
1890
1891 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1892 identifier_(NULL),
1893 specifier_(specifier),
1894 modifier_(modifier)
1895 {
1896 }
1897
1898 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1899 CYSetLast(modifier_) = modifier;
1900 return this;
1901 }
1902
1903 virtual CYExpression *Replace(CYContext &context);
1904 virtual void Output(CYOutput &out) const;
1905
1906 CYTypeFunctionWith *Function();
1907 };
1908
1909 struct CYEncodedType :
1910 CYExpression
1911 {
1912 CYTypedIdentifier *typed_;
1913
1914 CYEncodedType(CYTypedIdentifier *typed) :
1915 typed_(typed)
1916 {
1917 }
1918
1919 CYPrecedence(1)
1920
1921 virtual CYExpression *Replace(CYContext &context);
1922 virtual void Output(CYOutput &out, CYFlags flags) const;
1923 };
1924
1925 struct CYTypedParameter :
1926 CYNext<CYTypedParameter>,
1927 CYThing
1928 {
1929 CYTypedIdentifier *typed_;
1930
1931 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1932 CYNext<CYTypedParameter>(next),
1933 typed_(typed)
1934 {
1935 }
1936
1937 CYArgument *Argument(CYContext &context);
1938 CYFunctionParameter *Parameters(CYContext &context);
1939 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1940
1941 virtual void Output(CYOutput &out) const;
1942 };
1943
1944 struct CYLambda :
1945 CYExpression
1946 {
1947 CYTypedIdentifier *typed_;
1948 CYTypedParameter *parameters_;
1949 CYStatement *code_;
1950
1951 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1952 typed_(typed),
1953 parameters_(parameters),
1954 code_(code)
1955 {
1956 }
1957
1958 CYPrecedence(1)
1959
1960 virtual CYExpression *Replace(CYContext &context);
1961 virtual void Output(CYOutput &out, CYFlags flags) const;
1962 };
1963
1964 struct CYModule :
1965 CYNext<CYModule>,
1966 CYThing
1967 {
1968 CYWord *part_;
1969
1970 CYModule(CYWord *part, CYModule *next = NULL) :
1971 CYNext<CYModule>(next),
1972 part_(part)
1973 {
1974 }
1975
1976 CYString *Replace(CYContext &context, const char *separator) const;
1977 void Output(CYOutput &out) const;
1978 };
1979
1980 struct CYImport :
1981 CYStatement
1982 {
1983 CYModule *module_;
1984
1985 CYImport(CYModule *module) :
1986 module_(module)
1987 {
1988 }
1989
1990 CYCompact(None)
1991
1992 virtual CYStatement *Replace(CYContext &context);
1993 virtual void Output(CYOutput &out, CYFlags flags) const;
1994 };
1995
1996 struct CYExternal :
1997 CYStatement
1998 {
1999 CYString *abi_;
2000 CYTypedIdentifier *typed_;
2001
2002 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
2003 abi_(abi),
2004 typed_(typed)
2005 {
2006 }
2007
2008 CYCompact(None)
2009
2010 virtual CYStatement *Replace(CYContext &context);
2011 virtual void Output(CYOutput &out, CYFlags flags) const;
2012 };
2013
2014 struct CYTypeDefinition :
2015 CYStatement
2016 {
2017 CYTypedIdentifier *typed_;
2018
2019 CYTypeDefinition(CYTypedIdentifier *typed) :
2020 typed_(typed)
2021 {
2022 }
2023
2024 CYCompact(None)
2025
2026 virtual CYStatement *Replace(CYContext &context);
2027 virtual void Output(CYOutput &out, CYFlags flags) const;
2028 };
2029
2030 struct CYTypeBlockWith :
2031 CYTypeModifier
2032 {
2033 CYTypedParameter *parameters_;
2034
2035 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2036 CYTypeModifier(next),
2037 parameters_(parameters)
2038 {
2039 }
2040
2041 CYPrecedence(0)
2042
2043 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2044 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2045 };
2046
2047 struct CYTypeFunctionWith :
2048 CYTypeModifier
2049 {
2050 CYTypedParameter *parameters_;
2051
2052 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2053 CYTypeModifier(next),
2054 parameters_(parameters)
2055 {
2056 }
2057
2058 CYPrecedence(1)
2059
2060 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2061 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2062
2063 virtual CYTypeFunctionWith *Function() { return this; }
2064 };
2065
2066 namespace cy {
2067 namespace Syntax {
2068
2069 struct Catch :
2070 CYThing
2071 {
2072 CYIdentifier *name_;
2073 CYStatement *code_;
2074
2075 Catch(CYIdentifier *name, CYStatement *code) :
2076 name_(name),
2077 code_(code)
2078 {
2079 }
2080
2081 void Replace(CYContext &context);
2082 virtual void Output(CYOutput &out) const;
2083 };
2084
2085 struct Try :
2086 CYStatement
2087 {
2088 CYStatement *code_;
2089 Catch *catch_;
2090 CYFinally *finally_;
2091
2092 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2093 code_(code),
2094 catch_(_catch),
2095 finally_(finally)
2096 {
2097 }
2098
2099 CYCompact(Short)
2100
2101 virtual CYStatement *Replace(CYContext &context);
2102 virtual void Output(CYOutput &out, CYFlags flags) const;
2103 };
2104
2105 struct Throw :
2106 CYStatement
2107 {
2108 CYExpression *value_;
2109
2110 Throw(CYExpression *value = NULL) :
2111 value_(value)
2112 {
2113 }
2114
2115 CYCompact(None)
2116
2117 virtual CYStatement *Replace(CYContext &context);
2118 virtual void Output(CYOutput &out, CYFlags flags) const;
2119 };
2120
2121 } }
2122
2123 struct CYWith :
2124 CYStatement
2125 {
2126 CYExpression *scope_;
2127 CYStatement *code_;
2128
2129 CYWith(CYExpression *scope, CYStatement *code) :
2130 scope_(scope),
2131 code_(code)
2132 {
2133 }
2134
2135 CYCompact(Long)
2136
2137 virtual CYStatement *Replace(CYContext &context);
2138 virtual void Output(CYOutput &out, CYFlags flags) const;
2139 };
2140
2141 struct CYSwitch :
2142 CYStatement
2143 {
2144 CYExpression *value_;
2145 CYClause *clauses_;
2146
2147 CYSwitch(CYExpression *value, CYClause *clauses) :
2148 value_(value),
2149 clauses_(clauses)
2150 {
2151 }
2152
2153 CYCompact(Long)
2154
2155 virtual CYStatement *Replace(CYContext &context);
2156 virtual void Output(CYOutput &out, CYFlags flags) const;
2157 };
2158
2159 struct CYDebugger :
2160 CYStatement
2161 {
2162 CYDebugger()
2163 {
2164 }
2165
2166 CYCompact(None)
2167
2168 virtual CYStatement *Replace(CYContext &context);
2169 virtual void Output(CYOutput &out, CYFlags flags) const;
2170 };
2171
2172 struct CYCondition :
2173 CYExpression
2174 {
2175 CYExpression *test_;
2176 CYExpression *true_;
2177 CYExpression *false_;
2178
2179 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2180 test_(test),
2181 true_(_true),
2182 false_(_false)
2183 {
2184 }
2185
2186 CYPrecedence(15)
2187
2188 virtual CYExpression *Replace(CYContext &context);
2189 virtual void Output(CYOutput &out, CYFlags flags) const;
2190 };
2191
2192 struct CYAddressOf :
2193 CYPrefix
2194 {
2195 CYAddressOf(CYExpression *rhs) :
2196 CYPrefix(rhs)
2197 {
2198 }
2199
2200 virtual const char *Operator() const {
2201 return "&";
2202 }
2203
2204 CYAlphabetic(false)
2205
2206 virtual CYExpression *Replace(CYContext &context);
2207 };
2208
2209 struct CYIndirect :
2210 CYPrefix
2211 {
2212 CYIndirect(CYExpression *rhs) :
2213 CYPrefix(rhs)
2214 {
2215 }
2216
2217 virtual const char *Operator() const {
2218 return "*";
2219 }
2220
2221 CYAlphabetic(false)
2222
2223 virtual CYExpression *Replace(CYContext &context);
2224 };
2225
2226 #define CYReplace \
2227 virtual CYExpression *Replace(CYContext &context);
2228
2229 #define CYPostfix_(op, name, args...) \
2230 struct CY ## name : \
2231 CYPostfix \
2232 { args \
2233 CY ## name(CYExpression *lhs) : \
2234 CYPostfix(lhs) \
2235 { \
2236 } \
2237 \
2238 virtual const char *Operator() const { \
2239 return op; \
2240 } \
2241 };
2242
2243 #define CYPrefix_(alphabetic, op, name, args...) \
2244 struct CY ## name : \
2245 CYPrefix \
2246 { args \
2247 CY ## name(CYExpression *rhs) : \
2248 CYPrefix(rhs) \
2249 { \
2250 } \
2251 \
2252 CYAlphabetic(alphabetic) \
2253 \
2254 virtual const char *Operator() const { \
2255 return op; \
2256 } \
2257 };
2258
2259 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2260 struct CY ## name : \
2261 CYInfix \
2262 { args \
2263 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2264 CYInfix(lhs, rhs) \
2265 { \
2266 } \
2267 \
2268 CYAlphabetic(alphabetic) \
2269 CYPrecedence(precedence) \
2270 \
2271 virtual const char *Operator() const { \
2272 return op; \
2273 } \
2274 };
2275
2276 #define CYAssignment_(op, name, args...) \
2277 struct CY ## name ## Assign : \
2278 CYAssignment \
2279 { args \
2280 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2281 CYAssignment(lhs, rhs) \
2282 { \
2283 } \
2284 \
2285 virtual const char *Operator() const { \
2286 return op; \
2287 } \
2288 };
2289
2290 CYPostfix_("++", PostIncrement)
2291 CYPostfix_("--", PostDecrement)
2292
2293 CYPrefix_(true, "delete", Delete)
2294 CYPrefix_(true, "void", Void)
2295 CYPrefix_(true, "typeof", TypeOf)
2296 CYPrefix_(false, "++", PreIncrement)
2297 CYPrefix_(false, "--", PreDecrement)
2298 CYPrefix_(false, "+", Affirm)
2299 CYPrefix_(false, "-", Negate)
2300 CYPrefix_(false, "~", BitwiseNot)
2301 CYPrefix_(false, "!", LogicalNot)
2302
2303 CYInfix_(false, 5, "*", Multiply, CYReplace)
2304 CYInfix_(false, 5, "/", Divide)
2305 CYInfix_(false, 5, "%", Modulus)
2306 CYInfix_(false, 6, "+", Add, CYReplace)
2307 CYInfix_(false, 6, "-", Subtract)
2308 CYInfix_(false, 7, "<<", ShiftLeft)
2309 CYInfix_(false, 7, ">>", ShiftRightSigned)
2310 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2311 CYInfix_(false, 8, "<", Less)
2312 CYInfix_(false, 8, ">", Greater)
2313 CYInfix_(false, 8, "<=", LessOrEqual)
2314 CYInfix_(false, 8, ">=", GreaterOrEqual)
2315 CYInfix_(true, 8, "instanceof", InstanceOf)
2316 CYInfix_(true, 8, "in", In)
2317 CYInfix_(false, 9, "==", Equal)
2318 CYInfix_(false, 9, "!=", NotEqual)
2319 CYInfix_(false, 9, "===", Identical)
2320 CYInfix_(false, 9, "!==", NotIdentical)
2321 CYInfix_(false, 10, "&", BitwiseAnd)
2322 CYInfix_(false, 11, "^", BitwiseXOr)
2323 CYInfix_(false, 12, "|", BitwiseOr)
2324 CYInfix_(false, 13, "&&", LogicalAnd)
2325 CYInfix_(false, 14, "||", LogicalOr)
2326
2327 CYAssignment_("=", )
2328 CYAssignment_("*=", Multiply)
2329 CYAssignment_("/=", Divide)
2330 CYAssignment_("%=", Modulus)
2331 CYAssignment_("+=", Add)
2332 CYAssignment_("-=", Subtract)
2333 CYAssignment_("<<=", ShiftLeft)
2334 CYAssignment_(">>=", ShiftRightSigned)
2335 CYAssignment_(">>>=", ShiftRightUnsigned)
2336 CYAssignment_("&=", BitwiseAnd)
2337 CYAssignment_("^=", BitwiseXOr)
2338 CYAssignment_("|=", BitwiseOr)
2339
2340 #endif/*CYCRIPT_PARSER_HPP*/