]> git.saurik.com Git - cycript.git/blob - Parser.hpp
3fb903aa43cc3ddb3a8f4a2e988cb44d795bc7f9
[cycript.git] / Parser.hpp
1 #ifndef CYPARSER_HPP
2 #define CYPARSER_HPP
3
4 #include <cstdlib>
5
6 class CYParser {
7 public:
8 void *scanner_;
9
10 private:
11 void ScannerInit();
12 void ScannerDestroy();
13
14 public:
15 CYParser();
16 ~CYParser();
17 };
18
19 struct CYSource {
20 CYSource *next_;
21
22 void SetNext(CYSource *next) {
23 next_ = next;
24 }
25 };
26
27 struct CYName {
28 virtual const char *Name() const = 0;
29 };
30
31 struct CYToken {
32 virtual const char *Text() const = 0;
33 };
34
35 struct 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
55 struct CYIdentifier :
56 CYWord
57 {
58 const char *word_;
59
60 virtual const char *Text() const {
61 return word_;
62 }
63 };
64
65 struct CYLabel {
66 CYIdentifier *identifier_;
67 CYLabel *next_;
68
69 CYLabel(CYIdentifier *identifier, CYLabel *next) :
70 identifier_(identifier),
71 next_(next)
72 {
73 }
74 };
75
76 struct CYStatement :
77 CYSource
78 {
79 CYLabel *label_;
80
81 void AddLabel(CYIdentifier *identifier) {
82 label_ = new CYLabel(identifier, label_);
83 }
84 };
85
86 struct CYForInitialiser {
87 };
88
89 struct CYForInInitialiser {
90 };
91
92 struct CYExpression :
93 CYStatement,
94 CYForInitialiser,
95 CYForInInitialiser
96 {
97 };
98
99 struct CYLiteral :
100 CYExpression
101 {
102 };
103
104 struct 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
129 struct 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
143 struct CYNull :
144 CYWord,
145 CYLiteral
146 {
147 CYNull() :
148 CYWord("null")
149 {
150 }
151 };
152
153 struct CYThis :
154 CYWord,
155 CYExpression
156 {
157 CYThis() :
158 CYWord("this")
159 {
160 }
161 };
162
163 struct CYBoolean :
164 CYLiteral
165 {
166 };
167
168 struct CYFalse :
169 CYWord,
170 CYBoolean
171 {
172 CYFalse() :
173 CYWord("false")
174 {
175 }
176 };
177
178 struct CYTrue :
179 CYWord,
180 CYBoolean
181 {
182 CYTrue() :
183 CYWord("true")
184 {
185 }
186 };
187
188 struct CYVariable :
189 CYExpression
190 {
191 CYIdentifier *name_;
192
193 CYVariable(CYIdentifier *name) :
194 name_(name)
195 {
196 }
197 };
198
199 struct CYPrefix :
200 CYExpression
201 {
202 CYExpression *rhs_;
203
204 CYPrefix(CYExpression *rhs) :
205 rhs_(rhs)
206 {
207 }
208 };
209
210 struct 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
223 struct CYPostfix :
224 CYExpression
225 {
226 CYExpression *lhs_;
227
228 CYPostfix(CYExpression *lhs) :
229 lhs_(lhs)
230 {
231 }
232 };
233
234 struct CYAssignment :
235 CYInfix
236 {
237 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
238 CYInfix(lhs, rhs)
239 {
240 }
241 };
242
243 struct 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
256 struct CYBlank :
257 public CYWord
258 {
259 CYBlank() :
260 CYWord("")
261 {
262 }
263 };
264
265 struct 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
281 struct 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
294 struct 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
307 struct 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
321 struct CYParameter {
322 CYIdentifier *name_;
323 CYParameter *next_;
324
325 CYParameter(CYIdentifier *name, CYParameter *next) :
326 name_(name),
327 next_(next)
328 {
329 }
330 };
331
332 struct 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
349 struct 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
364 struct 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
379 struct CYCatch {
380 CYIdentifier *name_;
381 CYStatement *code_;
382
383 CYCatch(CYIdentifier *name, CYStatement *code) :
384 name_(name),
385 code_(code)
386 {
387 }
388 };
389
390 struct 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
403 struct 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
416 struct 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
429 struct 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
442 struct 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
457 struct 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
470 struct 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
483 struct 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
498 struct CYFunction :
499 CYLambda
500 {
501 CYFunction(CYIdentifier *name, CYParameter *parameters, CYSource *body) :
502 CYLambda(name, parameters, body)
503 {
504 }
505 };
506
507 struct CYContinue :
508 CYStatement
509 {
510 CYIdentifier *label_;
511
512 CYContinue(CYIdentifier *label) :
513 label_(label)
514 {
515 }
516 };
517
518 struct CYBreak :
519 CYStatement
520 {
521 CYIdentifier *label_;
522
523 CYBreak(CYIdentifier *label) :
524 label_(label)
525 {
526 }
527 };
528
529 struct CYReturn :
530 CYStatement
531 {
532 CYExpression *value_;
533
534 CYReturn(CYExpression *value) :
535 value_(value)
536 {
537 }
538 };
539
540 struct CYEmpty :
541 CYStatement
542 {
543 };
544
545 struct 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
560 struct CYThrow :
561 CYStatement
562 {
563 CYExpression *value_;
564
565 CYThrow(CYExpression *value) :
566 value_(value)
567 {
568 }
569 };
570
571 struct 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
584 struct 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
597 struct 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
651 CYPostfix_("++", PostIncrement)
652 CYPostfix_("--", PostDecrement)
653
654 CYPrefix_("delete", Delete)
655 CYPrefix_("void", Void)
656 CYPrefix_("typeof", TypeOf)
657 CYPrefix_("++", PreIncrement)
658 CYPrefix_("--", PreDecrement)
659 CYPrefix_("-", Negate)
660 CYPrefix_("~", BitwiseNot)
661 CYPrefix_("!", LogicalNot)
662 CYPrefix_("*", Indirect)
663 CYPrefix_("&", AddressOf)
664
665 CYInfix_("*", Multiply)
666 CYInfix_("/", Divide)
667 CYInfix_("%", Modulus)
668 CYInfix_("+", Add)
669 CYInfix_("-", Subtract)
670 CYInfix_("<<", ShiftLeft)
671 CYInfix_(">>", ShiftRightSigned)
672 CYInfix_(">>>", ShiftRightUnsigned)
673 CYInfix_("<", Less)
674 CYInfix_(">", Greater)
675 CYInfix_("<=", LessOrEqual)
676 CYInfix_(">=", GreaterOrEqual)
677 CYInfix_("instanceof", InstanceOf)
678 CYInfix_("in", In)
679 CYInfix_("==", Equal)
680 CYInfix_("!=", NotEqual)
681 CYInfix_("===", Identical)
682 CYInfix_("!==", NotIdentical)
683 CYInfix_("&", BitwiseAnd)
684 CYInfix_("^", BitwiseXOr)
685 CYInfix_("|", BitwiseOr)
686 CYInfix_("&&", LogicalAnd)
687 CYInfix_("||", LogicalOr)
688
689 CYAssignment_("=", )
690 CYAssignment_("*=", Multiply)
691 CYAssignment_("/=", Divide)
692 CYAssignment_("%=", Modulus)
693 CYAssignment_("+=", Add)
694 CYAssignment_("-=", Subtract)
695 CYAssignment_("<<=", ShiftLeft)
696 CYAssignment_(">>=", ShiftRightSigned)
697 CYAssignment_(">>>=", ShiftRightUnsigned)
698 CYAssignment_("&=", BitwiseAnd)
699 CYAssignment_("^=", BitwiseXOr)
700 CYAssignment_("|=", BitwiseOr)
701
702 #endif/*CYPARSER_HPP*/