]> git.saurik.com Git - cycript.git/blame_incremental - Parser.hpp
I finally finished the AST.
[cycript.git] / Parser.hpp
... / ...
CommitLineData
1#ifndef CYPARSER_HPP
2#define CYPARSER_HPP
3
4#include <cstdlib>
5
6class CYParser {
7 public:
8 void *scanner_;
9
10 private:
11 void ScannerInit();
12 void ScannerDestroy();
13
14 public:
15 CYParser();
16 ~CYParser();
17};
18
19struct CYSource {
20 CYSource *next_;
21
22 void SetNext(CYSource *next) {
23 next_ = next;
24 }
25};
26
27struct CYName {
28 virtual const char *Name() const = 0;
29};
30
31struct CYToken {
32 virtual const char *Text() const = 0;
33};
34
35struct CYWord :
36 virtual CYToken,
37 CYName
38{
39 const char *word_;
40
41 CYWord(const char *word) :
42 word_(word)
43 {
44 }
45
46 virtual const char *Text() const {
47 return word_;
48 }
49
50 virtual const char *Name() const {
51 return Text();
52 }
53};
54
55struct CYIdentifier :
56 CYWord
57{
58 const char *word_;
59
60 virtual const char *Text() const {
61 return word_;
62 }
63};
64
65struct CYLabel {
66 CYIdentifier *identifier_;
67 CYLabel *next_;
68
69 CYLabel(CYIdentifier *identifier, CYLabel *next) :
70 identifier_(identifier),
71 next_(next)
72 {
73 }
74};
75
76struct CYStatement :
77 CYSource
78{
79 CYLabel *label_;
80
81 void AddLabel(CYIdentifier *identifier) {
82 label_ = new CYLabel(identifier, label_);
83 }
84};
85
86struct CYForInitialiser {
87};
88
89struct CYForInInitialiser {
90};
91
92struct CYExpression :
93 CYStatement,
94 CYForInitialiser,
95 CYForInInitialiser
96{
97};
98
99struct CYLiteral :
100 CYExpression
101{
102};
103
104struct CYString :
105 CYLiteral,
106 CYName
107{
108 const char *value_;
109
110 CYString(const char *value) :
111 value_(value)
112 {
113 }
114
115 CYString(const CYIdentifier *identifier) :
116 value_(identifier->Text())
117 {
118 }
119
120 const char *String() const {
121 return value_;
122 }
123
124 virtual const char *Name() const {
125 return String();
126 }
127};
128
129struct CYNumber :
130 virtual CYToken,
131 CYLiteral,
132 CYName
133{
134 double Number() const {
135 throw;
136 }
137
138 virtual const char *Name() const {
139 throw;
140 }
141};
142
143struct CYNull :
144 CYWord,
145 CYLiteral
146{
147 CYNull() :
148 CYWord("null")
149 {
150 }
151};
152
153struct CYThis :
154 CYWord,
155 CYExpression
156{
157 CYThis() :
158 CYWord("this")
159 {
160 }
161};
162
163struct CYBoolean :
164 CYLiteral
165{
166};
167
168struct CYFalse :
169 CYWord,
170 CYBoolean
171{
172 CYFalse() :
173 CYWord("false")
174 {
175 }
176};
177
178struct CYTrue :
179 CYWord,
180 CYBoolean
181{
182 CYTrue() :
183 CYWord("true")
184 {
185 }
186};
187
188struct CYVariable :
189 CYExpression
190{
191 CYIdentifier *name_;
192
193 CYVariable(CYIdentifier *name) :
194 name_(name)
195 {
196 }
197};
198
199struct CYPrefix :
200 CYExpression
201{
202 CYExpression *rhs_;
203
204 CYPrefix(CYExpression *rhs) :
205 rhs_(rhs)
206 {
207 }
208};
209
210struct CYInfix :
211 CYExpression
212{
213 CYExpression *lhs_;
214 CYExpression *rhs_;
215
216 CYInfix(CYExpression *lhs, CYExpression *rhs) :
217 lhs_(lhs),
218 rhs_(rhs)
219 {
220 }
221};
222
223struct CYPostfix :
224 CYExpression
225{
226 CYExpression *lhs_;
227
228 CYPostfix(CYExpression *lhs) :
229 lhs_(lhs)
230 {
231 }
232};
233
234struct CYAssignment :
235 CYInfix
236{
237 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
238 CYInfix(lhs, rhs)
239 {
240 }
241};
242
243struct CYArgument {
244 CYWord *name_;
245 CYExpression *value_;
246 CYArgument *next_;
247
248 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
249 name_(name),
250 value_(value),
251 next_(next)
252 {
253 }
254};
255
256struct CYBlank :
257 public CYWord
258{
259 CYBlank() :
260 CYWord("")
261 {
262 }
263};
264
265struct CYClause {
266 CYExpression *case_;
267 CYStatement *code_;
268 CYClause *next_;
269
270 CYClause(CYExpression *_case, CYStatement *code) :
271 case_(_case),
272 code_(code)
273 {
274 }
275
276 void SetNext(CYClause *next) {
277 next_ = next;
278 }
279};
280
281struct CYElement :
282 CYLiteral
283{
284 CYExpression *value_;
285 CYElement *next_;
286
287 CYElement(CYExpression *value, CYElement *next) :
288 value_(value),
289 next_(next)
290 {
291 }
292};
293
294struct CYDeclaration :
295 CYForInInitialiser
296{
297 CYIdentifier *identifier_;
298 CYExpression *initialiser_;
299
300 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
301 identifier_(identifier),
302 initialiser_(initialiser)
303 {
304 }
305};
306
307struct CYDeclarations :
308 CYStatement,
309 CYForInitialiser
310{
311 CYDeclaration *declaration_;
312 CYDeclarations *next_;
313
314 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
315 declaration_(declaration),
316 next_(next)
317 {
318 }
319};
320
321struct CYParameter {
322 CYIdentifier *name_;
323 CYParameter *next_;
324
325 CYParameter(CYIdentifier *name, CYParameter *next) :
326 name_(name),
327 next_(next)
328 {
329 }
330};
331
332struct CYFor :
333 CYStatement
334{
335 CYForInitialiser *initialiser_;
336 CYExpression *test_;
337 CYExpression *increment_;
338 CYStatement *code_;
339
340 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
341 initialiser_(initialiser),
342 test_(test),
343 increment_(increment),
344 code_(code)
345 {
346 }
347};
348
349struct CYForIn :
350 CYStatement
351{
352 CYForInInitialiser *initialiser_;
353 CYExpression *set_;
354 CYStatement *code_;
355
356 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
357 initialiser_(initialiser),
358 set_(set),
359 code_(code)
360 {
361 }
362};
363
364struct CYProperty :
365 CYLiteral
366{
367 CYName *name_;
368 CYExpression *value_;
369 CYProperty *next_;
370
371 CYProperty(CYName *name, CYExpression *value, CYProperty *next) :
372 name_(name),
373 value_(value),
374 next_(next)
375 {
376 }
377};
378
379struct CYCatch {
380 CYIdentifier *name_;
381 CYStatement *code_;
382
383 CYCatch(CYIdentifier *name, CYStatement *code) :
384 name_(name),
385 code_(code)
386 {
387 }
388};
389
390struct CYMessage :
391 CYExpression
392{
393 CYExpression *self_;
394 CYArgument *arguments_;
395
396 CYMessage(CYExpression *self, CYArgument *arguments) :
397 self_(self),
398 arguments_(arguments)
399 {
400 }
401};
402
403struct CYMember :
404 CYExpression
405{
406 CYExpression *object_;
407 CYExpression *property_;
408
409 CYMember(CYExpression *object, CYExpression *property) :
410 object_(object),
411 property_(property)
412 {
413 }
414};
415
416struct CYNew :
417 CYExpression
418{
419 CYExpression *constructor_;
420 CYArgument *arguments_;
421
422 CYNew(CYExpression *constructor, CYArgument *arguments) :
423 constructor_(constructor),
424 arguments_(arguments)
425 {
426 }
427};
428
429struct CYCall :
430 CYExpression
431{
432 CYExpression *function_;
433 CYArgument *arguments_;
434
435 CYCall(CYExpression *function, CYArgument *arguments) :
436 function_(function),
437 arguments_(arguments)
438 {
439 }
440};
441
442struct CYIf :
443 CYStatement
444{
445 CYExpression *test_;
446 CYStatement *true_;
447 CYStatement *false_;
448
449 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
450 test_(test),
451 true_(_true),
452 false_(_false)
453 {
454 }
455};
456
457struct CYDoWhile :
458 CYStatement
459{
460 CYExpression *test_;
461 CYStatement *code_;
462
463 CYDoWhile(CYExpression *test, CYStatement *code) :
464 test_(test),
465 code_(code)
466 {
467 }
468};
469
470struct CYWhile :
471 CYStatement
472{
473 CYExpression *test_;
474 CYStatement *code_;
475
476 CYWhile(CYExpression *test, CYStatement *code) :
477 test_(test),
478 code_(code)
479 {
480 }
481};
482
483struct CYLambda :
484 CYExpression
485{
486 CYIdentifier *name_;
487 CYParameter *parameters_;
488 CYSource *body_;
489
490 CYLambda(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
491 name_(name),
492 parameters_(parameters),
493 body_(body)
494 {
495 }
496};
497
498struct CYFunction :
499 CYLambda
500{
501 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
502 CYLambda(name, parameters, body)
503 {
504 }
505};
506
507struct CYContinue :
508 CYStatement
509{
510 CYIdentifier *label_;
511
512 CYContinue(CYIdentifier *label) :
513 label_(label)
514 {
515 }
516};
517
518struct CYBreak :
519 CYStatement
520{
521 CYIdentifier *label_;
522
523 CYBreak(CYIdentifier *label) :
524 label_(label)
525 {
526 }
527};
528
529struct CYReturn :
530 CYStatement
531{
532 CYExpression *value_;
533
534 CYReturn(CYExpression *value) :
535 value_(value)
536 {
537 }
538};
539
540struct CYEmpty :
541 CYStatement
542{
543};
544
545struct CYTry :
546 CYStatement
547{
548 CYStatement *try_;
549 CYCatch *catch_;
550 CYStatement *finally_;
551
552 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
553 try_(_try),
554 catch_(_catch),
555 finally_(finally)
556 {
557 }
558};
559
560struct CYThrow :
561 CYStatement
562{
563 CYExpression *value_;
564
565 CYThrow(CYExpression *value) :
566 value_(value)
567 {
568 }
569};
570
571struct CYWith :
572 CYStatement
573{
574 CYExpression *scope_;
575 CYStatement *code_;
576
577 CYWith(CYExpression *scope, CYStatement *code) :
578 scope_(scope),
579 code_(code)
580 {
581 }
582};
583
584struct CYSwitch :
585 CYStatement
586{
587 CYExpression *value_;
588 CYClause *clauses_;
589
590 CYSwitch(CYExpression *value, CYClause *clauses) :
591 value_(value),
592 clauses_(clauses)
593 {
594 }
595};
596
597struct CYCondition :
598 CYExpression
599{
600 CYExpression *test_;
601 CYExpression *true_;
602 CYExpression *false_;
603
604 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
605 true_(_true),
606 false_(_false)
607 {
608 }
609};
610
611#define CYPostfix_(op, name) \
612 struct CY ## name : \
613 CYPostfix \
614 { \
615 CY ## name(CYExpression *lhs) : \
616 CYPostfix(lhs) \
617 { \
618 } \
619 };
620
621#define CYPrefix_(op, name) \
622 struct CY ## name : \
623 CYPrefix \
624 { \
625 CY ## name(CYExpression *rhs) : \
626 CYPrefix(rhs) \
627 { \
628 } \
629 };
630
631#define CYInfix_(op, name) \
632 struct CY ## name : \
633 CYInfix \
634 { \
635 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
636 CYInfix(lhs, rhs) \
637 { \
638 } \
639 };
640
641#define CYAssignment_(op, name) \
642 struct CY ## name ## Assign : \
643 CYAssignment \
644 { \
645 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
646 CYAssignment(lhs, rhs) \
647 { \
648 } \
649 };
650
651CYPostfix_("++", PostIncrement)
652CYPostfix_("--", PostDecrement)
653
654CYPrefix_("delete", Delete)
655CYPrefix_("void", Void)
656CYPrefix_("typeof", TypeOf)
657CYPrefix_("++", PreIncrement)
658CYPrefix_("--", PreDecrement)
659CYPrefix_("-", Negate)
660CYPrefix_("~", BitwiseNot)
661CYPrefix_("!", LogicalNot)
662CYPrefix_("*", Indirect)
663CYPrefix_("&", AddressOf)
664
665CYInfix_("*", Multiply)
666CYInfix_("/", Divide)
667CYInfix_("%", Modulus)
668CYInfix_("+", Add)
669CYInfix_("-", Subtract)
670CYInfix_("<<", ShiftLeft)
671CYInfix_(">>", ShiftRightSigned)
672CYInfix_(">>>", ShiftRightUnsigned)
673CYInfix_("<", Less)
674CYInfix_(">", Greater)
675CYInfix_("<=", LessOrEqual)
676CYInfix_(">=", GreaterOrEqual)
677CYInfix_("instanceof", InstanceOf)
678CYInfix_("in", In)
679CYInfix_("==", Equal)
680CYInfix_("!=", NotEqual)
681CYInfix_("===", Identical)
682CYInfix_("!==", NotIdentical)
683CYInfix_("&", BitwiseAnd)
684CYInfix_("^", BitwiseXOr)
685CYInfix_("|", BitwiseOr)
686CYInfix_("&&", LogicalAnd)
687CYInfix_("||", LogicalOr)
688
689CYAssignment_("=", )
690CYAssignment_("*=", Multiply)
691CYAssignment_("/=", Divide)
692CYAssignment_("%=", Modulus)
693CYAssignment_("+=", Add)
694CYAssignment_("-=", Subtract)
695CYAssignment_("<<=", ShiftLeft)
696CYAssignment_(">>=", ShiftRightSigned)
697CYAssignment_(">>>=", ShiftRightUnsigned)
698CYAssignment_("&=", BitwiseAnd)
699CYAssignment_("^=", BitwiseXOr)
700CYAssignment_("|=", BitwiseOr)
701
702#endif/*CYPARSER_HPP*/