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