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