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