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