]> git.saurik.com Git - cycript.git/blob - Parser.hpp
79658c29aa1c655ba4cf800621aa0393bff76121
[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 virtual bool Alphabetic() const = 0;
487 virtual const char *Operator() const = 0;
488
489 virtual void Output(std::ostream &out, CYFlags flags) const;
490 };
491
492 struct CYPostfix :
493 CYExpression
494 {
495 CYExpression *lhs_;
496
497 CYPostfix(CYExpression *lhs) :
498 lhs_(lhs)
499 {
500 }
501
502 virtual const char *Operator() const = 0;
503
504 virtual void Output(std::ostream &out, CYFlags flags) const;
505 };
506
507 struct CYAssignment :
508 CYExpression
509 {
510 CYExpression *lhs_;
511 CYExpression *rhs_;
512
513 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
514 lhs_(lhs),
515 rhs_(rhs)
516 {
517 }
518
519 virtual const char *Operator() const = 0;
520
521 virtual void Output(std::ostream &out, CYFlags flags) const;
522 };
523
524 struct CYArgument :
525 CYNext<CYArgument>
526 {
527 CYWord *name_;
528 CYExpression *value_;
529
530 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
531 CYNext<CYArgument>(next),
532 name_(name),
533 value_(value)
534 {
535 }
536
537 void Output(std::ostream &out) const;
538 };
539
540 struct CYBlank :
541 public CYWord
542 {
543 CYBlank() :
544 CYWord("")
545 {
546 }
547 };
548
549 struct CYClause :
550 CYThing,
551 CYNext<CYClause>
552 {
553 CYExpression *case_;
554 CYStatement *code_;
555
556 CYClause(CYExpression *_case, CYStatement *code) :
557 case_(_case),
558 code_(code)
559 {
560 }
561
562 virtual void Output(std::ostream &out) const;
563 };
564
565 struct CYElement :
566 CYNext<CYElement>
567 {
568 CYExpression *value_;
569
570 CYElement(CYExpression *value, CYElement *next) :
571 CYNext<CYElement>(next),
572 value_(value)
573 {
574 }
575
576 void Output(std::ostream &out) const;
577 };
578
579 struct CYArray :
580 CYLiteral
581 {
582 CYElement *elements_;
583
584 CYArray(CYElement *elements) :
585 elements_(elements)
586 {
587 }
588
589 virtual void Output(std::ostream &out, CYFlags flags) const;
590 };
591
592 struct CYDeclaration :
593 CYThing,
594 CYForInInitialiser
595 {
596 CYIdentifier *identifier_;
597 CYExpression *initialiser_;
598
599 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
600 identifier_(identifier),
601 initialiser_(initialiser)
602 {
603 }
604
605 virtual void Part(std::ostream &out) const;
606 virtual void Output(std::ostream &out) const;
607 };
608
609 struct CYDeclarations :
610 CYStatement,
611 CYForInitialiser
612 {
613 CYDeclaration *declaration_;
614 CYDeclarations *next_;
615
616 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
617 declaration_(declaration),
618 next_(next)
619 {
620 }
621
622 virtual void Part(std::ostream &out) const;
623 virtual void Output(std::ostream &out) const;
624 };
625
626 struct CYField :
627 CYNext<CYField>
628 {
629 virtual void Output(std::ostream &out) const;
630 };
631
632 struct CYMessageParameter :
633 CYNext<CYMessageParameter>
634 {
635 CYWord *tag_;
636 CYExpression *type_;
637 CYIdentifier *name_;
638
639 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
640 tag_(tag),
641 type_(type),
642 name_(name)
643 {
644 }
645 };
646
647 struct CYMessage :
648 CYNext<CYMessage>
649 {
650 bool instance_;
651 CYExpression *type_;
652 CYMessageParameter *parameter_;
653 CYSource *body_;
654
655 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
656 instance_(instance),
657 type_(type),
658 parameter_(parameter),
659 body_(body)
660 {
661 }
662
663 virtual void Output(std::ostream &out, bool replace) const;
664 };
665
666 struct CYClass :
667 CYSource
668 {
669 CYIdentifier *name_;
670 CYExpression *super_;
671 CYField *fields_;
672 CYMessage *messages_;
673
674 CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) :
675 name_(name),
676 super_(super),
677 fields_(fields),
678 messages_(messages)
679 {
680 }
681
682 virtual void Output(std::ostream &out) const;
683 };
684
685 struct CYCategory :
686 CYSource
687 {
688 CYClassName *name_;
689 CYMessage *messages_;
690
691 CYCategory(CYClassName *name, CYMessage *messages) :
692 name_(name),
693 messages_(messages)
694 {
695 }
696
697 virtual void Output(std::ostream &out) const;
698 };
699
700 struct CYFunctionParameter :
701 CYNext<CYFunctionParameter>,
702 CYThing
703 {
704 CYIdentifier *name_;
705
706 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
707 CYNext<CYFunctionParameter>(next),
708 name_(name)
709 {
710 }
711
712 virtual void Output(std::ostream &out) const;
713 };
714
715 struct CYFor :
716 CYStatement
717 {
718 CYForInitialiser *initialiser_;
719 CYExpression *test_;
720 CYExpression *increment_;
721 CYStatement *code_;
722
723 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
724 initialiser_(initialiser),
725 test_(test),
726 increment_(increment),
727 code_(code)
728 {
729 }
730
731 virtual void Output(std::ostream &out) const;
732 };
733
734 struct CYForIn :
735 CYStatement
736 {
737 CYForInInitialiser *initialiser_;
738 CYExpression *set_;
739 CYStatement *code_;
740
741 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
742 initialiser_(initialiser),
743 set_(set),
744 code_(code)
745 {
746 }
747
748 virtual void Output(std::ostream &out) const;
749 };
750
751 struct CYProperty :
752 CYNext<CYProperty>
753 {
754 CYPropertyName *name_;
755 CYExpression *value_;
756
757 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
758 CYNext<CYProperty>(next),
759 name_(name),
760 value_(value)
761 {
762 }
763
764 virtual void Output(std::ostream &out) const;
765 };
766
767 struct CYObject :
768 CYLiteral
769 {
770 CYProperty *property_;
771
772 CYObject(CYProperty *property) :
773 property_(property)
774 {
775 }
776
777 void Output(std::ostream &out, CYFlags flags) const;
778 };
779
780 struct CYCatch :
781 CYThing
782 {
783 CYIdentifier *name_;
784 CYStatement *code_;
785
786 CYCatch(CYIdentifier *name, CYStatement *code) :
787 name_(name),
788 code_(code)
789 {
790 }
791
792 virtual void Output(std::ostream &out) const;
793 };
794
795 struct CYSend :
796 CYExpression
797 {
798 CYExpression *self_;
799 CYArgument *arguments_;
800
801 CYSend(CYExpression *self, CYArgument *arguments) :
802 self_(self),
803 arguments_(arguments)
804 {
805 }
806
807 CYPrecedence(0)
808
809 virtual void Output(std::ostream &out, CYFlags flags) const;
810 };
811
812 struct CYMember :
813 CYExpression
814 {
815 CYExpression *object_;
816 CYExpression *property_;
817
818 CYMember(CYExpression *object, CYExpression *property) :
819 object_(object),
820 property_(property)
821 {
822 }
823
824 CYPrecedence(1)
825
826 virtual void Output(std::ostream &out, CYFlags flags) const;
827 };
828
829 struct CYNew :
830 CYExpression
831 {
832 CYExpression *constructor_;
833 CYArgument *arguments_;
834
835 CYNew(CYExpression *constructor, CYArgument *arguments) :
836 constructor_(constructor),
837 arguments_(arguments)
838 {
839 }
840
841 CYPrecedence(1)
842
843 virtual void Output(std::ostream &out, CYFlags flags) const;
844 };
845
846 struct CYCall :
847 CYExpression
848 {
849 CYExpression *function_;
850 CYArgument *arguments_;
851
852 CYCall(CYExpression *function, CYArgument *arguments) :
853 function_(function),
854 arguments_(arguments)
855 {
856 }
857
858 CYPrecedence(2)
859
860 virtual void Output(std::ostream &out, CYFlags flags) const;
861 };
862
863 struct CYIf :
864 CYStatement
865 {
866 CYExpression *test_;
867 CYStatement *true_;
868 CYStatement *false_;
869
870 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
871 test_(test),
872 true_(_true),
873 false_(_false)
874 {
875 }
876
877 virtual void Output(std::ostream &out) const;
878 };
879
880 struct CYDoWhile :
881 CYStatement
882 {
883 CYExpression *test_;
884 CYStatement *code_;
885
886 CYDoWhile(CYExpression *test, CYStatement *code) :
887 test_(test),
888 code_(code)
889 {
890 }
891
892 virtual void Output(std::ostream &out) const;
893 };
894
895 struct CYWhile :
896 CYStatement
897 {
898 CYExpression *test_;
899 CYStatement *code_;
900
901 CYWhile(CYExpression *test, CYStatement *code) :
902 test_(test),
903 code_(code)
904 {
905 }
906
907 virtual void Output(std::ostream &out) const;
908 };
909
910 struct CYLambda :
911 CYExpression
912 {
913 CYIdentifier *name_;
914 CYFunctionParameter *parameters_;
915 CYSource *body_;
916
917 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
918 name_(name),
919 parameters_(parameters),
920 body_(body)
921 {
922 }
923
924 CYPrecedence(0)
925
926 virtual void Output(std::ostream &out, CYFlags flags) const;
927 };
928
929 struct CYFunction :
930 CYLambda,
931 CYSource
932 {
933 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
934 CYLambda(name, parameters, body)
935 {
936 }
937
938 virtual void Output(std::ostream &out) const;
939 };
940
941 struct CYExpress :
942 CYStatement
943 {
944 CYExpression *expression_;
945
946 CYExpress(CYExpression *expression) :
947 expression_(expression)
948 {
949 }
950
951 virtual void Output(std::ostream &out) const;
952 };
953
954 struct CYContinue :
955 CYStatement
956 {
957 CYIdentifier *label_;
958
959 CYContinue(CYIdentifier *label) :
960 label_(label)
961 {
962 }
963
964 virtual void Output(std::ostream &out) const;
965 };
966
967 struct CYBreak :
968 CYStatement
969 {
970 CYIdentifier *label_;
971
972 CYBreak(CYIdentifier *label) :
973 label_(label)
974 {
975 }
976
977 virtual void Output(std::ostream &out) const;
978 };
979
980 struct CYReturn :
981 CYStatement
982 {
983 CYExpression *value_;
984
985 CYReturn(CYExpression *value) :
986 value_(value)
987 {
988 }
989
990 virtual void Output(std::ostream &out) const;
991 };
992
993 struct CYEmpty :
994 CYStatement
995 {
996 virtual void Output(std::ostream &out) const;
997 virtual void Output(std::ostream &out, bool block) const;
998 };
999
1000 struct CYTry :
1001 CYStatement
1002 {
1003 CYStatement *try_;
1004 CYCatch *catch_;
1005 CYStatement *finally_;
1006
1007 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1008 try_(_try),
1009 catch_(_catch),
1010 finally_(finally)
1011 {
1012 }
1013
1014 virtual void Output(std::ostream &out) const;
1015 };
1016
1017 struct CYThrow :
1018 CYStatement
1019 {
1020 CYExpression *value_;
1021
1022 CYThrow(CYExpression *value) :
1023 value_(value)
1024 {
1025 }
1026
1027 virtual void Output(std::ostream &out) const;
1028 };
1029
1030 struct CYWith :
1031 CYStatement
1032 {
1033 CYExpression *scope_;
1034 CYStatement *code_;
1035
1036 CYWith(CYExpression *scope, CYStatement *code) :
1037 scope_(scope),
1038 code_(code)
1039 {
1040 }
1041
1042 virtual void Output(std::ostream &out) const;
1043 };
1044
1045 struct CYSwitch :
1046 CYStatement
1047 {
1048 CYExpression *value_;
1049 CYClause *clauses_;
1050
1051 CYSwitch(CYExpression *value, CYClause *clauses) :
1052 value_(value),
1053 clauses_(clauses)
1054 {
1055 }
1056
1057 virtual void Output(std::ostream &out) const;
1058 };
1059
1060 struct CYCondition :
1061 CYExpression
1062 {
1063 CYExpression *test_;
1064 CYExpression *true_;
1065 CYExpression *false_;
1066
1067 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1068 test_(test),
1069 true_(_true),
1070 false_(_false)
1071 {
1072 }
1073
1074 CYPrecedence(15)
1075
1076 virtual void Output(std::ostream &out, CYFlags flags) const;
1077 };
1078
1079 struct CYAddressOf :
1080 CYPrefix
1081 {
1082 CYAddressOf(CYExpression *rhs) :
1083 CYPrefix(rhs)
1084 {
1085 }
1086
1087 virtual const char *Operator() const {
1088 return "&";
1089 }
1090
1091 CYAlphabetic(false)
1092 CYPrecedence(2)
1093
1094 virtual void Output(std::ostream &out, CYFlags flags) const;
1095 };
1096
1097 struct CYIndirect :
1098 CYPrefix
1099 {
1100 CYIndirect(CYExpression *rhs) :
1101 CYPrefix(rhs)
1102 {
1103 }
1104
1105 virtual const char *Operator() const {
1106 return "*";
1107 }
1108
1109 CYAlphabetic(false)
1110 CYPrecedence(1)
1111
1112 virtual void Output(std::ostream &out, CYFlags flags) const;
1113 };
1114
1115 #define CYPostfix_(op, name) \
1116 struct CY ## name : \
1117 CYPostfix \
1118 { \
1119 CY ## name(CYExpression *lhs) : \
1120 CYPostfix(lhs) \
1121 { \
1122 } \
1123 \
1124 CYPrecedence(3) \
1125 \
1126 virtual const char *Operator() const { \
1127 return op; \
1128 } \
1129 };
1130
1131 #define CYPrefix_(alphabetic, op, name) \
1132 struct CY ## name : \
1133 CYPrefix \
1134 { \
1135 CY ## name(CYExpression *rhs) : \
1136 CYPrefix(rhs) \
1137 { \
1138 } \
1139 \
1140 CYAlphabetic(alphabetic) \
1141 CYPrecedence(4) \
1142 \
1143 virtual const char *Operator() const { \
1144 return op; \
1145 } \
1146 };
1147
1148 #define CYInfix_(alphabetic, precedence, op, name) \
1149 struct CY ## name : \
1150 CYInfix \
1151 { \
1152 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1153 CYInfix(lhs, rhs) \
1154 { \
1155 } \
1156 \
1157 CYAlphabetic(alphabetic) \
1158 CYPrecedence(precedence) \
1159 \
1160 virtual const char *Operator() const { \
1161 return op; \
1162 } \
1163 };
1164
1165 #define CYAssignment_(op, name) \
1166 struct CY ## name ## Assign : \
1167 CYAssignment \
1168 { \
1169 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1170 CYAssignment(lhs, rhs) \
1171 { \
1172 } \
1173 \
1174 CYPrecedence(16) \
1175 \
1176 virtual const char *Operator() const { \
1177 return op; \
1178 } \
1179 };
1180
1181 CYPostfix_("++", PostIncrement)
1182 CYPostfix_("--", PostDecrement)
1183
1184 CYPrefix_(true, "delete", Delete)
1185 CYPrefix_(true, "void", Void)
1186 CYPrefix_(true, "typeof", TypeOf)
1187 CYPrefix_(false, "++", PreIncrement)
1188 CYPrefix_(false, "--", PreDecrement)
1189 CYPrefix_(false, "-", Negate)
1190 CYPrefix_(false, "~", BitwiseNot)
1191 CYPrefix_(false, "!", LogicalNot)
1192
1193 CYInfix_(false, 5, "*", Multiply)
1194 CYInfix_(false, 5, "/", Divide)
1195 CYInfix_(false, 5, "%", Modulus)
1196 CYInfix_(false, 6, "+", Add)
1197 CYInfix_(false, 6, "-", Subtract)
1198 CYInfix_(false, 7, "<<", ShiftLeft)
1199 CYInfix_(false, 7, ">>", ShiftRightSigned)
1200 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1201 CYInfix_(false, 8, "<", Less)
1202 CYInfix_(false, 8, ">", Greater)
1203 CYInfix_(false, 8, "<=", LessOrEqual)
1204 CYInfix_(false, 8, ">=", GreaterOrEqual)
1205 CYInfix_(true, 8, "instanceof", InstanceOf)
1206 CYInfix_(true, 8, "in", In)
1207 CYInfix_(false, 9, "==", Equal)
1208 CYInfix_(false, 9, "!=", NotEqual)
1209 CYInfix_(false, 9, "===", Identical)
1210 CYInfix_(false, 9, "!==", NotIdentical)
1211 CYInfix_(false, 10, "&", BitwiseAnd)
1212 CYInfix_(false, 11, "^", BitwiseXOr)
1213 CYInfix_(false, 12, "|", BitwiseOr)
1214 CYInfix_(false, 13, "&&", LogicalAnd)
1215 CYInfix_(false, 14, "||", LogicalOr)
1216
1217 CYAssignment_("=", )
1218 CYAssignment_("*=", Multiply)
1219 CYAssignment_("/=", Divide)
1220 CYAssignment_("%=", Modulus)
1221 CYAssignment_("+=", Add)
1222 CYAssignment_("-=", Subtract)
1223 CYAssignment_("<<=", ShiftLeft)
1224 CYAssignment_(">>=", ShiftRightSigned)
1225 CYAssignment_(">>>=", ShiftRightUnsigned)
1226 CYAssignment_("&=", BitwiseAnd)
1227 CYAssignment_("^=", BitwiseXOr)
1228 CYAssignment_("|=", BitwiseOr)
1229
1230 #endif/*CYPARSER_HPP*/