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