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