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