]> git.saurik.com Git - cycript.git/blob - Parser.hpp
d8eb56b42d25119cdd447320b0d62d3c34cb68b2
[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 CYNext<CYIdentifier>,
243 CYWord
244 {
245 CYIdentifier *replace_;
246 size_t offset_;
247
248 CYIdentifier(const char *word) :
249 CYWord(word),
250 replace_(NULL),
251 offset_(0)
252 {
253 }
254
255 virtual const char *Word() const;
256 CYIdentifier *Replace(CYContext &context);
257 };
258
259 struct CYComment :
260 CYStatement
261 {
262 const char *value_;
263
264 CYComment(const char *value) :
265 value_(value)
266 {
267 }
268
269 virtual CYStatement *Replace(CYContext &context);
270 virtual void Output(CYOutput &out, CYFlags flags) const;
271 };
272
273 struct CYLabel :
274 CYStatement
275 {
276 CYIdentifier *name_;
277 CYStatement *statement_;
278
279 CYLabel(CYIdentifier *name, CYStatement *statement) :
280 name_(name),
281 statement_(statement)
282 {
283 }
284
285 virtual CYStatement *Replace(CYContext &context);
286 virtual void Output(CYOutput &out, CYFlags flags) const;
287 };
288
289 struct CYCStringLess :
290 std::binary_function<const char *, const char *, bool>
291 {
292 _finline bool operator ()(const char *lhs, const char *rhs) const {
293 return strcmp(lhs, rhs) < 0;
294 }
295 };
296
297 struct CYIdentifierValueLess :
298 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
299 {
300 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
301 return CYCStringLess()(lhs->Word(), rhs->Word());
302 }
303 };
304
305 enum CYIdentifierFlags {
306 CYIdentifierArgument,
307 CYIdentifierVariable,
308 CYIdentifierOther,
309 CYIdentifierMagic,
310 };
311
312 typedef std::set<const char *, CYCStringLess> CYCStringSet;
313 typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
314 typedef std::vector<CYIdentifier *> CYIdentifierAddressVector;
315 typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
316
317 struct CYScope {
318 CYScope *parent_;
319 CYIdentifierAddressFlagsMap internal_;
320
321 CYIdentifierValueSet identifiers_;
322
323 CYScope() :
324 parent_(NULL)
325 {
326 }
327
328 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
329 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
330 void Merge(CYContext &context, CYIdentifier *identifier);
331 void Scope(CYContext &context, CYStatement *&statements);
332 };
333
334 struct CYProgram :
335 CYScope,
336 CYThing
337 {
338 CYStatement *statements_;
339 CYIdentifierAddressVector rename_;
340
341 CYProgram(CYStatement *statements) :
342 statements_(statements)
343 {
344 }
345
346 virtual void Replace(CYContext &context);
347 virtual void Output(CYOutput &out) const;
348 };
349
350 struct CYContext :
351 CYScope
352 {
353 apr_pool_t *pool_;
354 CYOptions &options_;
355 CYScope *scope_;
356 CYProgram *program_;
357
358 CYContext(apr_pool_t *pool, CYOptions &options) :
359 pool_(pool),
360 options_(options),
361 scope_(this),
362 program_(NULL)
363 {
364 }
365
366 template <typename Type_>
367 void Replace(Type_ *&value) {
368 for (;;) if (value == NULL)
369 break;
370 else {
371 Type_ *replace(value->Replace(*this));
372 if (replace != value)
373 value = replace;
374 else break;
375 }
376 }
377 };
378
379 struct CYBlock :
380 CYStatement,
381 CYThing
382 {
383 CYStatement *statements_;
384 CYScope *scope_;
385
386 CYBlock(CYStatement *statements, CYScope *scope = NULL) :
387 statements_(statements),
388 scope_(scope)
389 {
390 }
391
392 operator CYStatement *() const {
393 return statements_;
394 }
395
396 void AddPrev(CYStatement *statement) {
397 CYStatement *last(statement);
398 while (last->next_ != NULL)
399 last = last->next_;
400 last->SetNext(statements_);
401 statements_ = statement;
402 }
403
404 virtual CYStatement *Replace(CYContext &context);
405
406 virtual void Output(CYOutput &out) const;
407 virtual void Output(CYOutput &out, CYFlags flags) const;
408 };
409
410 enum CYState {
411 CYClear,
412 CYRestricted,
413 CYNewLine
414 };
415
416 class CYDriver {
417 public:
418 CYPool pool_;
419
420 CYState state_;
421 void *scanner_;
422
423 const char *data_;
424 size_t size_;
425 FILE *file_;
426
427 bool strict_;
428
429 enum Condition {
430 RegExpCondition,
431 XMLContentCondition,
432 XMLTagCondition,
433 };
434
435 std::string filename_;
436
437 struct Error {
438 bool warning_;
439 cy::location location_;
440 std::string message_;
441 };
442
443 typedef std::vector<Error> Errors;
444
445 CYProgram *program_;
446 Errors errors_;
447
448 private:
449 void ScannerInit();
450 void ScannerDestroy();
451
452 public:
453 CYDriver(const std::string &filename);
454 ~CYDriver();
455
456 Condition GetCondition();
457 void SetCondition(Condition condition);
458
459 void PushCondition(Condition condition);
460 void PopCondition();
461
462 void Warning(const cy::location &location, const char *message);
463 };
464
465 struct CYForInitialiser {
466 virtual ~CYForInitialiser() {
467 }
468
469 virtual void For(CYOutput &out) const = 0;
470 virtual CYExpression *Replace(CYContext &context) = 0;
471 };
472
473 struct CYForInInitialiser {
474 virtual ~CYForInInitialiser() {
475 }
476
477 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
478 virtual const char *ForEachIn() const = 0;
479 virtual CYExpression *ForEachIn(CYContext &out) = 0;
480 virtual CYExpression *Replace(CYContext &context) = 0;
481 };
482
483 struct CYNumber;
484 struct CYString;
485
486 struct CYExpression :
487 CYNext<CYExpression>,
488 CYForInitialiser,
489 CYForInInitialiser,
490 CYClassName,
491 CYThing
492 {
493 virtual unsigned Precedence() const = 0;
494
495 virtual bool RightHand() const {
496 return true;
497 }
498
499 virtual void For(CYOutput &out) const;
500 virtual void ForIn(CYOutput &out, CYFlags flags) const;
501
502 virtual const char *ForEachIn() const;
503 virtual CYExpression *ForEachIn(CYContext &out);
504
505 virtual void Output(CYOutput &out) const;
506 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
507 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
508
509 virtual CYExpression *ClassName(CYContext &context, bool object);
510 virtual void ClassName(CYOutput &out, bool object) const;
511
512 CYExpression *ReplaceAll(CYContext &context);
513
514 virtual CYExpression *Replace(CYContext &context) = 0;
515
516 virtual CYExpression *Primitive(CYContext &context) {
517 return this;
518 }
519
520 virtual CYNumber *Number(CYContext &context) {
521 return NULL;
522 }
523
524 virtual CYString *String(CYContext &context) {
525 return NULL;
526 }
527
528 virtual const char *Word() const {
529 return NULL;
530 }
531 };
532
533 #define CYAlphabetic(value) \
534 virtual bool Alphabetic() const { \
535 return value; \
536 }
537
538 #define CYPrecedence(value) \
539 virtual unsigned Precedence() const { \
540 return value; \
541 }
542
543 #define CYRightHand(value) \
544 virtual bool RightHand() const { \
545 return value; \
546 }
547
548 struct CYCompound :
549 CYExpression
550 {
551 CYExpression *expressions_;
552
553 CYCompound(CYExpression *expressions = NULL) :
554 expressions_(expressions)
555 {
556 }
557
558 void AddPrev(CYExpression *expression) {
559 CYExpression *last(expression);
560 while (last->next_ != NULL)
561 last = last->next_;
562 last->SetNext(expressions_);
563 expressions_ = expression;
564 }
565
566 CYPrecedence(17)
567
568 virtual CYExpression *Replace(CYContext &context);
569 void Output(CYOutput &out, CYFlags flags) const;
570 };
571
572 struct CYFunctionParameter :
573 CYNext<CYFunctionParameter>,
574 CYThing
575 {
576 CYIdentifier *name_;
577
578 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
579 CYNext<CYFunctionParameter>(next),
580 name_(name)
581 {
582 }
583
584 void Replace(CYContext &context);
585 virtual void Output(CYOutput &out) const;
586 };
587
588 struct CYComprehension :
589 CYNext<CYComprehension>,
590 CYThing
591 {
592 virtual const char *Name() const = 0;
593
594 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
595 CYFunctionParameter *Parameters(CYContext &context) const;
596 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
597 virtual void Output(CYOutput &out) const = 0;
598 };
599
600 struct CYForInComprehension :
601 CYComprehension
602 {
603 CYIdentifier *name_;
604 CYExpression *set_;
605
606 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
607 name_(name),
608 set_(set)
609 {
610 }
611
612 virtual const char *Name() const {
613 return name_->Word();
614 }
615
616 virtual CYFunctionParameter *Parameter(CYContext &context) const;
617 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
618 virtual void Output(CYOutput &out) const;
619 };
620
621 struct CYForEachInComprehension :
622 CYComprehension
623 {
624 CYIdentifier *name_;
625 CYExpression *set_;
626
627 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
628 name_(name),
629 set_(set)
630 {
631 }
632
633 virtual const char *Name() const {
634 return name_->Word();
635 }
636
637 virtual CYFunctionParameter *Parameter(CYContext &context) const;
638 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
639 virtual void Output(CYOutput &out) const;
640 };
641
642 struct CYIfComprehension :
643 CYComprehension
644 {
645 CYExpression *test_;
646
647 CYIfComprehension(CYExpression *test) :
648 test_(test)
649 {
650 }
651
652 virtual const char *Name() const {
653 return NULL;
654 }
655
656 virtual CYFunctionParameter *Parameter(CYContext &context) const;
657 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
658 virtual void Output(CYOutput &out) const;
659 };
660
661 struct CYArrayComprehension :
662 CYExpression
663 {
664 CYExpression *expression_;
665 CYComprehension *comprehensions_;
666
667 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
668 expression_(expression),
669 comprehensions_(comprehensions)
670 {
671 }
672
673 CYPrecedence(0)
674
675 virtual CYExpression *Replace(CYContext &context);
676 virtual void Output(CYOutput &out, CYFlags flags) const;
677 };
678
679 struct CYLiteral :
680 CYExpression
681 {
682 CYPrecedence(0)
683 CYRightHand(false)
684 };
685
686 struct CYTrivial :
687 CYLiteral
688 {
689 virtual CYExpression *Replace(CYContext &context);
690 };
691
692 struct CYMagic :
693 CYExpression
694 {
695 CYPrecedence(0)
696 CYRightHand(false)
697 };
698
699 struct CYRange {
700 uint64_t lo_;
701 uint64_t hi_;
702
703 CYRange(uint64_t lo, uint64_t hi) :
704 lo_(lo), hi_(hi)
705 {
706 }
707
708 bool operator [](uint8_t value) const {
709 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
710 }
711
712 void operator()(uint8_t value) {
713 if (value >> 7)
714 return;
715 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
716 }
717 };
718
719 extern CYRange DigitRange_;
720 extern CYRange WordStartRange_;
721 extern CYRange WordEndRange_;
722
723 struct CYString :
724 CYTrivial,
725 CYPropertyName
726 {
727 const char *value_;
728 size_t size_;
729
730 CYString() :
731 value_(NULL),
732 size_(0)
733 {
734 }
735
736 CYString(const char *value) :
737 value_(value),
738 size_(strlen(value))
739 {
740 }
741
742 CYString(const char *value, size_t size) :
743 value_(value),
744 size_(size)
745 {
746 }
747
748 CYString(const CYWord *word) :
749 value_(word->Word()),
750 size_(strlen(value_))
751 {
752 }
753
754 const char *Value() const {
755 return value_;
756 }
757
758 virtual const char *Word() const;
759
760 virtual CYNumber *Number(CYContext &context);
761 virtual CYString *String(CYContext &context);
762
763 CYString *Concat(CYContext &out, CYString *rhs) const;
764 virtual void Output(CYOutput &out, CYFlags flags) const;
765 virtual void PropertyName(CYOutput &out) const;
766 };
767
768 struct CYNumber :
769 CYTrivial,
770 CYPropertyName
771 {
772 double value_;
773
774 CYNumber(double value) :
775 value_(value)
776 {
777 }
778
779 double Value() const {
780 return value_;
781 }
782
783 virtual CYNumber *Number(CYContext &context);
784 virtual CYString *String(CYContext &context);
785
786 virtual void Output(CYOutput &out, CYFlags flags) const;
787 virtual void PropertyName(CYOutput &out) const;
788 };
789
790 struct CYRegEx :
791 CYTrivial
792 {
793 const char *value_;
794
795 CYRegEx(const char *value) :
796 value_(value)
797 {
798 }
799
800 const char *Value() const {
801 return value_;
802 }
803
804 virtual void Output(CYOutput &out, CYFlags flags) const;
805 };
806
807 struct CYNull :
808 CYWord,
809 CYTrivial
810 {
811 CYNull() :
812 CYWord("null")
813 {
814 }
815
816 virtual CYNumber *Number(CYContext &context);
817 virtual CYString *String(CYContext &context);
818
819 virtual void Output(CYOutput &out, CYFlags flags) const;
820 };
821
822 struct CYThis :
823 CYWord,
824 CYMagic
825 {
826 CYThis() :
827 CYWord("this")
828 {
829 }
830
831 virtual CYExpression *Replace(CYContext &context);
832 virtual void Output(CYOutput &out, CYFlags flags) const;
833 };
834
835 struct CYBoolean :
836 CYTrivial
837 {
838 virtual bool Value() const = 0;
839 virtual void Output(CYOutput &out, CYFlags flags) const;
840 };
841
842 struct CYFalse :
843 CYWord,
844 CYBoolean
845 {
846 CYFalse() :
847 CYWord("false")
848 {
849 }
850
851 virtual bool Value() const {
852 return false;
853 }
854
855 virtual CYNumber *Number(CYContext &context);
856 virtual CYString *String(CYContext &context);
857 };
858
859 struct CYTrue :
860 CYWord,
861 CYBoolean
862 {
863 CYTrue() :
864 CYWord("true")
865 {
866 }
867
868 virtual bool Value() const {
869 return true;
870 }
871
872 virtual CYNumber *Number(CYContext &context);
873 virtual CYString *String(CYContext &context);
874 };
875
876 struct CYVariable :
877 CYExpression
878 {
879 CYIdentifier *name_;
880
881 CYVariable(CYIdentifier *name) :
882 name_(name)
883 {
884 }
885
886 CYPrecedence(0)
887 CYRightHand(false)
888
889 virtual CYExpression *Replace(CYContext &context);
890 virtual void Output(CYOutput &out, CYFlags flags) const;
891 };
892
893 struct CYPrefix :
894 CYExpression
895 {
896 CYExpression *rhs_;
897
898 CYPrefix(CYExpression *rhs) :
899 rhs_(rhs)
900 {
901 }
902
903 virtual bool Alphabetic() const = 0;
904 virtual const char *Operator() const = 0;
905
906 CYPrecedence(4)
907
908 virtual CYExpression *Replace(CYContext &context);
909 virtual void Output(CYOutput &out, CYFlags flags) const;
910 };
911
912 struct CYInfix :
913 CYExpression
914 {
915 CYExpression *lhs_;
916 CYExpression *rhs_;
917
918 CYInfix(CYExpression *lhs, CYExpression *rhs) :
919 lhs_(lhs),
920 rhs_(rhs)
921 {
922 }
923
924 void SetLeft(CYExpression *lhs) {
925 lhs_ = lhs;
926 }
927
928 virtual bool Alphabetic() const = 0;
929 virtual const char *Operator() const = 0;
930
931 virtual CYExpression *Replace(CYContext &context);
932 virtual void Output(CYOutput &out, CYFlags flags) const;
933 };
934
935 struct CYPostfix :
936 CYExpression
937 {
938 CYExpression *lhs_;
939
940 CYPostfix(CYExpression *lhs) :
941 lhs_(lhs)
942 {
943 }
944
945 virtual const char *Operator() const = 0;
946
947 CYPrecedence(3)
948
949 virtual CYExpression *Replace(CYContext &context);
950 virtual void Output(CYOutput &out, CYFlags flags) const;
951 };
952
953 struct CYAssignment :
954 CYExpression
955 {
956 CYExpression *lhs_;
957 CYExpression *rhs_;
958
959 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
960 lhs_(lhs),
961 rhs_(rhs)
962 {
963 }
964
965 void SetLeft(CYExpression *lhs) {
966 lhs_ = lhs;
967 }
968
969 virtual const char *Operator() const = 0;
970
971 CYPrecedence(16)
972
973 virtual CYExpression *Replace(CYContext &context);
974 virtual void Output(CYOutput &out, CYFlags flags) const;
975 };
976
977 struct CYArgument :
978 CYNext<CYArgument>,
979 CYThing
980 {
981 CYWord *name_;
982 CYExpression *value_;
983
984 CYArgument(CYExpression *value, CYArgument *next = NULL) :
985 CYNext<CYArgument>(next),
986 name_(NULL),
987 value_(value)
988 {
989 }
990
991 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
992 CYNext<CYArgument>(next),
993 name_(name),
994 value_(value)
995 {
996 }
997
998 void Replace(CYContext &context);
999 void Output(CYOutput &out) const;
1000 };
1001
1002 struct CYBlank :
1003 public CYWord
1004 {
1005 CYBlank() :
1006 CYWord("")
1007 {
1008 }
1009 };
1010
1011 struct CYClause :
1012 CYThing,
1013 CYNext<CYClause>
1014 {
1015 CYExpression *case_;
1016 CYStatement *statements_;
1017
1018 CYClause(CYExpression *_case, CYStatement *statements) :
1019 case_(_case),
1020 statements_(statements)
1021 {
1022 }
1023
1024 void Replace(CYContext &context);
1025 virtual void Output(CYOutput &out) const;
1026 };
1027
1028 struct CYElement :
1029 CYNext<CYElement>,
1030 CYThing
1031 {
1032 CYExpression *value_;
1033
1034 CYElement(CYExpression *value, CYElement *next) :
1035 CYNext<CYElement>(next),
1036 value_(value)
1037 {
1038 }
1039
1040 void Replace(CYContext &context);
1041 void Output(CYOutput &out) const;
1042 };
1043
1044 struct CYArray :
1045 CYLiteral
1046 {
1047 CYElement *elements_;
1048
1049 CYArray(CYElement *elements = NULL) :
1050 elements_(elements)
1051 {
1052 }
1053
1054 virtual CYExpression *Replace(CYContext &context);
1055 virtual void Output(CYOutput &out, CYFlags flags) const;
1056 };
1057
1058 struct CYProperty :
1059 CYNext<CYProperty>,
1060 CYThing
1061 {
1062 CYPropertyName *name_;
1063 CYExpression *value_;
1064
1065 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1066 CYNext<CYProperty>(next),
1067 name_(name),
1068 value_(value)
1069 {
1070 }
1071
1072 void Replace(CYContext &context);
1073 virtual void Output(CYOutput &out) const;
1074 };
1075
1076 struct CYDeclaration :
1077 CYForInInitialiser
1078 {
1079 CYIdentifier *identifier_;
1080 CYExpression *initialiser_;
1081
1082 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
1083 identifier_(identifier),
1084 initialiser_(initialiser)
1085 {
1086 }
1087
1088 virtual void ForIn(CYOutput &out, CYFlags flags) const;
1089
1090 virtual const char *ForEachIn() const;
1091 virtual CYExpression *ForEachIn(CYContext &out);
1092
1093 virtual CYExpression *Replace(CYContext &context);
1094 virtual CYAssignment *Assignment(CYContext &context);
1095
1096 virtual void Output(CYOutput &out, CYFlags flags) const;
1097 };
1098
1099 struct CYDeclarations :
1100 CYNext<CYDeclarations>,
1101 CYThing,
1102 CYForInitialiser
1103 {
1104 CYDeclaration *declaration_;
1105
1106 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
1107 CYNext<CYDeclarations>(next),
1108 declaration_(declaration)
1109 {
1110 }
1111
1112 virtual void For(CYOutput &out) const;
1113
1114 virtual CYCompound *Replace(CYContext &context);
1115 CYProperty *Property(CYContext &context);
1116
1117 virtual void Output(CYOutput &out) const;
1118 virtual void Output(CYOutput &out, CYFlags flags) const;
1119 };
1120
1121 struct CYVar :
1122 CYStatement
1123 {
1124 CYDeclarations *declarations_;
1125
1126 CYVar(CYDeclarations *declarations) :
1127 declarations_(declarations)
1128 {
1129 }
1130
1131 virtual CYStatement *Replace(CYContext &context);
1132 virtual void Output(CYOutput &out, CYFlags flags) const;
1133 };
1134
1135 struct CYLet :
1136 CYStatement
1137 {
1138 CYDeclarations *declarations_;
1139 CYBlock code_;
1140
1141 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1142 declarations_(declarations),
1143 code_(statements)
1144 {
1145 }
1146
1147 virtual CYStatement *Replace(CYContext &context);
1148 virtual void Output(CYOutput &out, CYFlags flags) const;
1149 };
1150
1151 struct CYFor :
1152 CYStatement
1153 {
1154 CYForInitialiser *initialiser_;
1155 CYExpression *test_;
1156 CYExpression *increment_;
1157 CYStatement *code_;
1158
1159 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1160 initialiser_(initialiser),
1161 test_(test),
1162 increment_(increment),
1163 code_(code)
1164 {
1165 }
1166
1167 virtual CYStatement *Replace(CYContext &context);
1168 virtual void Output(CYOutput &out, CYFlags flags) const;
1169 };
1170
1171 struct CYForIn :
1172 CYStatement
1173 {
1174 CYForInInitialiser *initialiser_;
1175 CYExpression *set_;
1176 CYStatement *code_;
1177
1178 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1179 initialiser_(initialiser),
1180 set_(set),
1181 code_(code)
1182 {
1183 }
1184
1185 virtual CYStatement *Replace(CYContext &context);
1186 virtual void Output(CYOutput &out, CYFlags flags) const;
1187 };
1188
1189 struct CYForEachIn :
1190 CYStatement
1191 {
1192 CYForInInitialiser *initialiser_;
1193 CYExpression *set_;
1194 CYStatement *code_;
1195
1196 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1197 initialiser_(initialiser),
1198 set_(set),
1199 code_(code)
1200 {
1201 }
1202
1203 virtual CYStatement *Replace(CYContext &context);
1204 virtual void Output(CYOutput &out, CYFlags flags) const;
1205 };
1206
1207 struct CYObject :
1208 CYLiteral
1209 {
1210 CYProperty *properties_;
1211
1212 CYObject(CYProperty *properties) :
1213 properties_(properties)
1214 {
1215 }
1216
1217 virtual CYExpression *Replace(CYContext &context);
1218 void Output(CYOutput &out, CYFlags flags) const;
1219 };
1220
1221 struct CYMember :
1222 CYExpression
1223 {
1224 CYExpression *object_;
1225 CYExpression *property_;
1226
1227 CYMember(CYExpression *object, CYExpression *property) :
1228 object_(object),
1229 property_(property)
1230 {
1231 }
1232
1233 void SetLeft(CYExpression *object) {
1234 object_ = object;
1235 }
1236
1237 void Replace_(CYContext &context);
1238 };
1239
1240 struct CYDirectMember :
1241 CYMember
1242 {
1243 CYDirectMember(CYExpression *object, CYExpression *property) :
1244 CYMember(object, property)
1245 {
1246 }
1247
1248 CYPrecedence(1)
1249 CYRightHand(false)
1250
1251 virtual CYExpression *Replace(CYContext &context);
1252 virtual void Output(CYOutput &out, CYFlags flags) const;
1253 };
1254
1255 struct CYIndirectMember :
1256 CYMember
1257 {
1258 CYIndirectMember(CYExpression *object, CYExpression *property) :
1259 CYMember(object, property)
1260 {
1261 }
1262
1263 CYPrecedence(1)
1264 CYRightHand(false)
1265
1266 virtual CYExpression *Replace(CYContext &context);
1267 virtual void Output(CYOutput &out, CYFlags flags) const;
1268 };
1269
1270 struct CYNew :
1271 CYExpression
1272 {
1273 CYExpression *constructor_;
1274 CYArgument *arguments_;
1275
1276 CYNew(CYExpression *constructor, CYArgument *arguments) :
1277 constructor_(constructor),
1278 arguments_(arguments)
1279 {
1280 }
1281
1282 virtual unsigned Precedence() const {
1283 return arguments_ == NULL ? 2 : 1;
1284 }
1285
1286 CYRightHand(false)
1287
1288 virtual CYExpression *Replace(CYContext &context);
1289 virtual void Output(CYOutput &out, CYFlags flags) const;
1290 };
1291
1292 struct CYCall :
1293 CYExpression
1294 {
1295 CYExpression *function_;
1296 CYArgument *arguments_;
1297
1298 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
1299 function_(function),
1300 arguments_(arguments)
1301 {
1302 }
1303
1304 CYPrecedence(1)
1305 CYRightHand(false)
1306
1307 virtual CYExpression *Replace(CYContext &context);
1308 virtual void Output(CYOutput &out, CYFlags flags) const;
1309 };
1310
1311 struct CYIf :
1312 CYStatement
1313 {
1314 CYExpression *test_;
1315 CYStatement *true_;
1316 CYStatement *false_;
1317
1318 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
1319 test_(test),
1320 true_(_true),
1321 false_(_false)
1322 {
1323 }
1324
1325 virtual CYStatement *Replace(CYContext &context);
1326 virtual void Output(CYOutput &out, CYFlags flags) const;
1327 };
1328
1329 struct CYDoWhile :
1330 CYStatement
1331 {
1332 CYExpression *test_;
1333 CYStatement *code_;
1334
1335 CYDoWhile(CYExpression *test, CYStatement *code) :
1336 test_(test),
1337 code_(code)
1338 {
1339 }
1340
1341 virtual CYStatement *Replace(CYContext &context);
1342 virtual void Output(CYOutput &out, CYFlags flags) const;
1343 };
1344
1345 struct CYWhile :
1346 CYStatement
1347 {
1348 CYExpression *test_;
1349 CYStatement *code_;
1350
1351 CYWhile(CYExpression *test, CYStatement *code) :
1352 test_(test),
1353 code_(code)
1354 {
1355 }
1356
1357 virtual CYStatement *Replace(CYContext &context);
1358 virtual void Output(CYOutput &out, CYFlags flags) const;
1359 };
1360
1361 struct CYFunction :
1362 CYScope
1363 {
1364 CYIdentifier *name_;
1365 CYFunctionParameter *parameters_;
1366 CYBlock code_;
1367
1368 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1369 name_(name),
1370 parameters_(parameters),
1371 code_(statements, this)
1372 {
1373 }
1374
1375 virtual ~CYFunction() {
1376 }
1377
1378 void Inject(CYContext &context);
1379 virtual void Replace_(CYContext &context, bool outer);
1380 virtual void Output(CYOutput &out, CYFlags flags) const;
1381 };
1382
1383 struct CYFunctionExpression :
1384 CYFunction,
1385 CYExpression
1386 {
1387 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1388 CYFunction(name, parameters, statements)
1389 {
1390 }
1391
1392 CYPrecedence(0)
1393 CYRightHand(false)
1394
1395 virtual CYExpression *Replace(CYContext &context);
1396 virtual void Output(CYOutput &out, CYFlags flags) const;
1397 };
1398
1399 struct CYFunctionStatement :
1400 CYFunction,
1401 CYStatement
1402 {
1403 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1404 CYFunction(name, parameters, statements)
1405 {
1406 }
1407
1408 virtual CYStatement *Replace(CYContext &context);
1409 virtual void Output(CYOutput &out, CYFlags flags) const;
1410 };
1411
1412 struct CYExpress :
1413 CYStatement
1414 {
1415 CYExpression *expression_;
1416
1417 CYExpress(CYExpression *expression) :
1418 expression_(expression)
1419 {
1420 if (expression == NULL)
1421 throw;
1422 }
1423
1424 virtual CYStatement *Collapse(CYContext &context);
1425 virtual CYStatement *Replace(CYContext &context);
1426 virtual void Output(CYOutput &out, CYFlags flags) const;
1427 };
1428
1429 struct CYContinue :
1430 CYStatement
1431 {
1432 CYIdentifier *label_;
1433
1434 CYContinue(CYIdentifier *label) :
1435 label_(label)
1436 {
1437 }
1438
1439 virtual CYStatement *Replace(CYContext &context);
1440 virtual void Output(CYOutput &out, CYFlags flags) const;
1441 };
1442
1443 struct CYBreak :
1444 CYStatement
1445 {
1446 CYIdentifier *label_;
1447
1448 CYBreak(CYIdentifier *label) :
1449 label_(label)
1450 {
1451 }
1452
1453 virtual CYStatement *Replace(CYContext &context);
1454 virtual void Output(CYOutput &out, CYFlags flags) const;
1455 };
1456
1457 struct CYReturn :
1458 CYStatement
1459 {
1460 CYExpression *value_;
1461
1462 CYReturn(CYExpression *value) :
1463 value_(value)
1464 {
1465 }
1466
1467 virtual CYStatement *Replace(CYContext &context);
1468 virtual void Output(CYOutput &out, CYFlags flags) const;
1469 };
1470
1471 struct CYEmpty :
1472 CYStatement
1473 {
1474 virtual CYStatement *Collapse(CYContext &context);
1475 virtual CYStatement *Replace(CYContext &context);
1476 virtual void Output(CYOutput &out, CYFlags flags) const;
1477 };
1478
1479 struct CYFinally :
1480 CYThing
1481 {
1482 CYBlock code_;
1483
1484 CYFinally(CYStatement *statements) :
1485 code_(statements)
1486 {
1487 }
1488
1489 void Replace(CYContext &context);
1490 virtual void Output(CYOutput &out) const;
1491 };
1492
1493 namespace cy {
1494 namespace Syntax {
1495
1496 struct Catch :
1497 CYThing
1498 {
1499 CYIdentifier *name_;
1500 CYBlock code_;
1501
1502 Catch(CYIdentifier *name, CYStatement *statements) :
1503 name_(name),
1504 code_(statements)
1505 {
1506 }
1507
1508 void Replace(CYContext &context);
1509 virtual void Output(CYOutput &out) const;
1510 };
1511
1512 struct Try :
1513 CYStatement
1514 {
1515 CYBlock code_;
1516 Catch *catch_;
1517 CYFinally *finally_;
1518
1519 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
1520 code_(statements),
1521 catch_(_catch),
1522 finally_(finally)
1523 {
1524 }
1525
1526 virtual CYStatement *Replace(CYContext &context);
1527 virtual void Output(CYOutput &out, CYFlags flags) const;
1528 };
1529
1530 struct Throw :
1531 CYStatement
1532 {
1533 CYExpression *value_;
1534
1535 Throw(CYExpression *value) :
1536 value_(value)
1537 {
1538 }
1539
1540 virtual CYStatement *Replace(CYContext &context);
1541 virtual void Output(CYOutput &out, CYFlags flags) const;
1542 };
1543
1544 } }
1545
1546 struct CYWith :
1547 CYStatement
1548 {
1549 CYExpression *scope_;
1550 CYStatement *code_;
1551
1552 CYWith(CYExpression *scope, CYStatement *code) :
1553 scope_(scope),
1554 code_(code)
1555 {
1556 }
1557
1558 virtual CYStatement *Replace(CYContext &context);
1559 virtual void Output(CYOutput &out, CYFlags flags) const;
1560 };
1561
1562 struct CYSwitch :
1563 CYStatement
1564 {
1565 CYExpression *value_;
1566 CYClause *clauses_;
1567
1568 CYSwitch(CYExpression *value, CYClause *clauses) :
1569 value_(value),
1570 clauses_(clauses)
1571 {
1572 }
1573
1574 virtual CYStatement *Replace(CYContext &context);
1575 virtual void Output(CYOutput &out, CYFlags flags) const;
1576 };
1577
1578 struct CYCondition :
1579 CYExpression
1580 {
1581 CYExpression *test_;
1582 CYExpression *true_;
1583 CYExpression *false_;
1584
1585 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1586 test_(test),
1587 true_(_true),
1588 false_(_false)
1589 {
1590 }
1591
1592 CYPrecedence(15)
1593
1594 virtual CYExpression *Replace(CYContext &context);
1595 virtual void Output(CYOutput &out, CYFlags flags) const;
1596 };
1597
1598 struct CYAddressOf :
1599 CYPrefix
1600 {
1601 CYAddressOf(CYExpression *rhs) :
1602 CYPrefix(rhs)
1603 {
1604 }
1605
1606 virtual const char *Operator() const {
1607 return "&";
1608 }
1609
1610 CYAlphabetic(false)
1611
1612 virtual CYExpression *Replace(CYContext &context);
1613 };
1614
1615 struct CYIndirect :
1616 CYPrefix
1617 {
1618 CYIndirect(CYExpression *rhs) :
1619 CYPrefix(rhs)
1620 {
1621 }
1622
1623 virtual const char *Operator() const {
1624 return "*";
1625 }
1626
1627 CYAlphabetic(false)
1628
1629 virtual CYExpression *Replace(CYContext &context);
1630 };
1631
1632 #define CYReplace \
1633 virtual CYExpression *Replace(CYContext &context);
1634
1635 #define CYPostfix_(op, name, args...) \
1636 struct CY ## name : \
1637 CYPostfix \
1638 { args \
1639 CY ## name(CYExpression *lhs) : \
1640 CYPostfix(lhs) \
1641 { \
1642 } \
1643 \
1644 virtual const char *Operator() const { \
1645 return op; \
1646 } \
1647 };
1648
1649 #define CYPrefix_(alphabetic, op, name, args...) \
1650 struct CY ## name : \
1651 CYPrefix \
1652 { args \
1653 CY ## name(CYExpression *rhs) : \
1654 CYPrefix(rhs) \
1655 { \
1656 } \
1657 \
1658 CYAlphabetic(alphabetic) \
1659 \
1660 virtual const char *Operator() const { \
1661 return op; \
1662 } \
1663 };
1664
1665 #define CYInfix_(alphabetic, precedence, op, name, args...) \
1666 struct CY ## name : \
1667 CYInfix \
1668 { args \
1669 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1670 CYInfix(lhs, rhs) \
1671 { \
1672 } \
1673 \
1674 CYAlphabetic(alphabetic) \
1675 CYPrecedence(precedence) \
1676 \
1677 virtual const char *Operator() const { \
1678 return op; \
1679 } \
1680 };
1681
1682 #define CYAssignment_(op, name, args...) \
1683 struct CY ## name ## Assign : \
1684 CYAssignment \
1685 { args \
1686 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1687 CYAssignment(lhs, rhs) \
1688 { \
1689 } \
1690 \
1691 virtual const char *Operator() const { \
1692 return op; \
1693 } \
1694 };
1695
1696 CYPostfix_("++", PostIncrement)
1697 CYPostfix_("--", PostDecrement)
1698
1699 CYPrefix_(true, "delete", Delete)
1700 CYPrefix_(true, "void", Void)
1701 CYPrefix_(true, "typeof", TypeOf)
1702 CYPrefix_(false, "++", PreIncrement)
1703 CYPrefix_(false, "--", PreDecrement)
1704 CYPrefix_(false, "+", Affirm)
1705 CYPrefix_(false, "-", Negate)
1706 CYPrefix_(false, "~", BitwiseNot)
1707 CYPrefix_(false, "!", LogicalNot)
1708
1709 CYInfix_(false, 5, "*", Multiply)
1710 CYInfix_(false, 5, "/", Divide)
1711 CYInfix_(false, 5, "%", Modulus)
1712 CYInfix_(false, 6, "+", Add, CYReplace)
1713 CYInfix_(false, 6, "-", Subtract)
1714 CYInfix_(false, 7, "<<", ShiftLeft)
1715 CYInfix_(false, 7, ">>", ShiftRightSigned)
1716 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1717 CYInfix_(false, 8, "<", Less)
1718 CYInfix_(false, 8, ">", Greater)
1719 CYInfix_(false, 8, "<=", LessOrEqual)
1720 CYInfix_(false, 8, ">=", GreaterOrEqual)
1721 CYInfix_(true, 8, "instanceof", InstanceOf)
1722 CYInfix_(true, 8, "in", In)
1723 CYInfix_(false, 9, "==", Equal)
1724 CYInfix_(false, 9, "!=", NotEqual)
1725 CYInfix_(false, 9, "===", Identical)
1726 CYInfix_(false, 9, "!==", NotIdentical)
1727 CYInfix_(false, 10, "&", BitwiseAnd)
1728 CYInfix_(false, 11, "^", BitwiseXOr)
1729 CYInfix_(false, 12, "|", BitwiseOr)
1730 CYInfix_(false, 13, "&&", LogicalAnd)
1731 CYInfix_(false, 14, "||", LogicalOr)
1732
1733 CYAssignment_("=", )
1734 CYAssignment_("*=", Multiply)
1735 CYAssignment_("/=", Divide)
1736 CYAssignment_("%=", Modulus)
1737 CYAssignment_("+=", Add)
1738 CYAssignment_("-=", Subtract)
1739 CYAssignment_("<<=", ShiftLeft)
1740 CYAssignment_(">>=", ShiftRightSigned)
1741 CYAssignment_(">>>=", ShiftRightUnsigned)
1742 CYAssignment_("&=", BitwiseAnd)
1743 CYAssignment_("^=", BitwiseXOr)
1744 CYAssignment_("|=", BitwiseOr)
1745
1746 #endif/*CYPARSER_HPP*/