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