]> git.saurik.com Git - cycript.git/blame - Parser.hpp
Started setting up build environment for Cyrver, fixed Functor prototype, organized...
[cycript.git] / Parser.hpp
CommitLineData
b4aa79af
JF
1/* Cycript - Remove Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/* }}} */
39
63b4c5a8
JF
40#ifndef CYPARSER_HPP
41#define CYPARSER_HPP
42
cf7d4c69 43#include <cstdlib>
5999c315 44#include <string>
c3c20102 45#include <vector>
cf7d4c69 46
5befe15e 47#include "location.hh"
5999c315 48#include "Pooling.hpp"
924f67b2 49
5999c315
JF
50template <typename Type_>
51struct CYNext {
52 Type_ *next_;
63b4c5a8 53
5999c315
JF
54 CYNext() :
55 next_(NULL)
56 {
57 }
cf7d4c69 58
62014ea9
JF
59 CYNext(Type_ *next) :
60 next_(next)
61 {
62 }
63
5999c315 64 void SetNext(Type_ *next) {
cf7d4c69
JF
65 next_ = next;
66 }
67};
68
5999c315
JF
69struct CYThing {
70 virtual void Output(std::ostream &out) const = 0;
71};
72
73_finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) {
74 rhs.Output(out);
75 return out;
76}
77
5999c315 78struct CYSource :
b1ff2d78 79 CYNext<CYSource>
5999c315 80{
b1ff2d78 81 virtual void Show(std::ostream &out) const;
5999c315
JF
82 virtual void Output(std::ostream &out) const = 0;
83 virtual void Output(std::ostream &out, bool block) const;
84};
85
e5bc40db
JF
86struct CYPropertyName {
87 virtual void PropertyName(std::ostream &out) const = 0;
88};
89
90struct CYClassName {
91 virtual void ClassName(std::ostream &out) const = 0;
63b4c5a8
JF
92};
93
cf7d4c69 94struct CYWord :
e5bc40db
JF
95 CYThing,
96 CYPropertyName,
97 CYClassName
63b4c5a8 98{
cf7d4c69
JF
99 const char *word_;
100
101 CYWord(const char *word) :
102 word_(word)
103 {
104 }
105
5999c315 106 const char *Value() const {
cf7d4c69
JF
107 return word_;
108 }
109
5999c315 110 virtual void Output(std::ostream &out) const;
e5bc40db
JF
111
112 virtual void ClassName(std::ostream &out) const;
113 virtual void PropertyName(std::ostream &out) const;
63b4c5a8
JF
114};
115
cf7d4c69
JF
116struct CYIdentifier :
117 CYWord
63b4c5a8 118{
5999c315
JF
119 CYIdentifier(const char *word) :
120 CYWord(word)
121 {
cf7d4c69 122 }
63b4c5a8
JF
123};
124
62014ea9
JF
125struct CYLabel :
126 CYNext<CYLabel>
127{
cf7d4c69 128 CYIdentifier *identifier_;
cf7d4c69
JF
129
130 CYLabel(CYIdentifier *identifier, CYLabel *next) :
62014ea9
JF
131 CYNext<CYLabel>(next),
132 identifier_(identifier)
cf7d4c69
JF
133 {
134 }
135};
136
137struct CYStatement :
138 CYSource
63b4c5a8 139{
cf7d4c69
JF
140 CYLabel *label_;
141
142 void AddLabel(CYIdentifier *identifier) {
143 label_ = new CYLabel(identifier, label_);
144 }
145};
146
db5e2840
JF
147enum CYState {
148 CYClear,
149 CYRestricted,
150 CYNewLine
151};
152
5999c315
JF
153class CYDriver {
154 public:
155 CYPool pool_;
e7ed5354 156
db5e2840 157 CYState state_;
e7ed5354
JF
158 void *scanner_;
159
160 const char *data_;
161 size_t size_;
48e3be8a 162 FILE *file_;
e7ed5354 163
5999c315 164 std::string filename_;
e7ed5354 165
5befe15e
JF
166 struct Error {
167 cy::location location_;
168 std::string message_;
169 };
170
171 typedef std::vector<Error> Errors;
172
173 CYSource *source_;
174 Errors errors_;
5999c315
JF
175
176 private:
177 void ScannerInit();
178 void ScannerDestroy();
179
180 public:
181 CYDriver(const std::string &filename);
182 ~CYDriver();
183};
184
dea834b0
JF
185struct CYPart {
186 virtual void Part(std::ostream &out) const = 0;
187};
188
5999c315
JF
189struct CYForInitialiser :
190 CYPart
191{
cf7d4c69
JF
192};
193
5999c315
JF
194struct CYForInInitialiser :
195 CYPart
196{
63b4c5a8
JF
197};
198
b09da87b 199enum CYFlags {
478d4ed0
JF
200 CYNoFlags = 0,
201 CYNoBrace = (1 << 0),
202 CYNoFunction = (1 << 1),
203 CYNoLeader = (1 << 2),
204 CYNoTrailer = (1 << 3),
205 CYNoIn = (1 << 4),
1ef7d061 206 CYNoHyphen = (1 << 5),
b09da87b
JF
207};
208
cf7d4c69 209struct CYExpression :
5999c315 210 CYNext<CYExpression>,
cf7d4c69 211 CYForInitialiser,
e5bc40db
JF
212 CYForInInitialiser,
213 CYClassName
63b4c5a8 214{
d35a3b07 215 virtual unsigned Precedence() const = 0;
5999c315 216 virtual void Part(std::ostream &out) const;
b09da87b
JF
217 virtual void Output(std::ostream &out, CYFlags flags) const = 0;
218 void Output(std::ostream &out, unsigned precedence, CYFlags flags) const;
dea834b0 219
e5bc40db
JF
220 virtual void ClassName(std::ostream &out) const;
221
dea834b0
JF
222 virtual const char *Word() const {
223 return NULL;
224 }
63b4c5a8
JF
225};
226
b09da87b
JF
227#define CYAlphabetic(value) \
228 virtual bool Alphabetic() const { \
229 return value; \
230 }
231
d35a3b07
JF
232#define CYPrecedence(value) \
233 virtual unsigned Precedence() const { \
234 return value; \
235 }
236
237struct CYCompound :
238 CYExpression
239{
240 CYExpression *expressions_;
241
242 CYCompound(CYExpression *expressions) :
243 expressions_(expressions)
244 {
245 }
246
247 void AddPrev(CYExpression *expression) {
248 CYExpression *last(expression);
249 while (last->next_ != NULL)
250 last = last->next_;
251 last->SetNext(expressions_);
252 expressions_ = expression;
253 }
254
255 CYPrecedence(17)
256
b09da87b 257 void Output(std::ostream &out, CYFlags flags) const;
d35a3b07 258};
5999c315 259
cf7d4c69
JF
260struct CYLiteral :
261 CYExpression
63b4c5a8 262{
d35a3b07 263 CYPrecedence(0)
cf7d4c69 264};
63b4c5a8 265
478d4ed0
JF
266struct CYMagic :
267 CYExpression
268{
269 CYPrecedence(0)
270};
271
62014ea9
JF
272struct CYSelectorPart :
273 CYNext<CYSelectorPart>
e7ed5354
JF
274{
275 CYWord *name_;
276 bool value_;
e7ed5354 277
62014ea9
JF
278 CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) :
279 CYNext<CYSelectorPart>(next),
e7ed5354 280 name_(name),
62014ea9
JF
281 value_(value)
282 {
283 }
284
285 virtual void Output(std::ostream &out) const;
286};
287
288struct CYSelector :
289 CYLiteral
290{
291 CYSelectorPart *name_;
292
293 CYSelector(CYSelectorPart *name) :
294 name_(name)
e7ed5354
JF
295 {
296 }
297
dea834b0
JF
298 CYPrecedence(1)
299
b09da87b 300 virtual void Output(std::ostream &out, CYFlags flags) const;
e7ed5354
JF
301};
302
dea834b0
JF
303struct CYRange {
304 uint64_t lo_;
305 uint64_t hi_;
306
307 CYRange(uint64_t lo, uint64_t hi) :
308 lo_(lo), hi_(hi)
309 {
310 }
311
312 bool operator [](uint8_t value) const {
313 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
314 }
315
316 void operator()(uint8_t value) {
317 if (value >> 7)
318 return;
319 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
320 }
321};
322
283e7e33 323extern CYRange DigitRange_;
dea834b0
JF
324extern CYRange WordStartRange_;
325extern CYRange WordEndRange_;
326
cf7d4c69
JF
327struct CYString :
328 CYLiteral,
e5bc40db 329 CYPropertyName
cf7d4c69
JF
330{
331 const char *value_;
5999c315 332 size_t size_;
cf7d4c69 333
5999c315
JF
334 CYString(const char *value, size_t size) :
335 value_(value),
336 size_(size)
cf7d4c69
JF
337 {
338 }
339
340 CYString(const CYIdentifier *identifier) :
5999c315
JF
341 value_(identifier->Value()),
342 size_(strlen(value_))
cf7d4c69
JF
343 {
344 }
345
5999c315 346 const char *Value() const {
cf7d4c69
JF
347 return value_;
348 }
349
dea834b0
JF
350 virtual const char *Word() const {
351 if (size_ == 0 || !WordStartRange_[value_[0]])
352 return NULL;
353 for (size_t i(1); i != size_; ++i)
354 if (!WordEndRange_[value_[i]])
355 return NULL;
356 return Value();
357 }
358
b09da87b
JF
359 virtual void Output(std::ostream &out) const {
360 return Output(out, CYNoFlags);
361 }
362
363 virtual void Output(std::ostream &out, CYFlags flags) const;
e5bc40db 364 virtual void PropertyName(std::ostream &out) const;
63b4c5a8
JF
365};
366
cf7d4c69 367struct CYNumber :
cf7d4c69 368 CYLiteral,
e5bc40db 369 CYPropertyName
cf7d4c69 370{
5999c315
JF
371 double value_;
372
373 CYNumber(double value) :
374 value_(value)
375 {
376 }
377
378 double Value() const {
379 return value_;
cf7d4c69
JF
380 }
381
b09da87b
JF
382 virtual void Output(std::ostream &out) const {
383 return Output(out, CYNoFlags);
384 }
385
386 virtual void Output(std::ostream &out, CYFlags flags) const;
e5bc40db 387 virtual void PropertyName(std::ostream &out) const;
cf7d4c69
JF
388};
389
390struct CYNull :
391 CYWord,
392 CYLiteral
393{
394 CYNull() :
395 CYWord("null")
396 {
397 }
5999c315 398
b09da87b 399 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
400};
401
402struct CYThis :
403 CYWord,
478d4ed0 404 CYMagic
cf7d4c69
JF
405{
406 CYThis() :
407 CYWord("this")
408 {
409 }
5999c315 410
b09da87b 411 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
412};
413
414struct CYBoolean :
415 CYLiteral
416{
5999c315 417 virtual bool Value() const = 0;
b09da87b 418 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
419};
420
421struct CYFalse :
422 CYWord,
423 CYBoolean
424{
425 CYFalse() :
426 CYWord("false")
427 {
428 }
5999c315 429
b09da87b 430 virtual bool Value() const;
cf7d4c69
JF
431};
432
433struct CYTrue :
434 CYWord,
435 CYBoolean
436{
437 CYTrue() :
438 CYWord("true")
439 {
440 }
5999c315 441
b09da87b 442 virtual bool Value() const;
cf7d4c69
JF
443};
444
445struct CYVariable :
446 CYExpression
447{
448 CYIdentifier *name_;
449
450 CYVariable(CYIdentifier *name) :
451 name_(name)
452 {
453 }
5999c315 454
d35a3b07
JF
455 CYPrecedence(0)
456
b09da87b 457 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
458};
459
460struct CYPrefix :
63b4c5a8
JF
461 CYExpression
462{
463 CYExpression *rhs_;
464
cf7d4c69 465 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
466 rhs_(rhs)
467 {
468 }
5999c315 469
b09da87b 470 virtual bool Alphabetic() const = 0;
5999c315
JF
471 virtual const char *Operator() const = 0;
472
b09da87b 473 virtual void Output(std::ostream &out, CYFlags flags) const;
63b4c5a8
JF
474};
475
cf7d4c69 476struct CYInfix :
63b4c5a8
JF
477 CYExpression
478{
479 CYExpression *lhs_;
480 CYExpression *rhs_;
481
cf7d4c69 482 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
483 lhs_(lhs),
484 rhs_(rhs)
485 {
486 }
5999c315 487
0ff9f149
JF
488 void SetLeft(CYExpression *lhs) {
489 lhs_ = lhs;
490 }
491
b09da87b 492 virtual bool Alphabetic() const = 0;
5999c315
JF
493 virtual const char *Operator() const = 0;
494
b09da87b 495 virtual void Output(std::ostream &out, CYFlags flags) const;
63b4c5a8
JF
496};
497
cf7d4c69 498struct CYPostfix :
63b4c5a8
JF
499 CYExpression
500{
501 CYExpression *lhs_;
502
cf7d4c69 503 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
504 lhs_(lhs)
505 {
506 }
5999c315
JF
507
508 virtual const char *Operator() const = 0;
509
b09da87b 510 virtual void Output(std::ostream &out, CYFlags flags) const;
63b4c5a8
JF
511};
512
cf7d4c69 513struct CYAssignment :
d35a3b07 514 CYExpression
cf7d4c69 515{
d35a3b07
JF
516 CYExpression *lhs_;
517 CYExpression *rhs_;
518
cf7d4c69 519 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
d35a3b07
JF
520 lhs_(lhs),
521 rhs_(rhs)
cf7d4c69
JF
522 {
523 }
5999c315 524
0ff9f149
JF
525 void SetLeft(CYExpression *lhs) {
526 lhs_ = lhs;
527 }
528
5999c315 529 virtual const char *Operator() const = 0;
d35a3b07 530
b09da87b 531 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
532};
533
62014ea9
JF
534struct CYArgument :
535 CYNext<CYArgument>
536{
cf7d4c69
JF
537 CYWord *name_;
538 CYExpression *value_;
cf7d4c69
JF
539
540 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
62014ea9 541 CYNext<CYArgument>(next),
cf7d4c69 542 name_(name),
62014ea9 543 value_(value)
cf7d4c69
JF
544 {
545 }
5999c315 546
d35a3b07 547 void Output(std::ostream &out) const;
cf7d4c69
JF
548};
549
550struct CYBlank :
551 public CYWord
552{
553 CYBlank() :
554 CYWord("")
555 {
556 }
557};
558
5999c315
JF
559struct CYClause :
560 CYThing,
561 CYNext<CYClause>
562{
cf7d4c69
JF
563 CYExpression *case_;
564 CYStatement *code_;
cf7d4c69
JF
565
566 CYClause(CYExpression *_case, CYStatement *code) :
567 case_(_case),
568 code_(code)
569 {
570 }
571
5999c315 572 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
573};
574
62014ea9
JF
575struct CYElement :
576 CYNext<CYElement>
577{
cf7d4c69 578 CYExpression *value_;
cf7d4c69
JF
579
580 CYElement(CYExpression *value, CYElement *next) :
62014ea9
JF
581 CYNext<CYElement>(next),
582 value_(value)
cf7d4c69
JF
583 {
584 }
5999c315 585
5befe15e
JF
586 void Output(std::ostream &out) const;
587};
588
589struct CYArray :
590 CYLiteral
591{
592 CYElement *elements_;
593
594 CYArray(CYElement *elements) :
595 elements_(elements)
596 {
597 }
598
b09da87b 599 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
600};
601
602struct CYDeclaration :
5999c315 603 CYThing,
cf7d4c69
JF
604 CYForInInitialiser
605{
606 CYIdentifier *identifier_;
607 CYExpression *initialiser_;
608
609 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) :
610 identifier_(identifier),
611 initialiser_(initialiser)
612 {
613 }
5999c315
JF
614
615 virtual void Part(std::ostream &out) const;
616 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
617};
618
619struct CYDeclarations :
620 CYStatement,
621 CYForInitialiser
622{
623 CYDeclaration *declaration_;
624 CYDeclarations *next_;
625
626 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) :
627 declaration_(declaration),
628 next_(next)
629 {
630 }
5999c315
JF
631
632 virtual void Part(std::ostream &out) const;
633 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
634};
635
b09da87b
JF
636struct CYField :
637 CYNext<CYField>
638{
639 virtual void Output(std::ostream &out) const;
640};
641
642struct CYMessageParameter :
643 CYNext<CYMessageParameter>
644{
645 CYWord *tag_;
646 CYExpression *type_;
647 CYIdentifier *name_;
648
649 CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) :
650 tag_(tag),
651 type_(type),
652 name_(name)
653 {
654 }
655};
656
657struct CYMessage :
e5bc40db 658 CYNext<CYMessage>
b09da87b
JF
659{
660 bool instance_;
661 CYExpression *type_;
662 CYMessageParameter *parameter_;
663 CYSource *body_;
664
665 CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) :
666 instance_(instance),
667 type_(type),
668 parameter_(parameter),
669 body_(body)
670 {
671 }
672
e5bc40db 673 virtual void Output(std::ostream &out, bool replace) const;
b09da87b
JF
674};
675
676struct CYClass :
677 CYSource
678{
679 CYIdentifier *name_;
680 CYExpression *super_;
681 CYField *fields_;
682 CYMessage *messages_;
683
684 CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) :
685 name_(name),
686 super_(super),
687 fields_(fields),
688 messages_(messages)
689 {
690 }
691
692 virtual void Output(std::ostream &out) const;
693};
694
e5bc40db
JF
695struct CYCategory :
696 CYSource
697{
698 CYClassName *name_;
699 CYMessage *messages_;
700
701 CYCategory(CYClassName *name, CYMessage *messages) :
702 name_(name),
703 messages_(messages)
704 {
705 }
706
707 virtual void Output(std::ostream &out) const;
708};
709
b09da87b
JF
710struct CYFunctionParameter :
711 CYNext<CYFunctionParameter>,
5999c315
JF
712 CYThing
713{
cf7d4c69 714 CYIdentifier *name_;
cf7d4c69 715
b09da87b
JF
716 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) :
717 CYNext<CYFunctionParameter>(next),
62014ea9 718 name_(name)
cf7d4c69
JF
719 {
720 }
5999c315
JF
721
722 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
723};
724
725struct CYFor :
726 CYStatement
727{
728 CYForInitialiser *initialiser_;
729 CYExpression *test_;
730 CYExpression *increment_;
731 CYStatement *code_;
732
733 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
734 initialiser_(initialiser),
735 test_(test),
736 increment_(increment),
737 code_(code)
738 {
739 }
5999c315
JF
740
741 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
742};
743
744struct CYForIn :
745 CYStatement
746{
747 CYForInInitialiser *initialiser_;
748 CYExpression *set_;
749 CYStatement *code_;
750
751 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
752 initialiser_(initialiser),
753 set_(set),
754 code_(code)
755 {
756 }
5999c315
JF
757
758 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
759};
760
62014ea9
JF
761struct CYProperty :
762 CYNext<CYProperty>
763{
e5bc40db 764 CYPropertyName *name_;
cf7d4c69 765 CYExpression *value_;
cf7d4c69 766
e5bc40db 767 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) :
62014ea9 768 CYNext<CYProperty>(next),
cf7d4c69 769 name_(name),
62014ea9 770 value_(value)
cf7d4c69
JF
771 {
772 }
5999c315 773
5999c315 774 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
775};
776
693d501b
JF
777struct CYObject :
778 CYLiteral
779{
780 CYProperty *property_;
781
782 CYObject(CYProperty *property) :
783 property_(property)
784 {
785 }
786
b09da87b 787 void Output(std::ostream &out, CYFlags flags) const;
693d501b
JF
788};
789
5999c315
JF
790struct CYCatch :
791 CYThing
792{
cf7d4c69
JF
793 CYIdentifier *name_;
794 CYStatement *code_;
795
796 CYCatch(CYIdentifier *name, CYStatement *code) :
797 name_(name),
798 code_(code)
799 {
800 }
5999c315
JF
801
802 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
803};
804
b09da87b 805struct CYSend :
63b4c5a8
JF
806 CYExpression
807{
cf7d4c69
JF
808 CYExpression *self_;
809 CYArgument *arguments_;
63b4c5a8 810
b09da87b 811 CYSend(CYExpression *self, CYArgument *arguments) :
cf7d4c69
JF
812 self_(self),
813 arguments_(arguments)
63b4c5a8
JF
814 {
815 }
5999c315 816
d35a3b07
JF
817 CYPrecedence(0)
818
b09da87b 819 virtual void Output(std::ostream &out, CYFlags flags) const;
63b4c5a8
JF
820};
821
cf7d4c69
JF
822struct CYMember :
823 CYExpression
824{
825 CYExpression *object_;
826 CYExpression *property_;
827
828 CYMember(CYExpression *object, CYExpression *property) :
829 object_(object),
830 property_(property)
831 {
832 }
5999c315 833
9b5527f0
JF
834 void SetLeft(CYExpression *object) {
835 object_ = object;
836 }
837};
838
839struct CYDirectMember :
840 CYMember
841{
842 CYDirectMember(CYExpression *object, CYExpression *property) :
843 CYMember(object, property)
844 {
845 }
846
847 CYPrecedence(1)
848
849 virtual void Output(std::ostream &out, CYFlags flags) const;
850};
851
852struct CYIndirectMember :
853 CYMember
854{
855 CYIndirectMember(CYExpression *object, CYExpression *property) :
856 CYMember(object, property)
857 {
858 }
859
d35a3b07
JF
860 CYPrecedence(1)
861
b09da87b 862 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
863};
864
865struct CYNew :
866 CYExpression
867{
868 CYExpression *constructor_;
869 CYArgument *arguments_;
870
871 CYNew(CYExpression *constructor, CYArgument *arguments) :
872 constructor_(constructor),
873 arguments_(arguments)
874 {
875 }
5999c315 876
d35a3b07
JF
877 CYPrecedence(1)
878
b09da87b 879 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
880};
881
882struct CYCall :
883 CYExpression
884{
885 CYExpression *function_;
886 CYArgument *arguments_;
887
888 CYCall(CYExpression *function, CYArgument *arguments) :
889 function_(function),
890 arguments_(arguments)
891 {
892 }
5999c315 893
d35a3b07
JF
894 CYPrecedence(2)
895
b09da87b 896 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
897};
898
899struct CYIf :
900 CYStatement
901{
902 CYExpression *test_;
903 CYStatement *true_;
904 CYStatement *false_;
905
906 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) :
907 test_(test),
908 true_(_true),
909 false_(_false)
910 {
911 }
5999c315
JF
912
913 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
914};
915
916struct CYDoWhile :
917 CYStatement
918{
919 CYExpression *test_;
920 CYStatement *code_;
921
922 CYDoWhile(CYExpression *test, CYStatement *code) :
923 test_(test),
924 code_(code)
925 {
926 }
5999c315
JF
927
928 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
929};
930
931struct CYWhile :
932 CYStatement
933{
934 CYExpression *test_;
935 CYStatement *code_;
936
937 CYWhile(CYExpression *test, CYStatement *code) :
938 test_(test),
939 code_(code)
940 {
941 }
5999c315
JF
942
943 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
944};
945
946struct CYLambda :
947 CYExpression
948{
949 CYIdentifier *name_;
b09da87b 950 CYFunctionParameter *parameters_;
cf7d4c69
JF
951 CYSource *body_;
952
b09da87b 953 CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
cf7d4c69
JF
954 name_(name),
955 parameters_(parameters),
956 body_(body)
957 {
958 }
5999c315 959
d35a3b07
JF
960 CYPrecedence(0)
961
b09da87b 962 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
963};
964
965struct CYFunction :
5999c315
JF
966 CYLambda,
967 CYSource
cf7d4c69 968{
b09da87b 969 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) :
cf7d4c69
JF
970 CYLambda(name, parameters, body)
971 {
972 }
5999c315
JF
973
974 virtual void Output(std::ostream &out) const;
975};
976
977struct CYExpress :
978 CYStatement
979{
980 CYExpression *expression_;
981
982 CYExpress(CYExpression *expression) :
983 expression_(expression)
984 {
985 }
986
987 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
988};
989
990struct CYContinue :
991 CYStatement
992{
993 CYIdentifier *label_;
994
995 CYContinue(CYIdentifier *label) :
996 label_(label)
997 {
998 }
5999c315
JF
999
1000 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1001};
1002
1003struct CYBreak :
1004 CYStatement
1005{
1006 CYIdentifier *label_;
1007
1008 CYBreak(CYIdentifier *label) :
1009 label_(label)
1010 {
1011 }
5999c315
JF
1012
1013 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1014};
1015
1016struct CYReturn :
1017 CYStatement
1018{
1019 CYExpression *value_;
1020
1021 CYReturn(CYExpression *value) :
1022 value_(value)
1023 {
1024 }
5999c315
JF
1025
1026 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1027};
1028
1029struct CYEmpty :
1030 CYStatement
1031{
5999c315
JF
1032 virtual void Output(std::ostream &out) const;
1033 virtual void Output(std::ostream &out, bool block) const;
cf7d4c69
JF
1034};
1035
1036struct CYTry :
1037 CYStatement
1038{
1039 CYStatement *try_;
1040 CYCatch *catch_;
1041 CYStatement *finally_;
1042
1043 CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) :
1044 try_(_try),
1045 catch_(_catch),
1046 finally_(finally)
1047 {
1048 }
5999c315
JF
1049
1050 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1051};
1052
1053struct CYThrow :
1054 CYStatement
1055{
1056 CYExpression *value_;
1057
1058 CYThrow(CYExpression *value) :
1059 value_(value)
1060 {
1061 }
5999c315
JF
1062
1063 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1064};
1065
1066struct CYWith :
1067 CYStatement
1068{
1069 CYExpression *scope_;
1070 CYStatement *code_;
1071
1072 CYWith(CYExpression *scope, CYStatement *code) :
1073 scope_(scope),
1074 code_(code)
1075 {
1076 }
5999c315
JF
1077
1078 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1079};
1080
1081struct CYSwitch :
1082 CYStatement
1083{
1084 CYExpression *value_;
1085 CYClause *clauses_;
1086
1087 CYSwitch(CYExpression *value, CYClause *clauses) :
1088 value_(value),
1089 clauses_(clauses)
1090 {
1091 }
5999c315
JF
1092
1093 virtual void Output(std::ostream &out) const;
cf7d4c69
JF
1094};
1095
1096struct CYCondition :
1097 CYExpression
1098{
1099 CYExpression *test_;
1100 CYExpression *true_;
1101 CYExpression *false_;
1102
1103 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
91a416e4 1104 test_(test),
cf7d4c69
JF
1105 true_(_true),
1106 false_(_false)
1107 {
1108 }
5999c315 1109
d35a3b07
JF
1110 CYPrecedence(15)
1111
b09da87b 1112 virtual void Output(std::ostream &out, CYFlags flags) const;
5999c315
JF
1113};
1114
1115struct CYAddressOf :
1116 CYPrefix
1117{
1118 CYAddressOf(CYExpression *rhs) :
1119 CYPrefix(rhs)
1120 {
1121 }
1122
1123 virtual const char *Operator() const {
1124 return "&";
1125 }
1126
b09da87b 1127 CYAlphabetic(false)
d35a3b07
JF
1128 CYPrecedence(2)
1129
b09da87b 1130 virtual void Output(std::ostream &out, CYFlags flags) const;
5999c315
JF
1131};
1132
1133struct CYIndirect :
1134 CYPrefix
1135{
1136 CYIndirect(CYExpression *rhs) :
1137 CYPrefix(rhs)
1138 {
1139 }
1140
1141 virtual const char *Operator() const {
1142 return "*";
1143 }
1144
b09da87b 1145 CYAlphabetic(false)
d35a3b07
JF
1146 CYPrecedence(1)
1147
b09da87b 1148 virtual void Output(std::ostream &out, CYFlags flags) const;
cf7d4c69
JF
1149};
1150
1151#define CYPostfix_(op, name) \
1152 struct CY ## name : \
1153 CYPostfix \
1154 { \
1155 CY ## name(CYExpression *lhs) : \
1156 CYPostfix(lhs) \
1157 { \
1158 } \
d35a3b07
JF
1159 \
1160 CYPrecedence(3) \
5999c315
JF
1161 \
1162 virtual const char *Operator() const { \
1163 return op; \
1164 } \
cf7d4c69
JF
1165 };
1166
b09da87b 1167#define CYPrefix_(alphabetic, op, name) \
cf7d4c69
JF
1168 struct CY ## name : \
1169 CYPrefix \
1170 { \
1171 CY ## name(CYExpression *rhs) : \
1172 CYPrefix(rhs) \
1173 { \
1174 } \
d35a3b07 1175 \
b09da87b 1176 CYAlphabetic(alphabetic) \
d35a3b07 1177 CYPrecedence(4) \
5999c315
JF
1178 \
1179 virtual const char *Operator() const { \
1180 return op; \
1181 } \
cf7d4c69
JF
1182 };
1183
b09da87b 1184#define CYInfix_(alphabetic, precedence, op, name) \
cf7d4c69
JF
1185 struct CY ## name : \
1186 CYInfix \
1187 { \
1188 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1189 CYInfix(lhs, rhs) \
1190 { \
1191 } \
d35a3b07 1192 \
b09da87b 1193 CYAlphabetic(alphabetic) \
d35a3b07 1194 CYPrecedence(precedence) \
5999c315
JF
1195 \
1196 virtual const char *Operator() const { \
1197 return op; \
1198 } \
cf7d4c69
JF
1199 };
1200
1201#define CYAssignment_(op, name) \
1202 struct CY ## name ## Assign : \
1203 CYAssignment \
1204 { \
1205 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1206 CYAssignment(lhs, rhs) \
1207 { \
1208 } \
d35a3b07
JF
1209 \
1210 CYPrecedence(16) \
5999c315
JF
1211 \
1212 virtual const char *Operator() const { \
1213 return op; \
1214 } \
cf7d4c69
JF
1215 };
1216
1217CYPostfix_("++", PostIncrement)
1218CYPostfix_("--", PostDecrement)
1219
b09da87b
JF
1220CYPrefix_(true, "delete", Delete)
1221CYPrefix_(true, "void", Void)
1222CYPrefix_(true, "typeof", TypeOf)
1223CYPrefix_(false, "++", PreIncrement)
1224CYPrefix_(false, "--", PreDecrement)
1225CYPrefix_(false, "-", Negate)
1226CYPrefix_(false, "~", BitwiseNot)
1227CYPrefix_(false, "!", LogicalNot)
1228
1229CYInfix_(false, 5, "*", Multiply)
1230CYInfix_(false, 5, "/", Divide)
1231CYInfix_(false, 5, "%", Modulus)
1232CYInfix_(false, 6, "+", Add)
1233CYInfix_(false, 6, "-", Subtract)
1234CYInfix_(false, 7, "<<", ShiftLeft)
1235CYInfix_(false, 7, ">>", ShiftRightSigned)
1236CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1237CYInfix_(false, 8, "<", Less)
1238CYInfix_(false, 8, ">", Greater)
1239CYInfix_(false, 8, "<=", LessOrEqual)
1240CYInfix_(false, 8, ">=", GreaterOrEqual)
1241CYInfix_(true, 8, "instanceof", InstanceOf)
1242CYInfix_(true, 8, "in", In)
1243CYInfix_(false, 9, "==", Equal)
1244CYInfix_(false, 9, "!=", NotEqual)
1245CYInfix_(false, 9, "===", Identical)
1246CYInfix_(false, 9, "!==", NotIdentical)
1247CYInfix_(false, 10, "&", BitwiseAnd)
1248CYInfix_(false, 11, "^", BitwiseXOr)
1249CYInfix_(false, 12, "|", BitwiseOr)
1250CYInfix_(false, 13, "&&", LogicalAnd)
1251CYInfix_(false, 14, "||", LogicalOr)
cf7d4c69
JF
1252
1253CYAssignment_("=", )
1254CYAssignment_("*=", Multiply)
1255CYAssignment_("/=", Divide)
1256CYAssignment_("%=", Modulus)
1257CYAssignment_("+=", Add)
1258CYAssignment_("-=", Subtract)
1259CYAssignment_("<<=", ShiftLeft)
1260CYAssignment_(">>=", ShiftRightSigned)
1261CYAssignment_(">>>=", ShiftRightUnsigned)
1262CYAssignment_("&=", BitwiseAnd)
1263CYAssignment_("^=", BitwiseXOr)
1264CYAssignment_("|=", BitwiseOr)
1265
63b4c5a8 1266#endif/*CYPARSER_HPP*/