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