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