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