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