]> git.saurik.com Git - cycript.git/blob - Parser.hpp
88a4b1344ad4299926793ebf0a124f13302568ff
[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(std::ostream &out) const = 0;
71 };
72
73 _finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) {
74 rhs.Output(out);
75 return out;
76 }
77
78 struct CYSource :
79 CYNext<CYSource>
80 {
81 virtual void Show(std::ostream &out) const;
82 virtual void Output(std::ostream &out) const = 0;
83 virtual void Output(std::ostream &out, bool block) const;
84 };
85
86 struct CYPropertyName {
87 virtual void PropertyName(std::ostream &out) const = 0;
88 };
89
90 struct CYClassName {
91 virtual void ClassName(std::ostream &out) const = 0;
92 };
93
94 struct CYWord :
95 CYThing,
96 CYPropertyName,
97 CYClassName
98 {
99 const char *word_;
100
101 CYWord(const char *word) :
102 word_(word)
103 {
104 }
105
106 const char *Value() const {
107 return word_;
108 }
109
110 virtual void Output(std::ostream &out) const;
111
112 virtual void ClassName(std::ostream &out) const;
113 virtual void PropertyName(std::ostream &out) const;
114 };
115
116 struct CYIdentifier :
117 CYWord
118 {
119 CYIdentifier(const char *word) :
120 CYWord(word)
121 {
122 }
123 };
124
125 struct CYLabel :
126 CYNext<CYLabel>
127 {
128 CYIdentifier *identifier_;
129
130 CYLabel(CYIdentifier *identifier, CYLabel *next) :
131 CYNext<CYLabel>(next),
132 identifier_(identifier)
133 {
134 }
135 };
136
137 struct CYStatement :
138 CYSource
139 {
140 CYLabel *label_;
141
142 void AddLabel(CYIdentifier *identifier) {
143 label_ = new CYLabel(identifier, label_);
144 }
145 };
146
147 enum CYState {
148 CYClear,
149 CYRestricted,
150 CYNewLine
151 };
152
153 class CYDriver {
154 public:
155 CYPool pool_;
156
157 CYState state_;
158 void *scanner_;
159
160 const char *data_;
161 size_t size_;
162
163 std::string filename_;
164
165 struct Error {
166 cy::location location_;
167 std::string message_;
168 };
169
170 typedef std::vector<Error> Errors;
171
172 CYSource *source_;
173 Errors errors_;
174
175 private:
176 void ScannerInit();
177 void ScannerDestroy();
178
179 public:
180 CYDriver(const std::string &filename);
181 ~CYDriver();
182 };
183
184 struct CYPart {
185 virtual void Part(std::ostream &out) const = 0;
186 };
187
188 struct CYForInitialiser :
189 CYPart
190 {
191 };
192
193 struct CYForInInitialiser :
194 CYPart
195 {
196 };
197
198 enum CYFlags {
199 CYNoFlags = 0,
200 CYNoBrace = (1 << 0),
201 CYNoFunction = (1 << 1),
202 CYNoLeader = (1 << 2),
203 CYNoTrailer = (1 << 3),
204 CYNoIn = (1 << 4),
205 CYNoHyphen = (1 << 5),
206 };
207
208 struct CYExpression :
209 CYNext<CYExpression>,
210 CYForInitialiser,
211 CYForInInitialiser,
212 CYClassName
213 {
214 virtual unsigned Precedence() const = 0;
215 virtual void Part(std::ostream &out) const;
216 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
217 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
218
219 virtual void ClassName(std::ostream &out) const;
220
221 virtual const char *Word() const {
222 return NULL;
223 }
224 };
225
226 #define CYAlphabetic(value) \
227 virtual bool Alphabetic() const { \
228 return value; \
229 }
230
231 #define CYPrecedence(value) \
232 virtual unsigned Precedence() const { \
233 return value; \
234 }
235
236 struct CYCompound :
237 CYExpression
238 {
239 CYExpression *expressions_;
240
241 CYCompound(CYExpression *expressions) :
242 expressions_(expressions)
243 {
244 }
245
246 void AddPrev(CYExpression *expression) {
247 CYExpression *last(expression);
248 while (last->next_ != NULL)
249 last = last->next_;
250 last->SetNext(expressions_);
251 expressions_ = expression;
252 }
253
254 CYPrecedence(17)
255
256 void Output(std::ostream &out, CYFlags flags) const;
257 };
258
259 struct CYLiteral :
260 CYExpression
261 {
262 CYPrecedence(0)
263 };
264
265 struct CYMagic :
266 CYExpression
267 {
268 CYPrecedence(0)
269 };
270
271 struct CYSelectorPart :
272 CYNext<CYSelectorPart>
273 {
274 CYWord *name_;
275 bool value_;
276
277 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
278 CYNext<CYSelectorPart>(next),
279 name_(name),
280 value_(value)
281 {
282 }
283
284 virtual void Output(std::ostream &out) const;
285 };
286
287 struct CYSelector :
288 CYLiteral
289 {
290 CYSelectorPart *name_;
291
292 CYSelector(CYSelectorPart *name) :
293 name_(name)
294 {
295 }
296
297 CYPrecedence(1)
298
299 virtual void Output(std::ostream &out, CYFlags flags) const;
300 };
301
302 struct CYRange {
303 uint64_t lo_;
304 uint64_t hi_;
305
306 CYRange(uint64_t lo, uint64_t hi) :
307 lo_(lo), hi_(hi)
308 {
309 }
310
311 bool operator [](uint8_t value) const {
312 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
313 }
314
315 void operator()(uint8_t value) {
316 if (value >> 7)
317 return;
318 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
319 }
320 };
321
322 extern CYRange DigitRange_;
323 extern CYRange WordStartRange_;
324 extern CYRange WordEndRange_;
325
326 struct CYString :
327 CYLiteral,
328 CYPropertyName
329 {
330 const char *value_;
331 size_t size_;
332
333 CYString(const char *value, size_t size) :
334 value_(value),
335 size_(size)
336 {
337 }
338
339 CYString(const CYIdentifier *identifier) :
340 value_(identifier->Value()),
341 size_(strlen(value_))
342 {
343 }
344
345 const char *Value() const {
346 return value_;
347 }
348
349 virtual const char *Word() const {
350 if (size_ == 0 || !WordStartRange_[value_[0]])
351 return NULL;
352 for (size_t i(1); i != size_; ++i)
353 if (!WordEndRange_[value_[i]])
354 return NULL;
355 return Value();
356 }
357
358 virtual void Output(std::ostream &out) const {
359 return Output(out, CYNoFlags);
360 }
361
362 virtual void Output(std::ostream &out, CYFlags flags) const;
363 virtual void PropertyName(std::ostream &out) const;
364 };
365
366 struct CYNumber :
367 CYLiteral,
368 CYPropertyName
369 {
370 double value_;
371
372 CYNumber(double value) :
373 value_(value)
374 {
375 }
376
377 double Value() const {
378 return value_;
379 }
380
381 virtual void Output(std::ostream &out) const {
382 return Output(out, CYNoFlags);
383 }
384
385 virtual void Output(std::ostream &out, CYFlags flags) const;
386 virtual void PropertyName(std::ostream &out) const;
387 };
388
389 struct CYNull :
390 CYWord,
391 CYLiteral
392 {
393 CYNull() :
394 CYWord("null")
395 {
396 }
397
398 virtual void Output(std::ostream &out, CYFlags flags) const;
399 };
400
401 struct CYThis :
402 CYWord,
403 CYMagic
404 {
405 CYThis() :
406 CYWord("this")
407 {
408 }
409
410 virtual void Output(std::ostream &out, CYFlags flags) const;
411 };
412
413 struct CYBoolean :
414 CYLiteral
415 {
416 virtual bool Value() const = 0;
417 virtual void Output(std::ostream &out, CYFlags flags) const;
418 };
419
420 struct CYFalse :
421 CYWord,
422 CYBoolean
423 {
424 CYFalse() :
425 CYWord("false")
426 {
427 }
428
429 virtual bool Value() const;
430 };
431
432 struct CYTrue :
433 CYWord,
434 CYBoolean
435 {
436 CYTrue() :
437 CYWord("true")
438 {
439 }
440
441 virtual bool Value() const;
442 };
443
444 struct CYVariable :
445 CYExpression
446 {
447 CYIdentifier *name_;
448
449 CYVariable(CYIdentifier *name) :
450 name_(name)
451 {
452 }
453
454 CYPrecedence(0)
455
456 virtual void Output(std::ostream &out, CYFlags flags) const;
457 };
458
459 struct CYPrefix :
460 CYExpression
461 {
462 CYExpression *rhs_;
463
464 CYPrefix(CYExpression *rhs) :
465 rhs_(rhs)
466 {
467 }
468
469 virtual bool Alphabetic() const = 0;
470 virtual const char *Operator() const = 0;
471
472 virtual void Output(std::ostream &out, CYFlags flags) const;
473 };
474
475 struct CYInfix :
476 CYExpression
477 {
478 CYExpression *lhs_;
479 CYExpression *rhs_;
480
481 CYInfix(CYExpression *lhs, CYExpression *rhs) :
482 lhs_(lhs),
483 rhs_(rhs)
484 {
485 }
486
487 void SetLeft(CYExpression *lhs) {
488 lhs_ = lhs;
489 }
490
491 virtual bool Alphabetic() const = 0;
492 virtual const char *Operator() const = 0;
493
494 virtual void Output(std::ostream &out, CYFlags flags) const;
495 };
496
497 struct CYPostfix :
498 CYExpression
499 {
500 CYExpression *lhs_;
501
502 CYPostfix(CYExpression *lhs) :
503 lhs_(lhs)
504 {
505 }
506
507 virtual const char *Operator() const = 0;
508
509 virtual void Output(std::ostream &out, CYFlags flags) const;
510 };
511
512 struct CYAssignment :
513 CYExpression
514 {
515 CYExpression *lhs_;
516 CYExpression *rhs_;
517
518 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
519 lhs_(lhs),
520 rhs_(rhs)
521 {
522 }
523
524 void SetLeft(CYExpression *lhs) {
525 lhs_ = lhs;
526 }
527
528 virtual const char *Operator() const = 0;
529
530 virtual void Output(std::ostream &out, CYFlags flags) const;
531 };
532
533 struct CYArgument :
534 CYNext<CYArgument>
535 {
536 CYWord *name_;
537 CYExpression *value_;
538
539 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
540 CYNext<CYArgument>(next),
541 name_(name),
542 value_(value)
543 {
544 }
545
546 void Output(std::ostream &out) const;
547 };
548
549 struct CYBlank :
550 public CYWord
551 {
552 CYBlank() :
553 CYWord("")
554 {
555 }
556 };
557
558 struct CYClause :
559 CYThing,
560 CYNext<CYClause>
561 {
562 CYExpression *case_;
563 CYStatement *code_;
564
565 CYClause(CYExpression *_case, CYStatement *code) :
566 case_(_case),
567 code_(code)
568 {
569 }
570
571 virtual void Output(std::ostream &out) const;
572 };
573
574 struct CYElement :
575 CYNext<CYElement>
576 {
577 CYExpression *value_;
578
579 CYElement(CYExpression *value, CYElement *next) :
580 CYNext<CYElement>(next),
581 value_(value)
582 {
583 }
584
585 void Output(std::ostream &out) const;
586 };
587
588 struct CYArray :
589 CYLiteral
590 {
591 CYElement *elements_;
592
593 CYArray(CYElement *elements) :
594 elements_(elements)
595 {
596 }
597
598 virtual void Output(std::ostream &out, CYFlags flags) const;
599 };
600
601 struct CYDeclaration :
602 CYThing,
603 CYForInInitialiser
604 {
605 CYIdentifier *identifier_;
606 CYExpression *initialiser_;
607
608 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
609 identifier_(identifier),
610 initialiser_(initialiser)
611 {
612 }
613
614 virtual void Part(std::ostream &out) const;
615 virtual void Output(std::ostream &out) const;
616 };
617
618 struct CYDeclarations :
619 CYStatement,
620 CYForInitialiser
621 {
622 CYDeclaration *declaration_;
623 CYDeclarations *next_;
624
625 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
626 declaration_(declaration),
627 next_(next)
628 {
629 }
630
631 virtual void Part(std::ostream &out) const;
632 virtual void Output(std::ostream &out) const;
633 };
634
635 struct CYField :
636 CYNext<CYField>
637 {
638 virtual void Output(std::ostream &out) const;
639 };
640
641 struct CYMessageParameter :
642 CYNext<CYMessageParameter>
643 {
644 CYWord *tag_;
645 CYExpression *type_;
646 CYIdentifier *name_;
647
648 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
649 tag_(tag),
650 type_(type),
651 name_(name)
652 {
653 }
654 };
655
656 struct CYMessage :
657 CYNext<CYMessage>
658 {
659 bool instance_;
660 CYExpression *type_;
661 CYMessageParameter *parameter_;
662 CYSource *body_;
663
664 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
665 instance_(instance),
666 type_(type),
667 parameter_(parameter),
668 body_(body)
669 {
670 }
671
672 virtual void Output(std::ostream &out, bool replace) const;
673 };
674
675 struct CYClass :
676 CYSource
677 {
678 CYIdentifier *name_;
679 CYExpression *super_;
680 CYField *fields_;
681 CYMessage *messages_;
682
683 CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) :
684 name_(name),
685 super_(super),
686 fields_(fields),
687 messages_(messages)
688 {
689 }
690
691 virtual void Output(std::ostream &out) const;
692 };
693
694 struct CYCategory :
695 CYSource
696 {
697 CYClassName *name_;
698 CYMessage *messages_;
699
700 CYCategory(CYClassName *name, CYMessage *messages) :
701 name_(name),
702 messages_(messages)
703 {
704 }
705
706 virtual void Output(std::ostream &out) const;
707 };
708
709 struct CYFunctionParameter :
710 CYNext<CYFunctionParameter>,
711 CYThing
712 {
713 CYIdentifier *name_;
714
715 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
716 CYNext<CYFunctionParameter>(next),
717 name_(name)
718 {
719 }
720
721 virtual void Output(std::ostream &out) const;
722 };
723
724 struct CYFor :
725 CYStatement
726 {
727 CYForInitialiser *initialiser_;
728 CYExpression *test_;
729 CYExpression *increment_;
730 CYStatement *code_;
731
732 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
733 initialiser_(initialiser),
734 test_(test),
735 increment_(increment),
736 code_(code)
737 {
738 }
739
740 virtual void Output(std::ostream &out) const;
741 };
742
743 struct CYForIn :
744 CYStatement
745 {
746 CYForInInitialiser *initialiser_;
747 CYExpression *set_;
748 CYStatement *code_;
749
750 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
751 initialiser_(initialiser),
752 set_(set),
753 code_(code)
754 {
755 }
756
757 virtual void Output(std::ostream &out) const;
758 };
759
760 struct CYProperty :
761 CYNext<CYProperty>
762 {
763 CYPropertyName *name_;
764 CYExpression *value_;
765
766 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
767 CYNext<CYProperty>(next),
768 name_(name),
769 value_(value)
770 {
771 }
772
773 virtual void Output(std::ostream &out) const;
774 };
775
776 struct CYObject :
777 CYLiteral
778 {
779 CYProperty *property_;
780
781 CYObject(CYProperty *property) :
782 property_(property)
783 {
784 }
785
786 void Output(std::ostream &out, CYFlags flags) const;
787 };
788
789 struct CYCatch :
790 CYThing
791 {
792 CYIdentifier *name_;
793 CYStatement *code_;
794
795 CYCatch(CYIdentifier *name, CYStatement *code) :
796 name_(name),
797 code_(code)
798 {
799 }
800
801 virtual void Output(std::ostream &out) const;
802 };
803
804 struct CYSend :
805 CYExpression
806 {
807 CYExpression *self_;
808 CYArgument *arguments_;
809
810 CYSend(CYExpression *self, CYArgument *arguments) :
811 self_(self),
812 arguments_(arguments)
813 {
814 }
815
816 CYPrecedence(0)
817
818 virtual void Output(std::ostream &out, CYFlags flags) const;
819 };
820
821 struct CYMember :
822 CYExpression
823 {
824 CYExpression *object_;
825 CYExpression *property_;
826
827 CYMember(CYExpression *object, CYExpression *property) :
828 object_(object),
829 property_(property)
830 {
831 }
832
833 void SetLeft(CYExpression *object) {
834 object_ = object;
835 }
836 };
837
838 struct CYDirectMember :
839 CYMember
840 {
841 CYDirectMember(CYExpression *object, CYExpression *property) :
842 CYMember(object, property)
843 {
844 }
845
846 CYPrecedence(1)
847
848 virtual void Output(std::ostream &out, CYFlags flags) const;
849 };
850
851 struct CYIndirectMember :
852 CYMember
853 {
854 CYIndirectMember(CYExpression *object, CYExpression *property) :
855 CYMember(object, property)
856 {
857 }
858
859 CYPrecedence(1)
860
861 virtual void Output(std::ostream &out, CYFlags flags) const;
862 };
863
864 struct CYNew :
865 CYExpression
866 {
867 CYExpression *constructor_;
868 CYArgument *arguments_;
869
870 CYNew(CYExpression *constructor, CYArgument *arguments) :
871 constructor_(constructor),
872 arguments_(arguments)
873 {
874 }
875
876 CYPrecedence(1)
877
878 virtual void Output(std::ostream &out, CYFlags flags) const;
879 };
880
881 struct CYCall :
882 CYExpression
883 {
884 CYExpression *function_;
885 CYArgument *arguments_;
886
887 CYCall(CYExpression *function, CYArgument *arguments) :
888 function_(function),
889 arguments_(arguments)
890 {
891 }
892
893 CYPrecedence(2)
894
895 virtual void Output(std::ostream &out, CYFlags flags) const;
896 };
897
898 struct CYIf :
899 CYStatement
900 {
901 CYExpression *test_;
902 CYStatement *true_;
903 CYStatement *false_;
904
905 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
906 test_(test),
907 true_(_true),
908 false_(_false)
909 {
910 }
911
912 virtual void Output(std::ostream &out) const;
913 };
914
915 struct CYDoWhile :
916 CYStatement
917 {
918 CYExpression *test_;
919 CYStatement *code_;
920
921 CYDoWhile(CYExpression *test, CYStatement *code) :
922 test_(test),
923 code_(code)
924 {
925 }
926
927 virtual void Output(std::ostream &out) const;
928 };
929
930 struct CYWhile :
931 CYStatement
932 {
933 CYExpression *test_;
934 CYStatement *code_;
935
936 CYWhile(CYExpression *test, CYStatement *code) :
937 test_(test),
938 code_(code)
939 {
940 }
941
942 virtual void Output(std::ostream &out) const;
943 };
944
945 struct CYLambda :
946 CYExpression
947 {
948 CYIdentifier *name_;
949 CYFunctionParameter *parameters_;
950 CYSource *body_;
951
952 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
953 name_(name),
954 parameters_(parameters),
955 body_(body)
956 {
957 }
958
959 CYPrecedence(0)
960
961 virtual void Output(std::ostream &out, CYFlags flags) const;
962 };
963
964 struct CYFunction :
965 CYLambda,
966 CYSource
967 {
968 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
969 CYLambda(name, parameters, body)
970 {
971 }
972
973 virtual void Output(std::ostream &out) const;
974 };
975
976 struct CYExpress :
977 CYStatement
978 {
979 CYExpression *expression_;
980
981 CYExpress(CYExpression *expression) :
982 expression_(expression)
983 {
984 }
985
986 virtual void Output(std::ostream &out) const;
987 };
988
989 struct CYContinue :
990 CYStatement
991 {
992 CYIdentifier *label_;
993
994 CYContinue(CYIdentifier *label) :
995 label_(label)
996 {
997 }
998
999 virtual void Output(std::ostream &out) const;
1000 };
1001
1002 struct CYBreak :
1003 CYStatement
1004 {
1005 CYIdentifier *label_;
1006
1007 CYBreak(CYIdentifier *label) :
1008 label_(label)
1009 {
1010 }
1011
1012 virtual void Output(std::ostream &out) const;
1013 };
1014
1015 struct CYReturn :
1016 CYStatement
1017 {
1018 CYExpression *value_;
1019
1020 CYReturn(CYExpression *value) :
1021 value_(value)
1022 {
1023 }
1024
1025 virtual void Output(std::ostream &out) const;
1026 };
1027
1028 struct CYEmpty :
1029 CYStatement
1030 {
1031 virtual void Output(std::ostream &out) const;
1032 virtual void Output(std::ostream &out, bool block) const;
1033 };
1034
1035 struct CYTry :
1036 CYStatement
1037 {
1038 CYStatement *try_;
1039 CYCatch *catch_;
1040 CYStatement *finally_;
1041
1042 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1043 try_(_try),
1044 catch_(_catch),
1045 finally_(finally)
1046 {
1047 }
1048
1049 virtual void Output(std::ostream &out) const;
1050 };
1051
1052 struct CYThrow :
1053 CYStatement
1054 {
1055 CYExpression *value_;
1056
1057 CYThrow(CYExpression *value) :
1058 value_(value)
1059 {
1060 }
1061
1062 virtual void Output(std::ostream &out) const;
1063 };
1064
1065 struct CYWith :
1066 CYStatement
1067 {
1068 CYExpression *scope_;
1069 CYStatement *code_;
1070
1071 CYWith(CYExpression *scope, CYStatement *code) :
1072 scope_(scope),
1073 code_(code)
1074 {
1075 }
1076
1077 virtual void Output(std::ostream &out) const;
1078 };
1079
1080 struct CYSwitch :
1081 CYStatement
1082 {
1083 CYExpression *value_;
1084 CYClause *clauses_;
1085
1086 CYSwitch(CYExpression *value, CYClause *clauses) :
1087 value_(value),
1088 clauses_(clauses)
1089 {
1090 }
1091
1092 virtual void Output(std::ostream &out) const;
1093 };
1094
1095 struct CYCondition :
1096 CYExpression
1097 {
1098 CYExpression *test_;
1099 CYExpression *true_;
1100 CYExpression *false_;
1101
1102 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1103 test_(test),
1104 true_(_true),
1105 false_(_false)
1106 {
1107 }
1108
1109 CYPrecedence(15)
1110
1111 virtual void Output(std::ostream &out, CYFlags flags) const;
1112 };
1113
1114 struct CYAddressOf :
1115 CYPrefix
1116 {
1117 CYAddressOf(CYExpression *rhs) :
1118 CYPrefix(rhs)
1119 {
1120 }
1121
1122 virtual const char *Operator() const {
1123 return "&";
1124 }
1125
1126 CYAlphabetic(false)
1127 CYPrecedence(2)
1128
1129 virtual void Output(std::ostream &out, CYFlags flags) const;
1130 };
1131
1132 struct CYIndirect :
1133 CYPrefix
1134 {
1135 CYIndirect(CYExpression *rhs) :
1136 CYPrefix(rhs)
1137 {
1138 }
1139
1140 virtual const char *Operator() const {
1141 return "*";
1142 }
1143
1144 CYAlphabetic(false)
1145 CYPrecedence(1)
1146
1147 virtual void Output(std::ostream &out, CYFlags flags) const;
1148 };
1149
1150 #define CYPostfix_(op, name) \
1151 struct CY ## name : \
1152 CYPostfix \
1153 { \
1154 CY ## name(CYExpression *lhs) : \
1155 CYPostfix(lhs) \
1156 { \
1157 } \
1158 \
1159 CYPrecedence(3) \
1160 \
1161 virtual const char *Operator() const { \
1162 return op; \
1163 } \
1164 };
1165
1166 #define CYPrefix_(alphabetic, op, name) \
1167 struct CY ## name : \
1168 CYPrefix \
1169 { \
1170 CY ## name(CYExpression *rhs) : \
1171 CYPrefix(rhs) \
1172 { \
1173 } \
1174 \
1175 CYAlphabetic(alphabetic) \
1176 CYPrecedence(4) \
1177 \
1178 virtual const char *Operator() const { \
1179 return op; \
1180 } \
1181 };
1182
1183 #define CYInfix_(alphabetic, precedence, op, name) \
1184 struct CY ## name : \
1185 CYInfix \
1186 { \
1187 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1188 CYInfix(lhs, rhs) \
1189 { \
1190 } \
1191 \
1192 CYAlphabetic(alphabetic) \
1193 CYPrecedence(precedence) \
1194 \
1195 virtual const char *Operator() const { \
1196 return op; \
1197 } \
1198 };
1199
1200 #define CYAssignment_(op, name) \
1201 struct CY ## name ## Assign : \
1202 CYAssignment \
1203 { \
1204 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1205 CYAssignment(lhs, rhs) \
1206 { \
1207 } \
1208 \
1209 CYPrecedence(16) \
1210 \
1211 virtual const char *Operator() const { \
1212 return op; \
1213 } \
1214 };
1215
1216 CYPostfix_("++", PostIncrement)
1217 CYPostfix_("--", PostDecrement)
1218
1219 CYPrefix_(true, "delete", Delete)
1220 CYPrefix_(true, "void", Void)
1221 CYPrefix_(true, "typeof", TypeOf)
1222 CYPrefix_(false, "++", PreIncrement)
1223 CYPrefix_(false, "--", PreDecrement)
1224 CYPrefix_(false, "-", Negate)
1225 CYPrefix_(false, "~", BitwiseNot)
1226 CYPrefix_(false, "!", LogicalNot)
1227
1228 CYInfix_(false, 5, "*", Multiply)
1229 CYInfix_(false, 5, "/", Divide)
1230 CYInfix_(false, 5, "%", Modulus)
1231 CYInfix_(false, 6, "+", Add)
1232 CYInfix_(false, 6, "-", Subtract)
1233 CYInfix_(false, 7, "<<", ShiftLeft)
1234 CYInfix_(false, 7, ">>", ShiftRightSigned)
1235 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1236 CYInfix_(false, 8, "<", Less)
1237 CYInfix_(false, 8, ">", Greater)
1238 CYInfix_(false, 8, "<=", LessOrEqual)
1239 CYInfix_(false, 8, ">=", GreaterOrEqual)
1240 CYInfix_(true, 8, "instanceof", InstanceOf)
1241 CYInfix_(true, 8, "in", In)
1242 CYInfix_(false, 9, "==", Equal)
1243 CYInfix_(false, 9, "!=", NotEqual)
1244 CYInfix_(false, 9, "===", Identical)
1245 CYInfix_(false, 9, "!==", NotIdentical)
1246 CYInfix_(false, 10, "&", BitwiseAnd)
1247 CYInfix_(false, 11, "^", BitwiseXOr)
1248 CYInfix_(false, 12, "|", BitwiseOr)
1249 CYInfix_(false, 13, "&&", LogicalAnd)
1250 CYInfix_(false, 14, "||", LogicalOr)
1251
1252 CYAssignment_("=", )
1253 CYAssignment_("*=", Multiply)
1254 CYAssignment_("/=", Divide)
1255 CYAssignment_("%=", Modulus)
1256 CYAssignment_("+=", Add)
1257 CYAssignment_("-=", Subtract)
1258 CYAssignment_("<<=", ShiftLeft)
1259 CYAssignment_(">>=", ShiftRightSigned)
1260 CYAssignment_(">>>=", ShiftRightUnsigned)
1261 CYAssignment_("&=", BitwiseAnd)
1262 CYAssignment_("^=", BitwiseXOr)
1263 CYAssignment_("|=", BitwiseOr)
1264
1265 #endif/*CYPARSER_HPP*/