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