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