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