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