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