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