]> git.saurik.com Git - cycript.git/blame - Parser.hpp
Syntax highlight nil, an identifier, as a constant.
[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
d3b63265
JF
472class CYStream :
473 public std::istream
474{
475 private:
476 class CYBuffer :
477 public std::streambuf
478 {
479 public:
480 CYBuffer(const char *start, const char *end) {
481 setg(const_cast<char *>(start), const_cast<char *>(start), const_cast<char *>(end));
482 }
483 } buffer_;
484
485 public:
486 CYStream(const char *start, const char *end) :
487 std::istream(&buffer_),
488 buffer_(start, end)
489 {
490 }
491};
492
5999c315
JF
493class CYDriver {
494 public:
e7ed5354
JF
495 void *scanner_;
496
3ea7eed0 497 CYState state_;
3ea7eed0
JF
498 std::stack<bool> in_;
499
5602b1ee
JF
500 struct {
501 bool AtImplementation;
502 bool Function;
503 bool OpenBrace;
504 } no_;
505
d3b63265 506 std::istream &data_;
e7ed5354 507
b10bd496
JF
508 bool strict_;
509
63cd45c9 510 enum Condition {
697d6fd2 511 RegExpCondition,
691e4717
JF
512 XMLContentCondition,
513 XMLTagCondition,
63cd45c9
JF
514 };
515
5999c315 516 std::string filename_;
e7ed5354 517
5befe15e 518 struct Error {
b10bd496 519 bool warning_;
5befe15e
JF
520 cy::location location_;
521 std::string message_;
522 };
523
524 typedef std::vector<Error> Errors;
525
3b52fd1a 526 CYProgram *program_;
5befe15e 527 Errors errors_;
5999c315 528
7e5391fd
JF
529 bool auto_;
530
531 struct Context {
532 CYExpression *context_;
533
534 Context(CYExpression *context) :
535 context_(context)
536 {
537 }
538
539 typedef std::vector<CYWord *> Words;
540 Words words_;
541 };
542
543 typedef std::vector<Context> Contexts;
544 Contexts contexts_;
545
546 CYExpression *context_;
547
548 enum Mode {
549 AutoNone,
550 AutoPrimary,
551 AutoDirect,
552 AutoIndirect,
553 AutoMessage
554 } mode_;
555
5999c315
JF
556 private:
557 void ScannerInit();
558 void ScannerDestroy();
559
560 public:
d3b63265 561 CYDriver(std::istream &data, const std::string &filename = "");
5999c315 562 ~CYDriver();
63cd45c9 563
691e4717
JF
564 Condition GetCondition();
565 void SetCondition(Condition condition);
566
567 void PushCondition(Condition condition);
568 void PopCondition();
b10bd496
JF
569
570 void Warning(const cy::location &location, const char *message);
5999c315
JF
571};
572
cac61857 573struct CYForInitialiser {
7c6c5b0a
JF
574 virtual ~CYForInitialiser() {
575 }
576
029bc65b 577 virtual CYExpression *Replace(CYContext &context) = 0;
15b88a33 578 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
dea834b0
JF
579};
580
cac61857 581struct CYForInInitialiser {
7c6c5b0a
JF
582 virtual ~CYForInInitialiser() {
583 }
584
652ec1ba 585 virtual void ForIn(CYOutput &out, CYFlags flags) const = 0;
ad3b38bb 586 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0;
b158281e 587
029bc65b 588 virtual CYExpression *Replace(CYContext &context) = 0;
ae65d594 589 virtual CYAssignment *Assignment(CYContext &context) = 0;
c8a0500b
JF
590
591 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
b09da87b
JF
592};
593
4644480a
JF
594struct CYNumber;
595struct CYString;
596
cf7d4c69 597struct CYExpression :
5999c315 598 CYNext<CYExpression>,
cf7d4c69 599 CYForInitialiser,
e5bc40db 600 CYForInInitialiser,
96a7e5c2
JF
601 CYClassName,
602 CYThing
63b4c5a8 603{
d35a3b07 604 virtual unsigned Precedence() const = 0;
75b0a457 605
fb98ac0c
JF
606 virtual bool RightHand() const {
607 return true;
608 }
609
652ec1ba 610 virtual void ForIn(CYOutput &out, CYFlags flags) const;
ad3b38bb 611 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
75b0a457 612
6c093cce
JF
613 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
614
96a7e5c2 615 virtual void Output(CYOutput &out) const;
652ec1ba
JF
616 virtual void Output(CYOutput &out, CYFlags flags) const = 0;
617 void Output(CYOutput &out, unsigned precedence, CYFlags flags) const;
dea834b0 618
3b52fd1a 619 virtual CYExpression *ClassName(CYContext &context, bool object);
652ec1ba 620 virtual void ClassName(CYOutput &out, bool object) const;
e5bc40db 621
3b52fd1a 622 virtual CYExpression *Replace(CYContext &context) = 0;
ae65d594 623 virtual CYAssignment *Assignment(CYContext &context);
3b52fd1a 624
4644480a
JF
625 virtual CYExpression *Primitive(CYContext &context) {
626 return this;
627 }
628
629 virtual CYNumber *Number(CYContext &context) {
630 return NULL;
631 }
632
633 virtual CYString *String(CYContext &context) {
634 return NULL;
635 }
636
dea834b0
JF
637 virtual const char *Word() const {
638 return NULL;
639 }
63b4c5a8
JF
640};
641
b09da87b
JF
642#define CYAlphabetic(value) \
643 virtual bool Alphabetic() const { \
644 return value; \
645 }
646
d35a3b07 647#define CYPrecedence(value) \
8351aa30 648 static const unsigned Precedence_ = value; \
d35a3b07 649 virtual unsigned Precedence() const { \
8351aa30 650 return Precedence_; \
d35a3b07
JF
651 }
652
fb98ac0c
JF
653#define CYRightHand(value) \
654 virtual bool RightHand() const { \
655 return value; \
656 }
657
d35a3b07
JF
658struct CYCompound :
659 CYExpression
660{
661 CYExpression *expressions_;
662
029bc65b 663 CYCompound(CYExpression *expressions = NULL) :
d35a3b07
JF
664 expressions_(expressions)
665 {
666 }
667
668 void AddPrev(CYExpression *expression) {
bf45251b 669 CYSetLast(expression) = expressions_;
d35a3b07
JF
670 expressions_ = expression;
671 }
672
673 CYPrecedence(17)
674
3b52fd1a 675 virtual CYExpression *Replace(CYContext &context);
652ec1ba 676 void Output(CYOutput &out, CYFlags flags) const;
e06e5ee1
JF
677
678 virtual CYExpression *Primitive(CYContext &context);
d35a3b07 679};
5999c315 680
c8a0500b
JF
681struct CYDeclaration;
682
3b52fd1a
JF
683struct CYFunctionParameter :
684 CYNext<CYFunctionParameter>,
685 CYThing
686{
c8a0500b 687 CYForInInitialiser *initialiser_;
3b52fd1a 688
c8a0500b 689 CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) :
3b52fd1a 690 CYNext<CYFunctionParameter>(next),
c8a0500b 691 initialiser_(initialiser)
4e11a430
JF
692 {
693 }
694
c8a0500b
JF
695 void Replace(CYContext &context, CYBlock &code);
696 void Output(CYOutput &out) const;
3b52fd1a
JF
697};
698
75b0a457 699struct CYComprehension :
96a7e5c2
JF
700 CYNext<CYComprehension>,
701 CYThing
75b0a457 702{
c2529502
JF
703 CYComprehension(CYComprehension *next = NULL) :
704 CYNext<CYComprehension>(next)
705 {
706 }
707
75b0a457
JF
708 virtual const char *Name() const = 0;
709
3b52fd1a
JF
710 virtual CYFunctionParameter *Parameter(CYContext &context) const = 0;
711 CYFunctionParameter *Parameters(CYContext &context) const;
712 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 713 virtual void Output(CYOutput &out) const = 0;
75b0a457
JF
714};
715
716struct CYForInComprehension :
717 CYComprehension
718{
719 CYIdentifier *name_;
720 CYExpression *set_;
721
c2529502
JF
722 CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
723 CYComprehension(next),
75b0a457
JF
724 name_(name),
725 set_(set)
726 {
727 }
728
729 virtual const char *Name() const {
029bc65b 730 return name_->Word();
75b0a457
JF
731 }
732
3b52fd1a
JF
733 virtual CYFunctionParameter *Parameter(CYContext &context) const;
734 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 735 virtual void Output(CYOutput &out) const;
75b0a457
JF
736};
737
d5618df7 738struct CYForOfComprehension :
75b0a457
JF
739 CYComprehension
740{
741 CYIdentifier *name_;
742 CYExpression *set_;
743
c2529502
JF
744 CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) :
745 CYComprehension(next),
75b0a457
JF
746 name_(name),
747 set_(set)
748 {
749 }
750
751 virtual const char *Name() const {
029bc65b 752 return name_->Word();
75b0a457
JF
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 CYIfComprehension :
761 CYComprehension
762{
763 CYExpression *test_;
764
765 CYIfComprehension(CYExpression *test) :
766 test_(test)
767 {
768 }
769
770 virtual const char *Name() const {
771 return NULL;
772 }
773
3b52fd1a
JF
774 virtual CYFunctionParameter *Parameter(CYContext &context) const;
775 virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const;
4644480a 776 virtual void Output(CYOutput &out) const;
75b0a457
JF
777};
778
779struct CYArrayComprehension :
780 CYExpression
781{
782 CYExpression *expression_;
783 CYComprehension *comprehensions_;
784
785 CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) :
786 expression_(expression),
787 comprehensions_(comprehensions)
788 {
789 }
790
791 CYPrecedence(0)
792
3b52fd1a 793 virtual CYExpression *Replace(CYContext &context);
652ec1ba 794 virtual void Output(CYOutput &out, CYFlags flags) const;
75b0a457
JF
795};
796
cf7d4c69
JF
797struct CYLiteral :
798 CYExpression
63b4c5a8 799{
d35a3b07 800 CYPrecedence(0)
fb98ac0c 801 CYRightHand(false)
cf7d4c69 802};
63b4c5a8 803
3b52fd1a
JF
804struct CYTrivial :
805 CYLiteral
806{
807 virtual CYExpression *Replace(CYContext &context);
808};
809
478d4ed0
JF
810struct CYMagic :
811 CYExpression
812{
813 CYPrecedence(0)
fb98ac0c 814 CYRightHand(false)
478d4ed0
JF
815};
816
dea834b0
JF
817struct CYRange {
818 uint64_t lo_;
819 uint64_t hi_;
820
821 CYRange(uint64_t lo, uint64_t hi) :
822 lo_(lo), hi_(hi)
823 {
824 }
825
826 bool operator [](uint8_t value) const {
827 return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1;
828 }
829
830 void operator()(uint8_t value) {
831 if (value >> 7)
832 return;
833 (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f);
834 }
835};
836
283e7e33 837extern CYRange DigitRange_;
dea834b0
JF
838extern CYRange WordStartRange_;
839extern CYRange WordEndRange_;
840
cf7d4c69 841struct CYString :
3b52fd1a 842 CYTrivial,
e5bc40db 843 CYPropertyName
cf7d4c69
JF
844{
845 const char *value_;
5999c315 846 size_t size_;
cf7d4c69 847
3b52fd1a
JF
848 CYString() :
849 value_(NULL),
850 size_(0)
851 {
852 }
853
854 CYString(const char *value) :
855 value_(value),
856 size_(strlen(value))
857 {
858 }
859
5999c315
JF
860 CYString(const char *value, size_t size) :
861 value_(value),
862 size_(size)
cf7d4c69
JF
863 {
864 }
865
3b52fd1a 866 CYString(const CYWord *word) :
029bc65b 867 value_(word->Word()),
5999c315 868 size_(strlen(value_))
cf7d4c69
JF
869 {
870 }
871
5999c315 872 const char *Value() const {
cf7d4c69
JF
873 return value_;
874 }
875
11c1cc16 876 virtual const char *Word() const;
dea834b0 877
4644480a
JF
878 virtual CYNumber *Number(CYContext &context);
879 virtual CYString *String(CYContext &context);
880
5db9a7f5 881 CYString *Concat(CYContext &out, CYString *rhs) const;
652ec1ba
JF
882 virtual void Output(CYOutput &out, CYFlags flags) const;
883 virtual void PropertyName(CYOutput &out) const;
63b4c5a8
JF
884};
885
cf7d4c69 886struct CYNumber :
3b52fd1a 887 CYTrivial,
e5bc40db 888 CYPropertyName
cf7d4c69 889{
5999c315
JF
890 double value_;
891
892 CYNumber(double value) :
893 value_(value)
894 {
895 }
896
897 double Value() const {
898 return value_;
cf7d4c69
JF
899 }
900
4644480a
JF
901 virtual CYNumber *Number(CYContext &context);
902 virtual CYString *String(CYContext &context);
903
652ec1ba
JF
904 virtual void Output(CYOutput &out, CYFlags flags) const;
905 virtual void PropertyName(CYOutput &out) const;
cf7d4c69
JF
906};
907
63cd45c9 908struct CYRegEx :
3b52fd1a 909 CYTrivial
63cd45c9
JF
910{
911 const char *value_;
912
913 CYRegEx(const char *value) :
914 value_(value)
915 {
916 }
917
918 const char *Value() const {
919 return value_;
920 }
921
922 virtual void Output(CYOutput &out, CYFlags flags) const;
923};
924
cf7d4c69
JF
925struct CYNull :
926 CYWord,
3b52fd1a 927 CYTrivial
cf7d4c69
JF
928{
929 CYNull() :
930 CYWord("null")
931 {
932 }
5999c315 933
4644480a
JF
934 virtual CYNumber *Number(CYContext &context);
935 virtual CYString *String(CYContext &context);
936
652ec1ba 937 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
938};
939
940struct CYThis :
941 CYWord,
478d4ed0 942 CYMagic
cf7d4c69
JF
943{
944 CYThis() :
945 CYWord("this")
946 {
947 }
5999c315 948
3b52fd1a 949 virtual CYExpression *Replace(CYContext &context);
652ec1ba 950 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
951};
952
953struct CYBoolean :
3b52fd1a 954 CYTrivial
cf7d4c69 955{
5999c315 956 virtual bool Value() const = 0;
652ec1ba 957 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
958};
959
960struct CYFalse :
961 CYWord,
962 CYBoolean
963{
964 CYFalse() :
965 CYWord("false")
966 {
967 }
5999c315 968
11c1cc16
JF
969 virtual bool Value() const {
970 return false;
971 }
4644480a
JF
972
973 virtual CYNumber *Number(CYContext &context);
974 virtual CYString *String(CYContext &context);
cf7d4c69
JF
975};
976
977struct CYTrue :
978 CYWord,
979 CYBoolean
980{
981 CYTrue() :
982 CYWord("true")
983 {
984 }
5999c315 985
11c1cc16
JF
986 virtual bool Value() const {
987 return true;
988 }
4644480a
JF
989
990 virtual CYNumber *Number(CYContext &context);
991 virtual CYString *String(CYContext &context);
cf7d4c69
JF
992};
993
994struct CYVariable :
995 CYExpression
996{
997 CYIdentifier *name_;
998
999 CYVariable(CYIdentifier *name) :
1000 name_(name)
1001 {
1002 }
5999c315 1003
2eb8215d
JF
1004 CYVariable(const char *name) :
1005 name_(new($pool) CYIdentifier(name))
1006 {
1007 }
1008
d35a3b07 1009 CYPrecedence(0)
fb98ac0c 1010 CYRightHand(false)
d35a3b07 1011
3b52fd1a 1012 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1013 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1014};
1015
1016struct CYPrefix :
63b4c5a8
JF
1017 CYExpression
1018{
1019 CYExpression *rhs_;
1020
cf7d4c69 1021 CYPrefix(CYExpression *rhs) :
63b4c5a8
JF
1022 rhs_(rhs)
1023 {
1024 }
5999c315 1025
b09da87b 1026 virtual bool Alphabetic() const = 0;
5999c315
JF
1027 virtual const char *Operator() const = 0;
1028
3b52fd1a
JF
1029 CYPrecedence(4)
1030
1031 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1032 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
1033};
1034
cf7d4c69 1035struct CYInfix :
63b4c5a8
JF
1036 CYExpression
1037{
1038 CYExpression *lhs_;
1039 CYExpression *rhs_;
1040
cf7d4c69 1041 CYInfix(CYExpression *lhs, CYExpression *rhs) :
63b4c5a8
JF
1042 lhs_(lhs),
1043 rhs_(rhs)
1044 {
1045 }
5999c315 1046
0ff9f149
JF
1047 void SetLeft(CYExpression *lhs) {
1048 lhs_ = lhs;
1049 }
1050
b09da87b 1051 virtual bool Alphabetic() const = 0;
5999c315
JF
1052 virtual const char *Operator() const = 0;
1053
3b52fd1a 1054 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1055 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
1056};
1057
cf7d4c69 1058struct CYPostfix :
63b4c5a8
JF
1059 CYExpression
1060{
1061 CYExpression *lhs_;
1062
cf7d4c69 1063 CYPostfix(CYExpression *lhs) :
63b4c5a8
JF
1064 lhs_(lhs)
1065 {
1066 }
5999c315
JF
1067
1068 virtual const char *Operator() const = 0;
1069
3b52fd1a
JF
1070 CYPrecedence(3)
1071
1072 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1073 virtual void Output(CYOutput &out, CYFlags flags) const;
63b4c5a8
JF
1074};
1075
cf7d4c69 1076struct CYAssignment :
d35a3b07 1077 CYExpression
cf7d4c69 1078{
d35a3b07
JF
1079 CYExpression *lhs_;
1080 CYExpression *rhs_;
1081
cf7d4c69 1082 CYAssignment(CYExpression *lhs, CYExpression *rhs) :
d35a3b07
JF
1083 lhs_(lhs),
1084 rhs_(rhs)
cf7d4c69
JF
1085 {
1086 }
5999c315 1087
0ff9f149
JF
1088 void SetLeft(CYExpression *lhs) {
1089 lhs_ = lhs;
1090 }
1091
5999c315 1092 virtual const char *Operator() const = 0;
d35a3b07 1093
4de0686f
JF
1094 CYPrecedence(16)
1095
3b52fd1a 1096 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1097 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1098};
1099
62014ea9 1100struct CYArgument :
96a7e5c2
JF
1101 CYNext<CYArgument>,
1102 CYThing
62014ea9 1103{
cf7d4c69
JF
1104 CYWord *name_;
1105 CYExpression *value_;
cf7d4c69 1106
3b52fd1a
JF
1107 CYArgument(CYExpression *value, CYArgument *next = NULL) :
1108 CYNext<CYArgument>(next),
1109 name_(NULL),
1110 value_(value)
1111 {
1112 }
1113
cf7d4c69 1114 CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) :
62014ea9 1115 CYNext<CYArgument>(next),
cf7d4c69 1116 name_(name),
62014ea9 1117 value_(value)
cf7d4c69
JF
1118 {
1119 }
5999c315 1120
5192746c 1121 CYArgument *Replace(CYContext &context);
652ec1ba 1122 void Output(CYOutput &out) const;
cf7d4c69
JF
1123};
1124
1125struct CYBlank :
1126 public CYWord
1127{
1128 CYBlank() :
1129 CYWord("")
1130 {
1131 }
1132};
1133
5999c315
JF
1134struct CYClause :
1135 CYThing,
1136 CYNext<CYClause>
1137{
cf7d4c69 1138 CYExpression *case_;
3b52fd1a 1139 CYStatement *statements_;
cf7d4c69 1140
3b52fd1a 1141 CYClause(CYExpression *_case, CYStatement *statements) :
cf7d4c69 1142 case_(_case),
3b52fd1a 1143 statements_(statements)
cf7d4c69
JF
1144 {
1145 }
1146
fa389b0f 1147 void Replace(CYContext &context);
652ec1ba 1148 virtual void Output(CYOutput &out) const;
cf7d4c69
JF
1149};
1150
62014ea9 1151struct CYElement :
96a7e5c2
JF
1152 CYNext<CYElement>,
1153 CYThing
62014ea9 1154{
cf7d4c69 1155 CYExpression *value_;
cf7d4c69
JF
1156
1157 CYElement(CYExpression *value, CYElement *next) :
62014ea9
JF
1158 CYNext<CYElement>(next),
1159 value_(value)
cf7d4c69
JF
1160 {
1161 }
5999c315 1162
3b52fd1a 1163 void Replace(CYContext &context);
652ec1ba 1164 void Output(CYOutput &out) const;
5befe15e
JF
1165};
1166
1167struct CYArray :
1168 CYLiteral
1169{
1170 CYElement *elements_;
1171
3b52fd1a 1172 CYArray(CYElement *elements = NULL) :
5befe15e
JF
1173 elements_(elements)
1174 {
1175 }
1176
3b52fd1a 1177 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1178 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1179};
1180
550ee46a
JF
1181struct CYProperty :
1182 CYNext<CYProperty>,
1183 CYThing
1184{
1185 CYPropertyName *name_;
1186 CYExpression *value_;
1187
1188 CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) :
1189 CYNext<CYProperty>(next),
1190 name_(name),
1191 value_(value)
1192 {
1193 }
1194
1195 void Replace(CYContext &context);
1196 virtual void Output(CYOutput &out) const;
1197};
1198
cf7d4c69
JF
1199struct CYDeclaration :
1200 CYForInInitialiser
1201{
1202 CYIdentifier *identifier_;
1203 CYExpression *initialiser_;
1204
550ee46a 1205 CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) :
cf7d4c69
JF
1206 identifier_(identifier),
1207 initialiser_(initialiser)
1208 {
1209 }
5999c315 1210
652ec1ba 1211 virtual void ForIn(CYOutput &out, CYFlags flags) const;
ad3b38bb 1212 virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value);
3b52fd1a 1213
029bc65b 1214 virtual CYExpression *Replace(CYContext &context);
15b88a33 1215
029bc65b 1216 virtual CYAssignment *Assignment(CYContext &context);
15b88a33 1217 CYVariable *Variable(CYContext &context);
75b0a457 1218
652ec1ba 1219 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1220};
1221
1222struct CYDeclarations :
cac61857 1223 CYNext<CYDeclarations>,
15b88a33 1224 CYThing
cf7d4c69
JF
1225{
1226 CYDeclaration *declaration_;
cf7d4c69 1227
cacd1a88 1228 CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) :
cac61857
JF
1229 CYNext<CYDeclarations>(next),
1230 declaration_(declaration)
1231 {
1232 }
1233
15b88a33 1234 void Replace(CYContext &context);
96a7e5c2 1235
15b88a33 1236 CYCompound *Compound(CYContext &context);
550ee46a 1237 CYProperty *Property(CYContext &context);
15b88a33
JF
1238 CYArgument *Argument(CYContext &context);
1239 CYFunctionParameter *Parameter(CYContext &context);
3b52fd1a 1240
96a7e5c2 1241 virtual void Output(CYOutput &out) const;
652ec1ba 1242 virtual void Output(CYOutput &out, CYFlags flags) const;
cac61857
JF
1243};
1244
15b88a33
JF
1245struct CYForDeclarations :
1246 CYForInitialiser
1247{
1248 CYDeclarations *declarations_;
1249
1250 CYForDeclarations(CYDeclarations *declarations) :
1251 declarations_(declarations)
1252 {
1253 }
1254
1255 virtual CYCompound *Replace(CYContext &context);
1256 virtual void Output(CYOutput &out, CYFlags flags) const;
1257};
1258
cac61857
JF
1259struct CYVar :
1260 CYStatement
1261{
1262 CYDeclarations *declarations_;
1263
1264 CYVar(CYDeclarations *declarations) :
1265 declarations_(declarations)
1266 {
1267 }
1268
3b52fd1a 1269 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1270 virtual void Output(CYOutput &out, CYFlags flags) const;
cac61857
JF
1271};
1272
c8a0500b 1273struct CYLetStatement :
cac61857
JF
1274 CYStatement
1275{
1276 CYDeclarations *declarations_;
15b88a33 1277 CYStatement *code_;
cac61857 1278
c8a0500b 1279 CYLetStatement(CYDeclarations *declarations, CYStatement *code) :
cac61857 1280 declarations_(declarations),
15b88a33 1281 code_(code)
cf7d4c69
JF
1282 {
1283 }
5999c315 1284
3b52fd1a 1285 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1286 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1287};
1288
cf7d4c69
JF
1289struct CYFor :
1290 CYStatement
1291{
1292 CYForInitialiser *initialiser_;
1293 CYExpression *test_;
1294 CYExpression *increment_;
1295 CYStatement *code_;
1296
1297 CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) :
1298 initialiser_(initialiser),
1299 test_(test),
1300 increment_(increment),
1301 code_(code)
1302 {
1303 }
5999c315 1304
3b52fd1a 1305 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1306 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1307};
1308
1309struct CYForIn :
1310 CYStatement
1311{
1312 CYForInInitialiser *initialiser_;
1313 CYExpression *set_;
1314 CYStatement *code_;
1315
1316 CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
1317 initialiser_(initialiser),
1318 set_(set),
1319 code_(code)
1320 {
1321 }
5999c315 1322
3b52fd1a 1323 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1324 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1325};
1326
d5618df7 1327struct CYForOf :
75b0a457
JF
1328 CYStatement
1329{
1330 CYForInInitialiser *initialiser_;
1331 CYExpression *set_;
1332 CYStatement *code_;
1333
d5618df7 1334 CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) :
75b0a457
JF
1335 initialiser_(initialiser),
1336 set_(set),
1337 code_(code)
1338 {
1339 }
1340
3b52fd1a 1341 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1342 virtual void Output(CYOutput &out, CYFlags flags) const;
75b0a457
JF
1343};
1344
693d501b
JF
1345struct CYObject :
1346 CYLiteral
1347{
3b52fd1a 1348 CYProperty *properties_;
693d501b 1349
ab2aa221 1350 CYObject(CYProperty *properties = NULL) :
3b52fd1a 1351 properties_(properties)
693d501b
JF
1352 {
1353 }
1354
3b52fd1a 1355 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1356 void Output(CYOutput &out, CYFlags flags) const;
693d501b
JF
1357};
1358
cf7d4c69
JF
1359struct CYMember :
1360 CYExpression
1361{
1362 CYExpression *object_;
1363 CYExpression *property_;
1364
1365 CYMember(CYExpression *object, CYExpression *property) :
1366 object_(object),
1367 property_(property)
1368 {
1369 }
5999c315 1370
9b5527f0
JF
1371 void SetLeft(CYExpression *object) {
1372 object_ = object;
1373 }
1374};
1375
1376struct CYDirectMember :
1377 CYMember
1378{
1379 CYDirectMember(CYExpression *object, CYExpression *property) :
1380 CYMember(object, property)
1381 {
1382 }
1383
1384 CYPrecedence(1)
fb98ac0c 1385 CYRightHand(false)
9b5527f0 1386
3b52fd1a 1387 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1388 virtual void Output(CYOutput &out, CYFlags flags) const;
9b5527f0
JF
1389};
1390
1391struct CYIndirectMember :
1392 CYMember
1393{
1394 CYIndirectMember(CYExpression *object, CYExpression *property) :
1395 CYMember(object, property)
1396 {
1397 }
1398
d35a3b07 1399 CYPrecedence(1)
fb98ac0c 1400 CYRightHand(false)
d35a3b07 1401
3b52fd1a 1402 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1403 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1404};
1405
2eb8215d
JF
1406namespace cy {
1407namespace Syntax {
1408
1409struct New :
cf7d4c69
JF
1410 CYExpression
1411{
1412 CYExpression *constructor_;
1413 CYArgument *arguments_;
1414
2eb8215d 1415 New(CYExpression *constructor, CYArgument *arguments) :
cf7d4c69
JF
1416 constructor_(constructor),
1417 arguments_(arguments)
1418 {
1419 }
5999c315 1420
fb98ac0c
JF
1421 virtual unsigned Precedence() const {
1422 return arguments_ == NULL ? 2 : 1;
1423 }
1424
1425 CYRightHand(false)
d35a3b07 1426
3b52fd1a 1427 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1428 virtual void Output(CYOutput &out, CYFlags flags) const;
6c093cce
JF
1429
1430 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
cf7d4c69
JF
1431};
1432
2eb8215d
JF
1433} }
1434
cf7d4c69
JF
1435struct CYCall :
1436 CYExpression
1437{
1438 CYExpression *function_;
1439 CYArgument *arguments_;
1440
3b52fd1a 1441 CYCall(CYExpression *function, CYArgument *arguments = NULL) :
cf7d4c69
JF
1442 function_(function),
1443 arguments_(arguments)
1444 {
1445 }
5999c315 1446
fb98ac0c
JF
1447 CYPrecedence(1)
1448 CYRightHand(false)
d35a3b07 1449
3b52fd1a 1450 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1451 virtual void Output(CYOutput &out, CYFlags flags) const;
6c093cce
JF
1452
1453 virtual CYExpression *AddArgument(CYContext &context, CYExpression *value);
1454};
1455
1456struct CYRubyProc;
1457
1458struct CYRubyBlock :
1459 CYExpression
1460{
1461 CYExpression *call_;
1462 CYRubyProc *proc_;
1463
1464 CYRubyBlock(CYExpression *call, CYRubyProc *proc) :
1465 call_(call),
1466 proc_(proc)
1467 {
1468 }
1469
1470 CYPrecedence(1)
1471 CYRightHand(false)
1472
1473 virtual CYExpression *Replace(CYContext &context);
1474 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1475};
1476
1477struct CYIf :
1478 CYStatement
1479{
1480 CYExpression *test_;
1481 CYStatement *true_;
1482 CYStatement *false_;
1483
3b52fd1a 1484 CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) :
cf7d4c69
JF
1485 test_(test),
1486 true_(_true),
1487 false_(_false)
1488 {
1489 }
5999c315 1490
3b52fd1a 1491 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1492 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1493};
1494
1495struct CYDoWhile :
1496 CYStatement
1497{
1498 CYExpression *test_;
1499 CYStatement *code_;
1500
1501 CYDoWhile(CYExpression *test, CYStatement *code) :
1502 test_(test),
1503 code_(code)
1504 {
1505 }
5999c315 1506
3b52fd1a 1507 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1508 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1509};
1510
1511struct CYWhile :
1512 CYStatement
1513{
1514 CYExpression *test_;
1515 CYStatement *code_;
1516
1517 CYWhile(CYExpression *test, CYStatement *code) :
1518 test_(test),
1519 code_(code)
1520 {
1521 }
5999c315 1522
3b52fd1a 1523 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1524 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1525};
1526
6c093cce 1527// XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass)
a846a8cd 1528struct CYFunction {
cf7d4c69 1529 CYIdentifier *name_;
b09da87b 1530 CYFunctionParameter *parameters_;
3b52fd1a 1531 CYBlock code_;
a0be43fc 1532
ab2aa221 1533 CYNonLocal *nonlocal_;
a0be43fc 1534 CYThisScope this_;
cf7d4c69 1535
3b52fd1a 1536 CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
cf7d4c69
JF
1537 name_(name),
1538 parameters_(parameters),
ab2aa221
JF
1539 code_(statements),
1540 nonlocal_(NULL)
cf7d4c69
JF
1541 {
1542 }
5999c315 1543
7c6c5b0a
JF
1544 virtual ~CYFunction() {
1545 }
1546
14ec9e00
JF
1547 void Inject(CYContext &context);
1548 virtual void Replace_(CYContext &context, bool outer);
fb98ac0c
JF
1549 virtual void Output(CYOutput &out, CYFlags flags) const;
1550};
1551
6c093cce 1552// XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression
fb98ac0c
JF
1553struct CYFunctionExpression :
1554 CYFunction,
1555 CYExpression
1556{
3b52fd1a
JF
1557 CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1558 CYFunction(name, parameters, statements)
fb98ac0c
JF
1559 {
1560 }
1561
a0be43fc
JF
1562 CYPrecedence(0)
1563 CYRightHand(false)
1564
1565 virtual CYExpression *Replace(CYContext &context);
1566 virtual void Output(CYOutput &out, CYFlags flags) const;
1567};
1568
1569// XXX: this should derive from CYAnonymousFunction
1570struct CYFatArrow :
1571 CYFunction,
1572 CYExpression
1573{
1574 CYFatArrow(CYFunctionParameter *parameters, CYStatement *statements) :
1575 CYFunction(NULL, parameters, statements)
1576 {
1577 }
1578
d35a3b07 1579 CYPrecedence(0)
fb98ac0c 1580 CYRightHand(false)
d35a3b07 1581
3b52fd1a 1582 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1583 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1584};
1585
6c093cce
JF
1586// XXX: this should derive from CYAnonymousFunctionExpression
1587struct CYRubyProc :
1588 CYFunctionExpression
1589{
1590 CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) :
1591 CYFunctionExpression(NULL, parameters, statements)
1592 {
1593 }
1594
1595 virtual CYExpression *Replace(CYContext &context);
1596 virtual void Output(CYOutput &out, CYFlags flags) const;
1597};
1598
1599// XXX: this should derive from CYNamedFunction
fb98ac0c
JF
1600struct CYFunctionStatement :
1601 CYFunction,
b10bd496 1602 CYStatement
cf7d4c69 1603{
3b52fd1a
JF
1604 CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) :
1605 CYFunction(name, parameters, statements)
cf7d4c69
JF
1606 {
1607 }
5999c315 1608
3b52fd1a 1609 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1610 virtual void Output(CYOutput &out, CYFlags flags) const;
5999c315
JF
1611};
1612
1613struct CYExpress :
1614 CYStatement
1615{
1616 CYExpression *expression_;
1617
1618 CYExpress(CYExpression *expression) :
1619 expression_(expression)
1620 {
029bc65b
JF
1621 if (expression == NULL)
1622 throw;
5999c315
JF
1623 }
1624
3b52fd1a 1625 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1626 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1627};
1628
1629struct CYContinue :
1630 CYStatement
1631{
1632 CYIdentifier *label_;
1633
1634 CYContinue(CYIdentifier *label) :
1635 label_(label)
1636 {
1637 }
5999c315 1638
3b52fd1a 1639 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1640 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1641};
1642
1643struct CYBreak :
1644 CYStatement
1645{
1646 CYIdentifier *label_;
1647
1648 CYBreak(CYIdentifier *label) :
1649 label_(label)
1650 {
1651 }
5999c315 1652
3b52fd1a 1653 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1654 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1655};
1656
1657struct CYReturn :
1658 CYStatement
1659{
1660 CYExpression *value_;
1661
1662 CYReturn(CYExpression *value) :
1663 value_(value)
1664 {
1665 }
5999c315 1666
3b52fd1a 1667 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1668 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1669};
1670
1671struct CYEmpty :
1672 CYStatement
1673{
3b52fd1a 1674 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1675 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1676};
1677
96a7e5c2
JF
1678struct CYFinally :
1679 CYThing
1680{
3b52fd1a 1681 CYBlock code_;
b10bd496 1682
3b52fd1a
JF
1683 CYFinally(CYStatement *statements) :
1684 code_(statements)
b10bd496
JF
1685 {
1686 }
1687
3b52fd1a 1688 void Replace(CYContext &context);
b10bd496
JF
1689 virtual void Output(CYOutput &out) const;
1690};
1691
37954781
JF
1692namespace cy {
1693namespace Syntax {
1694
1695struct Catch :
1696 CYThing
1697{
1698 CYIdentifier *name_;
1699 CYBlock code_;
1700
1701 Catch(CYIdentifier *name, CYStatement *statements) :
1702 name_(name),
1703 code_(statements)
1704 {
1705 }
1706
1707 void Replace(CYContext &context);
1708 virtual void Output(CYOutput &out) const;
1709};
1710
1711struct Try :
cf7d4c69
JF
1712 CYStatement
1713{
3b52fd1a 1714 CYBlock code_;
37954781 1715 Catch *catch_;
b10bd496 1716 CYFinally *finally_;
cf7d4c69 1717
37954781 1718 Try(CYStatement *statements, Catch *_catch, CYFinally *finally) :
3b52fd1a 1719 code_(statements),
cf7d4c69
JF
1720 catch_(_catch),
1721 finally_(finally)
1722 {
1723 }
5999c315 1724
3b52fd1a 1725 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1726 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1727};
1728
37954781 1729struct Throw :
cf7d4c69
JF
1730 CYStatement
1731{
1732 CYExpression *value_;
1733
ab2aa221 1734 Throw(CYExpression *value = NULL) :
cf7d4c69
JF
1735 value_(value)
1736 {
1737 }
5999c315 1738
3b52fd1a 1739 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1740 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1741};
1742
37954781
JF
1743} }
1744
cf7d4c69
JF
1745struct CYWith :
1746 CYStatement
1747{
1748 CYExpression *scope_;
1749 CYStatement *code_;
1750
1751 CYWith(CYExpression *scope, CYStatement *code) :
1752 scope_(scope),
1753 code_(code)
1754 {
1755 }
5999c315 1756
3b52fd1a 1757 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1758 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1759};
1760
1761struct CYSwitch :
1762 CYStatement
1763{
1764 CYExpression *value_;
1765 CYClause *clauses_;
1766
1767 CYSwitch(CYExpression *value, CYClause *clauses) :
1768 value_(value),
1769 clauses_(clauses)
1770 {
1771 }
5999c315 1772
3b52fd1a 1773 virtual CYStatement *Replace(CYContext &context);
c8a0500b
JF
1774 virtual void Output(CYOutput &out, CYFlags flags) const;
1775};
1776
1777struct CYDebugger :
1778 CYStatement
1779{
1780 CYDebugger()
1781 {
1782 }
1783
1784 virtual CYStatement *Replace(CYContext &context);
fb98ac0c 1785 virtual void Output(CYOutput &out, CYFlags flags) const;
cf7d4c69
JF
1786};
1787
1788struct CYCondition :
1789 CYExpression
1790{
1791 CYExpression *test_;
1792 CYExpression *true_;
1793 CYExpression *false_;
1794
1795 CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) :
91a416e4 1796 test_(test),
cf7d4c69
JF
1797 true_(_true),
1798 false_(_false)
1799 {
1800 }
5999c315 1801
d35a3b07
JF
1802 CYPrecedence(15)
1803
3b52fd1a 1804 virtual CYExpression *Replace(CYContext &context);
652ec1ba 1805 virtual void Output(CYOutput &out, CYFlags flags) const;
5999c315
JF
1806};
1807
1808struct CYAddressOf :
1809 CYPrefix
1810{
1811 CYAddressOf(CYExpression *rhs) :
1812 CYPrefix(rhs)
1813 {
1814 }
1815
1816 virtual const char *Operator() const {
1817 return "&";
1818 }
1819
b09da87b 1820 CYAlphabetic(false)
d35a3b07 1821
3b52fd1a 1822 virtual CYExpression *Replace(CYContext &context);
5999c315
JF
1823};
1824
1825struct CYIndirect :
1826 CYPrefix
1827{
1828 CYIndirect(CYExpression *rhs) :
1829 CYPrefix(rhs)
1830 {
1831 }
1832
1833 virtual const char *Operator() const {
561ac418 1834 return "*";
5999c315
JF
1835 }
1836
b09da87b 1837 CYAlphabetic(false)
d35a3b07 1838
3b52fd1a 1839 virtual CYExpression *Replace(CYContext &context);
cf7d4c69
JF
1840};
1841
4644480a
JF
1842#define CYReplace \
1843 virtual CYExpression *Replace(CYContext &context);
1844
1845#define CYPostfix_(op, name, args...) \
cf7d4c69
JF
1846 struct CY ## name : \
1847 CYPostfix \
4644480a 1848 { args \
cf7d4c69
JF
1849 CY ## name(CYExpression *lhs) : \
1850 CYPostfix(lhs) \
1851 { \
1852 } \
5999c315
JF
1853 \
1854 virtual const char *Operator() const { \
1855 return op; \
1856 } \
cf7d4c69
JF
1857 };
1858
4644480a 1859#define CYPrefix_(alphabetic, op, name, args...) \
cf7d4c69
JF
1860 struct CY ## name : \
1861 CYPrefix \
4644480a 1862 { args \
cf7d4c69
JF
1863 CY ## name(CYExpression *rhs) : \
1864 CYPrefix(rhs) \
1865 { \
1866 } \
d35a3b07 1867 \
b09da87b 1868 CYAlphabetic(alphabetic) \
5999c315
JF
1869 \
1870 virtual const char *Operator() const { \
1871 return op; \
1872 } \
cf7d4c69
JF
1873 };
1874
4644480a 1875#define CYInfix_(alphabetic, precedence, op, name, args...) \
cf7d4c69
JF
1876 struct CY ## name : \
1877 CYInfix \
4644480a 1878 { args \
cf7d4c69
JF
1879 CY ## name(CYExpression *lhs, CYExpression *rhs) : \
1880 CYInfix(lhs, rhs) \
1881 { \
1882 } \
d35a3b07 1883 \
b09da87b 1884 CYAlphabetic(alphabetic) \
d35a3b07 1885 CYPrecedence(precedence) \
5999c315
JF
1886 \
1887 virtual const char *Operator() const { \
1888 return op; \
1889 } \
cf7d4c69
JF
1890 };
1891
4644480a 1892#define CYAssignment_(op, name, args...) \
cf7d4c69
JF
1893 struct CY ## name ## Assign : \
1894 CYAssignment \
4644480a 1895 { args \
cf7d4c69
JF
1896 CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \
1897 CYAssignment(lhs, rhs) \
1898 { \
1899 } \
5999c315
JF
1900 \
1901 virtual const char *Operator() const { \
1902 return op; \
1903 } \
cf7d4c69
JF
1904 };
1905
1906CYPostfix_("++", PostIncrement)
1907CYPostfix_("--", PostDecrement)
1908
b09da87b
JF
1909CYPrefix_(true, "delete", Delete)
1910CYPrefix_(true, "void", Void)
1911CYPrefix_(true, "typeof", TypeOf)
1912CYPrefix_(false, "++", PreIncrement)
1913CYPrefix_(false, "--", PreDecrement)
c0bc320e 1914CYPrefix_(false, "+", Affirm)
b09da87b
JF
1915CYPrefix_(false, "-", Negate)
1916CYPrefix_(false, "~", BitwiseNot)
1917CYPrefix_(false, "!", LogicalNot)
1918
1919CYInfix_(false, 5, "*", Multiply)
1920CYInfix_(false, 5, "/", Divide)
1921CYInfix_(false, 5, "%", Modulus)
4644480a 1922CYInfix_(false, 6, "+", Add, CYReplace)
b09da87b
JF
1923CYInfix_(false, 6, "-", Subtract)
1924CYInfix_(false, 7, "<<", ShiftLeft)
1925CYInfix_(false, 7, ">>", ShiftRightSigned)
1926CYInfix_(false, 7, ">>>", ShiftRightUnsigned)
1927CYInfix_(false, 8, "<", Less)
1928CYInfix_(false, 8, ">", Greater)
1929CYInfix_(false, 8, "<=", LessOrEqual)
1930CYInfix_(false, 8, ">=", GreaterOrEqual)
1931CYInfix_(true, 8, "instanceof", InstanceOf)
1932CYInfix_(true, 8, "in", In)
1933CYInfix_(false, 9, "==", Equal)
1934CYInfix_(false, 9, "!=", NotEqual)
1935CYInfix_(false, 9, "===", Identical)
1936CYInfix_(false, 9, "!==", NotIdentical)
1937CYInfix_(false, 10, "&", BitwiseAnd)
1938CYInfix_(false, 11, "^", BitwiseXOr)
1939CYInfix_(false, 12, "|", BitwiseOr)
1940CYInfix_(false, 13, "&&", LogicalAnd)
1941CYInfix_(false, 14, "||", LogicalOr)
cf7d4c69
JF
1942
1943CYAssignment_("=", )
1944CYAssignment_("*=", Multiply)
1945CYAssignment_("/=", Divide)
1946CYAssignment_("%=", Modulus)
1947CYAssignment_("+=", Add)
1948CYAssignment_("-=", Subtract)
1949CYAssignment_("<<=", ShiftLeft)
1950CYAssignment_(">>=", ShiftRightSigned)
1951CYAssignment_(">>>=", ShiftRightUnsigned)
1952CYAssignment_("&=", BitwiseAnd)
1953CYAssignment_("^=", BitwiseXOr)
1954CYAssignment_("|=", BitwiseOr)
1955
c5fa2867 1956#endif/*CYCRIPT_PARSER_HPP*/