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