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