]> git.saurik.com Git - cycript.git/blame - Parser.hpp
Fix on Linux?!
[cycript.git] / Parser.hpp
CommitLineData
e91fbe93 1/* Cycript - Inlining/Optimizing JavaScript Compiler
b4aa79af
JF
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
4de0686f
JF
43// XXX: wtf is this here?!
44#define CYPA 16
45
46#include <iostream>
47
5999c315 48#include <string>
c3c20102 49#include <vector>
029bc65b
JF
50#include <map>
51#include <set>
cf7d4c69 52
4de0686f
JF
53#include <cstdlib>
54
5befe15e 55#include "location.hh"
5999c315 56#include "Pooling.hpp"
029bc65b
JF
57#include "Options.hpp"
58
59class CYContext;
924f67b2 60
5999c315
JF
61template <typename Type_>
62struct CYNext {
63 Type_ *next_;
63b4c5a8 64
5999c315
JF
65 CYNext() :
66 next_(NULL)
67 {
68 }
cf7d4c69 69
62014ea9
JF
70 CYNext(Type_ *next) :
71 next_(next)
72 {
73 }
74
5999c315 75 void SetNext(Type_ *next) {
cf7d4c69
JF
76 next_ = next;
77 }
78};
79
5999c315 80struct CYThing {
7c6c5b0a
JF
81 virtual ~CYThing() {
82 }
83
652ec1ba 84 virtual void Output(struct CYOutput &out) const = 0;
5999c315
JF
85};
86
652ec1ba
JF
87struct CYOutput {
88 std::ostream &out_;
029bc65b 89 CYOptions &options_;
11c1cc16
JF
90 bool pretty_;
91 unsigned indent_;
320ce753 92 bool right_;
652ec1ba 93
96a7e5c2
JF
94 enum {
95 NoMode,
96 NoLetter,
c0bc320e 97 NoPlus,
96a7e5c2
JF
98 NoHyphen,
99 Terminated
100 } mode_;
101
029bc65b 102 CYOutput(std::ostream &out, CYOptions &options) :
11c1cc16 103 out_(out),
029bc65b 104 options_(options),
11c1cc16 105 pretty_(false),
96a7e5c2 106 indent_(0),
320ce753 107 right_(false),
96a7e5c2 108 mode_(NoMode)
652ec1ba
JF
109 {
110 }
111
96a7e5c2 112 void Check(char value);
1fdca2fa 113 void Terminate();
652ec1ba 114
96a7e5c2
JF
115 CYOutput &operator <<(char rhs);
116 CYOutput &operator <<(const char *rhs);
117
118 _finline CYOutput &operator <<(const CYThing *rhs) {
119 if (rhs != NULL)
120 rhs->Output(*this);
652ec1ba
JF
121 return *this;
122 }
123
124 _finline CYOutput &operator <<(const CYThing &rhs) {
125 rhs.Output(*this);
126 return *this;
127 }
128};
5999c315 129
e5bc40db 130struct CYPropertyName {
652ec1ba 131 virtual void PropertyName(CYOutput &out) const = 0;
7c6c5b0a
JF
132
133 virtual ~CYPropertyName() {
134 }
e5bc40db
JF
135};
136
3b52fd1a
JF
137struct CYExpression;
138
139enum CYNeeded {
140 CYNever = -1,
141 CYSometimes = 0,
142 CYAlways = 1,
143};
144
145enum CYFlags {
146 CYNoFlags = 0,
147 CYNoBrace = (1 << 0),
148 CYNoFunction = (1 << 1),
149 CYNoIn = (1 << 2),
150 CYNoCall = (1 << 3),
151 CYNoRightHand = (1 << 4),
152 CYNoDangle = (1 << 5),
9561f209 153 CYNoInteger = (1 << 6),
3b52fd1a
JF
154 CYNoBF = (CYNoBrace | CYNoFunction),
155};
156
3b52fd1a
JF
157struct CYStatement :
158 CYNext<CYStatement>
159{
7c6c5b0a
JF
160 virtual ~CYStatement() {
161 }
162
3b52fd1a
JF
163 void Single(CYOutput &out, CYFlags flags) const;
164 void Multiple(CYOutput &out, CYFlags flags = CYNoFlags) const;
165
166 CYStatement *ReplaceAll(CYContext &context);
029bc65b 167 virtual CYStatement *Collapse(CYContext &context);
3b52fd1a
JF
168
169 virtual CYStatement *Replace(CYContext &context) = 0;
170
171 private:
172 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
173};
174
175struct CYStatements {
176 CYStatement *first_;
177 CYStatement *last_;
178
179 CYStatements() :
180 first_(NULL),
181 last_(NULL)
182 {
183 }
184
185 operator CYStatement *() const {
186 return first_;
187 }
188
189 CYStatements &operator ->*(CYStatement *next) {
190 if (next != NULL)
191 if (first_ == NULL) {
192 first_ = next;
193 last_ = next;
194 } else for (;; last_ = last_->next_)
195 if (last_->next_ == NULL) {
196 last_->next_ = next;
197 last_ = next;
198 break;
199 }
200 return *this;
201 }
202};
203
e5bc40db 204struct CYClassName {
7c6c5b0a
JF
205 virtual ~CYClassName() {
206 }
207
3b52fd1a 208 virtual CYExpression *ClassName(CYContext &context, bool object) = 0;
652ec1ba 209 virtual void ClassName(CYOutput &out, bool object) const = 0;
63b4c5a8
JF
210};
211
cf7d4c69 212struct CYWord :
e5bc40db
JF
213 CYThing,
214 CYPropertyName,
215 CYClassName
63b4c5a8 216{
cf7d4c69
JF
217 const char *word_;
218
219 CYWord(const char *word) :
220 word_(word)
221 {
222 }
223
029bc65b
JF
224 void Set(const char *value) {
225 word_ = value;
cf7d4c69
JF
226 }
227
029bc65b 228 virtual const char *Word() const;
652ec1ba 229 virtual void Output(CYOutput &out) const;
e5bc40db 230
3b52fd1a 231 virtual CYExpression *ClassName(CYContext &context, bool object);
652ec1ba
JF
232 virtual void ClassName(CYOutput &out, bool object) const;
233 virtual void PropertyName(CYOutput &out) const;
63b4c5a8
JF
234};
235
652ec1ba 236_finline std::ostream &operator <<(std::ostream &lhs, const CYWord &rhs) {
029bc65b
JF
237 lhs << &rhs << '=';
238 return lhs << rhs.Word();
652ec1ba
JF
239}
240
cf7d4c69 241struct CYIdentifier :
14ec9e00 242 CYNext<CYIdentifier>,
cf7d4c69 243 CYWord
63b4c5a8 244{
029bc65b
JF
245 CYIdentifier *replace_;
246
5999c315 247 CYIdentifier(const char *word) :
029bc65b
JF
248 CYWord(word),
249 replace_(NULL)
5999c315 250 {
cf7d4c69 251 }
029bc65b
JF
252
253 virtual const char *Word() const;
254 CYIdentifier *Replace(CYContext &context);
63b4c5a8
JF
255};
256
320ce753
JF
257struct CYComment :
258 CYStatement
259{
260 const char *value_;
261
262 CYComment(const char *value) :
263 value_(value)
264 {
265 }
266
267 virtual CYStatement *Replace(CYContext &context);
268 virtual void Output(CYOutput &out, CYFlags flags) const;
269};
270
62014ea9 271struct CYLabel :
3b52fd1a 272 CYStatement
62014ea9 273{
9e562cfc 274 CYIdentifier *name_;
3b52fd1a 275 CYStatement *statement_;
cf7d4c69 276
3b52fd1a
JF
277 CYLabel(CYIdentifier *name, CYStatement *statement) :
278 name_(name),
279 statement_(statement)
cf7d4c69
JF
280 {
281 }
fb98ac0c 282
3b52fd1a
JF
283 virtual CYStatement *Replace(CYContext &context);
284 virtual void Output(CYOutput &out, CYFlags flags) const;
fb98ac0c
JF
285};
286
14ec9e00 287struct CYCStringLess :
029bc65b
JF
288 std::binary_function<const char *, const char *, bool>
289{
290 _finline bool operator ()(const char *lhs, const char *rhs) const {
291 return strcmp(lhs, rhs) < 0;
292 }
293};
294
295struct CYIdentifierValueLess :
296 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
297{
298 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
14ec9e00 299 return CYCStringLess()(lhs->Word(), rhs->Word());
029bc65b
JF
300 }
301};
302
303enum CYIdentifierFlags {
304 CYIdentifierArgument,
14ec9e00
JF
305 CYIdentifierVariable,
306 CYIdentifierOther,
307 CYIdentifierMagic,
029bc65b
JF
308};
309
14ec9e00 310typedef std::set<const char *, CYCStringLess> CYCStringSet;
029bc65b
JF
311typedef std::set<CYIdentifier *, CYIdentifierValueLess> CYIdentifierValueSet;
312typedef std::set<CYIdentifier *> CYIdentifierAddressSet;
14ec9e00 313typedef std::vector<CYIdentifier *> CYIdentifierAddressVector;
029bc65b
JF
314typedef std::map<CYIdentifier *, CYIdentifierFlags> CYIdentifierAddressFlagsMap;
315
316struct CYScope {
317 CYScope *parent_;
029bc65b 318 CYIdentifierAddressFlagsMap internal_;
a86e34d0
JF
319
320 CYIdentifierValueSet identifiers_;
14ec9e00 321 size_t offset_;
029bc65b
JF
322
323 CYScope() :
324 parent_(NULL),
325 offset_(0)
326 {
327 }
328
a86e34d0
JF
329 void Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags);
330 virtual CYIdentifier *Lookup(CYContext &context, CYIdentifier *identifier);
331 void Merge(CYContext &context, CYIdentifierAddressVector &external);
029bc65b
JF
332 void Scope(CYContext &context, CYStatement *&statements);
333};
334
3b52fd1a 335struct CYProgram :
029bc65b 336 CYScope,
3b52fd1a 337 CYThing
63b4c5a8 338{
3b52fd1a 339 CYStatement *statements_;
14ec9e00
JF
340 CYIdentifierAddressVector rename_;
341 CYCStringSet external_;
9e562cfc 342
3b52fd1a
JF
343 CYProgram(CYStatement *statements) :
344 statements_(statements)
9e562cfc
JF
345 {
346 }
cf7d4c69 347
3b52fd1a 348 virtual void Replace(CYContext &context);
3b52fd1a 349 virtual void Output(CYOutput &out) const;
9e562cfc
JF
350};
351
352struct CYBlock :
3b52fd1a
JF
353 CYStatement,
354 CYThing
9e562cfc
JF
355{
356 CYStatement *statements_;
029bc65b 357 CYScope *scope_;
9e562cfc 358
029bc65b
JF
359 CYBlock(CYStatement *statements, CYScope *scope = NULL) :
360 statements_(statements),
361 scope_(scope)
9e562cfc 362 {
cf7d4c69 363 }
9e562cfc 364
4644480a
JF
365 operator CYStatement *() const {
366 return statements_;
367 }
368
029bc65b
JF
369 void AddPrev(CYStatement *statement) {
370 CYStatement *last(statement);
371 while (last->next_ != NULL)
372 last = last->next_;
373 last->SetNext(statements_);
374 statements_ = statement;
375 }
376
3b52fd1a
JF
377 virtual CYStatement *Replace(CYContext &context);
378
379 virtual void Output(CYOutput &out) const;
fb98ac0c 380 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
381};
382
db5e2840
JF
383enum CYState {
384 CYClear,
385 CYRestricted,
386 CYNewLine
387};
388
5999c315
JF
389class CYDriver {
390 public:
391 CYPool pool_;
e7ed5354 392
db5e2840 393 CYState state_;
e7ed5354
JF
394 void *scanner_;
395
396 const char *data_;
397 size_t size_;
48e3be8a 398 FILE *file_;
e7ed5354 399
b10bd496
JF
400 bool strict_;
401
63cd45c9 402 enum Condition {
697d6fd2 403 RegExpCondition,
691e4717
JF
404 XMLContentCondition,
405 XMLTagCondition,
63cd45c9
JF
406 };
407
5999c315 408 std::string filename_;
e7ed5354 409
5befe15e 410 struct Error {
b10bd496 411 bool warning_;
5befe15e
JF
412 cy::location location_;
413 std::string message_;
414 };
415
416 typedef std::vector<Error> Errors;
417
3b52fd1a 418 CYProgram *program_;
5befe15e 419 Errors errors_;
5999c315
JF
420
421 private:
422 void ScannerInit();
423 void ScannerDestroy();
424
425 public:
426 CYDriver(const std::string &filename);
427 ~CYDriver();
63cd45c9 428
691e4717
JF
429 Condition GetCondition();
430 void SetCondition(Condition condition);
431
432 void PushCondition(Condition condition);
433 void PopCondition();
b10bd496
JF
434
435 void Warning(const cy::location &location, const char *message);
5999c315
JF
436};
437
cac61857 438struct CYForInitialiser {
7c6c5b0a
JF
439 virtual ~CYForInitialiser() {
440 }
441
652ec1ba 442 virtual void For(CYOutput &out) const = 0;
029bc65b 443 virtual CYExpression *Replace(CYContext &context) = 0;
dea834b0
JF
444};
445
cac61857 446struct CYForInInitialiser {
7c6c5b0a
JF
447 virtual ~CYForInInitialiser() {
448 }
449
652ec1ba 450 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
75b0a457 451 virtual const char *ForEachIn() const = 0;
3b52fd1a 452 virtual CYExpression *ForEachIn(CYContext &out) = 0;
029bc65b 453 virtual CYExpression *Replace(CYContext &context) = 0;
b09da87b
JF
454};
455
4644480a
JF
456struct CYNumber;
457struct CYString;
458
cf7d4c69 459struct CYExpression :
5999c315 460 CYNext<CYExpression>,
cf7d4c69 461 CYForInitialiser,
e5bc40db 462 CYForInInitialiser,
96a7e5c2
JF
463 CYClassName,
464 CYThing
63b4c5a8 465{
d35a3b07 466 virtual unsigned Precedence() const = 0;
75b0a457 467
fb98ac0c
JF
468 virtual bool RightHand() const {
469 return true;
470 }
471
652ec1ba
JF
472 virtual void For(CYOutput &out) const;
473 virtual void ForIn(CYOutput &out, CYFlags flags) const;
75b0a457 474
cac61857 475 virtual const char *ForEachIn() const;
3b52fd1a 476 virtual CYExpression *ForEachIn(CYContext &out);
75b0a457 477
96a7e5c2 478 virtual void Output(CYOutput &out) const;
652ec1ba
JF
479 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
480 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
dea834b0 481
3b52fd1a 482 virtual CYExpression *ClassName(CYContext &context, bool object);
652ec1ba 483 virtual void ClassName(CYOutput &out, bool object) const;
e5bc40db 484
3b52fd1a
JF
485 CYExpression *ReplaceAll(CYContext &context);
486
487 virtual CYExpression *Replace(CYContext &context) = 0;
488
4644480a
JF
489 virtual CYExpression *Primitive(CYContext &context) {
490 return this;
491 }
492
493 virtual CYNumber *Number(CYContext &context) {
494 return NULL;
495 }
496
497 virtual CYString *String(CYContext &context) {
498 return NULL;
499 }
500
dea834b0
JF
501 virtual const char *Word() const {
502 return NULL;
503 }
63b4c5a8
JF
504};
505
b09da87b
JF
506#define CYAlphabetic(value) \
507 virtual bool Alphabetic() const { \
508 return value; \
509 }
510
d35a3b07
JF
511#define CYPrecedence(value) \
512 virtual unsigned Precedence() const { \
513 return value; \
514 }
515
fb98ac0c
JF
516#define CYRightHand(value) \
517 virtual bool RightHand() const { \
518 return value; \
519 }
520
d35a3b07
JF
521struct CYCompound :
522 CYExpression
523{
524 CYExpression *expressions_;
525
029bc65b 526 CYCompound(CYExpression *expressions = NULL) :
d35a3b07
JF
527 expressions_(expressions)
528 {
529 }
530
531 void AddPrev(CYExpression *expression) {
532 CYExpression *last(expression);
533 while (last->next_ != NULL)
534 last = last->next_;
535 last->SetNext(expressions_);
536 expressions_ = expression;
537 }
538
539 CYPrecedence(17)
540
3b52fd1a 541 virtual CYExpression *Replace(CYContext &context);
652ec1ba 542 void Output(CYOutput &out, CYFlags flags) const;
d35a3b07 543};
5999c315 544
3b52fd1a
JF
545struct CYFunctionParameter :
546 CYNext<CYFunctionParameter>,
547 CYThing
548{
549 CYIdentifier *name_;
550
551 CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next = NULL) :
552 CYNext<CYFunctionParameter>(next),
553 name_(name)
554 {
555 }
556
029bc65b 557 void Replace(CYContext &context);
3b52fd1a
JF
558 virtual void Output(CYOutput &out) const;
559};
560
75b0a457 561struct CYComprehension :
96a7e5c2
JF
562 CYNext<CYComprehension>,
563 CYThing
75b0a457 564{
75b0a457
JF
565 virtual const char *Name() const = 0;
566
3b52fd1a
JF
567 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
568 CYFunctionParameter *Parameters(CYContext &context) const;
569 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 570 virtual void Output(CYOutput &out) const = 0;
75b0a457
JF
571};
572
573struct CYForInComprehension :
574 CYComprehension
575{
576 CYIdentifier *name_;
577 CYExpression *set_;
578
579 CYForInComprehension(CYIdentifier *name, CYExpression *set) :
580 name_(name),
581 set_(set)
582 {
583 }
584
585 virtual const char *Name() const {
029bc65b 586 return name_->Word();
75b0a457
JF
587 }
588
3b52fd1a
JF
589 virtual CYFunctionParameter *Parameter(CYContext &context) const;
590 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 591 virtual void Output(CYOutput &out) const;
75b0a457
JF
592};
593
594struct CYForEachInComprehension :
595 CYComprehension
596{
597 CYIdentifier *name_;
598 CYExpression *set_;
599
600 CYForEachInComprehension(CYIdentifier *name, CYExpression *set) :
601 name_(name),
602 set_(set)
603 {
604 }
605
606 virtual const char *Name() const {
029bc65b 607 return name_->Word();
75b0a457
JF
608 }
609
3b52fd1a
JF
610 virtual CYFunctionParameter *Parameter(CYContext &context) const;
611 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 612 virtual void Output(CYOutput &out) const;
75b0a457
JF
613};
614
615struct CYIfComprehension :
616 CYComprehension
617{
618 CYExpression *test_;
619
620 CYIfComprehension(CYExpression *test) :
621 test_(test)
622 {
623 }
624
625 virtual const char *Name() const {
626 return NULL;
627 }
628
3b52fd1a
JF
629 virtual CYFunctionParameter *Parameter(CYContext &context) const;
630 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 631 virtual void Output(CYOutput &out) const;
75b0a457
JF
632};
633
634struct CYArrayComprehension :
635 CYExpression
636{
637 CYExpression *expression_;
638 CYComprehension *comprehensions_;
639
640 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
641 expression_(expression),
642 comprehensions_(comprehensions)
643 {
644 }
645
646 CYPrecedence(0)
647
3b52fd1a 648 virtual CYExpression *Replace(CYContext &context);
652ec1ba 649 virtual void Output(CYOutput &out, CYFlags flags) const;
75b0a457
JF
650};
651
cf7d4c69
JF
652struct CYLiteral :
653 CYExpression
63b4c5a8 654{
d35a3b07 655 CYPrecedence(0)
fb98ac0c 656 CYRightHand(false)
cf7d4c69 657};
63b4c5a8 658
3b52fd1a
JF
659struct CYTrivial :
660 CYLiteral
661{
662 virtual CYExpression *Replace(CYContext &context);
663};
664
478d4ed0
JF
665struct CYMagic :
666 CYExpression
667{
668 CYPrecedence(0)
fb98ac0c 669 CYRightHand(false)
478d4ed0
JF
670};
671
dea834b0
JF
672struct CYRange {
673 uint64_t lo_;
674 uint64_t hi_;
675
676 CYRange(uint64_t lo, uint64_t hi) :
677 lo_(lo), hi_(hi)
678 {
679 }
680
681 bool operator [](uint8_t value) const {
682 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
683 }
684
685 void operator()(uint8_t value) {
686 if (value >> 7)
687 return;
688 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
689 }
690};
691
283e7e33 692extern CYRange DigitRange_;
dea834b0
JF
693extern CYRange WordStartRange_;
694extern CYRange WordEndRange_;
695
cf7d4c69 696struct CYString :
3b52fd1a 697 CYTrivial,
e5bc40db 698 CYPropertyName
cf7d4c69
JF
699{
700 const char *value_;
5999c315 701 size_t size_;
cf7d4c69 702
3b52fd1a
JF
703 CYString() :
704 value_(NULL),
705 size_(0)
706 {
707 }
708
709 CYString(const char *value) :
710 value_(value),
711 size_(strlen(value))
712 {
713 }
714
5999c315
JF
715 CYString(const char *value, size_t size) :
716 value_(value),
717 size_(size)
cf7d4c69
JF
718 {
719 }
720
3b52fd1a 721 CYString(const CYWord *word) :
029bc65b 722 value_(word->Word()),
5999c315 723 size_(strlen(value_))
cf7d4c69
JF
724 {
725 }
726
5999c315 727 const char *Value() const {
cf7d4c69
JF
728 return value_;
729 }
730
11c1cc16 731 virtual const char *Word() const;
dea834b0 732
4644480a
JF
733 virtual CYNumber *Number(CYContext &context);
734 virtual CYString *String(CYContext &context);
735
5db9a7f5 736 CYString *Concat(CYContext &out, CYString *rhs) const;
652ec1ba
JF
737 virtual void Output(CYOutput &out, CYFlags flags) const;
738 virtual void PropertyName(CYOutput &out) const;
63b4c5a8
JF
739};
740
cf7d4c69 741struct CYNumber :
3b52fd1a 742 CYTrivial,
e5bc40db 743 CYPropertyName
cf7d4c69 744{
5999c315
JF
745 double value_;
746
747 CYNumber(double value) :
748 value_(value)
749 {
750 }
751
752 double Value() const {
753 return value_;
cf7d4c69
JF
754 }
755
4644480a
JF
756 virtual CYNumber *Number(CYContext &context);
757 virtual CYString *String(CYContext &context);
758
652ec1ba
JF
759 virtual void Output(CYOutput &out, CYFlags flags) const;
760 virtual void PropertyName(CYOutput &out) const;
cf7d4c69
JF
761};
762
63cd45c9 763struct CYRegEx :
3b52fd1a 764 CYTrivial
63cd45c9
JF
765{
766 const char *value_;
767
768 CYRegEx(const char *value) :
769 value_(value)
770 {
771 }
772
773 const char *Value() const {
774 return value_;
775 }
776
777 virtual void Output(CYOutput &out, CYFlags flags) const;
778};
779
cf7d4c69
JF
780struct CYNull :
781 CYWord,
3b52fd1a 782 CYTrivial
cf7d4c69
JF
783{
784 CYNull() :
785 CYWord("null")
786 {
787 }
5999c315 788
4644480a
JF
789 virtual CYNumber *Number(CYContext &context);
790 virtual CYString *String(CYContext &context);
791
652ec1ba 792 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
793};
794
795struct CYThis :
796 CYWord,
478d4ed0 797 CYMagic
cf7d4c69
JF
798{
799 CYThis() :
800 CYWord("this")
801 {
802 }
5999c315 803
3b52fd1a 804 virtual CYExpression *Replace(CYContext &context);
652ec1ba 805 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
806};
807
808struct CYBoolean :
3b52fd1a 809 CYTrivial
cf7d4c69 810{
5999c315 811 virtual bool Value() const = 0;
652ec1ba 812 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
813};
814
815struct CYFalse :
816 CYWord,
817 CYBoolean
818{
819 CYFalse() :
820 CYWord("false")
821 {
822 }
5999c315 823
11c1cc16
JF
824 virtual bool Value() const {
825 return false;
826 }
4644480a
JF
827
828 virtual CYNumber *Number(CYContext &context);
829 virtual CYString *String(CYContext &context);
cf7d4c69
JF
830};
831
832struct CYTrue :
833 CYWord,
834 CYBoolean
835{
836 CYTrue() :
837 CYWord("true")
838 {
839 }
5999c315 840
11c1cc16
JF
841 virtual bool Value() const {
842 return true;
843 }
4644480a
JF
844
845 virtual CYNumber *Number(CYContext &context);
846 virtual CYString *String(CYContext &context);
cf7d4c69
JF
847};
848
849struct CYVariable :
850 CYExpression
851{
852 CYIdentifier *name_;
853
854 CYVariable(CYIdentifier *name) :
855 name_(name)
856 {
857 }
5999c315 858
d35a3b07 859 CYPrecedence(0)
fb98ac0c 860 CYRightHand(false)
d35a3b07 861
3b52fd1a 862 virtual CYExpression *Replace(CYContext &context);
652ec1ba 863 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
864};
865
866struct CYPrefix :
63b4c5a8
JF
867 CYExpression
868{
869 CYExpression *rhs_;
870
cf7d4c69 871 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
872 rhs_(rhs)
873 {
874 }
5999c315 875
b09da87b 876 virtual bool Alphabetic() const = 0;
5999c315
JF
877 virtual const char *Operator() const = 0;
878
3b52fd1a
JF
879 CYPrecedence(4)
880
881 virtual CYExpression *Replace(CYContext &context);
652ec1ba 882 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
883};
884
cf7d4c69 885struct CYInfix :
63b4c5a8
JF
886 CYExpression
887{
888 CYExpression *lhs_;
889 CYExpression *rhs_;
890
cf7d4c69 891 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
892 lhs_(lhs),
893 rhs_(rhs)
894 {
895 }
5999c315 896
0ff9f149
JF
897 void SetLeft(CYExpression *lhs) {
898 lhs_ = lhs;
899 }
900
b09da87b 901 virtual bool Alphabetic() const = 0;
5999c315
JF
902 virtual const char *Operator() const = 0;
903
3b52fd1a 904 virtual CYExpression *Replace(CYContext &context);
652ec1ba 905 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
906};
907
cf7d4c69 908struct CYPostfix :
63b4c5a8
JF
909 CYExpression
910{
911 CYExpression *lhs_;
912
cf7d4c69 913 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
914 lhs_(lhs)
915 {
916 }
5999c315
JF
917
918 virtual const char *Operator() const = 0;
919
3b52fd1a
JF
920 CYPrecedence(3)
921
922 virtual CYExpression *Replace(CYContext &context);
652ec1ba 923 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
924};
925
cf7d4c69 926struct CYAssignment :
d35a3b07 927 CYExpression
cf7d4c69 928{
d35a3b07
JF
929 CYExpression *lhs_;
930 CYExpression *rhs_;
931
cf7d4c69 932 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
d35a3b07
JF
933 lhs_(lhs),
934 rhs_(rhs)
cf7d4c69
JF
935 {
936 }
5999c315 937
0ff9f149
JF
938 void SetLeft(CYExpression *lhs) {
939 lhs_ = lhs;
940 }
941
5999c315 942 virtual const char *Operator() const = 0;
d35a3b07 943
4de0686f
JF
944 CYPrecedence(16)
945
3b52fd1a 946 virtual CYExpression *Replace(CYContext &context);
652ec1ba 947 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
948};
949
62014ea9 950struct CYArgument :
96a7e5c2
JF
951 CYNext<CYArgument>,
952 CYThing
62014ea9 953{
cf7d4c69
JF
954 CYWord *name_;
955 CYExpression *value_;
cf7d4c69 956
3b52fd1a
JF
957 CYArgument(CYExpression *value, CYArgument *next = NULL) :
958 CYNext<CYArgument>(next),
959 name_(NULL),
960 value_(value)
961 {
962 }
963
cf7d4c69 964 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
62014ea9 965 CYNext<CYArgument>(next),
cf7d4c69 966 name_(name),
62014ea9 967 value_(value)
cf7d4c69
JF
968 {
969 }
5999c315 970
3b52fd1a 971 void Replace(CYContext &context);
652ec1ba 972 void Output(CYOutput &out) const;
cf7d4c69
JF
973};
974
975struct CYBlank :
976 public CYWord
977{
978 CYBlank() :
979 CYWord("")
980 {
981 }
982};
983
5999c315
JF
984struct CYClause :
985 CYThing,
986 CYNext<CYClause>
987{
cf7d4c69 988 CYExpression *case_;
3b52fd1a 989 CYStatement *statements_;
cf7d4c69 990
3b52fd1a 991 CYClause(CYExpression *_case, CYStatement *statements) :
cf7d4c69 992 case_(_case),
3b52fd1a 993 statements_(statements)
cf7d4c69
JF
994 {
995 }
996
fa389b0f 997 void Replace(CYContext &context);
652ec1ba 998 virtual void Output(CYOutput &out) const;
cf7d4c69
JF
999};
1000
62014ea9 1001struct CYElement :
96a7e5c2
JF
1002 CYNext<CYElement>,
1003 CYThing
62014ea9 1004{
cf7d4c69 1005 CYExpression *value_;
cf7d4c69
JF
1006
1007 CYElement(CYExpression *value, CYElement *next) :
62014ea9
JF
1008 CYNext<CYElement>(next),
1009 value_(value)
cf7d4c69
JF
1010 {
1011 }
5999c315 1012
3b52fd1a 1013 void Replace(CYContext &context);
652ec1ba 1014 void Output(CYOutput &out) const;
5befe15e
JF
1015};
1016
1017struct CYArray :
1018 CYLiteral
1019{
1020 CYElement *elements_;
1021
3b52fd1a 1022 CYArray(CYElement *elements = NULL) :
5befe15e
JF
1023 elements_(elements)
1024 {
1025 }
1026
3b52fd1a 1027 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1028 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1029};
1030
550ee46a
JF
1031struct CYProperty :
1032 CYNext<CYProperty>,
1033 CYThing
1034{
1035 CYPropertyName *name_;
1036 CYExpression *value_;
1037
1038 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1039 CYNext<CYProperty>(next),
1040 name_(name),
1041 value_(value)
1042 {
1043 }
1044
1045 void Replace(CYContext &context);
1046 virtual void Output(CYOutput &out) const;
1047};
1048
cf7d4c69
JF
1049struct CYDeclaration :
1050 CYForInInitialiser
1051{
1052 CYIdentifier *identifier_;
1053 CYExpression *initialiser_;
1054
550ee46a 1055 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
cf7d4c69
JF
1056 identifier_(identifier),
1057 initialiser_(initialiser)
1058 {
1059 }
5999c315 1060
652ec1ba 1061 virtual void ForIn(CYOutput &out, CYFlags flags) const;
75b0a457 1062
cac61857 1063 virtual const char *ForEachIn() const;
3b52fd1a
JF
1064 virtual CYExpression *ForEachIn(CYContext &out);
1065
029bc65b
JF
1066 virtual CYExpression *Replace(CYContext &context);
1067 virtual CYAssignment *Assignment(CYContext &context);
75b0a457 1068
652ec1ba 1069 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1070};
1071
1072struct CYDeclarations :
cac61857 1073 CYNext<CYDeclarations>,
029bc65b
JF
1074 CYThing,
1075 CYForInitialiser
cf7d4c69
JF
1076{
1077 CYDeclaration *declaration_;
cf7d4c69 1078
cacd1a88 1079 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
cac61857
JF
1080 CYNext<CYDeclarations>(next),
1081 declaration_(declaration)
1082 {
1083 }
1084
652ec1ba 1085 virtual void For(CYOutput &out) const;
96a7e5c2 1086
029bc65b 1087 virtual CYCompound *Replace(CYContext &context);
550ee46a 1088 CYProperty *Property(CYContext &context);
3b52fd1a 1089
96a7e5c2 1090 virtual void Output(CYOutput &out) const;
652ec1ba 1091 virtual void Output(CYOutput &out, CYFlags flags) const;
cac61857
JF
1092};
1093
1094struct CYVar :
1095 CYStatement
1096{
1097 CYDeclarations *declarations_;
1098
1099 CYVar(CYDeclarations *declarations) :
1100 declarations_(declarations)
1101 {
1102 }
1103
3b52fd1a 1104 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1105 virtual void Output(CYOutput &out, CYFlags flags) const;
cac61857
JF
1106};
1107
1108struct CYLet :
1109 CYStatement
1110{
1111 CYDeclarations *declarations_;
3b52fd1a 1112 CYBlock code_;
cac61857
JF
1113
1114 CYLet(CYDeclarations *declarations, CYStatement *statements) :
1115 declarations_(declarations),
3b52fd1a 1116 code_(statements)
cf7d4c69
JF
1117 {
1118 }
5999c315 1119
3b52fd1a 1120 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1121 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1122};
1123
cf7d4c69
JF
1124struct CYFor :
1125 CYStatement
1126{
1127 CYForInitialiser *initialiser_;
1128 CYExpression *test_;
1129 CYExpression *increment_;
1130 CYStatement *code_;
1131
1132 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1133 initialiser_(initialiser),
1134 test_(test),
1135 increment_(increment),
1136 code_(code)
1137 {
1138 }
5999c315 1139
3b52fd1a 1140 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1141 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1142};
1143
1144struct CYForIn :
1145 CYStatement
1146{
1147 CYForInInitialiser *initialiser_;
1148 CYExpression *set_;
1149 CYStatement *code_;
1150
1151 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1152 initialiser_(initialiser),
1153 set_(set),
1154 code_(code)
1155 {
1156 }
5999c315 1157
3b52fd1a 1158 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1159 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1160};
1161
75b0a457
JF
1162struct CYForEachIn :
1163 CYStatement
1164{
1165 CYForInInitialiser *initialiser_;
1166 CYExpression *set_;
1167 CYStatement *code_;
1168
1169 CYForEachIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1170 initialiser_(initialiser),
1171 set_(set),
1172 code_(code)
1173 {
1174 }
1175
3b52fd1a 1176 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1177 virtual void Output(CYOutput &out, CYFlags flags) const;
75b0a457
JF
1178};
1179
693d501b
JF
1180struct CYObject :
1181 CYLiteral
1182{
3b52fd1a 1183 CYProperty *properties_;
693d501b 1184
3b52fd1a
JF
1185 CYObject(CYProperty *properties) :
1186 properties_(properties)
693d501b
JF
1187 {
1188 }
1189
3b52fd1a 1190 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1191 void Output(CYOutput &out, CYFlags flags) const;
693d501b
JF
1192};
1193
cf7d4c69
JF
1194struct CYMember :
1195 CYExpression
1196{
1197 CYExpression *object_;
1198 CYExpression *property_;
1199
1200 CYMember(CYExpression *object, CYExpression *property) :
1201 object_(object),
1202 property_(property)
1203 {
1204 }
5999c315 1205
9b5527f0
JF
1206 void SetLeft(CYExpression *object) {
1207 object_ = object;
1208 }
3b52fd1a
JF
1209
1210 void Replace_(CYContext &context);
9b5527f0
JF
1211};
1212
1213struct CYDirectMember :
1214 CYMember
1215{
1216 CYDirectMember(CYExpression *object, CYExpression *property) :
1217 CYMember(object, property)
1218 {
1219 }
1220
1221 CYPrecedence(1)
fb98ac0c 1222 CYRightHand(false)
9b5527f0 1223
3b52fd1a 1224 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1225 virtual void Output(CYOutput &out, CYFlags flags) const;
9b5527f0
JF
1226};
1227
1228struct CYIndirectMember :
1229 CYMember
1230{
1231 CYIndirectMember(CYExpression *object, CYExpression *property) :
1232 CYMember(object, property)
1233 {
1234 }
1235
d35a3b07 1236 CYPrecedence(1)
fb98ac0c 1237 CYRightHand(false)
d35a3b07 1238
3b52fd1a 1239 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1240 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1241};
1242
1243struct CYNew :
1244 CYExpression
1245{
1246 CYExpression *constructor_;
1247 CYArgument *arguments_;
1248
1249 CYNew(CYExpression *constructor, CYArgument *arguments) :
1250 constructor_(constructor),
1251 arguments_(arguments)
1252 {
1253 }
5999c315 1254
fb98ac0c
JF
1255 virtual unsigned Precedence() const {
1256 return arguments_ == NULL ? 2 : 1;
1257 }
1258
1259 CYRightHand(false)
d35a3b07 1260
3b52fd1a 1261 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1262 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1263};
1264
1265struct CYCall :
1266 CYExpression
1267{
1268 CYExpression *function_;
1269 CYArgument *arguments_;
1270
3b52fd1a 1271 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
cf7d4c69
JF
1272 function_(function),
1273 arguments_(arguments)
1274 {
1275 }
5999c315 1276
fb98ac0c
JF
1277 CYPrecedence(1)
1278 CYRightHand(false)
d35a3b07 1279
3b52fd1a 1280 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1281 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1282};
1283
1284struct CYIf :
1285 CYStatement
1286{
1287 CYExpression *test_;
1288 CYStatement *true_;
1289 CYStatement *false_;
1290
3b52fd1a 1291 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
cf7d4c69
JF
1292 test_(test),
1293 true_(_true),
1294 false_(_false)
1295 {
1296 }
5999c315 1297
3b52fd1a 1298 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1299 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1300};
1301
1302struct CYDoWhile :
1303 CYStatement
1304{
1305 CYExpression *test_;
1306 CYStatement *code_;
1307
1308 CYDoWhile(CYExpression *test, CYStatement *code) :
1309 test_(test),
1310 code_(code)
1311 {
1312 }
5999c315 1313
3b52fd1a 1314 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1315 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1316};
1317
1318struct CYWhile :
1319 CYStatement
1320{
1321 CYExpression *test_;
1322 CYStatement *code_;
1323
1324 CYWhile(CYExpression *test, CYStatement *code) :
1325 test_(test),
1326 code_(code)
1327 {
1328 }
5999c315 1329
3b52fd1a 1330 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1331 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1332};
1333
029bc65b
JF
1334struct CYFunction :
1335 CYScope
1336{
cf7d4c69 1337 CYIdentifier *name_;
b09da87b 1338 CYFunctionParameter *parameters_;
3b52fd1a 1339 CYBlock code_;
cf7d4c69 1340
3b52fd1a 1341 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
cf7d4c69
JF
1342 name_(name),
1343 parameters_(parameters),
029bc65b 1344 code_(statements, this)
cf7d4c69
JF
1345 {
1346 }
5999c315 1347
7c6c5b0a
JF
1348 virtual ~CYFunction() {
1349 }
1350
14ec9e00
JF
1351 void Inject(CYContext &context);
1352 virtual void Replace_(CYContext &context, bool outer);
fb98ac0c
JF
1353 virtual void Output(CYOutput &out, CYFlags flags) const;
1354};
1355
1356struct CYFunctionExpression :
1357 CYFunction,
1358 CYExpression
1359{
3b52fd1a
JF
1360 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1361 CYFunction(name, parameters, statements)
fb98ac0c
JF
1362 {
1363 }
1364
d35a3b07 1365 CYPrecedence(0)
fb98ac0c 1366 CYRightHand(false)
d35a3b07 1367
3b52fd1a 1368 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1369 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1370};
1371
fb98ac0c
JF
1372struct CYFunctionStatement :
1373 CYFunction,
b10bd496 1374 CYStatement
cf7d4c69 1375{
3b52fd1a
JF
1376 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1377 CYFunction(name, parameters, statements)
cf7d4c69
JF
1378 {
1379 }
5999c315 1380
3b52fd1a 1381 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1382 virtual void Output(CYOutput &out, CYFlags flags) const;
5999c315
JF
1383};
1384
1385struct CYExpress :
1386 CYStatement
1387{
1388 CYExpression *expression_;
1389
1390 CYExpress(CYExpression *expression) :
1391 expression_(expression)
1392 {
029bc65b
JF
1393 if (expression == NULL)
1394 throw;
5999c315
JF
1395 }
1396
029bc65b 1397 virtual CYStatement *Collapse(CYContext &context);
3b52fd1a 1398 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1399 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1400};
1401
1402struct CYContinue :
1403 CYStatement
1404{
1405 CYIdentifier *label_;
1406
1407 CYContinue(CYIdentifier *label) :
1408 label_(label)
1409 {
1410 }
5999c315 1411
3b52fd1a 1412 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1413 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1414};
1415
1416struct CYBreak :
1417 CYStatement
1418{
1419 CYIdentifier *label_;
1420
1421 CYBreak(CYIdentifier *label) :
1422 label_(label)
1423 {
1424 }
5999c315 1425
3b52fd1a 1426 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1427 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1428};
1429
1430struct CYReturn :
1431 CYStatement
1432{
1433 CYExpression *value_;
1434
1435 CYReturn(CYExpression *value) :
1436 value_(value)
1437 {
1438 }
5999c315 1439
3b52fd1a 1440 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1441 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1442};
1443
1444struct CYEmpty :
1445 CYStatement
1446{
029bc65b 1447 virtual CYStatement *Collapse(CYContext &context);
3b52fd1a 1448 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1449 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1450};
1451
96a7e5c2
JF
1452struct CYFinally :
1453 CYThing
1454{
3b52fd1a 1455 CYBlock code_;
b10bd496 1456
3b52fd1a
JF
1457 CYFinally(CYStatement *statements) :
1458 code_(statements)
b10bd496
JF
1459 {
1460 }
1461
3b52fd1a 1462 void Replace(CYContext &context);
b10bd496
JF
1463 virtual void Output(CYOutput &out) const;
1464};
1465
37954781
JF
1466namespace cy {
1467namespace Syntax {
1468
1469struct Catch :
1470 CYThing
1471{
1472 CYIdentifier *name_;
1473 CYBlock code_;
1474
1475 Catch(CYIdentifier *name, CYStatement *statements) :
1476 name_(name),
1477 code_(statements)
1478 {
1479 }
1480
1481 void Replace(CYContext &context);
1482 virtual void Output(CYOutput &out) const;
1483};
1484
1485struct Try :
cf7d4c69
JF
1486 CYStatement
1487{
3b52fd1a 1488 CYBlock code_;
37954781 1489 Catch *catch_;
b10bd496 1490 CYFinally *finally_;
cf7d4c69 1491
37954781 1492 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
3b52fd1a 1493 code_(statements),
cf7d4c69
JF
1494 catch_(_catch),
1495 finally_(finally)
1496 {
1497 }
5999c315 1498
3b52fd1a 1499 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1500 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1501};
1502
37954781 1503struct Throw :
cf7d4c69
JF
1504 CYStatement
1505{
1506 CYExpression *value_;
1507
37954781 1508 Throw(CYExpression *value) :
cf7d4c69
JF
1509 value_(value)
1510 {
1511 }
5999c315 1512
3b52fd1a 1513 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1514 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1515};
1516
37954781
JF
1517} }
1518
cf7d4c69
JF
1519struct CYWith :
1520 CYStatement
1521{
1522 CYExpression *scope_;
1523 CYStatement *code_;
1524
1525 CYWith(CYExpression *scope, CYStatement *code) :
1526 scope_(scope),
1527 code_(code)
1528 {
1529 }
5999c315 1530
3b52fd1a 1531 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1532 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1533};
1534
1535struct CYSwitch :
1536 CYStatement
1537{
1538 CYExpression *value_;
1539 CYClause *clauses_;
1540
1541 CYSwitch(CYExpression *value, CYClause *clauses) :
1542 value_(value),
1543 clauses_(clauses)
1544 {
1545 }
5999c315 1546
3b52fd1a 1547 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1548 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1549};
1550
1551struct CYCondition :
1552 CYExpression
1553{
1554 CYExpression *test_;
1555 CYExpression *true_;
1556 CYExpression *false_;
1557
1558 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
91a416e4 1559 test_(test),
cf7d4c69
JF
1560 true_(_true),
1561 false_(_false)
1562 {
1563 }
5999c315 1564
d35a3b07
JF
1565 CYPrecedence(15)
1566
3b52fd1a 1567 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1568 virtual void Output(CYOutput &out, CYFlags flags) const;
5999c315
JF
1569};
1570
1571struct CYAddressOf :
1572 CYPrefix
1573{
1574 CYAddressOf(CYExpression *rhs) :
1575 CYPrefix(rhs)
1576 {
1577 }
1578
1579 virtual const char *Operator() const {
1580 return "&";
1581 }
1582
b09da87b 1583 CYAlphabetic(false)
d35a3b07 1584
3b52fd1a 1585 virtual CYExpression *Replace(CYContext &context);
5999c315
JF
1586};
1587
1588struct CYIndirect :
1589 CYPrefix
1590{
1591 CYIndirect(CYExpression *rhs) :
1592 CYPrefix(rhs)
1593 {
1594 }
1595
1596 virtual const char *Operator() const {
561ac418 1597 return "*";
5999c315
JF
1598 }
1599
b09da87b 1600 CYAlphabetic(false)
d35a3b07 1601
3b52fd1a 1602 virtual CYExpression *Replace(CYContext &context);
cf7d4c69
JF
1603};
1604
4644480a
JF
1605#define CYReplace \
1606 virtual CYExpression *Replace(CYContext &context);
1607
1608#define CYPostfix_(op, name, args...) \
cf7d4c69
JF
1609 struct CY ## name : \
1610 CYPostfix \
4644480a 1611 { args \
cf7d4c69
JF
1612 CY ## name(CYExpression *lhs) : \
1613 CYPostfix(lhs) \
1614 { \
1615 } \
5999c315
JF
1616 \
1617 virtual const char *Operator() const { \
1618 return op; \
1619 } \
cf7d4c69
JF
1620 };
1621
4644480a 1622#define CYPrefix_(alphabetic, op, name, args...) \
cf7d4c69
JF
1623 struct CY ## name : \
1624 CYPrefix \
4644480a 1625 { args \
cf7d4c69
JF
1626 CY ## name(CYExpression *rhs) : \
1627 CYPrefix(rhs) \
1628 { \
1629 } \
d35a3b07 1630 \
b09da87b 1631 CYAlphabetic(alphabetic) \
5999c315
JF
1632 \
1633 virtual const char *Operator() const { \
1634 return op; \
1635 } \
cf7d4c69
JF
1636 };
1637
4644480a 1638#define CYInfix_(alphabetic, precedence, op, name, args...) \
cf7d4c69
JF
1639 struct CY ## name : \
1640 CYInfix \
4644480a 1641 { args \
cf7d4c69
JF
1642 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1643 CYInfix(lhs, rhs) \
1644 { \
1645 } \
d35a3b07 1646 \
b09da87b 1647 CYAlphabetic(alphabetic) \
d35a3b07 1648 CYPrecedence(precedence) \
5999c315
JF
1649 \
1650 virtual const char *Operator() const { \
1651 return op; \
1652 } \
cf7d4c69
JF
1653 };
1654
4644480a 1655#define CYAssignment_(op, name, args...) \
cf7d4c69
JF
1656 struct CY ## name ## Assign : \
1657 CYAssignment \
4644480a 1658 { args \
cf7d4c69
JF
1659 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1660 CYAssignment(lhs, rhs) \
1661 { \
1662 } \
5999c315
JF
1663 \
1664 virtual const char *Operator() const { \
1665 return op; \
1666 } \
cf7d4c69
JF
1667 };
1668
1669CYPostfix_("++", PostIncrement)
1670CYPostfix_("--", PostDecrement)
1671
b09da87b
JF
1672CYPrefix_(true, "delete", Delete)
1673CYPrefix_(true, "void", Void)
1674CYPrefix_(true, "typeof", TypeOf)
1675CYPrefix_(false, "++", PreIncrement)
1676CYPrefix_(false, "--", PreDecrement)
c0bc320e 1677CYPrefix_(false, "+", Affirm)
b09da87b
JF
1678CYPrefix_(false, "-", Negate)
1679CYPrefix_(false, "~", BitwiseNot)
1680CYPrefix_(false, "!", LogicalNot)
1681
1682CYInfix_(false, 5, "*", Multiply)
1683CYInfix_(false, 5, "/", Divide)
1684CYInfix_(false, 5, "%", Modulus)
4644480a 1685CYInfix_(false, 6, "+", Add, CYReplace)
b09da87b
JF
1686CYInfix_(false, 6, "-", Subtract)
1687CYInfix_(false, 7, "<<", ShiftLeft)
1688CYInfix_(false, 7, ">>", ShiftRightSigned)
1689CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1690CYInfix_(false, 8, "<", Less)
1691CYInfix_(false, 8, ">", Greater)
1692CYInfix_(false, 8, "<=", LessOrEqual)
1693CYInfix_(false, 8, ">=", GreaterOrEqual)
1694CYInfix_(true, 8, "instanceof", InstanceOf)
1695CYInfix_(true, 8, "in", In)
1696CYInfix_(false, 9, "==", Equal)
1697CYInfix_(false, 9, "!=", NotEqual)
1698CYInfix_(false, 9, "===", Identical)
1699CYInfix_(false, 9, "!==", NotIdentical)
1700CYInfix_(false, 10, "&", BitwiseAnd)
1701CYInfix_(false, 11, "^", BitwiseXOr)
1702CYInfix_(false, 12, "|", BitwiseOr)
1703CYInfix_(false, 13, "&&", LogicalAnd)
1704CYInfix_(false, 14, "||", LogicalOr)
cf7d4c69
JF
1705
1706CYAssignment_("=", )
1707CYAssignment_("*=", Multiply)
1708CYAssignment_("/=", Divide)
1709CYAssignment_("%=", Modulus)
1710CYAssignment_("+=", Add)
1711CYAssignment_("-=", Subtract)
1712CYAssignment_("<<=", ShiftLeft)
1713CYAssignment_(">>=", ShiftRightSigned)
1714CYAssignment_(">>>=", ShiftRightUnsigned)
1715CYAssignment_("&=", BitwiseAnd)
1716CYAssignment_("^=", BitwiseXOr)
1717CYAssignment_("|=", BitwiseOr)
1718
63b4c5a8 1719#endif/*CYPARSER_HPP*/