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