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