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