]> git.saurik.com Git - cycript.git/blob - Parser.hpp
Implemented automatic semicolon insertion token restrictions.
[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 CYLiteral
482 {
483 CYName *name_;
484 CYExpression *value_;
485 CYProperty *next_;
486
487 CYProperty(CYName *name, CYExpression *value, CYProperty *next) :
488 name_(name),
489 value_(value),
490 next_(next)
491 {
492 }
493
494 void Output(std::ostream &out, bool raw) const;
495 virtual void Output(std::ostream &out) const;
496 };
497
498 struct CYCatch :
499 CYThing
500 {
501 CYIdentifier *name_;
502 CYStatement *code_;
503
504 CYCatch(CYIdentifier *name, CYStatement *code) :
505 name_(name),
506 code_(code)
507 {
508 }
509
510 virtual void Output(std::ostream &out) const;
511 };
512
513 struct CYMessage :
514 CYExpression
515 {
516 CYExpression *self_;
517 CYArgument *arguments_;
518
519 CYMessage(CYExpression *self, CYArgument *arguments) :
520 self_(self),
521 arguments_(arguments)
522 {
523 }
524
525 virtual void Output(std::ostream &out) const;
526 };
527
528 struct CYMember :
529 CYExpression
530 {
531 CYExpression *object_;
532 CYExpression *property_;
533
534 CYMember(CYExpression *object, CYExpression *property) :
535 object_(object),
536 property_(property)
537 {
538 }
539
540 virtual void Output(std::ostream &out) const;
541 };
542
543 struct CYNew :
544 CYExpression
545 {
546 CYExpression *constructor_;
547 CYArgument *arguments_;
548
549 CYNew(CYExpression *constructor, CYArgument *arguments) :
550 constructor_(constructor),
551 arguments_(arguments)
552 {
553 }
554
555 virtual void Output(std::ostream &out) const;
556 };
557
558 struct CYCall :
559 CYExpression
560 {
561 CYExpression *function_;
562 CYArgument *arguments_;
563
564 CYCall(CYExpression *function, CYArgument *arguments) :
565 function_(function),
566 arguments_(arguments)
567 {
568 }
569
570 virtual void Output(std::ostream &out) const;
571 };
572
573 struct CYIf :
574 CYStatement
575 {
576 CYExpression *test_;
577 CYStatement *true_;
578 CYStatement *false_;
579
580 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
581 test_(test),
582 true_(_true),
583 false_(_false)
584 {
585 }
586
587 virtual void Output(std::ostream &out) const;
588 };
589
590 struct CYDoWhile :
591 CYStatement
592 {
593 CYExpression *test_;
594 CYStatement *code_;
595
596 CYDoWhile(CYExpression *test, CYStatement *code) :
597 test_(test),
598 code_(code)
599 {
600 }
601
602 virtual void Output(std::ostream &out) const;
603 };
604
605 struct CYWhile :
606 CYStatement
607 {
608 CYExpression *test_;
609 CYStatement *code_;
610
611 CYWhile(CYExpression *test, CYStatement *code) :
612 test_(test),
613 code_(code)
614 {
615 }
616
617 virtual void Output(std::ostream &out) const;
618 };
619
620 struct CYLambda :
621 CYExpression
622 {
623 CYIdentifier *name_;
624 CYParameter *parameters_;
625 CYSource *body_;
626
627 CYLambda(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
628 name_(name),
629 parameters_(parameters),
630 body_(body)
631 {
632 }
633
634 virtual void Output(std::ostream &out) const;
635 };
636
637 struct CYFunction :
638 CYLambda,
639 CYSource
640 {
641 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
642 CYLambda(name, parameters, body)
643 {
644 }
645
646 virtual void Output(std::ostream &out) const;
647 };
648
649 struct CYExpress :
650 CYStatement
651 {
652 CYExpression *expression_;
653
654 CYExpress(CYExpression *expression) :
655 expression_(expression)
656 {
657 }
658
659 virtual void Output(std::ostream &out) const;
660 };
661
662 struct CYContinue :
663 CYStatement
664 {
665 CYIdentifier *label_;
666
667 CYContinue(CYIdentifier *label) :
668 label_(label)
669 {
670 }
671
672 virtual void Output(std::ostream &out) const;
673 };
674
675 struct CYBreak :
676 CYStatement
677 {
678 CYIdentifier *label_;
679
680 CYBreak(CYIdentifier *label) :
681 label_(label)
682 {
683 }
684
685 virtual void Output(std::ostream &out) const;
686 };
687
688 struct CYReturn :
689 CYStatement
690 {
691 CYExpression *value_;
692
693 CYReturn(CYExpression *value) :
694 value_(value)
695 {
696 }
697
698 virtual void Output(std::ostream &out) const;
699 };
700
701 struct CYEmpty :
702 CYStatement
703 {
704 virtual void Output(std::ostream &out) const;
705 virtual void Output(std::ostream &out, bool block) const;
706 };
707
708 struct CYTry :
709 CYStatement
710 {
711 CYStatement *try_;
712 CYCatch *catch_;
713 CYStatement *finally_;
714
715 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
716 try_(_try),
717 catch_(_catch),
718 finally_(finally)
719 {
720 }
721
722 virtual void Output(std::ostream &out) const;
723 };
724
725 struct CYThrow :
726 CYStatement
727 {
728 CYExpression *value_;
729
730 CYThrow(CYExpression *value) :
731 value_(value)
732 {
733 }
734
735 virtual void Output(std::ostream &out) const;
736 };
737
738 struct CYWith :
739 CYStatement
740 {
741 CYExpression *scope_;
742 CYStatement *code_;
743
744 CYWith(CYExpression *scope, CYStatement *code) :
745 scope_(scope),
746 code_(code)
747 {
748 }
749
750 virtual void Output(std::ostream &out) const;
751 };
752
753 struct CYSwitch :
754 CYStatement
755 {
756 CYExpression *value_;
757 CYClause *clauses_;
758
759 CYSwitch(CYExpression *value, CYClause *clauses) :
760 value_(value),
761 clauses_(clauses)
762 {
763 }
764
765 virtual void Output(std::ostream &out) const;
766 };
767
768 struct CYCondition :
769 CYExpression
770 {
771 CYExpression *test_;
772 CYExpression *true_;
773 CYExpression *false_;
774
775 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
776 true_(_true),
777 false_(_false)
778 {
779 }
780
781 virtual void Output(std::ostream &out) const;
782 };
783
784 struct CYAddressOf :
785 CYPrefix
786 {
787 CYAddressOf(CYExpression *rhs) :
788 CYPrefix(rhs)
789 {
790 }
791
792 virtual const char *Operator() const {
793 return "&";
794 }
795
796 virtual void Output(std::ostream &out) const;
797 };
798
799 struct CYIndirect :
800 CYPrefix
801 {
802 CYIndirect(CYExpression *rhs) :
803 CYPrefix(rhs)
804 {
805 }
806
807 virtual const char *Operator() const {
808 return "*";
809 }
810
811 virtual void Output(std::ostream &out) const;
812 };
813
814 #define CYPostfix_(op, name) \
815 struct CY ## name : \
816 CYPostfix \
817 { \
818 CY ## name(CYExpression *lhs) : \
819 CYPostfix(lhs) \
820 { \
821 } \
822 \
823 virtual const char *Operator() const { \
824 return op; \
825 } \
826 };
827
828 #define CYPrefix_(op, name) \
829 struct CY ## name : \
830 CYPrefix \
831 { \
832 CY ## name(CYExpression *rhs) : \
833 CYPrefix(rhs) \
834 { \
835 } \
836 \
837 virtual const char *Operator() const { \
838 return op; \
839 } \
840 };
841
842 #define CYInfix_(op, name) \
843 struct CY ## name : \
844 CYInfix \
845 { \
846 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
847 CYInfix(lhs, rhs) \
848 { \
849 } \
850 \
851 virtual const char *Operator() const { \
852 return op; \
853 } \
854 };
855
856 #define CYAssignment_(op, name) \
857 struct CY ## name ## Assign : \
858 CYAssignment \
859 { \
860 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
861 CYAssignment(lhs, rhs) \
862 { \
863 } \
864 \
865 virtual const char *Operator() const { \
866 return op; \
867 } \
868 };
869
870 CYPostfix_("++", PostIncrement)
871 CYPostfix_("--", PostDecrement)
872
873 CYPrefix_("delete", Delete)
874 CYPrefix_("void", Void)
875 CYPrefix_("typeof", TypeOf)
876 CYPrefix_("++", PreIncrement)
877 CYPrefix_("--", PreDecrement)
878 CYPrefix_("-", Negate)
879 CYPrefix_("~", BitwiseNot)
880 CYPrefix_("!", LogicalNot)
881
882 CYInfix_("*", Multiply)
883 CYInfix_("/", Divide)
884 CYInfix_("%", Modulus)
885 CYInfix_("+", Add)
886 CYInfix_("-", Subtract)
887 CYInfix_("<<", ShiftLeft)
888 CYInfix_(">>", ShiftRightSigned)
889 CYInfix_(">>>", ShiftRightUnsigned)
890 CYInfix_("<", Less)
891 CYInfix_(">", Greater)
892 CYInfix_("<=", LessOrEqual)
893 CYInfix_(">=", GreaterOrEqual)
894 CYInfix_("instanceof", InstanceOf)
895 CYInfix_("in", In)
896 CYInfix_("==", Equal)
897 CYInfix_("!=", NotEqual)
898 CYInfix_("===", Identical)
899 CYInfix_("!==", NotIdentical)
900 CYInfix_("&", BitwiseAnd)
901 CYInfix_("^", BitwiseXOr)
902 CYInfix_("|", BitwiseOr)
903 CYInfix_("&&", LogicalAnd)
904 CYInfix_("||", LogicalOr)
905
906 CYAssignment_("=", )
907 CYAssignment_("*=", Multiply)
908 CYAssignment_("/=", Divide)
909 CYAssignment_("%=", Modulus)
910 CYAssignment_("+=", Add)
911 CYAssignment_("-=", Subtract)
912 CYAssignment_("<<=", ShiftLeft)
913 CYAssignment_(">>=", ShiftRightSigned)
914 CYAssignment_(">>>=", ShiftRightUnsigned)
915 CYAssignment_("&=", BitwiseAnd)
916 CYAssignment_("^=", BitwiseXOr)
917 CYAssignment_("|=", BitwiseOr)
918
919 #endif/*CYPARSER_HPP*/