]> git.saurik.com Git - cycript.git/blob - Parser.hpp
Added Instance constructor instance (for Instance.prototype), implemented Type class...
[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 CYName :
87 CYThing
88 {
89 virtual const char *Name() const = 0;
90 };
91
92 struct CYWord :
93 CYName
94 {
95 const char *word_;
96
97 CYWord(const char *word) :
98 word_(word)
99 {
100 }
101
102 const char *Value() const {
103 return word_;
104 }
105
106 virtual const char *Name() const {
107 return Value();
108 }
109
110 virtual void Output(std::ostream &out) const;
111 };
112
113 struct CYIdentifier :
114 CYWord
115 {
116 CYIdentifier(const char *word) :
117 CYWord(word)
118 {
119 }
120 };
121
122 struct CYLabel :
123 CYNext<CYLabel>
124 {
125 CYIdentifier *identifier_;
126
127 CYLabel(CYIdentifier *identifier, CYLabel *next) :
128 CYNext<CYLabel>(next),
129 identifier_(identifier)
130 {
131 }
132 };
133
134 struct CYStatement :
135 CYSource
136 {
137 CYLabel *label_;
138
139 void AddLabel(CYIdentifier *identifier) {
140 label_ = new CYLabel(identifier, label_);
141 }
142 };
143
144 enum CYState {
145 CYClear,
146 CYRestricted,
147 CYNewLine
148 };
149
150 class CYDriver {
151 public:
152 CYPool pool_;
153
154 CYState state_;
155 void *scanner_;
156
157 const char *data_;
158 size_t size_;
159
160 std::string filename_;
161
162 struct Error {
163 cy::location location_;
164 std::string message_;
165 };
166
167 typedef std::vector<Error> Errors;
168
169 CYSource *source_;
170 Errors errors_;
171
172 private:
173 void ScannerInit();
174 void ScannerDestroy();
175
176 public:
177 CYDriver(const std::string &filename);
178 ~CYDriver();
179 };
180
181 struct CYPart {
182 virtual void Part(std::ostream &out) const = 0;
183 };
184
185 struct CYForInitialiser :
186 CYPart
187 {
188 };
189
190 struct CYForInInitialiser :
191 CYPart
192 {
193 };
194
195 enum CYFlags {
196 CYNoFlags = 0,
197 CYNoBrace = (1 << 0),
198 CYNoFunction = (1 << 1),
199 CYNoLeader = (1 << 2),
200 CYNoTrailer = (1 << 3),
201 CYNoIn = (1 << 4),
202 };
203
204 struct CYExpression :
205 CYNext<CYExpression>,
206 CYForInitialiser,
207 CYForInInitialiser
208 {
209 virtual unsigned Precedence() const = 0;
210 virtual void Part(std::ostream &out) const;
211 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
212 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
213
214 virtual const char *Word() const {
215 return NULL;
216 }
217 };
218
219 #define CYAlphabetic(value) \
220 virtual bool Alphabetic() const { \
221 return value; \
222 }
223
224 #define CYPrecedence(value) \
225 virtual unsigned Precedence() const { \
226 return value; \
227 }
228
229 struct CYCompound :
230 CYExpression
231 {
232 CYExpression *expressions_;
233
234 CYCompound(CYExpression *expressions) :
235 expressions_(expressions)
236 {
237 }
238
239 void AddPrev(CYExpression *expression) {
240 CYExpression *last(expression);
241 while (last->next_ != NULL)
242 last = last->next_;
243 last->SetNext(expressions_);
244 expressions_ = expression;
245 }
246
247 CYPrecedence(17)
248
249 void Output(std::ostream &out, CYFlags flags) const;
250 };
251
252 struct CYLiteral :
253 CYExpression
254 {
255 CYPrecedence(0)
256 };
257
258 struct CYMagic :
259 CYExpression
260 {
261 CYPrecedence(0)
262 };
263
264 struct CYSelectorPart :
265 CYNext<CYSelectorPart>
266 {
267 CYWord *name_;
268 bool value_;
269
270 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
271 CYNext<CYSelectorPart>(next),
272 name_(name),
273 value_(value)
274 {
275 }
276
277 virtual void Output(std::ostream &out) const;
278 };
279
280 struct CYSelector :
281 CYLiteral
282 {
283 CYSelectorPart *name_;
284
285 CYSelector(CYSelectorPart *name) :
286 name_(name)
287 {
288 }
289
290 CYPrecedence(1)
291
292 virtual void Output(std::ostream &out, CYFlags flags) const;
293 };
294
295 struct CYRange {
296 uint64_t lo_;
297 uint64_t hi_;
298
299 CYRange(uint64_t lo, uint64_t hi) :
300 lo_(lo), hi_(hi)
301 {
302 }
303
304 bool operator [](uint8_t value) const {
305 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
306 }
307
308 void operator()(uint8_t value) {
309 if (value >> 7)
310 return;
311 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
312 }
313 };
314
315 extern CYRange DigitRange_;
316 extern CYRange WordStartRange_;
317 extern CYRange WordEndRange_;
318
319 struct CYString :
320 CYLiteral,
321 CYName
322 {
323 const char *value_;
324 size_t size_;
325
326 CYString(const char *value, size_t size) :
327 value_(value),
328 size_(size)
329 {
330 }
331
332 CYString(const CYIdentifier *identifier) :
333 value_(identifier->Value()),
334 size_(strlen(value_))
335 {
336 }
337
338 const char *Value() const {
339 return value_;
340 }
341
342 virtual const char *Name() const {
343 return Value();
344 }
345
346 virtual const char *Word() const {
347 if (size_ == 0 || !WordStartRange_[value_[0]])
348 return NULL;
349 for (size_t i(1); i != size_; ++i)
350 if (!WordEndRange_[value_[i]])
351 return NULL;
352 return Value();
353 }
354
355 virtual void Output(std::ostream &out) const {
356 return Output(out, CYNoFlags);
357 }
358
359 virtual void Output(std::ostream &out, CYFlags flags) const;
360 };
361
362 struct CYNumber :
363 CYLiteral,
364 CYName
365 {
366 double value_;
367
368 CYNumber(double value) :
369 value_(value)
370 {
371 }
372
373 double Value() const {
374 return value_;
375 }
376
377 virtual const char *Name() const {
378 throw;
379 }
380
381 virtual void Output(std::ostream &out) const {
382 return Output(out, CYNoFlags);
383 }
384
385 virtual void Output(std::ostream &out, CYFlags flags) 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 CYSource
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) 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 CYFunctionParameter :
686 CYNext<CYFunctionParameter>,
687 CYThing
688 {
689 CYIdentifier *name_;
690
691 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
692 CYNext<CYFunctionParameter>(next),
693 name_(name)
694 {
695 }
696
697 virtual void Output(std::ostream &out) const;
698 };
699
700 struct CYFor :
701 CYStatement
702 {
703 CYForInitialiser *initialiser_;
704 CYExpression *test_;
705 CYExpression *increment_;
706 CYStatement *code_;
707
708 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
709 initialiser_(initialiser),
710 test_(test),
711 increment_(increment),
712 code_(code)
713 {
714 }
715
716 virtual void Output(std::ostream &out) const;
717 };
718
719 struct CYForIn :
720 CYStatement
721 {
722 CYForInInitialiser *initialiser_;
723 CYExpression *set_;
724 CYStatement *code_;
725
726 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
727 initialiser_(initialiser),
728 set_(set),
729 code_(code)
730 {
731 }
732
733 virtual void Output(std::ostream &out) const;
734 };
735
736 struct CYProperty :
737 CYNext<CYProperty>
738 {
739 CYName *name_;
740 CYExpression *value_;
741
742 CYProperty(CYName *name, CYExpression *value, CYProperty *next) :
743 CYNext<CYProperty>(next),
744 name_(name),
745 value_(value)
746 {
747 }
748
749 virtual void Output(std::ostream &out) const;
750 };
751
752 struct CYObject :
753 CYLiteral
754 {
755 CYProperty *property_;
756
757 CYObject(CYProperty *property) :
758 property_(property)
759 {
760 }
761
762 void Output(std::ostream &out, CYFlags flags) const;
763 };
764
765 struct CYCatch :
766 CYThing
767 {
768 CYIdentifier *name_;
769 CYStatement *code_;
770
771 CYCatch(CYIdentifier *name, CYStatement *code) :
772 name_(name),
773 code_(code)
774 {
775 }
776
777 virtual void Output(std::ostream &out) const;
778 };
779
780 struct CYSend :
781 CYExpression
782 {
783 CYExpression *self_;
784 CYArgument *arguments_;
785
786 CYSend(CYExpression *self, CYArgument *arguments) :
787 self_(self),
788 arguments_(arguments)
789 {
790 }
791
792 CYPrecedence(0)
793
794 virtual void Output(std::ostream &out, CYFlags flags) const;
795 };
796
797 struct CYMember :
798 CYExpression
799 {
800 CYExpression *object_;
801 CYExpression *property_;
802
803 CYMember(CYExpression *object, CYExpression *property) :
804 object_(object),
805 property_(property)
806 {
807 }
808
809 CYPrecedence(1)
810
811 virtual void Output(std::ostream &out, CYFlags flags) const;
812 };
813
814 struct CYNew :
815 CYExpression
816 {
817 CYExpression *constructor_;
818 CYArgument *arguments_;
819
820 CYNew(CYExpression *constructor, CYArgument *arguments) :
821 constructor_(constructor),
822 arguments_(arguments)
823 {
824 }
825
826 CYPrecedence(1)
827
828 virtual void Output(std::ostream &out, CYFlags flags) const;
829 };
830
831 struct CYCall :
832 CYExpression
833 {
834 CYExpression *function_;
835 CYArgument *arguments_;
836
837 CYCall(CYExpression *function, CYArgument *arguments) :
838 function_(function),
839 arguments_(arguments)
840 {
841 }
842
843 CYPrecedence(2)
844
845 virtual void Output(std::ostream &out, CYFlags flags) const;
846 };
847
848 struct CYIf :
849 CYStatement
850 {
851 CYExpression *test_;
852 CYStatement *true_;
853 CYStatement *false_;
854
855 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
856 test_(test),
857 true_(_true),
858 false_(_false)
859 {
860 }
861
862 virtual void Output(std::ostream &out) const;
863 };
864
865 struct CYDoWhile :
866 CYStatement
867 {
868 CYExpression *test_;
869 CYStatement *code_;
870
871 CYDoWhile(CYExpression *test, CYStatement *code) :
872 test_(test),
873 code_(code)
874 {
875 }
876
877 virtual void Output(std::ostream &out) const;
878 };
879
880 struct CYWhile :
881 CYStatement
882 {
883 CYExpression *test_;
884 CYStatement *code_;
885
886 CYWhile(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 CYLambda :
896 CYExpression
897 {
898 CYIdentifier *name_;
899 CYFunctionParameter *parameters_;
900 CYSource *body_;
901
902 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
903 name_(name),
904 parameters_(parameters),
905 body_(body)
906 {
907 }
908
909 CYPrecedence(0)
910
911 virtual void Output(std::ostream &out, CYFlags flags) const;
912 };
913
914 struct CYFunction :
915 CYLambda,
916 CYSource
917 {
918 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
919 CYLambda(name, parameters, body)
920 {
921 }
922
923 virtual void Output(std::ostream &out) const;
924 };
925
926 struct CYExpress :
927 CYStatement
928 {
929 CYExpression *expression_;
930
931 CYExpress(CYExpression *expression) :
932 expression_(expression)
933 {
934 }
935
936 virtual void Output(std::ostream &out) const;
937 };
938
939 struct CYContinue :
940 CYStatement
941 {
942 CYIdentifier *label_;
943
944 CYContinue(CYIdentifier *label) :
945 label_(label)
946 {
947 }
948
949 virtual void Output(std::ostream &out) const;
950 };
951
952 struct CYBreak :
953 CYStatement
954 {
955 CYIdentifier *label_;
956
957 CYBreak(CYIdentifier *label) :
958 label_(label)
959 {
960 }
961
962 virtual void Output(std::ostream &out) const;
963 };
964
965 struct CYReturn :
966 CYStatement
967 {
968 CYExpression *value_;
969
970 CYReturn(CYExpression *value) :
971 value_(value)
972 {
973 }
974
975 virtual void Output(std::ostream &out) const;
976 };
977
978 struct CYEmpty :
979 CYStatement
980 {
981 virtual void Output(std::ostream &out) const;
982 virtual void Output(std::ostream &out, bool block) const;
983 };
984
985 struct CYTry :
986 CYStatement
987 {
988 CYStatement *try_;
989 CYCatch *catch_;
990 CYStatement *finally_;
991
992 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
993 try_(_try),
994 catch_(_catch),
995 finally_(finally)
996 {
997 }
998
999 virtual void Output(std::ostream &out) const;
1000 };
1001
1002 struct CYThrow :
1003 CYStatement
1004 {
1005 CYExpression *value_;
1006
1007 CYThrow(CYExpression *value) :
1008 value_(value)
1009 {
1010 }
1011
1012 virtual void Output(std::ostream &out) const;
1013 };
1014
1015 struct CYWith :
1016 CYStatement
1017 {
1018 CYExpression *scope_;
1019 CYStatement *code_;
1020
1021 CYWith(CYExpression *scope, CYStatement *code) :
1022 scope_(scope),
1023 code_(code)
1024 {
1025 }
1026
1027 virtual void Output(std::ostream &out) const;
1028 };
1029
1030 struct CYSwitch :
1031 CYStatement
1032 {
1033 CYExpression *value_;
1034 CYClause *clauses_;
1035
1036 CYSwitch(CYExpression *value, CYClause *clauses) :
1037 value_(value),
1038 clauses_(clauses)
1039 {
1040 }
1041
1042 virtual void Output(std::ostream &out) const;
1043 };
1044
1045 struct CYCondition :
1046 CYExpression
1047 {
1048 CYExpression *test_;
1049 CYExpression *true_;
1050 CYExpression *false_;
1051
1052 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
1053 test_(test),
1054 true_(_true),
1055 false_(_false)
1056 {
1057 }
1058
1059 CYPrecedence(15)
1060
1061 virtual void Output(std::ostream &out, CYFlags flags) const;
1062 };
1063
1064 struct CYAddressOf :
1065 CYPrefix
1066 {
1067 CYAddressOf(CYExpression *rhs) :
1068 CYPrefix(rhs)
1069 {
1070 }
1071
1072 virtual const char *Operator() const {
1073 return "&";
1074 }
1075
1076 CYAlphabetic(false)
1077 CYPrecedence(2)
1078
1079 virtual void Output(std::ostream &out, CYFlags flags) const;
1080 };
1081
1082 struct CYIndirect :
1083 CYPrefix
1084 {
1085 CYIndirect(CYExpression *rhs) :
1086 CYPrefix(rhs)
1087 {
1088 }
1089
1090 virtual const char *Operator() const {
1091 return "*";
1092 }
1093
1094 CYAlphabetic(false)
1095 CYPrecedence(1)
1096
1097 virtual void Output(std::ostream &out, CYFlags flags) const;
1098 };
1099
1100 #define CYPostfix_(op, name) \
1101 struct CY ## name : \
1102 CYPostfix \
1103 { \
1104 CY ## name(CYExpression *lhs) : \
1105 CYPostfix(lhs) \
1106 { \
1107 } \
1108 \
1109 CYPrecedence(3) \
1110 \
1111 virtual const char *Operator() const { \
1112 return op; \
1113 } \
1114 };
1115
1116 #define CYPrefix_(alphabetic, op, name) \
1117 struct CY ## name : \
1118 CYPrefix \
1119 { \
1120 CY ## name(CYExpression *rhs) : \
1121 CYPrefix(rhs) \
1122 { \
1123 } \
1124 \
1125 CYAlphabetic(alphabetic) \
1126 CYPrecedence(4) \
1127 \
1128 virtual const char *Operator() const { \
1129 return op; \
1130 } \
1131 };
1132
1133 #define CYInfix_(alphabetic, precedence, op, name) \
1134 struct CY ## name : \
1135 CYInfix \
1136 { \
1137 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1138 CYInfix(lhs, rhs) \
1139 { \
1140 } \
1141 \
1142 CYAlphabetic(alphabetic) \
1143 CYPrecedence(precedence) \
1144 \
1145 virtual const char *Operator() const { \
1146 return op; \
1147 } \
1148 };
1149
1150 #define CYAssignment_(op, name) \
1151 struct CY ## name ## Assign : \
1152 CYAssignment \
1153 { \
1154 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1155 CYAssignment(lhs, rhs) \
1156 { \
1157 } \
1158 \
1159 CYPrecedence(16) \
1160 \
1161 virtual const char *Operator() const { \
1162 return op; \
1163 } \
1164 };
1165
1166 CYPostfix_("++", PostIncrement)
1167 CYPostfix_("--", PostDecrement)
1168
1169 CYPrefix_(true, "delete", Delete)
1170 CYPrefix_(true, "void", Void)
1171 CYPrefix_(true, "typeof", TypeOf)
1172 CYPrefix_(false, "++", PreIncrement)
1173 CYPrefix_(false, "--", PreDecrement)
1174 CYPrefix_(false, "-", Negate)
1175 CYPrefix_(false, "~", BitwiseNot)
1176 CYPrefix_(false, "!", LogicalNot)
1177
1178 CYInfix_(false, 5, "*", Multiply)
1179 CYInfix_(false, 5, "/", Divide)
1180 CYInfix_(false, 5, "%", Modulus)
1181 CYInfix_(false, 6, "+", Add)
1182 CYInfix_(false, 6, "-", Subtract)
1183 CYInfix_(false, 7, "<<", ShiftLeft)
1184 CYInfix_(false, 7, ">>", ShiftRightSigned)
1185 CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1186 CYInfix_(false, 8, "<", Less)
1187 CYInfix_(false, 8, ">", Greater)
1188 CYInfix_(false, 8, "<=", LessOrEqual)
1189 CYInfix_(false, 8, ">=", GreaterOrEqual)
1190 CYInfix_(true, 8, "instanceof", InstanceOf)
1191 CYInfix_(true, 8, "in", In)
1192 CYInfix_(false, 9, "==", Equal)
1193 CYInfix_(false, 9, "!=", NotEqual)
1194 CYInfix_(false, 9, "===", Identical)
1195 CYInfix_(false, 9, "!==", NotIdentical)
1196 CYInfix_(false, 10, "&", BitwiseAnd)
1197 CYInfix_(false, 11, "^", BitwiseXOr)
1198 CYInfix_(false, 12, "|", BitwiseOr)
1199 CYInfix_(false, 13, "&&", LogicalAnd)
1200 CYInfix_(false, 14, "||", LogicalOr)
1201
1202 CYAssignment_("=", )
1203 CYAssignment_("*=", Multiply)
1204 CYAssignment_("/=", Divide)
1205 CYAssignment_("%=", Modulus)
1206 CYAssignment_("+=", Add)
1207 CYAssignment_("-=", Subtract)
1208 CYAssignment_("<<=", ShiftLeft)
1209 CYAssignment_(">>=", ShiftRightSigned)
1210 CYAssignment_(">>>=", ShiftRightUnsigned)
1211 CYAssignment_("&=", BitwiseAnd)
1212 CYAssignment_("^=", BitwiseXOr)
1213 CYAssignment_("|=", BitwiseOr)
1214
1215 #endif/*CYPARSER_HPP*/