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