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