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