]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime | |
2 | * Copyright (C) 2009-2010 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 | CYSetLast(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 | CYSetLast(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 | ||
632 | struct CYDeclaration; | |
633 | ||
634 | struct CYFunctionParameter : | |
635 | CYNext<CYFunctionParameter>, | |
636 | CYThing | |
637 | { | |
638 | CYForInInitialiser *initialiser_; | |
639 | ||
640 | CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) : | |
641 | CYNext<CYFunctionParameter>(next), | |
642 | initialiser_(initialiser) | |
643 | { | |
644 | } | |
645 | ||
646 | void Replace(CYContext &context, CYBlock &code); | |
647 | void Output(CYOutput &out) const; | |
648 | }; | |
649 | ||
650 | struct CYComprehension : | |
651 | CYNext<CYComprehension>, | |
652 | CYThing | |
653 | { | |
654 | virtual const char *Name() const = 0; | |
655 | ||
656 | virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; | |
657 | CYFunctionParameter *Parameters(CYContext &context) const; | |
658 | virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; | |
659 | virtual void Output(CYOutput &out) const = 0; | |
660 | }; | |
661 | ||
662 | struct CYForInComprehension : | |
663 | CYComprehension | |
664 | { | |
665 | CYIdentifier *name_; | |
666 | CYExpression *set_; | |
667 | ||
668 | CYForInComprehension(CYIdentifier *name, CYExpression *set) : | |
669 | name_(name), | |
670 | set_(set) | |
671 | { | |
672 | } | |
673 | ||
674 | virtual const char *Name() const { | |
675 | return name_->Word(); | |
676 | } | |
677 | ||
678 | virtual CYFunctionParameter *Parameter(CYContext &context) const; | |
679 | virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; | |
680 | virtual void Output(CYOutput &out) const; | |
681 | }; | |
682 | ||
683 | struct CYForOfComprehension : | |
684 | CYComprehension | |
685 | { | |
686 | CYIdentifier *name_; | |
687 | CYExpression *set_; | |
688 | ||
689 | CYForOfComprehension(CYIdentifier *name, CYExpression *set) : | |
690 | name_(name), | |
691 | set_(set) | |
692 | { | |
693 | } | |
694 | ||
695 | virtual const char *Name() const { | |
696 | return name_->Word(); | |
697 | } | |
698 | ||
699 | virtual CYFunctionParameter *Parameter(CYContext &context) const; | |
700 | virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; | |
701 | virtual void Output(CYOutput &out) const; | |
702 | }; | |
703 | ||
704 | struct CYIfComprehension : | |
705 | CYComprehension | |
706 | { | |
707 | CYExpression *test_; | |
708 | ||
709 | CYIfComprehension(CYExpression *test) : | |
710 | test_(test) | |
711 | { | |
712 | } | |
713 | ||
714 | virtual const char *Name() const { | |
715 | return NULL; | |
716 | } | |
717 | ||
718 | virtual CYFunctionParameter *Parameter(CYContext &context) const; | |
719 | virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; | |
720 | virtual void Output(CYOutput &out) const; | |
721 | }; | |
722 | ||
723 | struct CYArrayComprehension : | |
724 | CYExpression | |
725 | { | |
726 | CYExpression *expression_; | |
727 | CYComprehension *comprehensions_; | |
728 | ||
729 | CYArrayComprehension(CYExpression *expression, CYComprehension *comprehensions) : | |
730 | expression_(expression), | |
731 | comprehensions_(comprehensions) | |
732 | { | |
733 | } | |
734 | ||
735 | CYPrecedence(0) | |
736 | ||
737 | virtual CYExpression *Replace(CYContext &context); | |
738 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
739 | }; | |
740 | ||
741 | struct CYLiteral : | |
742 | CYExpression | |
743 | { | |
744 | CYPrecedence(0) | |
745 | CYRightHand(false) | |
746 | }; | |
747 | ||
748 | struct CYTrivial : | |
749 | CYLiteral | |
750 | { | |
751 | virtual CYExpression *Replace(CYContext &context); | |
752 | }; | |
753 | ||
754 | struct CYMagic : | |
755 | CYExpression | |
756 | { | |
757 | CYPrecedence(0) | |
758 | CYRightHand(false) | |
759 | }; | |
760 | ||
761 | struct CYRange { | |
762 | uint64_t lo_; | |
763 | uint64_t hi_; | |
764 | ||
765 | CYRange(uint64_t lo, uint64_t hi) : | |
766 | lo_(lo), hi_(hi) | |
767 | { | |
768 | } | |
769 | ||
770 | bool operator [](uint8_t value) const { | |
771 | return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1; | |
772 | } | |
773 | ||
774 | void operator()(uint8_t value) { | |
775 | if (value >> 7) | |
776 | return; | |
777 | (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f); | |
778 | } | |
779 | }; | |
780 | ||
781 | extern CYRange DigitRange_; | |
782 | extern CYRange WordStartRange_; | |
783 | extern CYRange WordEndRange_; | |
784 | ||
785 | struct CYString : | |
786 | CYTrivial, | |
787 | CYPropertyName | |
788 | { | |
789 | const char *value_; | |
790 | size_t size_; | |
791 | ||
792 | CYString() : | |
793 | value_(NULL), | |
794 | size_(0) | |
795 | { | |
796 | } | |
797 | ||
798 | CYString(const char *value) : | |
799 | value_(value), | |
800 | size_(strlen(value)) | |
801 | { | |
802 | } | |
803 | ||
804 | CYString(const char *value, size_t size) : | |
805 | value_(value), | |
806 | size_(size) | |
807 | { | |
808 | } | |
809 | ||
810 | CYString(const CYWord *word) : | |
811 | value_(word->Word()), | |
812 | size_(strlen(value_)) | |
813 | { | |
814 | } | |
815 | ||
816 | const char *Value() const { | |
817 | return value_; | |
818 | } | |
819 | ||
820 | virtual const char *Word() const; | |
821 | ||
822 | virtual CYNumber *Number(CYContext &context); | |
823 | virtual CYString *String(CYContext &context); | |
824 | ||
825 | CYString *Concat(CYContext &out, CYString *rhs) const; | |
826 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
827 | virtual void PropertyName(CYOutput &out) const; | |
828 | }; | |
829 | ||
830 | struct CYNumber : | |
831 | CYTrivial, | |
832 | CYPropertyName | |
833 | { | |
834 | double value_; | |
835 | ||
836 | CYNumber(double value) : | |
837 | value_(value) | |
838 | { | |
839 | } | |
840 | ||
841 | double Value() const { | |
842 | return value_; | |
843 | } | |
844 | ||
845 | virtual CYNumber *Number(CYContext &context); | |
846 | virtual CYString *String(CYContext &context); | |
847 | ||
848 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
849 | virtual void PropertyName(CYOutput &out) const; | |
850 | }; | |
851 | ||
852 | struct CYRegEx : | |
853 | CYTrivial | |
854 | { | |
855 | const char *value_; | |
856 | ||
857 | CYRegEx(const char *value) : | |
858 | value_(value) | |
859 | { | |
860 | } | |
861 | ||
862 | const char *Value() const { | |
863 | return value_; | |
864 | } | |
865 | ||
866 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
867 | }; | |
868 | ||
869 | struct CYNull : | |
870 | CYWord, | |
871 | CYTrivial | |
872 | { | |
873 | CYNull() : | |
874 | CYWord("null") | |
875 | { | |
876 | } | |
877 | ||
878 | virtual CYNumber *Number(CYContext &context); | |
879 | virtual CYString *String(CYContext &context); | |
880 | ||
881 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
882 | }; | |
883 | ||
884 | struct CYThis : | |
885 | CYWord, | |
886 | CYMagic | |
887 | { | |
888 | CYThis() : | |
889 | CYWord("this") | |
890 | { | |
891 | } | |
892 | ||
893 | virtual CYExpression *Replace(CYContext &context); | |
894 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
895 | }; | |
896 | ||
897 | struct CYBoolean : | |
898 | CYTrivial | |
899 | { | |
900 | virtual bool Value() const = 0; | |
901 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
902 | }; | |
903 | ||
904 | struct CYFalse : | |
905 | CYWord, | |
906 | CYBoolean | |
907 | { | |
908 | CYFalse() : | |
909 | CYWord("false") | |
910 | { | |
911 | } | |
912 | ||
913 | virtual bool Value() const { | |
914 | return false; | |
915 | } | |
916 | ||
917 | virtual CYNumber *Number(CYContext &context); | |
918 | virtual CYString *String(CYContext &context); | |
919 | }; | |
920 | ||
921 | struct CYTrue : | |
922 | CYWord, | |
923 | CYBoolean | |
924 | { | |
925 | CYTrue() : | |
926 | CYWord("true") | |
927 | { | |
928 | } | |
929 | ||
930 | virtual bool Value() const { | |
931 | return true; | |
932 | } | |
933 | ||
934 | virtual CYNumber *Number(CYContext &context); | |
935 | virtual CYString *String(CYContext &context); | |
936 | }; | |
937 | ||
938 | struct CYVariable : | |
939 | CYExpression | |
940 | { | |
941 | CYIdentifier *name_; | |
942 | ||
943 | CYVariable(CYIdentifier *name) : | |
944 | name_(name) | |
945 | { | |
946 | } | |
947 | ||
948 | CYVariable(const char *name) : | |
949 | name_(new($pool) CYIdentifier(name)) | |
950 | { | |
951 | } | |
952 | ||
953 | CYPrecedence(0) | |
954 | CYRightHand(false) | |
955 | ||
956 | virtual CYExpression *Replace(CYContext &context); | |
957 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
958 | }; | |
959 | ||
960 | struct CYPrefix : | |
961 | CYExpression | |
962 | { | |
963 | CYExpression *rhs_; | |
964 | ||
965 | CYPrefix(CYExpression *rhs) : | |
966 | rhs_(rhs) | |
967 | { | |
968 | } | |
969 | ||
970 | virtual bool Alphabetic() const = 0; | |
971 | virtual const char *Operator() const = 0; | |
972 | ||
973 | CYPrecedence(4) | |
974 | ||
975 | virtual CYExpression *Replace(CYContext &context); | |
976 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
977 | }; | |
978 | ||
979 | struct CYInfix : | |
980 | CYExpression | |
981 | { | |
982 | CYExpression *lhs_; | |
983 | CYExpression *rhs_; | |
984 | ||
985 | CYInfix(CYExpression *lhs, CYExpression *rhs) : | |
986 | lhs_(lhs), | |
987 | rhs_(rhs) | |
988 | { | |
989 | } | |
990 | ||
991 | void SetLeft(CYExpression *lhs) { | |
992 | lhs_ = lhs; | |
993 | } | |
994 | ||
995 | virtual bool Alphabetic() const = 0; | |
996 | virtual const char *Operator() const = 0; | |
997 | ||
998 | virtual CYExpression *Replace(CYContext &context); | |
999 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1000 | }; | |
1001 | ||
1002 | struct CYPostfix : | |
1003 | CYExpression | |
1004 | { | |
1005 | CYExpression *lhs_; | |
1006 | ||
1007 | CYPostfix(CYExpression *lhs) : | |
1008 | lhs_(lhs) | |
1009 | { | |
1010 | } | |
1011 | ||
1012 | virtual const char *Operator() const = 0; | |
1013 | ||
1014 | CYPrecedence(3) | |
1015 | ||
1016 | virtual CYExpression *Replace(CYContext &context); | |
1017 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1018 | }; | |
1019 | ||
1020 | struct CYAssignment : | |
1021 | CYExpression | |
1022 | { | |
1023 | CYExpression *lhs_; | |
1024 | CYExpression *rhs_; | |
1025 | ||
1026 | CYAssignment(CYExpression *lhs, CYExpression *rhs) : | |
1027 | lhs_(lhs), | |
1028 | rhs_(rhs) | |
1029 | { | |
1030 | } | |
1031 | ||
1032 | void SetLeft(CYExpression *lhs) { | |
1033 | lhs_ = lhs; | |
1034 | } | |
1035 | ||
1036 | virtual const char *Operator() const = 0; | |
1037 | ||
1038 | CYPrecedence(16) | |
1039 | ||
1040 | virtual CYExpression *Replace(CYContext &context); | |
1041 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1042 | }; | |
1043 | ||
1044 | struct CYArgument : | |
1045 | CYNext<CYArgument>, | |
1046 | CYThing | |
1047 | { | |
1048 | CYWord *name_; | |
1049 | CYExpression *value_; | |
1050 | ||
1051 | CYArgument(CYExpression *value, CYArgument *next = NULL) : | |
1052 | CYNext<CYArgument>(next), | |
1053 | name_(NULL), | |
1054 | value_(value) | |
1055 | { | |
1056 | } | |
1057 | ||
1058 | CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) : | |
1059 | CYNext<CYArgument>(next), | |
1060 | name_(name), | |
1061 | value_(value) | |
1062 | { | |
1063 | } | |
1064 | ||
1065 | CYArgument *Replace(CYContext &context); | |
1066 | void Output(CYOutput &out) const; | |
1067 | }; | |
1068 | ||
1069 | struct CYBlank : | |
1070 | public CYWord | |
1071 | { | |
1072 | CYBlank() : | |
1073 | CYWord("") | |
1074 | { | |
1075 | } | |
1076 | }; | |
1077 | ||
1078 | struct CYClause : | |
1079 | CYThing, | |
1080 | CYNext<CYClause> | |
1081 | { | |
1082 | CYExpression *case_; | |
1083 | CYStatement *statements_; | |
1084 | ||
1085 | CYClause(CYExpression *_case, CYStatement *statements) : | |
1086 | case_(_case), | |
1087 | statements_(statements) | |
1088 | { | |
1089 | } | |
1090 | ||
1091 | void Replace(CYContext &context); | |
1092 | virtual void Output(CYOutput &out) const; | |
1093 | }; | |
1094 | ||
1095 | struct CYElement : | |
1096 | CYNext<CYElement>, | |
1097 | CYThing | |
1098 | { | |
1099 | CYExpression *value_; | |
1100 | ||
1101 | CYElement(CYExpression *value, CYElement *next) : | |
1102 | CYNext<CYElement>(next), | |
1103 | value_(value) | |
1104 | { | |
1105 | } | |
1106 | ||
1107 | void Replace(CYContext &context); | |
1108 | void Output(CYOutput &out) const; | |
1109 | }; | |
1110 | ||
1111 | struct CYArray : | |
1112 | CYLiteral | |
1113 | { | |
1114 | CYElement *elements_; | |
1115 | ||
1116 | CYArray(CYElement *elements = NULL) : | |
1117 | elements_(elements) | |
1118 | { | |
1119 | } | |
1120 | ||
1121 | virtual CYExpression *Replace(CYContext &context); | |
1122 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1123 | }; | |
1124 | ||
1125 | struct CYProperty : | |
1126 | CYNext<CYProperty>, | |
1127 | CYThing | |
1128 | { | |
1129 | CYPropertyName *name_; | |
1130 | CYExpression *value_; | |
1131 | ||
1132 | CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next = NULL) : | |
1133 | CYNext<CYProperty>(next), | |
1134 | name_(name), | |
1135 | value_(value) | |
1136 | { | |
1137 | } | |
1138 | ||
1139 | void Replace(CYContext &context); | |
1140 | virtual void Output(CYOutput &out) const; | |
1141 | }; | |
1142 | ||
1143 | struct CYDeclaration : | |
1144 | CYForInInitialiser | |
1145 | { | |
1146 | CYIdentifier *identifier_; | |
1147 | CYExpression *initialiser_; | |
1148 | ||
1149 | CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser = NULL) : | |
1150 | identifier_(identifier), | |
1151 | initialiser_(initialiser) | |
1152 | { | |
1153 | } | |
1154 | ||
1155 | virtual void ForIn(CYOutput &out, CYFlags flags) const; | |
1156 | virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value); | |
1157 | ||
1158 | virtual CYExpression *Replace(CYContext &context); | |
1159 | ||
1160 | virtual CYAssignment *Assignment(CYContext &context); | |
1161 | CYVariable *Variable(CYContext &context); | |
1162 | ||
1163 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1164 | }; | |
1165 | ||
1166 | struct CYDeclarations : | |
1167 | CYNext<CYDeclarations>, | |
1168 | CYThing | |
1169 | { | |
1170 | CYDeclaration *declaration_; | |
1171 | ||
1172 | CYDeclarations(CYDeclaration *declaration, CYDeclarations *next = NULL) : | |
1173 | CYNext<CYDeclarations>(next), | |
1174 | declaration_(declaration) | |
1175 | { | |
1176 | } | |
1177 | ||
1178 | void Replace(CYContext &context); | |
1179 | ||
1180 | CYCompound *Compound(CYContext &context); | |
1181 | CYProperty *Property(CYContext &context); | |
1182 | CYArgument *Argument(CYContext &context); | |
1183 | CYFunctionParameter *Parameter(CYContext &context); | |
1184 | ||
1185 | virtual void Output(CYOutput &out) const; | |
1186 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1187 | }; | |
1188 | ||
1189 | struct CYForDeclarations : | |
1190 | CYForInitialiser | |
1191 | { | |
1192 | CYDeclarations *declarations_; | |
1193 | ||
1194 | CYForDeclarations(CYDeclarations *declarations) : | |
1195 | declarations_(declarations) | |
1196 | { | |
1197 | } | |
1198 | ||
1199 | virtual CYCompound *Replace(CYContext &context); | |
1200 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1201 | }; | |
1202 | ||
1203 | struct CYVar : | |
1204 | CYStatement | |
1205 | { | |
1206 | CYDeclarations *declarations_; | |
1207 | ||
1208 | CYVar(CYDeclarations *declarations) : | |
1209 | declarations_(declarations) | |
1210 | { | |
1211 | } | |
1212 | ||
1213 | virtual CYStatement *Replace(CYContext &context); | |
1214 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1215 | }; | |
1216 | ||
1217 | struct CYLetStatement : | |
1218 | CYStatement | |
1219 | { | |
1220 | CYDeclarations *declarations_; | |
1221 | CYStatement *code_; | |
1222 | ||
1223 | CYLetStatement(CYDeclarations *declarations, CYStatement *code) : | |
1224 | declarations_(declarations), | |
1225 | code_(code) | |
1226 | { | |
1227 | } | |
1228 | ||
1229 | virtual CYStatement *Replace(CYContext &context); | |
1230 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1231 | }; | |
1232 | ||
1233 | struct CYFor : | |
1234 | CYStatement | |
1235 | { | |
1236 | CYForInitialiser *initialiser_; | |
1237 | CYExpression *test_; | |
1238 | CYExpression *increment_; | |
1239 | CYStatement *code_; | |
1240 | ||
1241 | CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : | |
1242 | initialiser_(initialiser), | |
1243 | test_(test), | |
1244 | increment_(increment), | |
1245 | code_(code) | |
1246 | { | |
1247 | } | |
1248 | ||
1249 | virtual CYStatement *Replace(CYContext &context); | |
1250 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1251 | }; | |
1252 | ||
1253 | struct CYForIn : | |
1254 | CYStatement | |
1255 | { | |
1256 | CYForInInitialiser *initialiser_; | |
1257 | CYExpression *set_; | |
1258 | CYStatement *code_; | |
1259 | ||
1260 | CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : | |
1261 | initialiser_(initialiser), | |
1262 | set_(set), | |
1263 | code_(code) | |
1264 | { | |
1265 | } | |
1266 | ||
1267 | virtual CYStatement *Replace(CYContext &context); | |
1268 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1269 | }; | |
1270 | ||
1271 | struct CYForOf : | |
1272 | CYStatement | |
1273 | { | |
1274 | CYForInInitialiser *initialiser_; | |
1275 | CYExpression *set_; | |
1276 | CYStatement *code_; | |
1277 | ||
1278 | CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : | |
1279 | initialiser_(initialiser), | |
1280 | set_(set), | |
1281 | code_(code) | |
1282 | { | |
1283 | } | |
1284 | ||
1285 | virtual CYStatement *Replace(CYContext &context); | |
1286 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1287 | }; | |
1288 | ||
1289 | struct CYObject : | |
1290 | CYLiteral | |
1291 | { | |
1292 | CYProperty *properties_; | |
1293 | ||
1294 | CYObject(CYProperty *properties = NULL) : | |
1295 | properties_(properties) | |
1296 | { | |
1297 | } | |
1298 | ||
1299 | virtual CYExpression *Replace(CYContext &context); | |
1300 | void Output(CYOutput &out, CYFlags flags) const; | |
1301 | }; | |
1302 | ||
1303 | struct CYMember : | |
1304 | CYExpression | |
1305 | { | |
1306 | CYExpression *object_; | |
1307 | CYExpression *property_; | |
1308 | ||
1309 | CYMember(CYExpression *object, CYExpression *property) : | |
1310 | object_(object), | |
1311 | property_(property) | |
1312 | { | |
1313 | } | |
1314 | ||
1315 | void SetLeft(CYExpression *object) { | |
1316 | object_ = object; | |
1317 | } | |
1318 | }; | |
1319 | ||
1320 | struct CYDirectMember : | |
1321 | CYMember | |
1322 | { | |
1323 | CYDirectMember(CYExpression *object, CYExpression *property) : | |
1324 | CYMember(object, property) | |
1325 | { | |
1326 | } | |
1327 | ||
1328 | CYPrecedence(1) | |
1329 | CYRightHand(false) | |
1330 | ||
1331 | virtual CYExpression *Replace(CYContext &context); | |
1332 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1333 | }; | |
1334 | ||
1335 | struct CYIndirectMember : | |
1336 | CYMember | |
1337 | { | |
1338 | CYIndirectMember(CYExpression *object, CYExpression *property) : | |
1339 | CYMember(object, property) | |
1340 | { | |
1341 | } | |
1342 | ||
1343 | CYPrecedence(1) | |
1344 | CYRightHand(false) | |
1345 | ||
1346 | virtual CYExpression *Replace(CYContext &context); | |
1347 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1348 | }; | |
1349 | ||
1350 | namespace cy { | |
1351 | namespace Syntax { | |
1352 | ||
1353 | struct New : | |
1354 | CYExpression | |
1355 | { | |
1356 | CYExpression *constructor_; | |
1357 | CYArgument *arguments_; | |
1358 | ||
1359 | New(CYExpression *constructor, CYArgument *arguments) : | |
1360 | constructor_(constructor), | |
1361 | arguments_(arguments) | |
1362 | { | |
1363 | } | |
1364 | ||
1365 | virtual unsigned Precedence() const { | |
1366 | return arguments_ == NULL ? 2 : 1; | |
1367 | } | |
1368 | ||
1369 | CYRightHand(false) | |
1370 | ||
1371 | virtual CYExpression *Replace(CYContext &context); | |
1372 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1373 | ||
1374 | virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); | |
1375 | }; | |
1376 | ||
1377 | } } | |
1378 | ||
1379 | struct CYCall : | |
1380 | CYExpression | |
1381 | { | |
1382 | CYExpression *function_; | |
1383 | CYArgument *arguments_; | |
1384 | ||
1385 | CYCall(CYExpression *function, CYArgument *arguments = NULL) : | |
1386 | function_(function), | |
1387 | arguments_(arguments) | |
1388 | { | |
1389 | } | |
1390 | ||
1391 | CYPrecedence(1) | |
1392 | CYRightHand(false) | |
1393 | ||
1394 | virtual CYExpression *Replace(CYContext &context); | |
1395 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1396 | ||
1397 | virtual CYExpression *AddArgument(CYContext &context, CYExpression *value); | |
1398 | }; | |
1399 | ||
1400 | struct CYRubyProc; | |
1401 | ||
1402 | struct CYRubyBlock : | |
1403 | CYExpression | |
1404 | { | |
1405 | CYExpression *call_; | |
1406 | CYRubyProc *proc_; | |
1407 | ||
1408 | CYRubyBlock(CYExpression *call, CYRubyProc *proc) : | |
1409 | call_(call), | |
1410 | proc_(proc) | |
1411 | { | |
1412 | } | |
1413 | ||
1414 | CYPrecedence(1) | |
1415 | CYRightHand(false) | |
1416 | ||
1417 | virtual CYExpression *Replace(CYContext &context); | |
1418 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1419 | }; | |
1420 | ||
1421 | struct CYIf : | |
1422 | CYStatement | |
1423 | { | |
1424 | CYExpression *test_; | |
1425 | CYStatement *true_; | |
1426 | CYStatement *false_; | |
1427 | ||
1428 | CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false = NULL) : | |
1429 | test_(test), | |
1430 | true_(_true), | |
1431 | false_(_false) | |
1432 | { | |
1433 | } | |
1434 | ||
1435 | virtual CYStatement *Replace(CYContext &context); | |
1436 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1437 | }; | |
1438 | ||
1439 | struct CYDoWhile : | |
1440 | CYStatement | |
1441 | { | |
1442 | CYExpression *test_; | |
1443 | CYStatement *code_; | |
1444 | ||
1445 | CYDoWhile(CYExpression *test, CYStatement *code) : | |
1446 | test_(test), | |
1447 | code_(code) | |
1448 | { | |
1449 | } | |
1450 | ||
1451 | virtual CYStatement *Replace(CYContext &context); | |
1452 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1453 | }; | |
1454 | ||
1455 | struct CYWhile : | |
1456 | CYStatement | |
1457 | { | |
1458 | CYExpression *test_; | |
1459 | CYStatement *code_; | |
1460 | ||
1461 | CYWhile(CYExpression *test, CYStatement *code) : | |
1462 | test_(test), | |
1463 | code_(code) | |
1464 | { | |
1465 | } | |
1466 | ||
1467 | virtual CYStatement *Replace(CYContext &context); | |
1468 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1469 | }; | |
1470 | ||
1471 | // XXX: this should be split up into CYAnonymousFunction and CYNamedFunction (subclass) | |
1472 | struct CYFunction { | |
1473 | CYIdentifier *name_; | |
1474 | CYFunctionParameter *parameters_; | |
1475 | CYBlock code_; | |
1476 | CYNonLocal *nonlocal_; | |
1477 | ||
1478 | CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : | |
1479 | name_(name), | |
1480 | parameters_(parameters), | |
1481 | code_(statements), | |
1482 | nonlocal_(NULL) | |
1483 | { | |
1484 | } | |
1485 | ||
1486 | virtual ~CYFunction() { | |
1487 | } | |
1488 | ||
1489 | void Inject(CYContext &context); | |
1490 | virtual void Replace_(CYContext &context, bool outer); | |
1491 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1492 | }; | |
1493 | ||
1494 | // XXX: this should be split up into CYAnonymousFunctionExpression and CYNamedFunctionExpression | |
1495 | struct CYFunctionExpression : | |
1496 | CYFunction, | |
1497 | CYExpression | |
1498 | { | |
1499 | CYFunctionExpression(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : | |
1500 | CYFunction(name, parameters, statements) | |
1501 | { | |
1502 | } | |
1503 | ||
1504 | CYPrecedence(0) | |
1505 | CYRightHand(false) | |
1506 | ||
1507 | virtual CYExpression *Replace(CYContext &context); | |
1508 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1509 | }; | |
1510 | ||
1511 | // XXX: this should derive from CYAnonymousFunctionExpression | |
1512 | struct CYRubyProc : | |
1513 | CYFunctionExpression | |
1514 | { | |
1515 | CYRubyProc(CYFunctionParameter *parameters, CYStatement *statements) : | |
1516 | CYFunctionExpression(NULL, parameters, statements) | |
1517 | { | |
1518 | } | |
1519 | ||
1520 | virtual CYExpression *Replace(CYContext &context); | |
1521 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1522 | }; | |
1523 | ||
1524 | // XXX: this should derive from CYNamedFunction | |
1525 | struct CYFunctionStatement : | |
1526 | CYFunction, | |
1527 | CYStatement | |
1528 | { | |
1529 | CYFunctionStatement(CYIdentifier *name, CYFunctionParameter *parameters, CYStatement *statements) : | |
1530 | CYFunction(name, parameters, statements) | |
1531 | { | |
1532 | } | |
1533 | ||
1534 | virtual CYStatement *Replace(CYContext &context); | |
1535 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1536 | }; | |
1537 | ||
1538 | struct CYExpress : | |
1539 | CYStatement | |
1540 | { | |
1541 | CYExpression *expression_; | |
1542 | ||
1543 | CYExpress(CYExpression *expression) : | |
1544 | expression_(expression) | |
1545 | { | |
1546 | if (expression == NULL) | |
1547 | throw; | |
1548 | } | |
1549 | ||
1550 | virtual CYStatement *Replace(CYContext &context); | |
1551 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1552 | }; | |
1553 | ||
1554 | struct CYContinue : | |
1555 | CYStatement | |
1556 | { | |
1557 | CYIdentifier *label_; | |
1558 | ||
1559 | CYContinue(CYIdentifier *label) : | |
1560 | label_(label) | |
1561 | { | |
1562 | } | |
1563 | ||
1564 | virtual CYStatement *Replace(CYContext &context); | |
1565 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1566 | }; | |
1567 | ||
1568 | struct CYBreak : | |
1569 | CYStatement | |
1570 | { | |
1571 | CYIdentifier *label_; | |
1572 | ||
1573 | CYBreak(CYIdentifier *label) : | |
1574 | label_(label) | |
1575 | { | |
1576 | } | |
1577 | ||
1578 | virtual CYStatement *Replace(CYContext &context); | |
1579 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1580 | }; | |
1581 | ||
1582 | struct CYReturn : | |
1583 | CYStatement | |
1584 | { | |
1585 | CYExpression *value_; | |
1586 | ||
1587 | CYReturn(CYExpression *value) : | |
1588 | value_(value) | |
1589 | { | |
1590 | } | |
1591 | ||
1592 | virtual CYStatement *Replace(CYContext &context); | |
1593 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1594 | }; | |
1595 | ||
1596 | struct CYEmpty : | |
1597 | CYStatement | |
1598 | { | |
1599 | virtual CYStatement *Replace(CYContext &context); | |
1600 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1601 | }; | |
1602 | ||
1603 | struct CYFinally : | |
1604 | CYThing | |
1605 | { | |
1606 | CYBlock code_; | |
1607 | ||
1608 | CYFinally(CYStatement *statements) : | |
1609 | code_(statements) | |
1610 | { | |
1611 | } | |
1612 | ||
1613 | void Replace(CYContext &context); | |
1614 | virtual void Output(CYOutput &out) const; | |
1615 | }; | |
1616 | ||
1617 | namespace cy { | |
1618 | namespace Syntax { | |
1619 | ||
1620 | struct Catch : | |
1621 | CYThing | |
1622 | { | |
1623 | CYIdentifier *name_; | |
1624 | CYBlock code_; | |
1625 | ||
1626 | Catch(CYIdentifier *name, CYStatement *statements) : | |
1627 | name_(name), | |
1628 | code_(statements) | |
1629 | { | |
1630 | } | |
1631 | ||
1632 | void Replace(CYContext &context); | |
1633 | virtual void Output(CYOutput &out) const; | |
1634 | }; | |
1635 | ||
1636 | struct Try : | |
1637 | CYStatement | |
1638 | { | |
1639 | CYBlock code_; | |
1640 | Catch *catch_; | |
1641 | CYFinally *finally_; | |
1642 | ||
1643 | Try(CYStatement *statements, Catch *_catch, CYFinally *finally) : | |
1644 | code_(statements), | |
1645 | catch_(_catch), | |
1646 | finally_(finally) | |
1647 | { | |
1648 | } | |
1649 | ||
1650 | virtual CYStatement *Replace(CYContext &context); | |
1651 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1652 | }; | |
1653 | ||
1654 | struct Throw : | |
1655 | CYStatement | |
1656 | { | |
1657 | CYExpression *value_; | |
1658 | ||
1659 | Throw(CYExpression *value = NULL) : | |
1660 | value_(value) | |
1661 | { | |
1662 | } | |
1663 | ||
1664 | virtual CYStatement *Replace(CYContext &context); | |
1665 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1666 | }; | |
1667 | ||
1668 | } } | |
1669 | ||
1670 | struct CYWith : | |
1671 | CYStatement | |
1672 | { | |
1673 | CYExpression *scope_; | |
1674 | CYStatement *code_; | |
1675 | ||
1676 | CYWith(CYExpression *scope, CYStatement *code) : | |
1677 | scope_(scope), | |
1678 | code_(code) | |
1679 | { | |
1680 | } | |
1681 | ||
1682 | virtual CYStatement *Replace(CYContext &context); | |
1683 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1684 | }; | |
1685 | ||
1686 | struct CYSwitch : | |
1687 | CYStatement | |
1688 | { | |
1689 | CYExpression *value_; | |
1690 | CYClause *clauses_; | |
1691 | ||
1692 | CYSwitch(CYExpression *value, CYClause *clauses) : | |
1693 | value_(value), | |
1694 | clauses_(clauses) | |
1695 | { | |
1696 | } | |
1697 | ||
1698 | virtual CYStatement *Replace(CYContext &context); | |
1699 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1700 | }; | |
1701 | ||
1702 | struct CYDebugger : | |
1703 | CYStatement | |
1704 | { | |
1705 | CYDebugger() | |
1706 | { | |
1707 | } | |
1708 | ||
1709 | virtual CYStatement *Replace(CYContext &context); | |
1710 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1711 | }; | |
1712 | ||
1713 | struct CYCondition : | |
1714 | CYExpression | |
1715 | { | |
1716 | CYExpression *test_; | |
1717 | CYExpression *true_; | |
1718 | CYExpression *false_; | |
1719 | ||
1720 | CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) : | |
1721 | test_(test), | |
1722 | true_(_true), | |
1723 | false_(_false) | |
1724 | { | |
1725 | } | |
1726 | ||
1727 | CYPrecedence(15) | |
1728 | ||
1729 | virtual CYExpression *Replace(CYContext &context); | |
1730 | virtual void Output(CYOutput &out, CYFlags flags) const; | |
1731 | }; | |
1732 | ||
1733 | struct CYAddressOf : | |
1734 | CYPrefix | |
1735 | { | |
1736 | CYAddressOf(CYExpression *rhs) : | |
1737 | CYPrefix(rhs) | |
1738 | { | |
1739 | } | |
1740 | ||
1741 | virtual const char *Operator() const { | |
1742 | return "&"; | |
1743 | } | |
1744 | ||
1745 | CYAlphabetic(false) | |
1746 | ||
1747 | virtual CYExpression *Replace(CYContext &context); | |
1748 | }; | |
1749 | ||
1750 | struct CYIndirect : | |
1751 | CYPrefix | |
1752 | { | |
1753 | CYIndirect(CYExpression *rhs) : | |
1754 | CYPrefix(rhs) | |
1755 | { | |
1756 | } | |
1757 | ||
1758 | virtual const char *Operator() const { | |
1759 | return "*"; | |
1760 | } | |
1761 | ||
1762 | CYAlphabetic(false) | |
1763 | ||
1764 | virtual CYExpression *Replace(CYContext &context); | |
1765 | }; | |
1766 | ||
1767 | #define CYReplace \ | |
1768 | virtual CYExpression *Replace(CYContext &context); | |
1769 | ||
1770 | #define CYPostfix_(op, name, args...) \ | |
1771 | struct CY ## name : \ | |
1772 | CYPostfix \ | |
1773 | { args \ | |
1774 | CY ## name(CYExpression *lhs) : \ | |
1775 | CYPostfix(lhs) \ | |
1776 | { \ | |
1777 | } \ | |
1778 | \ | |
1779 | virtual const char *Operator() const { \ | |
1780 | return op; \ | |
1781 | } \ | |
1782 | }; | |
1783 | ||
1784 | #define CYPrefix_(alphabetic, op, name, args...) \ | |
1785 | struct CY ## name : \ | |
1786 | CYPrefix \ | |
1787 | { args \ | |
1788 | CY ## name(CYExpression *rhs) : \ | |
1789 | CYPrefix(rhs) \ | |
1790 | { \ | |
1791 | } \ | |
1792 | \ | |
1793 | CYAlphabetic(alphabetic) \ | |
1794 | \ | |
1795 | virtual const char *Operator() const { \ | |
1796 | return op; \ | |
1797 | } \ | |
1798 | }; | |
1799 | ||
1800 | #define CYInfix_(alphabetic, precedence, op, name, args...) \ | |
1801 | struct CY ## name : \ | |
1802 | CYInfix \ | |
1803 | { args \ | |
1804 | CY ## name(CYExpression *lhs, CYExpression *rhs) : \ | |
1805 | CYInfix(lhs, rhs) \ | |
1806 | { \ | |
1807 | } \ | |
1808 | \ | |
1809 | CYAlphabetic(alphabetic) \ | |
1810 | CYPrecedence(precedence) \ | |
1811 | \ | |
1812 | virtual const char *Operator() const { \ | |
1813 | return op; \ | |
1814 | } \ | |
1815 | }; | |
1816 | ||
1817 | #define CYAssignment_(op, name, args...) \ | |
1818 | struct CY ## name ## Assign : \ | |
1819 | CYAssignment \ | |
1820 | { args \ | |
1821 | CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \ | |
1822 | CYAssignment(lhs, rhs) \ | |
1823 | { \ | |
1824 | } \ | |
1825 | \ | |
1826 | virtual const char *Operator() const { \ | |
1827 | return op; \ | |
1828 | } \ | |
1829 | }; | |
1830 | ||
1831 | CYPostfix_("++", PostIncrement) | |
1832 | CYPostfix_("--", PostDecrement) | |
1833 | ||
1834 | CYPrefix_(true, "delete", Delete) | |
1835 | CYPrefix_(true, "void", Void) | |
1836 | CYPrefix_(true, "typeof", TypeOf) | |
1837 | CYPrefix_(false, "++", PreIncrement) | |
1838 | CYPrefix_(false, "--", PreDecrement) | |
1839 | CYPrefix_(false, "+", Affirm) | |
1840 | CYPrefix_(false, "-", Negate) | |
1841 | CYPrefix_(false, "~", BitwiseNot) | |
1842 | CYPrefix_(false, "!", LogicalNot) | |
1843 | ||
1844 | CYInfix_(false, 5, "*", Multiply) | |
1845 | CYInfix_(false, 5, "/", Divide) | |
1846 | CYInfix_(false, 5, "%", Modulus) | |
1847 | CYInfix_(false, 6, "+", Add, CYReplace) | |
1848 | CYInfix_(false, 6, "-", Subtract) | |
1849 | CYInfix_(false, 7, "<<", ShiftLeft) | |
1850 | CYInfix_(false, 7, ">>", ShiftRightSigned) | |
1851 | CYInfix_(false, 7, ">>>", ShiftRightUnsigned) | |
1852 | CYInfix_(false, 8, "<", Less) | |
1853 | CYInfix_(false, 8, ">", Greater) | |
1854 | CYInfix_(false, 8, "<=", LessOrEqual) | |
1855 | CYInfix_(false, 8, ">=", GreaterOrEqual) | |
1856 | CYInfix_(true, 8, "instanceof", InstanceOf) | |
1857 | CYInfix_(true, 8, "in", In) | |
1858 | CYInfix_(false, 9, "==", Equal) | |
1859 | CYInfix_(false, 9, "!=", NotEqual) | |
1860 | CYInfix_(false, 9, "===", Identical) | |
1861 | CYInfix_(false, 9, "!==", NotIdentical) | |
1862 | CYInfix_(false, 10, "&", BitwiseAnd) | |
1863 | CYInfix_(false, 11, "^", BitwiseXOr) | |
1864 | CYInfix_(false, 12, "|", BitwiseOr) | |
1865 | CYInfix_(false, 13, "&&", LogicalAnd) | |
1866 | CYInfix_(false, 14, "||", LogicalOr) | |
1867 | ||
1868 | CYAssignment_("=", ) | |
1869 | CYAssignment_("*=", Multiply) | |
1870 | CYAssignment_("/=", Divide) | |
1871 | CYAssignment_("%=", Modulus) | |
1872 | CYAssignment_("+=", Add) | |
1873 | CYAssignment_("-=", Subtract) | |
1874 | CYAssignment_("<<=", ShiftLeft) | |
1875 | CYAssignment_(">>=", ShiftRightSigned) | |
1876 | CYAssignment_(">>>=", ShiftRightUnsigned) | |
1877 | CYAssignment_("&=", BitwiseAnd) | |
1878 | CYAssignment_("^=", BitwiseXOr) | |
1879 | CYAssignment_("|=", BitwiseOr) | |
1880 | ||
1881 | #endif/*CYCRIPT_PARSER_HPP*/ |