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