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