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