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