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