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