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