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