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