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