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