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