]> git.saurik.com Git - cycript.git/blame - Parser.hpp
I nailed the interactive parser!
[cycript.git] / Parser.hpp
CommitLineData
63b4c5a8
JF
1#ifndef CYPARSER_HPP
2#define CYPARSER_HPP
3
cf7d4c69 4#include <cstdlib>
5999c315 5#include <string>
cf7d4c69 6
5999c315 7#include "Pooling.hpp"
924f67b2 8
5999c315
JF
9template <typename Type_>
10struct CYNext {
11 Type_ *next_;
63b4c5a8 12
5999c315
JF
13 CYNext() :
14 next_(NULL)
15 {
16 }
cf7d4c69 17
5999c315 18 void SetNext(Type_ *next) {
cf7d4c69
JF
19 next_ = next;
20 }
21};
22
5999c315
JF
23struct 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
32struct CYPart {
33 virtual void Part(std::ostream &out) const = 0;
63b4c5a8
JF
34};
35
5999c315 36struct CYSource :
b1ff2d78 37 CYNext<CYSource>
5999c315 38{
b1ff2d78 39 virtual void Show(std::ostream &out) const;
5999c315
JF
40 virtual void Output(std::ostream &out) const = 0;
41 virtual void Output(std::ostream &out, bool block) const;
42};
43
44struct CYName :
45 CYThing
46{
47 virtual const char *Name() const = 0;
63b4c5a8
JF
48};
49
cf7d4c69 50struct CYWord :
cf7d4c69 51 CYName
63b4c5a8 52{
cf7d4c69
JF
53 const char *word_;
54
55 CYWord(const char *word) :
56 word_(word)
57 {
58 }
59
5999c315 60 const char *Value() const {
cf7d4c69
JF
61 return word_;
62 }
63
64 virtual const char *Name() const {
5999c315 65 return Value();
cf7d4c69 66 }
5999c315
JF
67
68 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
69};
70
cf7d4c69
JF
71struct CYIdentifier :
72 CYWord
63b4c5a8 73{
5999c315
JF
74 CYIdentifier(const char *word) :
75 CYWord(word)
76 {
cf7d4c69 77 }
63b4c5a8
JF
78};
79
cf7d4c69
JF
80struct CYLabel {
81 CYIdentifier *identifier_;
82 CYLabel *next_;
83
84 CYLabel(CYIdentifier *identifier, CYLabel *next) :
85 identifier_(identifier),
86 next_(next)
87 {
88 }
89};
90
91struct CYStatement :
92 CYSource
63b4c5a8 93{
cf7d4c69
JF
94 CYLabel *label_;
95
96 void AddLabel(CYIdentifier *identifier) {
97 label_ = new CYLabel(identifier, label_);
98 }
99};
100
5999c315
JF
101class 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();
b1ff2d78
JF
115
116 void Clear();
5999c315
JF
117};
118
119struct CYForInitialiser :
120 CYPart
121{
cf7d4c69
JF
122};
123
5999c315
JF
124struct CYForInInitialiser :
125 CYPart
126{
63b4c5a8
JF
127};
128
cf7d4c69 129struct CYExpression :
5999c315 130 CYNext<CYExpression>,
cf7d4c69
JF
131 CYForInitialiser,
132 CYForInInitialiser
63b4c5a8 133{
5999c315
JF
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;
63b4c5a8
JF
137};
138
5999c315
JF
139_finline std::ostream &operator <<(std::ostream &out, const CYExpression &rhs) {
140 rhs.Output(out, false);
141 return out;
142}
143
cf7d4c69
JF
144struct CYLiteral :
145 CYExpression
63b4c5a8 146{
cf7d4c69 147};
63b4c5a8 148
cf7d4c69
JF
149struct CYString :
150 CYLiteral,
151 CYName
152{
153 const char *value_;
5999c315 154 size_t size_;
cf7d4c69 155
5999c315
JF
156 CYString(const char *value, size_t size) :
157 value_(value),
158 size_(size)
cf7d4c69
JF
159 {
160 }
161
162 CYString(const CYIdentifier *identifier) :
5999c315
JF
163 value_(identifier->Value()),
164 size_(strlen(value_))
cf7d4c69
JF
165 {
166 }
167
5999c315 168 const char *Value() const {
cf7d4c69
JF
169 return value_;
170 }
171
172 virtual const char *Name() const {
5999c315 173 return Value();
63b4c5a8 174 }
5999c315
JF
175
176 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
177};
178
cf7d4c69 179struct CYNumber :
cf7d4c69
JF
180 CYLiteral,
181 CYName
182{
5999c315
JF
183 double value_;
184
185 CYNumber(double value) :
186 value_(value)
187 {
188 }
189
190 double Value() const {
191 return value_;
cf7d4c69
JF
192 }
193
194 virtual const char *Name() const {
195 throw;
196 }
5999c315
JF
197
198 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
199};
200
201struct CYNull :
202 CYWord,
203 CYLiteral
204{
205 CYNull() :
206 CYWord("null")
207 {
208 }
5999c315
JF
209
210 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
211};
212
213struct CYThis :
214 CYWord,
215 CYExpression
216{
217 CYThis() :
218 CYWord("this")
219 {
220 }
5999c315
JF
221
222 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
223};
224
225struct CYBoolean :
226 CYLiteral
227{
5999c315
JF
228 virtual bool Value() const = 0;
229 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
230};
231
232struct CYFalse :
233 CYWord,
234 CYBoolean
235{
236 CYFalse() :
237 CYWord("false")
238 {
239 }
5999c315
JF
240
241 virtual bool Value() const {
242 return false;
243 }
cf7d4c69
JF
244};
245
246struct CYTrue :
247 CYWord,
248 CYBoolean
249{
250 CYTrue() :
251 CYWord("true")
252 {
253 }
5999c315
JF
254
255 virtual bool Value() const {
256 return true;
257 }
cf7d4c69
JF
258};
259
260struct CYVariable :
261 CYExpression
262{
263 CYIdentifier *name_;
264
265 CYVariable(CYIdentifier *name) :
266 name_(name)
267 {
268 }
5999c315
JF
269
270 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
271};
272
273struct CYPrefix :
63b4c5a8
JF
274 CYExpression
275{
276 CYExpression *rhs_;
277
cf7d4c69 278 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
279 rhs_(rhs)
280 {
281 }
5999c315
JF
282
283 virtual const char *Operator() const = 0;
284
285 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
286};
287
cf7d4c69 288struct CYInfix :
63b4c5a8
JF
289 CYExpression
290{
291 CYExpression *lhs_;
292 CYExpression *rhs_;
293
cf7d4c69 294 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
295 lhs_(lhs),
296 rhs_(rhs)
297 {
298 }
5999c315
JF
299
300 virtual const char *Operator() const = 0;
301
302 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
303};
304
cf7d4c69 305struct CYPostfix :
63b4c5a8
JF
306 CYExpression
307{
308 CYExpression *lhs_;
309
cf7d4c69 310 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
311 lhs_(lhs)
312 {
313 }
5999c315
JF
314
315 virtual const char *Operator() const = 0;
316
317 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
318};
319
cf7d4c69
JF
320struct CYAssignment :
321 CYInfix
322{
323 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
324 CYInfix(lhs, rhs)
325 {
326 }
5999c315
JF
327
328 virtual const char *Operator() const = 0;
cf7d4c69
JF
329};
330
331struct 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 }
5999c315
JF
342
343 void Output(std::ostream &out, bool send) const;
cf7d4c69
JF
344};
345
346struct CYBlank :
347 public CYWord
348{
349 CYBlank() :
350 CYWord("")
351 {
352 }
353};
354
5999c315
JF
355struct CYClause :
356 CYThing,
357 CYNext<CYClause>
358{
cf7d4c69
JF
359 CYExpression *case_;
360 CYStatement *code_;
cf7d4c69
JF
361
362 CYClause(CYExpression *_case, CYStatement *code) :
363 case_(_case),
364 code_(code)
365 {
366 }
367
5999c315 368 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
369};
370
371struct 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 }
5999c315
JF
382
383 void Output(std::ostream &out, bool raw) const;
384 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
385};
386
387struct CYDeclaration :
5999c315 388 CYThing,
cf7d4c69
JF
389 CYForInInitialiser
390{
391 CYIdentifier *identifier_;
392 CYExpression *initialiser_;
393
394 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
395 identifier_(identifier),
396 initialiser_(initialiser)
397 {
398 }
5999c315
JF
399
400 virtual void Part(std::ostream &out) const;
401 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
402};
403
404struct 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 }
5999c315
JF
416
417 virtual void Part(std::ostream &out) const;
418 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
419};
420
5999c315
JF
421struct CYParameter :
422 CYThing
423{
cf7d4c69
JF
424 CYIdentifier *name_;
425 CYParameter *next_;
426
427 CYParameter(CYIdentifier *name, CYParameter *next) :
428 name_(name),
429 next_(next)
430 {
431 }
5999c315
JF
432
433 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
434};
435
436struct 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 }
5999c315
JF
451
452 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
453};
454
455struct 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 }
5999c315
JF
468
469 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
470};
471
472struct 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 }
5999c315
JF
485
486 void Output(std::ostream &out, bool raw) const;
487 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
488};
489
5999c315
JF
490struct CYCatch :
491 CYThing
492{
cf7d4c69
JF
493 CYIdentifier *name_;
494 CYStatement *code_;
495
496 CYCatch(CYIdentifier *name, CYStatement *code) :
497 name_(name),
498 code_(code)
499 {
500 }
5999c315
JF
501
502 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
503};
504
505struct CYMessage :
63b4c5a8
JF
506 CYExpression
507{
cf7d4c69
JF
508 CYExpression *self_;
509 CYArgument *arguments_;
63b4c5a8 510
cf7d4c69
JF
511 CYMessage(CYExpression *self, CYArgument *arguments) :
512 self_(self),
513 arguments_(arguments)
63b4c5a8
JF
514 {
515 }
5999c315
JF
516
517 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
518};
519
cf7d4c69
JF
520struct 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 }
5999c315
JF
531
532 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
533};
534
535struct 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 }
5999c315
JF
546
547 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
548};
549
550struct 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 }
5999c315
JF
561
562 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
563};
564
565struct 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 }
5999c315
JF
578
579 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
580};
581
582struct 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 }
5999c315
JF
593
594 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
595};
596
597struct 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 }
5999c315
JF
608
609 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
610};
611
612struct 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 }
5999c315
JF
625
626 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
627};
628
629struct CYFunction :
5999c315
JF
630 CYLambda,
631 CYSource
cf7d4c69
JF
632{
633 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
634 CYLambda(name, parameters, body)
635 {
636 }
5999c315
JF
637
638 virtual void Output(std::ostream &out) const;
639};
640
641struct 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;
cf7d4c69
JF
652};
653
654struct CYContinue :
655 CYStatement
656{
657 CYIdentifier *label_;
658
659 CYContinue(CYIdentifier *label) :
660 label_(label)
661 {
662 }
5999c315
JF
663
664 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
665};
666
667struct CYBreak :
668 CYStatement
669{
670 CYIdentifier *label_;
671
672 CYBreak(CYIdentifier *label) :
673 label_(label)
674 {
675 }
5999c315
JF
676
677 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
678};
679
680struct CYReturn :
681 CYStatement
682{
683 CYExpression *value_;
684
685 CYReturn(CYExpression *value) :
686 value_(value)
687 {
688 }
5999c315
JF
689
690 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
691};
692
693struct CYEmpty :
694 CYStatement
695{
5999c315
JF
696 virtual void Output(std::ostream &out) const;
697 virtual void Output(std::ostream &out, bool block) const;
cf7d4c69
JF
698};
699
700struct 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 }
5999c315
JF
713
714 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
715};
716
717struct CYThrow :
718 CYStatement
719{
720 CYExpression *value_;
721
722 CYThrow(CYExpression *value) :
723 value_(value)
724 {
725 }
5999c315
JF
726
727 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
728};
729
730struct 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 }
5999c315
JF
741
742 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
743};
744
745struct 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 }
5999c315
JF
756
757 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
758};
759
760struct 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 }
5999c315
JF
772
773 virtual void Output(std::ostream &out) const;
774};
775
776struct 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
791struct 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;
cf7d4c69
JF
804};
805
806#define CYPostfix_(op, name) \
807 struct CY ## name : \
808 CYPostfix \
809 { \
810 CY ## name(CYExpression *lhs) : \
811 CYPostfix(lhs) \
812 { \
813 } \
5999c315
JF
814 \
815 virtual const char *Operator() const { \
816 return op; \
817 } \
cf7d4c69
JF
818 };
819
820#define CYPrefix_(op, name) \
821 struct CY ## name : \
822 CYPrefix \
823 { \
824 CY ## name(CYExpression *rhs) : \
825 CYPrefix(rhs) \
826 { \
827 } \
5999c315
JF
828 \
829 virtual const char *Operator() const { \
830 return op; \
831 } \
cf7d4c69
JF
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 } \
5999c315
JF
842 \
843 virtual const char *Operator() const { \
844 return op; \
845 } \
cf7d4c69
JF
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 } \
5999c315
JF
856 \
857 virtual const char *Operator() const { \
858 return op; \
859 } \
cf7d4c69
JF
860 };
861
862CYPostfix_("++", PostIncrement)
863CYPostfix_("--", PostDecrement)
864
865CYPrefix_("delete", Delete)
866CYPrefix_("void", Void)
867CYPrefix_("typeof", TypeOf)
868CYPrefix_("++", PreIncrement)
869CYPrefix_("--", PreDecrement)
870CYPrefix_("-", Negate)
871CYPrefix_("~", BitwiseNot)
872CYPrefix_("!", LogicalNot)
cf7d4c69
JF
873
874CYInfix_("*", Multiply)
875CYInfix_("/", Divide)
876CYInfix_("%", Modulus)
877CYInfix_("+", Add)
878CYInfix_("-", Subtract)
879CYInfix_("<<", ShiftLeft)
880CYInfix_(">>", ShiftRightSigned)
881CYInfix_(">>>", ShiftRightUnsigned)
882CYInfix_("<", Less)
883CYInfix_(">", Greater)
884CYInfix_("<=", LessOrEqual)
885CYInfix_(">=", GreaterOrEqual)
886CYInfix_("instanceof", InstanceOf)
887CYInfix_("in", In)
888CYInfix_("==", Equal)
889CYInfix_("!=", NotEqual)
890CYInfix_("===", Identical)
891CYInfix_("!==", NotIdentical)
892CYInfix_("&", BitwiseAnd)
893CYInfix_("^", BitwiseXOr)
894CYInfix_("|", BitwiseOr)
895CYInfix_("&&", LogicalAnd)
896CYInfix_("||", LogicalOr)
897
898CYAssignment_("=", )
899CYAssignment_("*=", Multiply)
900CYAssignment_("/=", Divide)
901CYAssignment_("%=", Modulus)
902CYAssignment_("+=", Add)
903CYAssignment_("-=", Subtract)
904CYAssignment_("<<=", ShiftLeft)
905CYAssignment_(">>=", ShiftRightSigned)
906CYAssignment_(">>>=", ShiftRightUnsigned)
907CYAssignment_("&=", BitwiseAnd)
908CYAssignment_("^=", BitwiseXOr)
909CYAssignment_("|=", BitwiseOr)
910
63b4c5a8 911#endif/*CYPARSER_HPP*/