]> git.saurik.com Git - cycript.git/blob - Parser.hpp
b848c7b5d576d10ed857a3ccd95170934b7b1927
[cycript.git] / Parser.hpp
1 #ifndef CYPARSER_HPP
2 #define CYPARSER_HPP
3
4 #include <cstdlib>
5 #include <string>
6 #include <vector>
7
8 #include "location.hh"
9 #include "Pooling.hpp"
10
11 template <typename Type_>
12 struct CYNext {
13 Type_ *next_;
14
15 CYNext() :
16 next_(NULL)
17 {
18 }
19
20 CYNext(Type_ *next) :
21 next_(next)
22 {
23 }
24
25 void SetNext(Type_ *next) {
26 next_ = next;
27 }
28 };
29
30 struct CYThing {
31 virtual void Output(std::ostream &out) const = 0;
32 };
33
34 _finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) {
35 rhs.Output(out);
36 return out;
37 }
38
39 struct CYPart {
40 virtual void Part(std::ostream &out) const = 0;
41 };
42
43 struct CYSource :
44 CYNext<CYSource>
45 {
46 virtual void Show(std::ostream &out) const;
47 virtual void Output(std::ostream &out) const = 0;
48 virtual void Output(std::ostream &out, bool block) const;
49 };
50
51 struct CYName :
52 CYThing
53 {
54 virtual const char *Name() const = 0;
55 };
56
57 struct CYWord :
58 CYName
59 {
60 const char *word_;
61
62 CYWord(const char *word) :
63 word_(word)
64 {
65 }
66
67 const char *Value() const {
68 return word_;
69 }
70
71 virtual const char *Name() const {
72 return Value();
73 }
74
75 virtual void Output(std::ostream &out) const;
76 };
77
78 struct CYIdentifier :
79 CYWord
80 {
81 CYIdentifier(const char *word) :
82 CYWord(word)
83 {
84 }
85 };
86
87 struct CYLabel :
88 CYNext<CYLabel>
89 {
90 CYIdentifier *identifier_;
91
92 CYLabel(CYIdentifier *identifier, CYLabel *next) :
93 CYNext<CYLabel>(next),
94 identifier_(identifier)
95 {
96 }
97 };
98
99 struct CYStatement :
100 CYSource
101 {
102 CYLabel *label_;
103
104 void AddLabel(CYIdentifier *identifier) {
105 label_ = new CYLabel(identifier, label_);
106 }
107 };
108
109 enum CYState {
110 CYClear,
111 CYRestricted,
112 CYNewLine
113 };
114
115 class CYDriver {
116 public:
117 CYPool pool_;
118
119 CYState state_;
120 void *scanner_;
121
122 const char *data_;
123 size_t size_;
124
125 std::string filename_;
126
127 struct Error {
128 cy::location location_;
129 std::string message_;
130 };
131
132 typedef std::vector<Error> Errors;
133
134 CYSource *source_;
135 Errors errors_;
136
137 private:
138 void ScannerInit();
139 void ScannerDestroy();
140
141 public:
142 CYDriver(const std::string &filename);
143 ~CYDriver();
144 };
145
146 struct CYForInitialiser :
147 CYPart
148 {
149 };
150
151 struct CYForInInitialiser :
152 CYPart
153 {
154 };
155
156 struct CYExpression :
157 CYNext<CYExpression>,
158 CYForInitialiser,
159 CYForInInitialiser
160 {
161 virtual void Part(std::ostream &out) const;
162 virtual void Output(std::ostream &out) const = 0;
163 void Output(std::ostream &out, bool raw) const;
164 };
165
166 _finline std::ostream &operator <<(std::ostream &out, const CYExpression &rhs) {
167 rhs.Output(out, false);
168 return out;
169 }
170
171 struct CYLiteral :
172 CYExpression
173 {
174 };
175
176 struct CYSelectorPart :
177 CYNext<CYSelectorPart>
178 {
179 CYWord *name_;
180 bool value_;
181
182 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
183 CYNext<CYSelectorPart>(next),
184 name_(name),
185 value_(value)
186 {
187 }
188
189 virtual void Output(std::ostream &out) const;
190 };
191
192 struct CYSelector :
193 CYLiteral
194 {
195 CYSelectorPart *name_;
196
197 CYSelector(CYSelectorPart *name) :
198 name_(name)
199 {
200 }
201
202 virtual void Output(std::ostream &out) const;
203 };
204
205 struct CYString :
206 CYLiteral,
207 CYName
208 {
209 const char *value_;
210 size_t size_;
211
212 CYString(const char *value, size_t size) :
213 value_(value),
214 size_(size)
215 {
216 }
217
218 CYString(const CYIdentifier *identifier) :
219 value_(identifier->Value()),
220 size_(strlen(value_))
221 {
222 }
223
224 const char *Value() const {
225 return value_;
226 }
227
228 virtual const char *Name() const {
229 return Value();
230 }
231
232 virtual void Output(std::ostream &out) const;
233 };
234
235 struct CYNumber :
236 CYLiteral,
237 CYName
238 {
239 double value_;
240
241 CYNumber(double value) :
242 value_(value)
243 {
244 }
245
246 double Value() const {
247 return value_;
248 }
249
250 virtual const char *Name() const {
251 throw;
252 }
253
254 virtual void Output(std::ostream &out) const;
255 };
256
257 struct CYNull :
258 CYWord,
259 CYLiteral
260 {
261 CYNull() :
262 CYWord("null")
263 {
264 }
265
266 virtual void Output(std::ostream &out) const;
267 };
268
269 struct CYThis :
270 CYWord,
271 CYExpression
272 {
273 CYThis() :
274 CYWord("this")
275 {
276 }
277
278 virtual void Output(std::ostream &out) const;
279 };
280
281 struct CYBoolean :
282 CYLiteral
283 {
284 virtual bool Value() const = 0;
285 virtual void Output(std::ostream &out) const;
286 };
287
288 struct CYFalse :
289 CYWord,
290 CYBoolean
291 {
292 CYFalse() :
293 CYWord("false")
294 {
295 }
296
297 virtual bool Value() const {
298 return false;
299 }
300 };
301
302 struct CYTrue :
303 CYWord,
304 CYBoolean
305 {
306 CYTrue() :
307 CYWord("true")
308 {
309 }
310
311 virtual bool Value() const {
312 return true;
313 }
314 };
315
316 struct CYVariable :
317 CYExpression
318 {
319 CYIdentifier *name_;
320
321 CYVariable(CYIdentifier *name) :
322 name_(name)
323 {
324 }
325
326 virtual void Output(std::ostream &out) const;
327 };
328
329 struct CYPrefix :
330 CYExpression
331 {
332 CYExpression *rhs_;
333
334 CYPrefix(CYExpression *rhs) :
335 rhs_(rhs)
336 {
337 }
338
339 virtual const char *Operator() const = 0;
340
341 virtual void Output(std::ostream &out) const;
342 };
343
344 struct CYInfix :
345 CYExpression
346 {
347 CYExpression *lhs_;
348 CYExpression *rhs_;
349
350 CYInfix(CYExpression *lhs, CYExpression *rhs) :
351 lhs_(lhs),
352 rhs_(rhs)
353 {
354 }
355
356 virtual const char *Operator() const = 0;
357
358 virtual void Output(std::ostream &out) const;
359 };
360
361 struct CYPostfix :
362 CYExpression
363 {
364 CYExpression *lhs_;
365
366 CYPostfix(CYExpression *lhs) :
367 lhs_(lhs)
368 {
369 }
370
371 virtual const char *Operator() const = 0;
372
373 virtual void Output(std::ostream &out) const;
374 };
375
376 struct CYAssignment :
377 CYInfix
378 {
379 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
380 CYInfix(lhs, rhs)
381 {
382 }
383
384 virtual const char *Operator() const = 0;
385 };
386
387 struct CYArgument :
388 CYNext<CYArgument>
389 {
390 CYWord *name_;
391 CYExpression *value_;
392
393 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
394 CYNext<CYArgument>(next),
395 name_(name),
396 value_(value)
397 {
398 }
399
400 void Output(std::ostream &out, bool send) const;
401 };
402
403 struct CYBlank :
404 public CYWord
405 {
406 CYBlank() :
407 CYWord("")
408 {
409 }
410 };
411
412 struct CYClause :
413 CYThing,
414 CYNext<CYClause>
415 {
416 CYExpression *case_;
417 CYStatement *code_;
418
419 CYClause(CYExpression *_case, CYStatement *code) :
420 case_(_case),
421 code_(code)
422 {
423 }
424
425 virtual void Output(std::ostream &out) const;
426 };
427
428 struct CYElement :
429 CYNext<CYElement>
430 {
431 CYExpression *value_;
432
433 CYElement(CYExpression *value, CYElement *next) :
434 CYNext<CYElement>(next),
435 value_(value)
436 {
437 }
438
439 void Output(std::ostream &out) const;
440 };
441
442 struct CYArray :
443 CYLiteral
444 {
445 CYElement *elements_;
446
447 CYArray(CYElement *elements) :
448 elements_(elements)
449 {
450 }
451
452 virtual void Output(std::ostream &out) const;
453 };
454
455 struct CYDeclaration :
456 CYThing,
457 CYForInInitialiser
458 {
459 CYIdentifier *identifier_;
460 CYExpression *initialiser_;
461
462 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
463 identifier_(identifier),
464 initialiser_(initialiser)
465 {
466 }
467
468 virtual void Part(std::ostream &out) const;
469 virtual void Output(std::ostream &out) const;
470 };
471
472 struct CYDeclarations :
473 CYStatement,
474 CYForInitialiser
475 {
476 CYDeclaration *declaration_;
477 CYDeclarations *next_;
478
479 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
480 declaration_(declaration),
481 next_(next)
482 {
483 }
484
485 virtual void Part(std::ostream &out) const;
486 virtual void Output(std::ostream &out) const;
487 };
488
489 struct CYParameter :
490 CYNext<CYParameter>,
491 CYThing
492 {
493 CYIdentifier *name_;
494
495 CYParameter(CYIdentifier *name, CYParameter *next) :
496 CYNext<CYParameter>(next),
497 name_(name)
498 {
499 }
500
501 virtual void Output(std::ostream &out) const;
502 };
503
504 struct CYFor :
505 CYStatement
506 {
507 CYForInitialiser *initialiser_;
508 CYExpression *test_;
509 CYExpression *increment_;
510 CYStatement *code_;
511
512 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
513 initialiser_(initialiser),
514 test_(test),
515 increment_(increment),
516 code_(code)
517 {
518 }
519
520 virtual void Output(std::ostream &out) const;
521 };
522
523 struct CYForIn :
524 CYStatement
525 {
526 CYForInInitialiser *initialiser_;
527 CYExpression *set_;
528 CYStatement *code_;
529
530 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
531 initialiser_(initialiser),
532 set_(set),
533 code_(code)
534 {
535 }
536
537 virtual void Output(std::ostream &out) const;
538 };
539
540 struct CYProperty :
541 CYNext<CYProperty>
542 {
543 CYName *name_;
544 CYExpression *value_;
545
546 CYProperty(CYName *name, CYExpression *value, CYProperty *next) :
547 CYNext<CYProperty>(next),
548 name_(name),
549 value_(value)
550 {
551 }
552
553 virtual void Output(std::ostream &out) const;
554 };
555
556 struct CYObject :
557 CYLiteral
558 {
559 CYProperty *property_;
560
561 CYObject(CYProperty *property) :
562 property_(property)
563 {
564 }
565
566 void Output(std::ostream &out) const;
567 };
568
569 struct CYCatch :
570 CYThing
571 {
572 CYIdentifier *name_;
573 CYStatement *code_;
574
575 CYCatch(CYIdentifier *name, CYStatement *code) :
576 name_(name),
577 code_(code)
578 {
579 }
580
581 virtual void Output(std::ostream &out) const;
582 };
583
584 struct CYMessage :
585 CYExpression
586 {
587 CYExpression *self_;
588 CYArgument *arguments_;
589
590 CYMessage(CYExpression *self, CYArgument *arguments) :
591 self_(self),
592 arguments_(arguments)
593 {
594 }
595
596 virtual void Output(std::ostream &out) const;
597 };
598
599 struct CYMember :
600 CYExpression
601 {
602 CYExpression *object_;
603 CYExpression *property_;
604
605 CYMember(CYExpression *object, CYExpression *property) :
606 object_(object),
607 property_(property)
608 {
609 }
610
611 virtual void Output(std::ostream &out) const;
612 };
613
614 struct CYNew :
615 CYExpression
616 {
617 CYExpression *constructor_;
618 CYArgument *arguments_;
619
620 CYNew(CYExpression *constructor, CYArgument *arguments) :
621 constructor_(constructor),
622 arguments_(arguments)
623 {
624 }
625
626 virtual void Output(std::ostream &out) const;
627 };
628
629 struct CYCall :
630 CYExpression
631 {
632 CYExpression *function_;
633 CYArgument *arguments_;
634
635 CYCall(CYExpression *function, CYArgument *arguments) :
636 function_(function),
637 arguments_(arguments)
638 {
639 }
640
641 virtual void Output(std::ostream &out) const;
642 };
643
644 struct CYIf :
645 CYStatement
646 {
647 CYExpression *test_;
648 CYStatement *true_;
649 CYStatement *false_;
650
651 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
652 test_(test),
653 true_(_true),
654 false_(_false)
655 {
656 }
657
658 virtual void Output(std::ostream &out) const;
659 };
660
661 struct CYDoWhile :
662 CYStatement
663 {
664 CYExpression *test_;
665 CYStatement *code_;
666
667 CYDoWhile(CYExpression *test, CYStatement *code) :
668 test_(test),
669 code_(code)
670 {
671 }
672
673 virtual void Output(std::ostream &out) const;
674 };
675
676 struct CYWhile :
677 CYStatement
678 {
679 CYExpression *test_;
680 CYStatement *code_;
681
682 CYWhile(CYExpression *test, CYStatement *code) :
683 test_(test),
684 code_(code)
685 {
686 }
687
688 virtual void Output(std::ostream &out) const;
689 };
690
691 struct CYLambda :
692 CYExpression
693 {
694 CYIdentifier *name_;
695 CYParameter *parameters_;
696 CYSource *body_;
697
698 CYLambda(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
699 name_(name),
700 parameters_(parameters),
701 body_(body)
702 {
703 }
704
705 virtual void Output(std::ostream &out) const;
706 };
707
708 struct CYFunction :
709 CYLambda,
710 CYSource
711 {
712 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
713 CYLambda(name, parameters, body)
714 {
715 }
716
717 virtual void Output(std::ostream &out) const;
718 };
719
720 struct CYExpress :
721 CYStatement
722 {
723 CYExpression *expression_;
724
725 CYExpress(CYExpression *expression) :
726 expression_(expression)
727 {
728 }
729
730 virtual void Output(std::ostream &out) const;
731 };
732
733 struct CYContinue :
734 CYStatement
735 {
736 CYIdentifier *label_;
737
738 CYContinue(CYIdentifier *label) :
739 label_(label)
740 {
741 }
742
743 virtual void Output(std::ostream &out) const;
744 };
745
746 struct CYBreak :
747 CYStatement
748 {
749 CYIdentifier *label_;
750
751 CYBreak(CYIdentifier *label) :
752 label_(label)
753 {
754 }
755
756 virtual void Output(std::ostream &out) const;
757 };
758
759 struct CYReturn :
760 CYStatement
761 {
762 CYExpression *value_;
763
764 CYReturn(CYExpression *value) :
765 value_(value)
766 {
767 }
768
769 virtual void Output(std::ostream &out) const;
770 };
771
772 struct CYEmpty :
773 CYStatement
774 {
775 virtual void Output(std::ostream &out) const;
776 virtual void Output(std::ostream &out, bool block) const;
777 };
778
779 struct CYTry :
780 CYStatement
781 {
782 CYStatement *try_;
783 CYCatch *catch_;
784 CYStatement *finally_;
785
786 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
787 try_(_try),
788 catch_(_catch),
789 finally_(finally)
790 {
791 }
792
793 virtual void Output(std::ostream &out) const;
794 };
795
796 struct CYThrow :
797 CYStatement
798 {
799 CYExpression *value_;
800
801 CYThrow(CYExpression *value) :
802 value_(value)
803 {
804 }
805
806 virtual void Output(std::ostream &out) const;
807 };
808
809 struct CYWith :
810 CYStatement
811 {
812 CYExpression *scope_;
813 CYStatement *code_;
814
815 CYWith(CYExpression *scope, CYStatement *code) :
816 scope_(scope),
817 code_(code)
818 {
819 }
820
821 virtual void Output(std::ostream &out) const;
822 };
823
824 struct CYSwitch :
825 CYStatement
826 {
827 CYExpression *value_;
828 CYClause *clauses_;
829
830 CYSwitch(CYExpression *value, CYClause *clauses) :
831 value_(value),
832 clauses_(clauses)
833 {
834 }
835
836 virtual void Output(std::ostream &out) const;
837 };
838
839 struct CYCondition :
840 CYExpression
841 {
842 CYExpression *test_;
843 CYExpression *true_;
844 CYExpression *false_;
845
846 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
847 true_(_true),
848 false_(_false)
849 {
850 }
851
852 virtual void Output(std::ostream &out) const;
853 };
854
855 struct CYAddressOf :
856 CYPrefix
857 {
858 CYAddressOf(CYExpression *rhs) :
859 CYPrefix(rhs)
860 {
861 }
862
863 virtual const char *Operator() const {
864 return "&";
865 }
866
867 virtual void Output(std::ostream &out) const;
868 };
869
870 struct CYIndirect :
871 CYPrefix
872 {
873 CYIndirect(CYExpression *rhs) :
874 CYPrefix(rhs)
875 {
876 }
877
878 virtual const char *Operator() const {
879 return "*";
880 }
881
882 virtual void Output(std::ostream &out) const;
883 };
884
885 #define CYPostfix_(op, name) \
886 struct CY ## name : \
887 CYPostfix \
888 { \
889 CY ## name(CYExpression *lhs) : \
890 CYPostfix(lhs) \
891 { \
892 } \
893 \
894 virtual const char *Operator() const { \
895 return op; \
896 } \
897 };
898
899 #define CYPrefix_(op, name) \
900 struct CY ## name : \
901 CYPrefix \
902 { \
903 CY ## name(CYExpression *rhs) : \
904 CYPrefix(rhs) \
905 { \
906 } \
907 \
908 virtual const char *Operator() const { \
909 return op; \
910 } \
911 };
912
913 #define CYInfix_(op, name) \
914 struct CY ## name : \
915 CYInfix \
916 { \
917 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
918 CYInfix(lhs, rhs) \
919 { \
920 } \
921 \
922 virtual const char *Operator() const { \
923 return op; \
924 } \
925 };
926
927 #define CYAssignment_(op, name) \
928 struct CY ## name ## Assign : \
929 CYAssignment \
930 { \
931 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
932 CYAssignment(lhs, rhs) \
933 { \
934 } \
935 \
936 virtual const char *Operator() const { \
937 return op; \
938 } \
939 };
940
941 CYPostfix_("++", PostIncrement)
942 CYPostfix_("--", PostDecrement)
943
944 CYPrefix_("delete", Delete)
945 CYPrefix_("void", Void)
946 CYPrefix_("typeof", TypeOf)
947 CYPrefix_("++", PreIncrement)
948 CYPrefix_("--", PreDecrement)
949 CYPrefix_("-", Negate)
950 CYPrefix_("~", BitwiseNot)
951 CYPrefix_("!", LogicalNot)
952
953 CYInfix_("*", Multiply)
954 CYInfix_("/", Divide)
955 CYInfix_("%", Modulus)
956 CYInfix_("+", Add)
957 CYInfix_("-", Subtract)
958 CYInfix_("<<", ShiftLeft)
959 CYInfix_(">>", ShiftRightSigned)
960 CYInfix_(">>>", ShiftRightUnsigned)
961 CYInfix_("<", Less)
962 CYInfix_(">", Greater)
963 CYInfix_("<=", LessOrEqual)
964 CYInfix_(">=", GreaterOrEqual)
965 CYInfix_("instanceof", InstanceOf)
966 CYInfix_("in", In)
967 CYInfix_("==", Equal)
968 CYInfix_("!=", NotEqual)
969 CYInfix_("===", Identical)
970 CYInfix_("!==", NotIdentical)
971 CYInfix_("&", BitwiseAnd)
972 CYInfix_("^", BitwiseXOr)
973 CYInfix_("|", BitwiseOr)
974 CYInfix_("&&", LogicalAnd)
975 CYInfix_("||", LogicalOr)
976
977 CYAssignment_("=", )
978 CYAssignment_("*=", Multiply)
979 CYAssignment_("/=", Divide)
980 CYAssignment_("%=", Modulus)
981 CYAssignment_("+=", Add)
982 CYAssignment_("-=", Subtract)
983 CYAssignment_("<<=", ShiftLeft)
984 CYAssignment_(">>=", ShiftRightSigned)
985 CYAssignment_(">>>=", ShiftRightUnsigned)
986 CYAssignment_("&=", BitwiseAnd)
987 CYAssignment_("^=", BitwiseXOr)
988 CYAssignment_("|=", BitwiseOr)
989
990 #endif/*CYPARSER_HPP*/