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