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