]> git.saurik.com Git - cycript.git/blob - Syntax.hpp
Slightly improve syntax hierarchy near CYFunction.
[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 struct CYFunction {
1484 CYIdentifier *name_;
1485 CYFunctionParameter *parameters_;
1486 CYStatement *code_;
1487
1488 CYNonLocal *nonlocal_;
1489 bool implicit_;
1490 CYThisScope this_;
1491
1492 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1493 name_(name),
1494 parameters_(parameters),
1495 code_(code),
1496 nonlocal_(NULL),
1497 implicit_(false)
1498 {
1499 }
1500
1501 void Inject(CYContext &context);
1502 virtual void Replace_(CYContext &context, bool outer);
1503 virtual void Output(CYOutput &out, CYFlags flags) const;
1504 };
1505
1506 struct CYFunctionExpression :
1507 CYFunction,
1508 CYExpression
1509 {
1510 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1511 CYFunction(name, parameters, code)
1512 {
1513 }
1514
1515 CYPrecedence(0)
1516 CYRightHand(false)
1517
1518 virtual CYExpression *Replace(CYContext &context);
1519 virtual void Output(CYOutput &out, CYFlags flags) const;
1520 };
1521
1522 struct CYFatArrow :
1523 CYFunction,
1524 CYExpression
1525 {
1526 CYFatArrow(CYFunctionParameter *parameters, CYStatement *code) :
1527 CYFunction(NULL, parameters, code)
1528 {
1529 }
1530
1531 CYPrecedence(0)
1532 CYRightHand(false)
1533
1534 virtual CYExpression *Replace(CYContext &context);
1535 virtual void Output(CYOutput &out, CYFlags flags) const;
1536 };
1537
1538 struct CYRubyProc :
1539 CYFunction,
1540 CYExpression
1541 {
1542 CYRubyProc(CYFunctionParameter *parameters, CYStatement *code) :
1543 CYFunction(NULL, parameters, code)
1544 {
1545 }
1546
1547 virtual CYExpression *Replace(CYContext &context);
1548 virtual void Output(CYOutput &out, CYFlags flags) const;
1549 };
1550
1551 struct CYFunctionStatement :
1552 CYFunction,
1553 CYStatement
1554 {
1555 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *code) :
1556 CYFunction(name, parameters, code)
1557 {
1558 }
1559
1560 CYCompact(None)
1561
1562 virtual CYStatement *Replace(CYContext &context);
1563 virtual void Output(CYOutput &out, CYFlags flags) const;
1564 };
1565
1566 struct CYExpress :
1567 CYStatement
1568 {
1569 CYExpression *expression_;
1570
1571 CYExpress(CYExpression *expression) :
1572 expression_(expression)
1573 {
1574 if (expression_ == NULL)
1575 throw;
1576 }
1577
1578 CYCompact(None)
1579
1580 virtual CYStatement *Replace(CYContext &context);
1581 virtual void Output(CYOutput &out, CYFlags flags) const;
1582
1583 virtual CYStatement *Return();
1584 };
1585
1586 struct CYContinue :
1587 CYStatement
1588 {
1589 CYIdentifier *label_;
1590
1591 CYContinue(CYIdentifier *label) :
1592 label_(label)
1593 {
1594 }
1595
1596 CYCompact(Short)
1597
1598 virtual CYStatement *Replace(CYContext &context);
1599 virtual void Output(CYOutput &out, CYFlags flags) const;
1600 };
1601
1602 struct CYBreak :
1603 CYStatement
1604 {
1605 CYIdentifier *label_;
1606
1607 CYBreak(CYIdentifier *label) :
1608 label_(label)
1609 {
1610 }
1611
1612 CYCompact(Short)
1613
1614 virtual CYStatement *Replace(CYContext &context);
1615 virtual void Output(CYOutput &out, CYFlags flags) const;
1616 };
1617
1618 struct CYReturn :
1619 CYStatement
1620 {
1621 CYExpression *value_;
1622
1623 CYReturn(CYExpression *value) :
1624 value_(value)
1625 {
1626 }
1627
1628 CYCompact(None)
1629
1630 virtual CYStatement *Replace(CYContext &context);
1631 virtual void Output(CYOutput &out, CYFlags flags) const;
1632 };
1633
1634 struct CYYieldGenerator :
1635 CYExpression
1636 {
1637 CYExpression *value_;
1638
1639 CYYieldGenerator(CYExpression *value) :
1640 value_(value)
1641 {
1642 }
1643
1644 CYPrecedence(0)
1645
1646 virtual CYExpression *Replace(CYContext &context);
1647 virtual void Output(CYOutput &out, CYFlags flags) const;
1648 };
1649
1650 struct CYYieldValue :
1651 CYExpression
1652 {
1653 CYExpression *value_;
1654
1655 CYYieldValue(CYExpression *value) :
1656 value_(value)
1657 {
1658 }
1659
1660 CYPrecedence(0)
1661
1662 virtual CYExpression *Replace(CYContext &context);
1663 virtual void Output(CYOutput &out, CYFlags flags) const;
1664 };
1665
1666 struct CYEmpty :
1667 CYStatement
1668 {
1669 CYCompact(Short)
1670
1671 virtual CYStatement *Replace(CYContext &context);
1672 virtual void Output(CYOutput &out, CYFlags flags) const;
1673 };
1674
1675 struct CYFinally :
1676 CYThing
1677 {
1678 CYStatement *code_;
1679
1680 CYFinally(CYStatement *code) :
1681 code_(code)
1682 {
1683 }
1684
1685 void Replace(CYContext &context);
1686 virtual void Output(CYOutput &out) const;
1687 };
1688
1689 struct CYTypeSpecifier :
1690 CYThing
1691 {
1692 virtual CYExpression *Replace(CYContext &context) = 0;
1693 };
1694
1695 struct CYTypeError :
1696 CYTypeSpecifier
1697 {
1698 CYTypeError() {
1699 }
1700
1701 virtual CYExpression *Replace(CYContext &context);
1702 virtual void Output(CYOutput &out) const;
1703 };
1704
1705 struct CYTypeVoid :
1706 CYTypeSpecifier
1707 {
1708 CYTypeVoid() {
1709 }
1710
1711 virtual CYExpression *Replace(CYContext &context);
1712 virtual void Output(CYOutput &out) const;
1713 };
1714
1715 struct CYTypeVariable :
1716 CYTypeSpecifier
1717 {
1718 CYIdentifier *name_;
1719
1720 CYTypeVariable(CYIdentifier *name) :
1721 name_(name)
1722 {
1723 }
1724
1725 CYTypeVariable(const char *name) :
1726 name_(new($pool) CYIdentifier(name))
1727 {
1728 }
1729
1730 virtual CYExpression *Replace(CYContext &context);
1731 virtual void Output(CYOutput &out) const;
1732 };
1733
1734 struct CYTypeUnsigned :
1735 CYTypeSpecifier
1736 {
1737 CYTypeSpecifier *specifier_;
1738
1739 CYTypeUnsigned(CYTypeSpecifier *specifier) :
1740 specifier_(specifier)
1741 {
1742 }
1743
1744 virtual CYExpression *Replace(CYContext &context);
1745 virtual void Output(CYOutput &out) const;
1746 };
1747
1748 struct CYTypeSigned :
1749 CYTypeSpecifier
1750 {
1751 CYTypeSpecifier *specifier_;
1752
1753 CYTypeSigned(CYTypeSpecifier *specifier) :
1754 specifier_(specifier)
1755 {
1756 }
1757
1758 virtual CYExpression *Replace(CYContext &context);
1759 virtual void Output(CYOutput &out) const;
1760 };
1761
1762 struct CYTypeLong :
1763 CYTypeSpecifier
1764 {
1765 CYTypeSpecifier *specifier_;
1766
1767 CYTypeLong(CYTypeSpecifier *specifier) :
1768 specifier_(specifier)
1769 {
1770 }
1771
1772 virtual CYExpression *Replace(CYContext &context);
1773 virtual void Output(CYOutput &out) const;
1774 };
1775
1776 struct CYTypeShort :
1777 CYTypeSpecifier
1778 {
1779 CYTypeSpecifier *specifier_;
1780
1781 CYTypeShort(CYTypeSpecifier *specifier) :
1782 specifier_(specifier)
1783 {
1784 }
1785
1786 virtual CYExpression *Replace(CYContext &context);
1787 virtual void Output(CYOutput &out) const;
1788 };
1789
1790 struct CYTypeFunctionWith;
1791
1792 struct CYTypeModifier :
1793 CYNext<CYTypeModifier>
1794 {
1795 CYTypeModifier(CYTypeModifier *next) :
1796 CYNext<CYTypeModifier>(next)
1797 {
1798 }
1799
1800 virtual int Precedence() const = 0;
1801
1802 virtual CYExpression *Replace_(CYContext &context, CYExpression *type) = 0;
1803 CYExpression *Replace(CYContext &context, CYExpression *type);
1804
1805 virtual void Output(CYOutput &out, CYIdentifier *identifier) const = 0;
1806 void Output(CYOutput &out, int precedence, CYIdentifier *identifier) const;
1807
1808 virtual CYTypeFunctionWith *Function() { return NULL; }
1809 };
1810
1811 struct CYTypeArrayOf :
1812 CYTypeModifier
1813 {
1814 CYExpression *size_;
1815
1816 CYTypeArrayOf(CYExpression *size, CYTypeModifier *next = NULL) :
1817 CYTypeModifier(next),
1818 size_(size)
1819 {
1820 }
1821
1822 CYPrecedence(1)
1823
1824 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1825 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1826 };
1827
1828 struct CYTypeConstant :
1829 CYTypeModifier
1830 {
1831 CYTypeConstant(CYTypeModifier *next = NULL) :
1832 CYTypeModifier(next)
1833 {
1834 }
1835
1836 CYPrecedence(0)
1837
1838 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1839 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1840 };
1841
1842 struct CYTypePointerTo :
1843 CYTypeModifier
1844 {
1845 CYTypePointerTo(CYTypeModifier *next = NULL) :
1846 CYTypeModifier(next)
1847 {
1848 }
1849
1850 CYPrecedence(0)
1851
1852 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1853 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1854 };
1855
1856 struct CYTypeVolatile :
1857 CYTypeModifier
1858 {
1859 CYTypeVolatile(CYTypeModifier *next = NULL) :
1860 CYTypeModifier(next)
1861 {
1862 }
1863
1864 CYPrecedence(0)
1865
1866 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
1867 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
1868 };
1869
1870 struct CYTypedIdentifier :
1871 CYNext<CYTypedIdentifier>,
1872 CYThing
1873 {
1874 CYLocation location_;
1875 CYIdentifier *identifier_;
1876 CYTypeSpecifier *specifier_;
1877 CYTypeModifier *modifier_;
1878
1879 CYTypedIdentifier(const CYLocation &location, CYIdentifier *identifier = NULL) :
1880 location_(location),
1881 identifier_(identifier),
1882 specifier_(NULL),
1883 modifier_(NULL)
1884 {
1885 }
1886
1887 CYTypedIdentifier(CYTypeSpecifier *specifier, CYTypeModifier *modifier = NULL) :
1888 identifier_(NULL),
1889 specifier_(specifier),
1890 modifier_(modifier)
1891 {
1892 }
1893
1894 inline CYTypedIdentifier *Modify(CYTypeModifier *modifier) {
1895 CYSetLast(modifier_) = modifier;
1896 return this;
1897 }
1898
1899 virtual CYExpression *Replace(CYContext &context);
1900 virtual void Output(CYOutput &out) const;
1901
1902 CYTypeFunctionWith *Function();
1903 };
1904
1905 struct CYEncodedType :
1906 CYExpression
1907 {
1908 CYTypedIdentifier *typed_;
1909
1910 CYEncodedType(CYTypedIdentifier *typed) :
1911 typed_(typed)
1912 {
1913 }
1914
1915 CYPrecedence(1)
1916
1917 virtual CYExpression *Replace(CYContext &context);
1918 virtual void Output(CYOutput &out, CYFlags flags) const;
1919 };
1920
1921 struct CYTypedParameter :
1922 CYNext<CYTypedParameter>,
1923 CYThing
1924 {
1925 CYTypedIdentifier *typed_;
1926
1927 CYTypedParameter(CYTypedIdentifier *typed, CYTypedParameter *next) :
1928 CYNext<CYTypedParameter>(next),
1929 typed_(typed)
1930 {
1931 }
1932
1933 CYArgument *Argument(CYContext &context);
1934 CYFunctionParameter *Parameters(CYContext &context);
1935 CYExpression *TypeSignature(CYContext &context, CYExpression *prefix);
1936
1937 virtual void Output(CYOutput &out) const;
1938 };
1939
1940 struct CYLambda :
1941 CYExpression
1942 {
1943 CYTypedIdentifier *typed_;
1944 CYTypedParameter *parameters_;
1945 CYStatement *code_;
1946
1947 CYLambda(CYTypedIdentifier *typed, CYTypedParameter *parameters, CYStatement *code) :
1948 typed_(typed),
1949 parameters_(parameters),
1950 code_(code)
1951 {
1952 }
1953
1954 CYPrecedence(1)
1955
1956 virtual CYExpression *Replace(CYContext &context);
1957 virtual void Output(CYOutput &out, CYFlags flags) const;
1958 };
1959
1960 struct CYModule :
1961 CYNext<CYModule>,
1962 CYThing
1963 {
1964 CYWord *part_;
1965
1966 CYModule(CYWord *part, CYModule *next = NULL) :
1967 CYNext<CYModule>(next),
1968 part_(part)
1969 {
1970 }
1971
1972 CYString *Replace(CYContext &context, const char *separator) const;
1973 void Output(CYOutput &out) const;
1974 };
1975
1976 struct CYImport :
1977 CYStatement
1978 {
1979 CYModule *module_;
1980
1981 CYImport(CYModule *module) :
1982 module_(module)
1983 {
1984 }
1985
1986 CYCompact(None)
1987
1988 virtual CYStatement *Replace(CYContext &context);
1989 virtual void Output(CYOutput &out, CYFlags flags) const;
1990 };
1991
1992 struct CYExternal :
1993 CYStatement
1994 {
1995 CYString *abi_;
1996 CYTypedIdentifier *typed_;
1997
1998 CYExternal(CYString *abi, CYTypedIdentifier *typed) :
1999 abi_(abi),
2000 typed_(typed)
2001 {
2002 }
2003
2004 CYCompact(None)
2005
2006 virtual CYStatement *Replace(CYContext &context);
2007 virtual void Output(CYOutput &out, CYFlags flags) const;
2008 };
2009
2010 struct CYTypeDefinition :
2011 CYStatement
2012 {
2013 CYTypedIdentifier *typed_;
2014
2015 CYTypeDefinition(CYTypedIdentifier *typed) :
2016 typed_(typed)
2017 {
2018 }
2019
2020 CYCompact(None)
2021
2022 virtual CYStatement *Replace(CYContext &context);
2023 virtual void Output(CYOutput &out, CYFlags flags) const;
2024 };
2025
2026 struct CYTypeBlockWith :
2027 CYTypeModifier
2028 {
2029 CYTypedParameter *parameters_;
2030
2031 CYTypeBlockWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2032 CYTypeModifier(next),
2033 parameters_(parameters)
2034 {
2035 }
2036
2037 CYPrecedence(0)
2038
2039 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2040 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2041 };
2042
2043 struct CYTypeFunctionWith :
2044 CYTypeModifier
2045 {
2046 CYTypedParameter *parameters_;
2047
2048 CYTypeFunctionWith(CYTypedParameter *parameters, CYTypeModifier *next = NULL) :
2049 CYTypeModifier(next),
2050 parameters_(parameters)
2051 {
2052 }
2053
2054 CYPrecedence(1)
2055
2056 virtual CYExpression *Replace_(CYContext &context, CYExpression *type);
2057 virtual void Output(CYOutput &out, CYIdentifier *identifier) const;
2058
2059 virtual CYTypeFunctionWith *Function() { return this; }
2060 };
2061
2062 namespace cy {
2063 namespace Syntax {
2064
2065 struct Catch :
2066 CYThing
2067 {
2068 CYIdentifier *name_;
2069 CYStatement *code_;
2070
2071 Catch(CYIdentifier *name, CYStatement *code) :
2072 name_(name),
2073 code_(code)
2074 {
2075 }
2076
2077 void Replace(CYContext &context);
2078 virtual void Output(CYOutput &out) const;
2079 };
2080
2081 struct Try :
2082 CYStatement
2083 {
2084 CYStatement *code_;
2085 Catch *catch_;
2086 CYFinally *finally_;
2087
2088 Try(CYStatement *code, Catch *_catch, CYFinally *finally) :
2089 code_(code),
2090 catch_(_catch),
2091 finally_(finally)
2092 {
2093 }
2094
2095 CYCompact(Short)
2096
2097 virtual CYStatement *Replace(CYContext &context);
2098 virtual void Output(CYOutput &out, CYFlags flags) const;
2099 };
2100
2101 struct Throw :
2102 CYStatement
2103 {
2104 CYExpression *value_;
2105
2106 Throw(CYExpression *value = NULL) :
2107 value_(value)
2108 {
2109 }
2110
2111 CYCompact(None)
2112
2113 virtual CYStatement *Replace(CYContext &context);
2114 virtual void Output(CYOutput &out, CYFlags flags) const;
2115 };
2116
2117 } }
2118
2119 struct CYWith :
2120 CYStatement
2121 {
2122 CYExpression *scope_;
2123 CYStatement *code_;
2124
2125 CYWith(CYExpression *scope, CYStatement *code) :
2126 scope_(scope),
2127 code_(code)
2128 {
2129 }
2130
2131 CYCompact(Long)
2132
2133 virtual CYStatement *Replace(CYContext &context);
2134 virtual void Output(CYOutput &out, CYFlags flags) const;
2135 };
2136
2137 struct CYSwitch :
2138 CYStatement
2139 {
2140 CYExpression *value_;
2141 CYClause *clauses_;
2142
2143 CYSwitch(CYExpression *value, CYClause *clauses) :
2144 value_(value),
2145 clauses_(clauses)
2146 {
2147 }
2148
2149 CYCompact(Long)
2150
2151 virtual CYStatement *Replace(CYContext &context);
2152 virtual void Output(CYOutput &out, CYFlags flags) const;
2153 };
2154
2155 struct CYDebugger :
2156 CYStatement
2157 {
2158 CYDebugger()
2159 {
2160 }
2161
2162 CYCompact(None)
2163
2164 virtual CYStatement *Replace(CYContext &context);
2165 virtual void Output(CYOutput &out, CYFlags flags) const;
2166 };
2167
2168 struct CYCondition :
2169 CYExpression
2170 {
2171 CYExpression *test_;
2172 CYExpression *true_;
2173 CYExpression *false_;
2174
2175 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
2176 test_(test),
2177 true_(_true),
2178 false_(_false)
2179 {
2180 }
2181
2182 CYPrecedence(15)
2183
2184 virtual CYExpression *Replace(CYContext &context);
2185 virtual void Output(CYOutput &out, CYFlags flags) const;
2186 };
2187
2188 struct CYAddressOf :
2189 CYPrefix
2190 {
2191 CYAddressOf(CYExpression *rhs) :
2192 CYPrefix(rhs)
2193 {
2194 }
2195
2196 virtual const char *Operator() const {
2197 return "&";
2198 }
2199
2200 CYAlphabetic(false)
2201
2202 virtual CYExpression *Replace(CYContext &context);
2203 };
2204
2205 struct CYIndirect :
2206 CYPrefix
2207 {
2208 CYIndirect(CYExpression *rhs) :
2209 CYPrefix(rhs)
2210 {
2211 }
2212
2213 virtual const char *Operator() const {
2214 return "*";
2215 }
2216
2217 CYAlphabetic(false)
2218
2219 virtual CYExpression *Replace(CYContext &context);
2220 };
2221
2222 #define CYReplace \
2223 virtual CYExpression *Replace(CYContext &context);
2224
2225 #define CYPostfix_(op, name, args...) \
2226 struct CY ## name : \
2227 CYPostfix \
2228 { args \
2229 CY ## name(CYExpression *lhs) : \
2230 CYPostfix(lhs) \
2231 { \
2232 } \
2233 \
2234 virtual const char *Operator() const { \
2235 return op; \
2236 } \
2237 };
2238
2239 #define CYPrefix_(alphabetic, op, name, args...) \
2240 struct CY ## name : \
2241 CYPrefix \
2242 { args \
2243 CY ## name(CYExpression *rhs) : \
2244 CYPrefix(rhs) \
2245 { \
2246 } \
2247 \
2248 CYAlphabetic(alphabetic) \
2249 \
2250 virtual const char *Operator() const { \
2251 return op; \
2252 } \
2253 };
2254
2255 #define CYInfix_(alphabetic, precedence, op, name, args...) \
2256 struct CY ## name : \
2257 CYInfix \
2258 { args \
2259 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
2260 CYInfix(lhs, rhs) \
2261 { \
2262 } \
2263 \
2264 CYAlphabetic(alphabetic) \
2265 CYPrecedence(precedence) \
2266 \
2267 virtual const char *Operator() const { \
2268 return op; \
2269 } \
2270 };
2271
2272 #define CYAssignment_(op, name, args...) \
2273 struct CY ## name ## Assign : \
2274 CYAssignment \
2275 { args \
2276 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
2277 CYAssignment(lhs, rhs) \
2278 { \
2279 } \
2280 \
2281 virtual const char *Operator() const { \
2282 return op; \
2283 } \
2284 };
2285
2286 CYPostfix_("++", PostIncrement)
2287 CYPostfix_("--", PostDecrement)
2288
2289 CYPrefix_(true, "delete", Delete)
2290 CYPrefix_(true, "void", Void)
2291 CYPrefix_(true, "typeof", TypeOf)
2292 CYPrefix_(false, "++", PreIncrement)
2293 CYPrefix_(false, "--", PreDecrement)
2294 CYPrefix_(false, "+", Affirm)
2295 CYPrefix_(false, "-", Negate)
2296 CYPrefix_(false, "~", BitwiseNot)
2297 CYPrefix_(false, "!", LogicalNot)
2298
2299 CYInfix_(false, 5, "*", Multiply, CYReplace)
2300 CYInfix_(false, 5, "/", Divide)
2301 CYInfix_(false, 5, "%", Modulus)
2302 CYInfix_(false, 6, "+", Add, CYReplace)
2303 CYInfix_(false, 6, "-", Subtract)
2304 CYInfix_(false, 7, "<<", ShiftLeft)
2305 CYInfix_(false, 7, ">>", ShiftRightSigned)
2306 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
2307 CYInfix_(false, 8, "<", Less)
2308 CYInfix_(false, 8, ">", Greater)
2309 CYInfix_(false, 8, "<=", LessOrEqual)
2310 CYInfix_(false, 8, ">=", GreaterOrEqual)
2311 CYInfix_(true, 8, "instanceof", InstanceOf)
2312 CYInfix_(true, 8, "in", In)
2313 CYInfix_(false, 9, "==", Equal)
2314 CYInfix_(false, 9, "!=", NotEqual)
2315 CYInfix_(false, 9, "===", Identical)
2316 CYInfix_(false, 9, "!==", NotIdentical)
2317 CYInfix_(false, 10, "&", BitwiseAnd)
2318 CYInfix_(false, 11, "^", BitwiseXOr)
2319 CYInfix_(false, 12, "|", BitwiseOr)
2320 CYInfix_(false, 13, "&&", LogicalAnd)
2321 CYInfix_(false, 14, "||", LogicalOr)
2322
2323 CYAssignment_("=", )
2324 CYAssignment_("*=", Multiply)
2325 CYAssignment_("/=", Divide)
2326 CYAssignment_("%=", Modulus)
2327 CYAssignment_("+=", Add)
2328 CYAssignment_("-=", Subtract)
2329 CYAssignment_("<<=", ShiftLeft)
2330 CYAssignment_(">>=", ShiftRightSigned)
2331 CYAssignment_(">>>=", ShiftRightUnsigned)
2332 CYAssignment_("&=", BitwiseAnd)
2333 CYAssignment_("^=", BitwiseXOr)
2334 CYAssignment_("|=", BitwiseOr)
2335
2336 #endif/*CYCRIPT_PARSER_HPP*/