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