]> git.saurik.com Git - cycript.git/blame - Parser.hpp
Picked some minor nits.
[cycript.git] / Parser.hpp
CommitLineData
63b4c5a8
JF
1#ifndef CYPARSER_HPP
2#define CYPARSER_HPP
3
cf7d4c69 4#include <cstdlib>
5999c315 5#include <string>
c3c20102 6#include <vector>
cf7d4c69 7
5999c315 8#include "Pooling.hpp"
924f67b2 9
5999c315
JF
10template <typename Type_>
11struct CYNext {
12 Type_ *next_;
63b4c5a8 13
5999c315
JF
14 CYNext() :
15 next_(NULL)
16 {
17 }
cf7d4c69 18
5999c315 19 void SetNext(Type_ *next) {
cf7d4c69
JF
20 next_ = next;
21 }
22};
23
5999c315
JF
24struct 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
33struct CYPart {
34 virtual void Part(std::ostream &out) const = 0;
63b4c5a8
JF
35};
36
5999c315 37struct CYSource :
b1ff2d78 38 CYNext<CYSource>
5999c315 39{
b1ff2d78 40 virtual void Show(std::ostream &out) const;
5999c315
JF
41 virtual void Output(std::ostream &out) const = 0;
42 virtual void Output(std::ostream &out, bool block) const;
43};
44
45struct CYName :
46 CYThing
47{
48 virtual const char *Name() const = 0;
63b4c5a8
JF
49};
50
cf7d4c69 51struct CYWord :
cf7d4c69 52 CYName
63b4c5a8 53{
cf7d4c69
JF
54 const char *word_;
55
56 CYWord(const char *word) :
57 word_(word)
58 {
59 }
60
5999c315 61 const char *Value() const {
cf7d4c69
JF
62 return word_;
63 }
64
65 virtual const char *Name() const {
5999c315 66 return Value();
cf7d4c69 67 }
5999c315
JF
68
69 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
70};
71
cf7d4c69
JF
72struct CYIdentifier :
73 CYWord
63b4c5a8 74{
5999c315
JF
75 CYIdentifier(const char *word) :
76 CYWord(word)
77 {
cf7d4c69 78 }
63b4c5a8
JF
79};
80
cf7d4c69
JF
81struct CYLabel {
82 CYIdentifier *identifier_;
83 CYLabel *next_;
84
85 CYLabel(CYIdentifier *identifier, CYLabel *next) :
86 identifier_(identifier),
87 next_(next)
88 {
89 }
90};
91
92struct CYStatement :
93 CYSource
63b4c5a8 94{
cf7d4c69
JF
95 CYLabel *label_;
96
97 void AddLabel(CYIdentifier *identifier) {
98 label_ = new CYLabel(identifier, label_);
99 }
100};
101
db5e2840
JF
102enum CYState {
103 CYClear,
104 CYRestricted,
105 CYNewLine
106};
107
5999c315
JF
108class CYDriver {
109 public:
110 CYPool pool_;
db5e2840 111 CYState state_;
5999c315 112 std::string filename_;
c3c20102 113 std::vector<CYSource *> source_;
5999c315
JF
114 void *scanner_;
115
116 private:
117 void ScannerInit();
118 void ScannerDestroy();
119
120 public:
121 CYDriver(const std::string &filename);
122 ~CYDriver();
b1ff2d78
JF
123
124 void Clear();
5999c315
JF
125};
126
127struct CYForInitialiser :
128 CYPart
129{
cf7d4c69
JF
130};
131
5999c315
JF
132struct CYForInInitialiser :
133 CYPart
134{
63b4c5a8
JF
135};
136
cf7d4c69 137struct CYExpression :
5999c315 138 CYNext<CYExpression>,
cf7d4c69
JF
139 CYForInitialiser,
140 CYForInInitialiser
63b4c5a8 141{
5999c315
JF
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;
63b4c5a8
JF
145};
146
5999c315
JF
147_finline std::ostream &operator <<(std::ostream &out, const CYExpression &rhs) {
148 rhs.Output(out, false);
149 return out;
150}
151
cf7d4c69
JF
152struct CYLiteral :
153 CYExpression
63b4c5a8 154{
cf7d4c69 155};
63b4c5a8 156
cf7d4c69
JF
157struct CYString :
158 CYLiteral,
159 CYName
160{
161 const char *value_;
5999c315 162 size_t size_;
cf7d4c69 163
5999c315
JF
164 CYString(const char *value, size_t size) :
165 value_(value),
166 size_(size)
cf7d4c69
JF
167 {
168 }
169
170 CYString(const CYIdentifier *identifier) :
5999c315
JF
171 value_(identifier->Value()),
172 size_(strlen(value_))
cf7d4c69
JF
173 {
174 }
175
5999c315 176 const char *Value() const {
cf7d4c69
JF
177 return value_;
178 }
179
180 virtual const char *Name() const {
5999c315 181 return Value();
63b4c5a8 182 }
5999c315
JF
183
184 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
185};
186
cf7d4c69 187struct CYNumber :
cf7d4c69
JF
188 CYLiteral,
189 CYName
190{
5999c315
JF
191 double value_;
192
193 CYNumber(double value) :
194 value_(value)
195 {
196 }
197
198 double Value() const {
199 return value_;
cf7d4c69
JF
200 }
201
202 virtual const char *Name() const {
203 throw;
204 }
5999c315
JF
205
206 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
207};
208
209struct CYNull :
210 CYWord,
211 CYLiteral
212{
213 CYNull() :
214 CYWord("null")
215 {
216 }
5999c315
JF
217
218 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
219};
220
221struct CYThis :
222 CYWord,
223 CYExpression
224{
225 CYThis() :
226 CYWord("this")
227 {
228 }
5999c315
JF
229
230 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
231};
232
233struct CYBoolean :
234 CYLiteral
235{
5999c315
JF
236 virtual bool Value() const = 0;
237 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
238};
239
240struct CYFalse :
241 CYWord,
242 CYBoolean
243{
244 CYFalse() :
245 CYWord("false")
246 {
247 }
5999c315
JF
248
249 virtual bool Value() const {
250 return false;
251 }
cf7d4c69
JF
252};
253
254struct CYTrue :
255 CYWord,
256 CYBoolean
257{
258 CYTrue() :
259 CYWord("true")
260 {
261 }
5999c315
JF
262
263 virtual bool Value() const {
264 return true;
265 }
cf7d4c69
JF
266};
267
268struct CYVariable :
269 CYExpression
270{
271 CYIdentifier *name_;
272
273 CYVariable(CYIdentifier *name) :
274 name_(name)
275 {
276 }
5999c315
JF
277
278 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
279};
280
281struct CYPrefix :
63b4c5a8
JF
282 CYExpression
283{
284 CYExpression *rhs_;
285
cf7d4c69 286 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
287 rhs_(rhs)
288 {
289 }
5999c315
JF
290
291 virtual const char *Operator() const = 0;
292
293 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
294};
295
cf7d4c69 296struct CYInfix :
63b4c5a8
JF
297 CYExpression
298{
299 CYExpression *lhs_;
300 CYExpression *rhs_;
301
cf7d4c69 302 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
303 lhs_(lhs),
304 rhs_(rhs)
305 {
306 }
5999c315
JF
307
308 virtual const char *Operator() const = 0;
309
310 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
311};
312
cf7d4c69 313struct CYPostfix :
63b4c5a8
JF
314 CYExpression
315{
316 CYExpression *lhs_;
317
cf7d4c69 318 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
319 lhs_(lhs)
320 {
321 }
5999c315
JF
322
323 virtual const char *Operator() const = 0;
324
325 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
326};
327
cf7d4c69
JF
328struct CYAssignment :
329 CYInfix
330{
331 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
332 CYInfix(lhs, rhs)
333 {
334 }
5999c315
JF
335
336 virtual const char *Operator() const = 0;
cf7d4c69
JF
337};
338
339struct 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 }
5999c315
JF
350
351 void Output(std::ostream &out, bool send) const;
cf7d4c69
JF
352};
353
354struct CYBlank :
355 public CYWord
356{
357 CYBlank() :
358 CYWord("")
359 {
360 }
361};
362
5999c315
JF
363struct CYClause :
364 CYThing,
365 CYNext<CYClause>
366{
cf7d4c69
JF
367 CYExpression *case_;
368 CYStatement *code_;
cf7d4c69
JF
369
370 CYClause(CYExpression *_case, CYStatement *code) :
371 case_(_case),
372 code_(code)
373 {
374 }
375
5999c315 376 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
377};
378
379struct 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 }
5999c315
JF
390
391 void Output(std::ostream &out, bool raw) const;
392 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
393};
394
395struct CYDeclaration :
5999c315 396 CYThing,
cf7d4c69
JF
397 CYForInInitialiser
398{
399 CYIdentifier *identifier_;
400 CYExpression *initialiser_;
401
402 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
403 identifier_(identifier),
404 initialiser_(initialiser)
405 {
406 }
5999c315
JF
407
408 virtual void Part(std::ostream &out) const;
409 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
410};
411
412struct 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 }
5999c315
JF
424
425 virtual void Part(std::ostream &out) const;
426 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
427};
428
5999c315
JF
429struct CYParameter :
430 CYThing
431{
cf7d4c69
JF
432 CYIdentifier *name_;
433 CYParameter *next_;
434
435 CYParameter(CYIdentifier *name, CYParameter *next) :
436 name_(name),
437 next_(next)
438 {
439 }
5999c315
JF
440
441 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
442};
443
444struct 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 }
5999c315
JF
459
460 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
461};
462
463struct 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 }
5999c315
JF
476
477 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
478};
479
693d501b 480struct CYProperty {
cf7d4c69
JF
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 }
5999c315 491
5999c315 492 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
493};
494
693d501b
JF
495struct 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
5999c315
JF
508struct CYCatch :
509 CYThing
510{
cf7d4c69
JF
511 CYIdentifier *name_;
512 CYStatement *code_;
513
514 CYCatch(CYIdentifier *name, CYStatement *code) :
515 name_(name),
516 code_(code)
517 {
518 }
5999c315
JF
519
520 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
521};
522
523struct CYMessage :
63b4c5a8
JF
524 CYExpression
525{
cf7d4c69
JF
526 CYExpression *self_;
527 CYArgument *arguments_;
63b4c5a8 528
cf7d4c69
JF
529 CYMessage(CYExpression *self, CYArgument *arguments) :
530 self_(self),
531 arguments_(arguments)
63b4c5a8
JF
532 {
533 }
5999c315
JF
534
535 virtual void Output(std::ostream &out) const;
63b4c5a8
JF
536};
537
cf7d4c69
JF
538struct 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 }
5999c315
JF
549
550 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
551};
552
553struct 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 }
5999c315
JF
564
565 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
566};
567
568struct 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 }
5999c315
JF
579
580 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
581};
582
583struct 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 }
5999c315
JF
596
597 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
598};
599
600struct 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 }
5999c315
JF
611
612 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
613};
614
615struct 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 }
5999c315
JF
626
627 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
628};
629
630struct 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 }
5999c315
JF
643
644 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
645};
646
647struct CYFunction :
5999c315
JF
648 CYLambda,
649 CYSource
cf7d4c69
JF
650{
651 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
652 CYLambda(name, parameters, body)
653 {
654 }
5999c315
JF
655
656 virtual void Output(std::ostream &out) const;
657};
658
659struct 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;
cf7d4c69
JF
670};
671
672struct CYContinue :
673 CYStatement
674{
675 CYIdentifier *label_;
676
677 CYContinue(CYIdentifier *label) :
678 label_(label)
679 {
680 }
5999c315
JF
681
682 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
683};
684
685struct CYBreak :
686 CYStatement
687{
688 CYIdentifier *label_;
689
690 CYBreak(CYIdentifier *label) :
691 label_(label)
692 {
693 }
5999c315
JF
694
695 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
696};
697
698struct CYReturn :
699 CYStatement
700{
701 CYExpression *value_;
702
703 CYReturn(CYExpression *value) :
704 value_(value)
705 {
706 }
5999c315
JF
707
708 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
709};
710
711struct CYEmpty :
712 CYStatement
713{
5999c315
JF
714 virtual void Output(std::ostream &out) const;
715 virtual void Output(std::ostream &out, bool block) const;
cf7d4c69
JF
716};
717
718struct 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 }
5999c315
JF
731
732 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
733};
734
735struct CYThrow :
736 CYStatement
737{
738 CYExpression *value_;
739
740 CYThrow(CYExpression *value) :
741 value_(value)
742 {
743 }
5999c315
JF
744
745 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
746};
747
748struct 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 }
5999c315
JF
759
760 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
761};
762
763struct 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 }
5999c315
JF
774
775 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
776};
777
778struct 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 }
5999c315
JF
790
791 virtual void Output(std::ostream &out) const;
792};
793
794struct 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
809struct 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;
cf7d4c69
JF
822};
823
824#define CYPostfix_(op, name) \
825 struct CY ## name : \
826 CYPostfix \
827 { \
828 CY ## name(CYExpression *lhs) : \
829 CYPostfix(lhs) \
830 { \
831 } \
5999c315
JF
832 \
833 virtual const char *Operator() const { \
834 return op; \
835 } \
cf7d4c69
JF
836 };
837
838#define CYPrefix_(op, name) \
839 struct CY ## name : \
840 CYPrefix \
841 { \
842 CY ## name(CYExpression *rhs) : \
843 CYPrefix(rhs) \
844 { \
845 } \
5999c315
JF
846 \
847 virtual const char *Operator() const { \
848 return op; \
849 } \
cf7d4c69
JF
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 } \
5999c315
JF
860 \
861 virtual const char *Operator() const { \
862 return op; \
863 } \
cf7d4c69
JF
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 } \
5999c315
JF
874 \
875 virtual const char *Operator() const { \
876 return op; \
877 } \
cf7d4c69
JF
878 };
879
880CYPostfix_("++", PostIncrement)
881CYPostfix_("--", PostDecrement)
882
883CYPrefix_("delete", Delete)
884CYPrefix_("void", Void)
885CYPrefix_("typeof", TypeOf)
886CYPrefix_("++", PreIncrement)
887CYPrefix_("--", PreDecrement)
888CYPrefix_("-", Negate)
889CYPrefix_("~", BitwiseNot)
890CYPrefix_("!", LogicalNot)
cf7d4c69
JF
891
892CYInfix_("*", Multiply)
893CYInfix_("/", Divide)
894CYInfix_("%", Modulus)
895CYInfix_("+", Add)
896CYInfix_("-", Subtract)
897CYInfix_("<<", ShiftLeft)
898CYInfix_(">>", ShiftRightSigned)
899CYInfix_(">>>", ShiftRightUnsigned)
900CYInfix_("<", Less)
901CYInfix_(">", Greater)
902CYInfix_("<=", LessOrEqual)
903CYInfix_(">=", GreaterOrEqual)
904CYInfix_("instanceof", InstanceOf)
905CYInfix_("in", In)
906CYInfix_("==", Equal)
907CYInfix_("!=", NotEqual)
908CYInfix_("===", Identical)
909CYInfix_("!==", NotIdentical)
910CYInfix_("&", BitwiseAnd)
911CYInfix_("^", BitwiseXOr)
912CYInfix_("|", BitwiseOr)
913CYInfix_("&&", LogicalAnd)
914CYInfix_("||", LogicalOr)
915
916CYAssignment_("=", )
917CYAssignment_("*=", Multiply)
918CYAssignment_("/=", Divide)
919CYAssignment_("%=", Modulus)
920CYAssignment_("+=", Add)
921CYAssignment_("-=", Subtract)
922CYAssignment_("<<=", ShiftLeft)
923CYAssignment_(">>=", ShiftRightSigned)
924CYAssignment_(">>>=", ShiftRightUnsigned)
925CYAssignment_("&=", BitwiseAnd)
926CYAssignment_("^=", BitwiseXOr)
927CYAssignment_("|=", BitwiseOr)
928
63b4c5a8 929#endif/*CYPARSER_HPP*/