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