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