]> git.saurik.com Git - cycript.git/blob - Parser.hpp
Started working on variable name replacement strategies and generally improved compiler.
[cycript.git] / Parser.hpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #ifndef CYPARSER_HPP
41 #define CYPARSER_HPP
42
43 // XXX: wtf is this here?!
44 #define CYPA 16
45
46 #include <iostream>
47
48 #include <string>
49 #include <vector>
50 #include <map>
51 #include <set>
52
53 #include <cstdlib>
54
55 #include "location.hh"
56 #include "Pooling.hpp"
57 #include "Options.hpp"
58
59 class CYContext;
60
61 template <typename Type_>
62 struct CYNext {
63 Type_ *next_;
64
65 CYNext() :
66 next_(NULL)
67 {
68 }
69
70 CYNext(Type_ *next) :
71 next_(next)
72 {
73 }
74
75 void SetNext(Type_ *next) {
76 next_ = next;
77 }
78 };
79
80 struct CYThing {
81 virtual ~CYThing() {
82 }
83
84 virtual void Output(struct CYOutput &out) const = 0;
85 };
86
87 struct CYOutput {
88 std::ostream &out_;
89 CYOptions &options_;
90 bool pretty_;
91 unsigned indent_;
92 bool right_;
93
94 enum {
95 NoMode,
96 NoLetter,
97 NoPlus,
98 NoHyphen,
99 Terminated
100 } mode_;
101
102 CYOutput(std::ostream &out, CYOptions &options) :
103 out_(out),
104 options_(options),
105 pretty_(false),
106 indent_(0),
107 right_(false),
108 mode_(NoMode)
109 {
110 }
111
112 void Check(char value);
113 void Terminate();
114
115 CYOutput &operator <<(char rhs);
116 CYOutput &operator <<(const char *rhs);
117
118 _finline CYOutput &operator <<(const CYThing *rhs) {
119 if (rhs != NULL)
120 rhs->Output(*this);
121 return *this;
122 }
123
124 _finline CYOutput &operator <<(const CYThing &rhs) {
125 rhs.Output(*this);
126 return *this;
127 }
128 };
129
130 struct CYPropertyName {
131 virtual void PropertyName(CYOutput &out) const = 0;
132
133 virtual ~CYPropertyName() {
134 }
135 };
136
137 struct CYExpression;
138
139 enum CYNeeded {
140 CYNever = -1,
141 CYSometimes = 0,
142 CYAlways = 1,
143 };
144
145 enum CYFlags {
146 CYNoFlags = 0,
147 CYNoBrace = (1 << 0),
148 CYNoFunction = (1 << 1),
149 CYNoIn = (1 << 2),
150 CYNoCall = (1 << 3),
151 CYNoRightHand = (1 << 4),
152 CYNoDangle = (1 << 5),
153 CYNoInteger = (1 << 6),
154 CYNoBF = (CYNoBrace | CYNoFunction),
155 };
156
157 struct CYStatement :
158 CYNext<CYStatement>
159 {
160 virtual ~CYStatement() {
161 }
162
163 void Single(CYOutput &out, CYFlags flags) const;
164 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
165
166 CYStatement *ReplaceAll(CYContext &context);
167 virtual CYStatement *Collapse(CYContext &context);
168
169 virtual CYStatement *Replace(CYContext &context) = 0;
170
171 private:
172 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
173 };
174
175 struct CYStatements {
176 CYStatement *first_;
177 CYStatement *last_;
178
179 CYStatements() :
180 first_(NULL),
181 last_(NULL)
182 {
183 }
184
185 operator CYStatement *() const {
186 return first_;
187 }
188
189 CYStatements &operator ->*(CYStatement *next) {
190 if (next != NULL)
191 if (first_ == NULL) {
192 first_ = next;
193 last_ = next;
194 } else for (;; last_ = last_->next_)
195 if (last_->next_ == NULL) {
196 last_->next_ = next;
197 last_ = next;
198 break;
199 }
200 return *this;
201 }
202 };
203
204 struct CYClassName {
205 virtual ~CYClassName() {
206 }
207
208 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
209 virtual void ClassName(CYOutput &out, bool object) const = 0;
210 };
211
212 struct CYWord :
213 CYThing,
214 CYPropertyName,
215 CYClassName
216 {
217 const char *word_;
218
219 CYWord(const char *word) :
220 word_(word)
221 {
222 }
223
224 void Set(const char *value) {
225 word_ = value;
226 }
227
228 virtual const char *Word() const;
229 virtual void Output(CYOutput &out) const;
230
231 virtual CYExpression *ClassName(CYContext &context, bool object);
232 virtual void ClassName(CYOutput &out, bool object) const;
233 virtual void PropertyName(CYOutput &out) const;
234 };
235
236 _finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
237 lhs << &rhs << '=';
238 return lhs << rhs.Word();
239 }
240
241 struct CYIdentifier :
242 CYWord
243 {
244 CYIdentifier *replace_;
245
246 CYIdentifier(const char *word) :
247 CYWord(word),
248 replace_(NULL)
249 {
250 }
251
252 virtual const char *Word() const;
253 CYIdentifier *Replace(CYContext &context);
254 };
255
256 struct CYComment :
257 CYStatement
258 {
259 const char *value_;
260
261 CYComment(const char *value) :
262 value_(value)
263 {
264 }
265
266 virtual CYStatement *Replace(CYContext &context);
267 virtual void Output(CYOutput &out, CYFlags flags) const;
268 };
269
270 struct CYLabel :
271 CYStatement
272 {
273 CYIdentifier *name_;
274 CYStatement *statement_;
275
276 CYLabel(CYIdentifier *name, CYStatement *statement) :
277 name_(name),
278 statement_(statement)
279 {
280 }
281
282 virtual CYStatement *Replace(CYContext &context);
283 virtual void Output(CYOutput &out, CYFlags flags) const;
284 };
285
286 struct CStringLess :
287 std::binary_function<const char *, const char *, bool>
288 {
289 _finline bool operator ()(const char *lhs, const char *rhs) const {
290 return strcmp(lhs, rhs) < 0;
291 }
292 };
293
294 struct CYIdentifierValueLess :
295 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
296 {
297 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
298 return CStringLess()(lhs->Word(), rhs->Word());
299 }
300 };
301
302 enum CYIdentifierFlags {
303 CYIdentifierArgument,
304 CYIdentifierInternal,
305 CYIdentifierVariable
306 };
307
308 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
309 typedef std::set<CYIdentifier *> CYIdentifierAddressSet;
310 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
311
312 struct CYScope {
313 CYScope *parent_;
314 CYIdentifierValueSet identifiers_;
315 CYIdentifierAddressFlagsMap internal_;
316 unsigned offset_;
317
318 CYScope() :
319 parent_(NULL),
320 offset_(0)
321 {
322 }
323
324 void Add(CYContext &context, CYIdentifierAddressSet &external);
325 void Scope(CYContext &context, CYStatement *&statements);
326 };
327
328 struct CYProgram :
329 CYScope,
330 CYThing
331 {
332 CYStatement *statements_;
333
334 CYProgram(CYStatement *statements) :
335 statements_(statements)
336 {
337 }
338
339 virtual void Replace(CYContext &context);
340
341 virtual void Output(CYOutput &out) const;
342 };
343
344 struct CYBlock :
345 CYStatement,
346 CYThing
347 {
348 CYStatement *statements_;
349 CYScope *scope_;
350
351 CYBlock(CYStatement *statements, CYScope *scope = NULL) :
352 statements_(statements),
353 scope_(scope)
354 {
355 }
356
357 operator CYStatement *() const {
358 return statements_;
359 }
360
361 void AddPrev(CYStatement *statement) {
362 CYStatement *last(statement);
363 while (last->next_ != NULL)
364 last = last->next_;
365 last->SetNext(statements_);
366 statements_ = statement;
367 }
368
369 virtual CYStatement *Replace(CYContext &context);
370
371 virtual void Output(CYOutput &out) const;
372 virtual void Output(CYOutput &out, CYFlags flags) const;
373 };
374
375 enum CYState {
376 CYClear,
377 CYRestricted,
378 CYNewLine
379 };
380
381 class CYDriver {
382 public:
383 CYPool pool_;
384
385 CYState state_;
386 void *scanner_;
387
388 const char *data_;
389 size_t size_;
390 FILE *file_;
391
392 bool strict_;
393
394 enum Condition {
395 RegExpCondition,
396 XMLContentCondition,
397 XMLTagCondition,
398 };
399
400 std::string filename_;
401
402 struct Error {
403 bool warning_;
404 cy::location location_;
405 std::string message_;
406 };
407
408 typedef std::vector<Error> Errors;
409
410 CYProgram *program_;
411 Errors errors_;
412
413 private:
414 void ScannerInit();
415 void ScannerDestroy();
416
417 public:
418 CYDriver(const std::string &filename);
419 ~CYDriver();
420
421 Condition GetCondition();
422 void SetCondition(Condition condition);
423
424 void PushCondition(Condition condition);
425 void PopCondition();
426
427 void Warning(const cy::location &location, const char *message);
428 };
429
430 struct CYForInitialiser {
431 virtual ~CYForInitialiser() {
432 }
433
434 virtual void For(CYOutput &out) const = 0;
435 virtual CYExpression *Replace(CYContext &context) = 0;
436 };
437
438 struct CYForInInitialiser {
439 virtual ~CYForInInitialiser() {
440 }
441
442 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
443 virtual const char *ForEachIn() const = 0;
444 virtual CYExpression *ForEachIn(CYContext &out) = 0;
445 virtual CYExpression *Replace(CYContext &context) = 0;
446 };
447
448 struct CYNumber;
449 struct CYString;
450
451 struct CYExpression :
452 CYNext<CYExpression>,
453 CYForInitialiser,
454 CYForInInitialiser,
455 CYClassName,
456 CYThing
457 {
458 virtual unsigned Precedence() const = 0;
459
460 virtual bool RightHand() const {
461 return true;
462 }
463
464 virtual void For(CYOutput &out) const;
465 virtual void ForIn(CYOutput &out, CYFlags flags) const;
466
467 virtual const char *ForEachIn() const;
468 virtual CYExpression *ForEachIn(CYContext &out);
469
470 virtual void Output(CYOutput &out) const;
471 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
472 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
473
474 virtual CYExpression *ClassName(CYContext &context, bool object);
475 virtual void ClassName(CYOutput &out, bool object) const;
476
477 CYExpression *ReplaceAll(CYContext &context);
478
479 virtual CYExpression *Replace(CYContext &context) = 0;
480
481 virtual CYExpression *Primitive(CYContext &context) {
482 return this;
483 }
484
485 virtual CYNumber *Number(CYContext &context) {
486 return NULL;
487 }
488
489 virtual CYString *String(CYContext &context) {
490 return NULL;
491 }
492
493 virtual const char *Word() const {
494 return NULL;
495 }
496 };
497
498 #define CYAlphabetic(value) \
499 virtual bool Alphabetic() const { \
500 return value; \
501 }
502
503 #define CYPrecedence(value) \
504 virtual unsigned Precedence() const { \
505 return value; \
506 }
507
508 #define CYRightHand(value) \
509 virtual bool RightHand() const { \
510 return value; \
511 }
512
513 struct CYCompound :
514 CYExpression
515 {
516 CYExpression *expressions_;
517
518 CYCompound(CYExpression *expressions = NULL) :
519 expressions_(expressions)
520 {
521 }
522
523 void AddPrev(CYExpression *expression) {
524 CYExpression *last(expression);
525 while (last->next_ != NULL)
526 last = last->next_;
527 last->SetNext(expressions_);
528 expressions_ = expression;
529 }
530
531 CYPrecedence(17)
532
533 virtual CYExpression *Replace(CYContext &context);
534 void Output(CYOutput &out, CYFlags flags) const;
535 };
536
537 struct CYFunctionParameter :
538 CYNext<CYFunctionParameter>,
539 CYThing
540 {
541 CYIdentifier *name_;
542
543 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
544 CYNext<CYFunctionParameter>(next),
545 name_(name)
546 {
547 }
548
549 void Replace(CYContext &context);
550 virtual void Output(CYOutput &out) const;
551 };
552
553 struct CYComprehension :
554 CYNext<CYComprehension>,
555 CYThing
556 {
557 virtual const char *Name() const = 0;
558
559 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
560 CYFunctionParameter *Parameters(CYContext &context) const;
561 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
562 virtual void Output(CYOutput &out) const = 0;
563 };
564
565 struct CYForInComprehension :
566 CYComprehension
567 {
568 CYIdentifier *name_;
569 CYExpression *set_;
570
571 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
572 name_(name),
573 set_(set)
574 {
575 }
576
577 virtual const char *Name() const {
578 return name_->Word();
579 }
580
581 virtual CYFunctionParameter *Parameter(CYContext &context) const;
582 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
583 virtual void Output(CYOutput &out) const;
584 };
585
586 struct CYForEachInComprehension :
587 CYComprehension
588 {
589 CYIdentifier *name_;
590 CYExpression *set_;
591
592 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
593 name_(name),
594 set_(set)
595 {
596 }
597
598 virtual const char *Name() const {
599 return name_->Word();
600 }
601
602 virtual CYFunctionParameter *Parameter(CYContext &context) const;
603 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
604 virtual void Output(CYOutput &out) const;
605 };
606
607 struct CYIfComprehension :
608 CYComprehension
609 {
610 CYExpression *test_;
611
612 CYIfComprehension(CYExpression *test) :
613 test_(test)
614 {
615 }
616
617 virtual const char *Name() const {
618 return NULL;
619 }
620
621 virtual CYFunctionParameter *Parameter(CYContext &context) const;
622 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
623 virtual void Output(CYOutput &out) const;
624 };
625
626 struct CYArrayComprehension :
627 CYExpression
628 {
629 CYExpression *expression_;
630 CYComprehension *comprehensions_;
631
632 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
633 expression_(expression),
634 comprehensions_(comprehensions)
635 {
636 }
637
638 CYPrecedence(0)
639
640 virtual CYExpression *Replace(CYContext &context);
641 virtual void Output(CYOutput &out, CYFlags flags) const;
642 };
643
644 struct CYLiteral :
645 CYExpression
646 {
647 CYPrecedence(0)
648 CYRightHand(false)
649 };
650
651 struct CYTrivial :
652 CYLiteral
653 {
654 virtual CYExpression *Replace(CYContext &context);
655 };
656
657 struct CYMagic :
658 CYExpression
659 {
660 CYPrecedence(0)
661 CYRightHand(false)
662 };
663
664 struct CYRange {
665 uint64_t lo_;
666 uint64_t hi_;
667
668 CYRange(uint64_t lo, uint64_t hi) :
669 lo_(lo), hi_(hi)
670 {
671 }
672
673 bool operator [](uint8_t value) const {
674 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
675 }
676
677 void operator()(uint8_t value) {
678 if (value >> 7)
679 return;
680 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
681 }
682 };
683
684 extern CYRange DigitRange_;
685 extern CYRange WordStartRange_;
686 extern CYRange WordEndRange_;
687
688 struct CYString :
689 CYTrivial,
690 CYPropertyName
691 {
692 const char *value_;
693 size_t size_;
694
695 CYString() :
696 value_(NULL),
697 size_(0)
698 {
699 }
700
701 CYString(const char *value) :
702 value_(value),
703 size_(strlen(value))
704 {
705 }
706
707 CYString(const char *value, size_t size) :
708 value_(value),
709 size_(size)
710 {
711 }
712
713 CYString(const CYWord *word) :
714 value_(word->Word()),
715 size_(strlen(value_))
716 {
717 }
718
719 const char *Value() const {
720 return value_;
721 }
722
723 virtual const char *Word() const;
724
725 virtual CYNumber *Number(CYContext &context);
726 virtual CYString *String(CYContext &context);
727
728 CYString *Concat(CYContext &out, CYString *rhs) const;
729 virtual void Output(CYOutput &out, CYFlags flags) const;
730 virtual void PropertyName(CYOutput &out) const;
731 };
732
733 struct CYNumber :
734 CYTrivial,
735 CYPropertyName
736 {
737 double value_;
738
739 CYNumber(double value) :
740 value_(value)
741 {
742 }
743
744 double Value() const {
745 return value_;
746 }
747
748 virtual CYNumber *Number(CYContext &context);
749 virtual CYString *String(CYContext &context);
750
751 virtual void Output(CYOutput &out, CYFlags flags) const;
752 virtual void PropertyName(CYOutput &out) const;
753 };
754
755 struct CYRegEx :
756 CYTrivial
757 {
758 const char *value_;
759
760 CYRegEx(const char *value) :
761 value_(value)
762 {
763 }
764
765 const char *Value() const {
766 return value_;
767 }
768
769 virtual void Output(CYOutput &out, CYFlags flags) const;
770 };
771
772 struct CYNull :
773 CYWord,
774 CYTrivial
775 {
776 CYNull() :
777 CYWord("null")
778 {
779 }
780
781 virtual CYNumber *Number(CYContext &context);
782 virtual CYString *String(CYContext &context);
783
784 virtual void Output(CYOutput &out, CYFlags flags) const;
785 };
786
787 struct CYThis :
788 CYWord,
789 CYMagic
790 {
791 CYThis() :
792 CYWord("this")
793 {
794 }
795
796 virtual CYExpression *Replace(CYContext &context);
797 virtual void Output(CYOutput &out, CYFlags flags) const;
798 };
799
800 struct CYBoolean :
801 CYTrivial
802 {
803 virtual bool Value() const = 0;
804 virtual void Output(CYOutput &out, CYFlags flags) const;
805 };
806
807 struct CYFalse :
808 CYWord,
809 CYBoolean
810 {
811 CYFalse() :
812 CYWord("false")
813 {
814 }
815
816 virtual bool Value() const {
817 return false;
818 }
819
820 virtual CYNumber *Number(CYContext &context);
821 virtual CYString *String(CYContext &context);
822 };
823
824 struct CYTrue :
825 CYWord,
826 CYBoolean
827 {
828 CYTrue() :
829 CYWord("true")
830 {
831 }
832
833 virtual bool Value() const {
834 return true;
835 }
836
837 virtual CYNumber *Number(CYContext &context);
838 virtual CYString *String(CYContext &context);
839 };
840
841 struct CYVariable :
842 CYExpression
843 {
844 CYIdentifier *name_;
845
846 CYVariable(CYIdentifier *name) :
847 name_(name)
848 {
849 }
850
851 CYPrecedence(0)
852 CYRightHand(false)
853
854 virtual CYExpression *Replace(CYContext &context);
855 virtual void Output(CYOutput &out, CYFlags flags) const;
856 };
857
858 struct CYPrefix :
859 CYExpression
860 {
861 CYExpression *rhs_;
862
863 CYPrefix(CYExpression *rhs) :
864 rhs_(rhs)
865 {
866 }
867
868 virtual bool Alphabetic() const = 0;
869 virtual const char *Operator() const = 0;
870
871 CYPrecedence(4)
872
873 virtual CYExpression *Replace(CYContext &context);
874 virtual void Output(CYOutput &out, CYFlags flags) const;
875 };
876
877 struct CYInfix :
878 CYExpression
879 {
880 CYExpression *lhs_;
881 CYExpression *rhs_;
882
883 CYInfix(CYExpression *lhs, CYExpression *rhs) :
884 lhs_(lhs),
885 rhs_(rhs)
886 {
887 }
888
889 void SetLeft(CYExpression *lhs) {
890 lhs_ = lhs;
891 }
892
893 virtual bool Alphabetic() const = 0;
894 virtual const char *Operator() const = 0;
895
896 virtual CYExpression *Replace(CYContext &context);
897 virtual void Output(CYOutput &out, CYFlags flags) const;
898 };
899
900 struct CYPostfix :
901 CYExpression
902 {
903 CYExpression *lhs_;
904
905 CYPostfix(CYExpression *lhs) :
906 lhs_(lhs)
907 {
908 }
909
910 virtual const char *Operator() const = 0;
911
912 CYPrecedence(3)
913
914 virtual CYExpression *Replace(CYContext &context);
915 virtual void Output(CYOutput &out, CYFlags flags) const;
916 };
917
918 struct CYAssignment :
919 CYExpression
920 {
921 CYExpression *lhs_;
922 CYExpression *rhs_;
923
924 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
925 lhs_(lhs),
926 rhs_(rhs)
927 {
928 }
929
930 void SetLeft(CYExpression *lhs) {
931 lhs_ = lhs;
932 }
933
934 virtual const char *Operator() const = 0;
935
936 CYPrecedence(16)
937
938 virtual CYExpression *Replace(CYContext &context);
939 virtual void Output(CYOutput &out, CYFlags flags) const;
940 };
941
942 struct CYArgument :
943 CYNext<CYArgument>,
944 CYThing
945 {
946 CYWord *name_;
947 CYExpression *value_;
948
949 CYArgument(CYExpression *value, CYArgument *next = NULL) :
950 CYNext<CYArgument>(next),
951 name_(NULL),
952 value_(value)
953 {
954 }
955
956 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
957 CYNext<CYArgument>(next),
958 name_(name),
959 value_(value)
960 {
961 }
962
963 void Replace(CYContext &context);
964 void Output(CYOutput &out) const;
965 };
966
967 struct CYBlank :
968 public CYWord
969 {
970 CYBlank() :
971 CYWord("")
972 {
973 }
974 };
975
976 struct CYClause :
977 CYThing,
978 CYNext<CYClause>
979 {
980 CYExpression *case_;
981 CYStatement *statements_;
982
983 CYClause(CYExpression *_case, CYStatement *statements) :
984 case_(_case),
985 statements_(statements)
986 {
987 }
988
989 void Replace(CYContext &context);
990 virtual void Output(CYOutput &out) const;
991 };
992
993 struct CYElement :
994 CYNext<CYElement>,
995 CYThing
996 {
997 CYExpression *value_;
998
999 CYElement(CYExpression *value, CYElement *next) :
1000 CYNext<CYElement>(next),
1001 value_(value)
1002 {
1003 }
1004
1005 void Replace(CYContext &context);
1006 void Output(CYOutput &out) const;
1007 };
1008
1009 struct CYArray :
1010 CYLiteral
1011 {
1012 CYElement *elements_;
1013
1014 CYArray(CYElement *elements = NULL) :
1015 elements_(elements)
1016 {
1017 }
1018
1019 virtual CYExpression *Replace(CYContext &context);
1020 virtual void Output(CYOutput &out, CYFlags flags) const;
1021 };
1022
1023 struct CYProperty :
1024 CYNext<CYProperty>,
1025 CYThing
1026 {
1027 CYPropertyName *name_;
1028 CYExpression *value_;
1029
1030 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1031 CYNext<CYProperty>(next),
1032 name_(name),
1033 value_(value)
1034 {
1035 }
1036
1037 void Replace(CYContext &context);
1038 virtual void Output(CYOutput &out) const;
1039 };
1040
1041 struct CYDeclaration :
1042 CYForInInitialiser
1043 {
1044 CYIdentifier *identifier_;
1045 CYExpression *initialiser_;
1046
1047 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1048 identifier_(identifier),
1049 initialiser_(initialiser)
1050 {
1051 }
1052
1053 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1054
1055 virtual const char *ForEachIn() const;
1056 virtual CYExpression *ForEachIn(CYContext &out);
1057
1058 virtual CYExpression *Replace(CYContext &context);
1059 virtual CYAssignment *Assignment(CYContext &context);
1060
1061 virtual void Output(CYOutput &out, CYFlags flags) const;
1062 };
1063
1064 struct CYDeclarations :
1065 CYNext<CYDeclarations>,
1066 CYThing,
1067 CYForInitialiser
1068 {
1069 CYDeclaration *declaration_;
1070
1071 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1072 CYNext<CYDeclarations>(next),
1073 declaration_(declaration)
1074 {
1075 }
1076
1077 virtual void For(CYOutput &out) const;
1078
1079 virtual CYCompound *Replace(CYContext &context);
1080 CYProperty *Property(CYContext &context);
1081
1082 virtual void Output(CYOutput &out) const;
1083 virtual void Output(CYOutput &out, CYFlags flags) const;
1084 };
1085
1086 struct CYVar :
1087 CYStatement
1088 {
1089 CYDeclarations *declarations_;
1090
1091 CYVar(CYDeclarations *declarations) :
1092 declarations_(declarations)
1093 {
1094 }
1095
1096 virtual CYStatement *Replace(CYContext &context);
1097 virtual void Output(CYOutput &out, CYFlags flags) const;
1098 };
1099
1100 struct CYLet :
1101 CYStatement
1102 {
1103 CYDeclarations *declarations_;
1104 CYBlock code_;
1105
1106 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1107 declarations_(declarations),
1108 code_(statements)
1109 {
1110 }
1111
1112 virtual CYStatement *Replace(CYContext &context);
1113 virtual void Output(CYOutput &out, CYFlags flags) const;
1114 };
1115
1116 struct CYFor :
1117 CYStatement
1118 {
1119 CYForInitialiser *initialiser_;
1120 CYExpression *test_;
1121 CYExpression *increment_;
1122 CYStatement *code_;
1123
1124 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1125 initialiser_(initialiser),
1126 test_(test),
1127 increment_(increment),
1128 code_(code)
1129 {
1130 }
1131
1132 virtual CYStatement *Replace(CYContext &context);
1133 virtual void Output(CYOutput &out, CYFlags flags) const;
1134 };
1135
1136 struct CYForIn :
1137 CYStatement
1138 {
1139 CYForInInitialiser *initialiser_;
1140 CYExpression *set_;
1141 CYStatement *code_;
1142
1143 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1144 initialiser_(initialiser),
1145 set_(set),
1146 code_(code)
1147 {
1148 }
1149
1150 virtual CYStatement *Replace(CYContext &context);
1151 virtual void Output(CYOutput &out, CYFlags flags) const;
1152 };
1153
1154 struct CYForEachIn :
1155 CYStatement
1156 {
1157 CYForInInitialiser *initialiser_;
1158 CYExpression *set_;
1159 CYStatement *code_;
1160
1161 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1162 initialiser_(initialiser),
1163 set_(set),
1164 code_(code)
1165 {
1166 }
1167
1168 virtual CYStatement *Replace(CYContext &context);
1169 virtual void Output(CYOutput &out, CYFlags flags) const;
1170 };
1171
1172 struct CYObject :
1173 CYLiteral
1174 {
1175 CYProperty *properties_;
1176
1177 CYObject(CYProperty *properties) :
1178 properties_(properties)
1179 {
1180 }
1181
1182 virtual CYExpression *Replace(CYContext &context);
1183 void Output(CYOutput &out, CYFlags flags) const;
1184 };
1185
1186 struct CYMember :
1187 CYExpression
1188 {
1189 CYExpression *object_;
1190 CYExpression *property_;
1191
1192 CYMember(CYExpression *object, CYExpression *property) :
1193 object_(object),
1194 property_(property)
1195 {
1196 }
1197
1198 void SetLeft(CYExpression *object) {
1199 object_ = object;
1200 }
1201
1202 void Replace_(CYContext &context);
1203 };
1204
1205 struct CYDirectMember :
1206 CYMember
1207 {
1208 CYDirectMember(CYExpression *object, CYExpression *property) :
1209 CYMember(object, property)
1210 {
1211 }
1212
1213 CYPrecedence(1)
1214 CYRightHand(false)
1215
1216 virtual CYExpression *Replace(CYContext &context);
1217 virtual void Output(CYOutput &out, CYFlags flags) const;
1218 };
1219
1220 struct CYIndirectMember :
1221 CYMember
1222 {
1223 CYIndirectMember(CYExpression *object, CYExpression *property) :
1224 CYMember(object, property)
1225 {
1226 }
1227
1228 CYPrecedence(1)
1229 CYRightHand(false)
1230
1231 virtual CYExpression *Replace(CYContext &context);
1232 virtual void Output(CYOutput &out, CYFlags flags) const;
1233 };
1234
1235 struct CYNew :
1236 CYExpression
1237 {
1238 CYExpression *constructor_;
1239 CYArgument *arguments_;
1240
1241 CYNew(CYExpression *constructor, CYArgument *arguments) :
1242 constructor_(constructor),
1243 arguments_(arguments)
1244 {
1245 }
1246
1247 virtual unsigned Precedence() const {
1248 return arguments_ == NULL ? 2 : 1;
1249 }
1250
1251 CYRightHand(false)
1252
1253 virtual CYExpression *Replace(CYContext &context);
1254 virtual void Output(CYOutput &out, CYFlags flags) const;
1255 };
1256
1257 struct CYCall :
1258 CYExpression
1259 {
1260 CYExpression *function_;
1261 CYArgument *arguments_;
1262
1263 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1264 function_(function),
1265 arguments_(arguments)
1266 {
1267 }
1268
1269 CYPrecedence(1)
1270 CYRightHand(false)
1271
1272 virtual CYExpression *Replace(CYContext &context);
1273 virtual void Output(CYOutput &out, CYFlags flags) const;
1274 };
1275
1276 struct CYIf :
1277 CYStatement
1278 {
1279 CYExpression *test_;
1280 CYStatement *true_;
1281 CYStatement *false_;
1282
1283 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1284 test_(test),
1285 true_(_true),
1286 false_(_false)
1287 {
1288 }
1289
1290 virtual CYStatement *Replace(CYContext &context);
1291 virtual void Output(CYOutput &out, CYFlags flags) const;
1292 };
1293
1294 struct CYDoWhile :
1295 CYStatement
1296 {
1297 CYExpression *test_;
1298 CYStatement *code_;
1299
1300 CYDoWhile(CYExpression *test, CYStatement *code) :
1301 test_(test),
1302 code_(code)
1303 {
1304 }
1305
1306 virtual CYStatement *Replace(CYContext &context);
1307 virtual void Output(CYOutput &out, CYFlags flags) const;
1308 };
1309
1310 struct CYWhile :
1311 CYStatement
1312 {
1313 CYExpression *test_;
1314 CYStatement *code_;
1315
1316 CYWhile(CYExpression *test, CYStatement *code) :
1317 test_(test),
1318 code_(code)
1319 {
1320 }
1321
1322 virtual CYStatement *Replace(CYContext &context);
1323 virtual void Output(CYOutput &out, CYFlags flags) const;
1324 };
1325
1326 struct CYFunction :
1327 CYScope
1328 {
1329 CYIdentifier *name_;
1330 CYFunctionParameter *parameters_;
1331 CYBlock code_;
1332
1333 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1334 name_(name),
1335 parameters_(parameters),
1336 code_(statements, this)
1337 {
1338 }
1339
1340 virtual ~CYFunction() {
1341 }
1342
1343 virtual void Replace_(CYContext &context);
1344 virtual void Output(CYOutput &out, CYFlags flags) const;
1345 };
1346
1347 struct CYFunctionExpression :
1348 CYFunction,
1349 CYExpression
1350 {
1351 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1352 CYFunction(name, parameters, statements)
1353 {
1354 }
1355
1356 CYPrecedence(0)
1357 CYRightHand(false)
1358
1359 virtual CYExpression *Replace(CYContext &context);
1360 virtual void Output(CYOutput &out, CYFlags flags) const;
1361 };
1362
1363 struct CYFunctionStatement :
1364 CYFunction,
1365 CYStatement
1366 {
1367 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1368 CYFunction(name, parameters, statements)
1369 {
1370 }
1371
1372 virtual CYStatement *Replace(CYContext &context);
1373 virtual void Output(CYOutput &out, CYFlags flags) const;
1374 };
1375
1376 struct CYExpress :
1377 CYStatement
1378 {
1379 CYExpression *expression_;
1380
1381 CYExpress(CYExpression *expression) :
1382 expression_(expression)
1383 {
1384 if (expression == NULL)
1385 throw;
1386 }
1387
1388 virtual CYStatement *Collapse(CYContext &context);
1389 virtual CYStatement *Replace(CYContext &context);
1390 virtual void Output(CYOutput &out, CYFlags flags) const;
1391 };
1392
1393 struct CYContinue :
1394 CYStatement
1395 {
1396 CYIdentifier *label_;
1397
1398 CYContinue(CYIdentifier *label) :
1399 label_(label)
1400 {
1401 }
1402
1403 virtual CYStatement *Replace(CYContext &context);
1404 virtual void Output(CYOutput &out, CYFlags flags) const;
1405 };
1406
1407 struct CYBreak :
1408 CYStatement
1409 {
1410 CYIdentifier *label_;
1411
1412 CYBreak(CYIdentifier *label) :
1413 label_(label)
1414 {
1415 }
1416
1417 virtual CYStatement *Replace(CYContext &context);
1418 virtual void Output(CYOutput &out, CYFlags flags) const;
1419 };
1420
1421 struct CYReturn :
1422 CYStatement
1423 {
1424 CYExpression *value_;
1425
1426 CYReturn(CYExpression *value) :
1427 value_(value)
1428 {
1429 }
1430
1431 virtual CYStatement *Replace(CYContext &context);
1432 virtual void Output(CYOutput &out, CYFlags flags) const;
1433 };
1434
1435 struct CYEmpty :
1436 CYStatement
1437 {
1438 virtual CYStatement *Collapse(CYContext &context);
1439 virtual CYStatement *Replace(CYContext &context);
1440 virtual void Output(CYOutput &out, CYFlags flags) const;
1441 };
1442
1443 struct CYFinally :
1444 CYThing
1445 {
1446 CYBlock code_;
1447
1448 CYFinally(CYStatement *statements) :
1449 code_(statements)
1450 {
1451 }
1452
1453 void Replace(CYContext &context);
1454 virtual void Output(CYOutput &out) const;
1455 };
1456
1457 namespace cy {
1458 namespace Syntax {
1459
1460 struct Catch :
1461 CYThing
1462 {
1463 CYIdentifier *name_;
1464 CYBlock code_;
1465
1466 Catch(CYIdentifier *name, CYStatement *statements) :
1467 name_(name),
1468 code_(statements)
1469 {
1470 }
1471
1472 void Replace(CYContext &context);
1473 virtual void Output(CYOutput &out) const;
1474 };
1475
1476 struct Try :
1477 CYStatement
1478 {
1479 CYBlock code_;
1480 Catch *catch_;
1481 CYFinally *finally_;
1482
1483 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1484 code_(statements),
1485 catch_(_catch),
1486 finally_(finally)
1487 {
1488 }
1489
1490 virtual CYStatement *Replace(CYContext &context);
1491 virtual void Output(CYOutput &out, CYFlags flags) const;
1492 };
1493
1494 struct Throw :
1495 CYStatement
1496 {
1497 CYExpression *value_;
1498
1499 Throw(CYExpression *value) :
1500 value_(value)
1501 {
1502 }
1503
1504 virtual CYStatement *Replace(CYContext &context);
1505 virtual void Output(CYOutput &out, CYFlags flags) const;
1506 };
1507
1508 } }
1509
1510 struct CYWith :
1511 CYStatement
1512 {
1513 CYExpression *scope_;
1514 CYStatement *code_;
1515
1516 CYWith(CYExpression *scope, CYStatement *code) :
1517 scope_(scope),
1518 code_(code)
1519 {
1520 }
1521
1522 virtual CYStatement *Replace(CYContext &context);
1523 virtual void Output(CYOutput &out, CYFlags flags) const;
1524 };
1525
1526 struct CYSwitch :
1527 CYStatement
1528 {
1529 CYExpression *value_;
1530 CYClause *clauses_;
1531
1532 CYSwitch(CYExpression *value, CYClause *clauses) :
1533 value_(value),
1534 clauses_(clauses)
1535 {
1536 }
1537
1538 virtual CYStatement *Replace(CYContext &context);
1539 virtual void Output(CYOutput &out, CYFlags flags) const;
1540 };
1541
1542 struct CYCondition :
1543 CYExpression
1544 {
1545 CYExpression *test_;
1546 CYExpression *true_;
1547 CYExpression *false_;
1548
1549 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1550 test_(test),
1551 true_(_true),
1552 false_(_false)
1553 {
1554 }
1555
1556 CYPrecedence(15)
1557
1558 virtual CYExpression *Replace(CYContext &context);
1559 virtual void Output(CYOutput &out, CYFlags flags) const;
1560 };
1561
1562 struct CYAddressOf :
1563 CYPrefix
1564 {
1565 CYAddressOf(CYExpression *rhs) :
1566 CYPrefix(rhs)
1567 {
1568 }
1569
1570 virtual const char *Operator() const {
1571 return "&";
1572 }
1573
1574 CYAlphabetic(false)
1575
1576 virtual CYExpression *Replace(CYContext &context);
1577 };
1578
1579 struct CYIndirect :
1580 CYPrefix
1581 {
1582 CYIndirect(CYExpression *rhs) :
1583 CYPrefix(rhs)
1584 {
1585 }
1586
1587 virtual const char *Operator() const {
1588 return "*";
1589 }
1590
1591 CYAlphabetic(false)
1592
1593 virtual CYExpression *Replace(CYContext &context);
1594 };
1595
1596 #define CYReplace \
1597 virtual CYExpression *Replace(CYContext &context);
1598
1599 #define CYPostfix_(op, name, args...) \
1600 struct CY ## name : \
1601 CYPostfix \
1602 { args \
1603 CY ## name(CYExpression *lhs) : \
1604 CYPostfix(lhs) \
1605 { \
1606 } \
1607 \
1608 virtual const char *Operator() const { \
1609 return op; \
1610 } \
1611 };
1612
1613 #define CYPrefix_(alphabetic, op, name, args...) \
1614 struct CY ## name : \
1615 CYPrefix \
1616 { args \
1617 CY ## name(CYExpression *rhs) : \
1618 CYPrefix(rhs) \
1619 { \
1620 } \
1621 \
1622 CYAlphabetic(alphabetic) \
1623 \
1624 virtual const char *Operator() const { \
1625 return op; \
1626 } \
1627 };
1628
1629 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1630 struct CY ## name : \
1631 CYInfix \
1632 { args \
1633 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1634 CYInfix(lhs, rhs) \
1635 { \
1636 } \
1637 \
1638 CYAlphabetic(alphabetic) \
1639 CYPrecedence(precedence) \
1640 \
1641 virtual const char *Operator() const { \
1642 return op; \
1643 } \
1644 };
1645
1646 #define CYAssignment_(op, name, args...) \
1647 struct CY ## name ## Assign : \
1648 CYAssignment \
1649 { args \
1650 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1651 CYAssignment(lhs, rhs) \
1652 { \
1653 } \
1654 \
1655 virtual const char *Operator() const { \
1656 return op; \
1657 } \
1658 };
1659
1660 CYPostfix_("++", PostIncrement)
1661 CYPostfix_("--", PostDecrement)
1662
1663 CYPrefix_(true, "delete", Delete)
1664 CYPrefix_(true, "void", Void)
1665 CYPrefix_(true, "typeof", TypeOf)
1666 CYPrefix_(false, "++", PreIncrement)
1667 CYPrefix_(false, "--", PreDecrement)
1668 CYPrefix_(false, "+", Affirm)
1669 CYPrefix_(false, "-", Negate)
1670 CYPrefix_(false, "~", BitwiseNot)
1671 CYPrefix_(false, "!", LogicalNot)
1672
1673 CYInfix_(false, 5, "*", Multiply)
1674 CYInfix_(false, 5, "/", Divide)
1675 CYInfix_(false, 5, "%", Modulus)
1676 CYInfix_(false, 6, "+", Add, CYReplace)
1677 CYInfix_(false, 6, "-", Subtract)
1678 CYInfix_(false, 7, "<<", ShiftLeft)
1679 CYInfix_(false, 7, ">>", ShiftRightSigned)
1680 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1681 CYInfix_(false, 8, "<", Less)
1682 CYInfix_(false, 8, ">", Greater)
1683 CYInfix_(false, 8, "<=", LessOrEqual)
1684 CYInfix_(false, 8, ">=", GreaterOrEqual)
1685 CYInfix_(true, 8, "instanceof", InstanceOf)
1686 CYInfix_(true, 8, "in", In)
1687 CYInfix_(false, 9, "==", Equal)
1688 CYInfix_(false, 9, "!=", NotEqual)
1689 CYInfix_(false, 9, "===", Identical)
1690 CYInfix_(false, 9, "!==", NotIdentical)
1691 CYInfix_(false, 10, "&", BitwiseAnd)
1692 CYInfix_(false, 11, "^", BitwiseXOr)
1693 CYInfix_(false, 12, "|", BitwiseOr)
1694 CYInfix_(false, 13, "&&", LogicalAnd)
1695 CYInfix_(false, 14, "||", LogicalOr)
1696
1697 CYAssignment_("=", )
1698 CYAssignment_("*=", Multiply)
1699 CYAssignment_("/=", Divide)
1700 CYAssignment_("%=", Modulus)
1701 CYAssignment_("+=", Add)
1702 CYAssignment_("-=", Subtract)
1703 CYAssignment_("<<=", ShiftLeft)
1704 CYAssignment_(">>=", ShiftRightSigned)
1705 CYAssignment_(">>>=", ShiftRightUnsigned)
1706 CYAssignment_("&=", BitwiseAnd)
1707 CYAssignment_("^=", BitwiseXOr)
1708 CYAssignment_("|=", BitwiseOr)
1709
1710 #endif/*CYPARSER_HPP*/