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