]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Remove Execution Server and Disassembler | |
2 | * Copyright (C) 2009 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* Modified BSD License {{{ */ | |
6 | /* | |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
38 | /* }}} */ | |
39 | ||
40 | #ifndef CYPARSER_HPP | |
41 | #define CYPARSER_HPP | |
42 | ||
43 | #include <cstdlib> | |
44 | #include <string> | |
45 | #include <vector> | |
46 | ||
47 | #include "location.hh" | |
48 | #include "Pooling.hpp" | |
49 | ||
50 | template <typename Type_> | |
51 | struct CYNext { | |
52 | Type_ *next_; | |
53 | ||
54 | CYNext() : | |
55 | next_(NULL) | |
56 | { | |
57 | } | |
58 | ||
59 | CYNext(Type_ *next) : | |
60 | next_(next) | |
61 | { | |
62 | } | |
63 | ||
64 | void SetNext(Type_ *next) { | |
65 | next_ = next; | |
66 | } | |
67 | }; | |
68 | ||
69 | struct CYThing { | |
70 | virtual void Output(std::ostream &out) const = 0; | |
71 | }; | |
72 | ||
73 | _finline std::ostream &operator <<(std::ostream &out, const CYThing &rhs) { | |
74 | rhs.Output(out); | |
75 | return out; | |
76 | } | |
77 | ||
78 | struct CYSource : | |
79 | CYNext<CYSource> | |
80 | { | |
81 | virtual bool IsBlock() const { | |
82 | return next_ != NULL; | |
83 | } | |
84 | ||
85 | virtual void Show(std::ostream &out) const; | |
86 | virtual void Output(std::ostream &out) const = 0; | |
87 | virtual void Output(std::ostream &out, bool block) const; | |
88 | virtual void Output_(std::ostream &out) const; | |
89 | }; | |
90 | ||
91 | struct CYPropertyName { | |
92 | virtual void PropertyName(std::ostream &out) const = 0; | |
93 | }; | |
94 | ||
95 | struct CYClassName { | |
96 | virtual void ClassName(std::ostream &out, bool object) const = 0; | |
97 | }; | |
98 | ||
99 | struct CYWord : | |
100 | CYThing, | |
101 | CYPropertyName, | |
102 | CYClassName | |
103 | { | |
104 | const char *word_; | |
105 | ||
106 | CYWord(const char *word) : | |
107 | word_(word) | |
108 | { | |
109 | } | |
110 | ||
111 | const char *Value() const { | |
112 | return word_; | |
113 | } | |
114 | ||
115 | virtual void Output(std::ostream &out) const; | |
116 | ||
117 | virtual void ClassName(std::ostream &out, bool object) const; | |
118 | virtual void PropertyName(std::ostream &out) const; | |
119 | }; | |
120 | ||
121 | struct CYIdentifier : | |
122 | CYWord | |
123 | { | |
124 | CYIdentifier(const char *word) : | |
125 | CYWord(word) | |
126 | { | |
127 | } | |
128 | }; | |
129 | ||
130 | struct CYLabel : | |
131 | CYNext<CYLabel> | |
132 | { | |
133 | CYIdentifier *name_; | |
134 | ||
135 | CYLabel(CYIdentifier *name, CYLabel *next) : | |
136 | CYNext<CYLabel>(next), | |
137 | name_(name) | |
138 | { | |
139 | } | |
140 | }; | |
141 | ||
142 | struct CYStatement : | |
143 | CYSource | |
144 | { | |
145 | CYLabel *labels_; | |
146 | ||
147 | CYStatement() : | |
148 | labels_(NULL) | |
149 | { | |
150 | } | |
151 | ||
152 | void AddLabel(CYIdentifier *identifier) { | |
153 | labels_ = new CYLabel(identifier, labels_); | |
154 | } | |
155 | ||
156 | virtual void Output_(std::ostream &out) const; | |
157 | }; | |
158 | ||
159 | struct CYBlock : | |
160 | CYStatement | |
161 | { | |
162 | CYStatement *statements_; | |
163 | ||
164 | CYBlock(CYStatement *statements) : | |
165 | statements_(statements) | |
166 | { | |
167 | } | |
168 | ||
169 | virtual bool IsBlock() const { | |
170 | return true; | |
171 | } | |
172 | ||
173 | virtual void Output(std::ostream &out) const; | |
174 | }; | |
175 | ||
176 | enum CYState { | |
177 | CYClear, | |
178 | CYRestricted, | |
179 | CYNewLine | |
180 | }; | |
181 | ||
182 | class CYDriver { | |
183 | public: | |
184 | CYPool pool_; | |
185 | ||
186 | CYState state_; | |
187 | void *scanner_; | |
188 | ||
189 | const char *data_; | |
190 | size_t size_; | |
191 | FILE *file_; | |
192 | ||
193 | std::string filename_; | |
194 | ||
195 | struct Error { | |
196 | cy::location location_; | |
197 | std::string message_; | |
198 | }; | |
199 | ||
200 | typedef std::vector<Error> Errors; | |
201 | ||
202 | CYSource *source_; | |
203 | Errors errors_; | |
204 | ||
205 | private: | |
206 | void ScannerInit(); | |
207 | void ScannerDestroy(); | |
208 | ||
209 | public: | |
210 | CYDriver(const std::string &filename); | |
211 | ~CYDriver(); | |
212 | }; | |
213 | ||
214 | struct CYPart { | |
215 | virtual void Part(std::ostream &out) const = 0; | |
216 | }; | |
217 | ||
218 | struct CYForInitialiser : | |
219 | CYPart | |
220 | { | |
221 | }; | |
222 | ||
223 | struct CYForInInitialiser : | |
224 | CYPart | |
225 | { | |
226 | }; | |
227 | ||
228 | enum CYFlags { | |
229 | CYNoFlags = 0, | |
230 | CYNoBrace = (1 << 0), | |
231 | CYNoFunction = (1 << 1), | |
232 | CYNoLeader = (1 << 2), | |
233 | CYNoTrailer = (1 << 3), | |
234 | CYNoIn = (1 << 4), | |
235 | CYNoHyphen = (1 << 5), | |
236 | }; | |
237 | ||
238 | struct CYExpression : | |
239 | CYNext<CYExpression>, | |
240 | CYForInitialiser, | |
241 | CYForInInitialiser, | |
242 | CYClassName | |
243 | { | |
244 | virtual unsigned Precedence() const = 0; | |
245 | virtual void Part(std::ostream &out) const; | |
246 | virtual void Output(std::ostream &out, CYFlags flags) const = 0; | |
247 | void Output(std::ostream &out, unsigned precedence, CYFlags flags) const; | |
248 | ||
249 | virtual void ClassName(std::ostream &out, bool object) const; | |
250 | ||
251 | virtual const char *Word() const { | |
252 | return NULL; | |
253 | } | |
254 | }; | |
255 | ||
256 | #define CYAlphabetic(value) \ | |
257 | virtual bool Alphabetic() const { \ | |
258 | return value; \ | |
259 | } | |
260 | ||
261 | #define CYPrecedence(value) \ | |
262 | virtual unsigned Precedence() const { \ | |
263 | return value; \ | |
264 | } | |
265 | ||
266 | struct CYCompound : | |
267 | CYExpression | |
268 | { | |
269 | CYExpression *expressions_; | |
270 | ||
271 | CYCompound(CYExpression *expressions) : | |
272 | expressions_(expressions) | |
273 | { | |
274 | } | |
275 | ||
276 | void AddPrev(CYExpression *expression) { | |
277 | CYExpression *last(expression); | |
278 | while (last->next_ != NULL) | |
279 | last = last->next_; | |
280 | last->SetNext(expressions_); | |
281 | expressions_ = expression; | |
282 | } | |
283 | ||
284 | CYPrecedence(17) | |
285 | ||
286 | void Output(std::ostream &out, CYFlags flags) const; | |
287 | }; | |
288 | ||
289 | struct CYLiteral : | |
290 | CYExpression | |
291 | { | |
292 | CYPrecedence(0) | |
293 | }; | |
294 | ||
295 | struct CYMagic : | |
296 | CYExpression | |
297 | { | |
298 | CYPrecedence(0) | |
299 | }; | |
300 | ||
301 | struct CYSelectorPart : | |
302 | CYNext<CYSelectorPart> | |
303 | { | |
304 | CYWord *name_; | |
305 | bool value_; | |
306 | ||
307 | CYSelectorPart(CYWord *name, bool value, CYSelectorPart *next) : | |
308 | CYNext<CYSelectorPart>(next), | |
309 | name_(name), | |
310 | value_(value) | |
311 | { | |
312 | } | |
313 | ||
314 | virtual void Output(std::ostream &out) const; | |
315 | }; | |
316 | ||
317 | struct CYSelector : | |
318 | CYLiteral | |
319 | { | |
320 | CYSelectorPart *name_; | |
321 | ||
322 | CYSelector(CYSelectorPart *name) : | |
323 | name_(name) | |
324 | { | |
325 | } | |
326 | ||
327 | CYPrecedence(1) | |
328 | ||
329 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
330 | }; | |
331 | ||
332 | struct CYRange { | |
333 | uint64_t lo_; | |
334 | uint64_t hi_; | |
335 | ||
336 | CYRange(uint64_t lo, uint64_t hi) : | |
337 | lo_(lo), hi_(hi) | |
338 | { | |
339 | } | |
340 | ||
341 | bool operator [](uint8_t value) const { | |
342 | return !(value >> 7) && (value >> 6 ? hi_ : lo_) >> (value & 0x3f) & 0x1; | |
343 | } | |
344 | ||
345 | void operator()(uint8_t value) { | |
346 | if (value >> 7) | |
347 | return; | |
348 | (value >> 6 ? hi_ : lo_) |= uint64_t(0x1) << (value & 0x3f); | |
349 | } | |
350 | }; | |
351 | ||
352 | extern CYRange DigitRange_; | |
353 | extern CYRange WordStartRange_; | |
354 | extern CYRange WordEndRange_; | |
355 | ||
356 | struct CYString : | |
357 | CYLiteral, | |
358 | CYPropertyName | |
359 | { | |
360 | const char *value_; | |
361 | size_t size_; | |
362 | ||
363 | CYString(const char *value, size_t size) : | |
364 | value_(value), | |
365 | size_(size) | |
366 | { | |
367 | } | |
368 | ||
369 | CYString(const CYIdentifier *identifier) : | |
370 | value_(identifier->Value()), | |
371 | size_(strlen(value_)) | |
372 | { | |
373 | } | |
374 | ||
375 | const char *Value() const { | |
376 | return value_; | |
377 | } | |
378 | ||
379 | virtual const char *Word() const { | |
380 | if (size_ == 0 || !WordStartRange_[value_[0]]) | |
381 | return NULL; | |
382 | for (size_t i(1); i != size_; ++i) | |
383 | if (!WordEndRange_[value_[i]]) | |
384 | return NULL; | |
385 | return Value(); | |
386 | } | |
387 | ||
388 | virtual void Output(std::ostream &out) const { | |
389 | return Output(out, CYNoFlags); | |
390 | } | |
391 | ||
392 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
393 | virtual void PropertyName(std::ostream &out) const; | |
394 | }; | |
395 | ||
396 | struct CYNumber : | |
397 | CYLiteral, | |
398 | CYPropertyName | |
399 | { | |
400 | double value_; | |
401 | ||
402 | CYNumber(double value) : | |
403 | value_(value) | |
404 | { | |
405 | } | |
406 | ||
407 | double Value() const { | |
408 | return value_; | |
409 | } | |
410 | ||
411 | virtual void Output(std::ostream &out) const { | |
412 | return Output(out, CYNoFlags); | |
413 | } | |
414 | ||
415 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
416 | virtual void PropertyName(std::ostream &out) const; | |
417 | }; | |
418 | ||
419 | struct CYNull : | |
420 | CYWord, | |
421 | CYLiteral | |
422 | { | |
423 | CYNull() : | |
424 | CYWord("null") | |
425 | { | |
426 | } | |
427 | ||
428 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
429 | }; | |
430 | ||
431 | struct CYThis : | |
432 | CYWord, | |
433 | CYMagic | |
434 | { | |
435 | CYThis() : | |
436 | CYWord("this") | |
437 | { | |
438 | } | |
439 | ||
440 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
441 | }; | |
442 | ||
443 | struct CYBoolean : | |
444 | CYLiteral | |
445 | { | |
446 | virtual bool Value() const = 0; | |
447 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
448 | }; | |
449 | ||
450 | struct CYFalse : | |
451 | CYWord, | |
452 | CYBoolean | |
453 | { | |
454 | CYFalse() : | |
455 | CYWord("false") | |
456 | { | |
457 | } | |
458 | ||
459 | virtual bool Value() const; | |
460 | }; | |
461 | ||
462 | struct CYTrue : | |
463 | CYWord, | |
464 | CYBoolean | |
465 | { | |
466 | CYTrue() : | |
467 | CYWord("true") | |
468 | { | |
469 | } | |
470 | ||
471 | virtual bool Value() const; | |
472 | }; | |
473 | ||
474 | struct CYVariable : | |
475 | CYExpression | |
476 | { | |
477 | CYIdentifier *name_; | |
478 | ||
479 | CYVariable(CYIdentifier *name) : | |
480 | name_(name) | |
481 | { | |
482 | } | |
483 | ||
484 | CYPrecedence(0) | |
485 | ||
486 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
487 | }; | |
488 | ||
489 | struct CYPrefix : | |
490 | CYExpression | |
491 | { | |
492 | CYExpression *rhs_; | |
493 | ||
494 | CYPrefix(CYExpression *rhs) : | |
495 | rhs_(rhs) | |
496 | { | |
497 | } | |
498 | ||
499 | virtual bool Alphabetic() const = 0; | |
500 | virtual const char *Operator() const = 0; | |
501 | ||
502 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
503 | }; | |
504 | ||
505 | struct CYInfix : | |
506 | CYExpression | |
507 | { | |
508 | CYExpression *lhs_; | |
509 | CYExpression *rhs_; | |
510 | ||
511 | CYInfix(CYExpression *lhs, CYExpression *rhs) : | |
512 | lhs_(lhs), | |
513 | rhs_(rhs) | |
514 | { | |
515 | } | |
516 | ||
517 | void SetLeft(CYExpression *lhs) { | |
518 | lhs_ = lhs; | |
519 | } | |
520 | ||
521 | virtual bool Alphabetic() const = 0; | |
522 | virtual const char *Operator() const = 0; | |
523 | ||
524 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
525 | }; | |
526 | ||
527 | struct CYPostfix : | |
528 | CYExpression | |
529 | { | |
530 | CYExpression *lhs_; | |
531 | ||
532 | CYPostfix(CYExpression *lhs) : | |
533 | lhs_(lhs) | |
534 | { | |
535 | } | |
536 | ||
537 | virtual const char *Operator() const = 0; | |
538 | ||
539 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
540 | }; | |
541 | ||
542 | struct CYAssignment : | |
543 | CYExpression | |
544 | { | |
545 | CYExpression *lhs_; | |
546 | CYExpression *rhs_; | |
547 | ||
548 | CYAssignment(CYExpression *lhs, CYExpression *rhs) : | |
549 | lhs_(lhs), | |
550 | rhs_(rhs) | |
551 | { | |
552 | } | |
553 | ||
554 | void SetLeft(CYExpression *lhs) { | |
555 | lhs_ = lhs; | |
556 | } | |
557 | ||
558 | virtual const char *Operator() const = 0; | |
559 | ||
560 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
561 | }; | |
562 | ||
563 | struct CYArgument : | |
564 | CYNext<CYArgument> | |
565 | { | |
566 | CYWord *name_; | |
567 | CYExpression *value_; | |
568 | ||
569 | CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) : | |
570 | CYNext<CYArgument>(next), | |
571 | name_(name), | |
572 | value_(value) | |
573 | { | |
574 | } | |
575 | ||
576 | void Output(std::ostream &out) const; | |
577 | }; | |
578 | ||
579 | struct CYBlank : | |
580 | public CYWord | |
581 | { | |
582 | CYBlank() : | |
583 | CYWord("") | |
584 | { | |
585 | } | |
586 | }; | |
587 | ||
588 | struct CYClause : | |
589 | CYThing, | |
590 | CYNext<CYClause> | |
591 | { | |
592 | CYExpression *case_; | |
593 | CYStatement *code_; | |
594 | ||
595 | CYClause(CYExpression *_case, CYStatement *code) : | |
596 | case_(_case), | |
597 | code_(code) | |
598 | { | |
599 | } | |
600 | ||
601 | virtual void Output(std::ostream &out) const; | |
602 | }; | |
603 | ||
604 | struct CYElement : | |
605 | CYNext<CYElement> | |
606 | { | |
607 | CYExpression *value_; | |
608 | ||
609 | CYElement(CYExpression *value, CYElement *next) : | |
610 | CYNext<CYElement>(next), | |
611 | value_(value) | |
612 | { | |
613 | } | |
614 | ||
615 | void Output(std::ostream &out) const; | |
616 | }; | |
617 | ||
618 | struct CYArray : | |
619 | CYLiteral | |
620 | { | |
621 | CYElement *elements_; | |
622 | ||
623 | CYArray(CYElement *elements) : | |
624 | elements_(elements) | |
625 | { | |
626 | } | |
627 | ||
628 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
629 | }; | |
630 | ||
631 | struct CYDeclaration : | |
632 | CYThing, | |
633 | CYForInInitialiser | |
634 | { | |
635 | CYIdentifier *identifier_; | |
636 | CYExpression *initialiser_; | |
637 | ||
638 | CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) : | |
639 | identifier_(identifier), | |
640 | initialiser_(initialiser) | |
641 | { | |
642 | } | |
643 | ||
644 | virtual void Part(std::ostream &out) const; | |
645 | virtual void Output(std::ostream &out) const; | |
646 | }; | |
647 | ||
648 | struct CYDeclarations : | |
649 | CYStatement, | |
650 | CYForInitialiser | |
651 | { | |
652 | CYDeclaration *declaration_; | |
653 | CYDeclarations *next_; | |
654 | ||
655 | CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) : | |
656 | declaration_(declaration), | |
657 | next_(next) | |
658 | { | |
659 | } | |
660 | ||
661 | virtual void Part(std::ostream &out) const; | |
662 | virtual void Output(std::ostream &out) const; | |
663 | }; | |
664 | ||
665 | struct CYField : | |
666 | CYNext<CYField> | |
667 | { | |
668 | virtual void Output(std::ostream &out) const; | |
669 | }; | |
670 | ||
671 | struct CYMessageParameter : | |
672 | CYNext<CYMessageParameter> | |
673 | { | |
674 | CYWord *tag_; | |
675 | CYExpression *type_; | |
676 | CYIdentifier *name_; | |
677 | ||
678 | CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) : | |
679 | tag_(tag), | |
680 | type_(type), | |
681 | name_(name) | |
682 | { | |
683 | } | |
684 | }; | |
685 | ||
686 | struct CYMessage : | |
687 | CYNext<CYMessage> | |
688 | { | |
689 | bool instance_; | |
690 | CYExpression *type_; | |
691 | CYMessageParameter *parameter_; | |
692 | CYSource *body_; | |
693 | ||
694 | CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) : | |
695 | instance_(instance), | |
696 | type_(type), | |
697 | parameter_(parameter), | |
698 | body_(body) | |
699 | { | |
700 | } | |
701 | ||
702 | virtual void Output(std::ostream &out, bool replace) const; | |
703 | }; | |
704 | ||
705 | struct CYClass : | |
706 | CYExpression | |
707 | { | |
708 | CYClassName *name_; | |
709 | CYExpression *super_; | |
710 | CYField *fields_; | |
711 | CYMessage *messages_; | |
712 | ||
713 | CYClass(CYClassName *name, CYExpression *super, CYField *fields, CYMessage *messages) : | |
714 | name_(name), | |
715 | super_(super), | |
716 | fields_(fields), | |
717 | messages_(messages) | |
718 | { | |
719 | } | |
720 | ||
721 | CYPrecedence(0) | |
722 | ||
723 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
724 | }; | |
725 | ||
726 | struct CYCategory : | |
727 | CYStatement | |
728 | { | |
729 | CYClassName *name_; | |
730 | CYMessage *messages_; | |
731 | ||
732 | CYCategory(CYClassName *name, CYMessage *messages) : | |
733 | name_(name), | |
734 | messages_(messages) | |
735 | { | |
736 | } | |
737 | ||
738 | virtual void Output(std::ostream &out) const; | |
739 | }; | |
740 | ||
741 | struct CYFunctionParameter : | |
742 | CYNext<CYFunctionParameter>, | |
743 | CYThing | |
744 | { | |
745 | CYIdentifier *name_; | |
746 | ||
747 | CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) : | |
748 | CYNext<CYFunctionParameter>(next), | |
749 | name_(name) | |
750 | { | |
751 | } | |
752 | ||
753 | virtual void Output(std::ostream &out) const; | |
754 | }; | |
755 | ||
756 | struct CYFor : | |
757 | CYStatement | |
758 | { | |
759 | CYForInitialiser *initialiser_; | |
760 | CYExpression *test_; | |
761 | CYExpression *increment_; | |
762 | CYStatement *code_; | |
763 | ||
764 | CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : | |
765 | initialiser_(initialiser), | |
766 | test_(test), | |
767 | increment_(increment), | |
768 | code_(code) | |
769 | { | |
770 | } | |
771 | ||
772 | virtual void Output(std::ostream &out) const; | |
773 | }; | |
774 | ||
775 | struct CYForIn : | |
776 | CYStatement | |
777 | { | |
778 | CYForInInitialiser *initialiser_; | |
779 | CYExpression *set_; | |
780 | CYStatement *code_; | |
781 | ||
782 | CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : | |
783 | initialiser_(initialiser), | |
784 | set_(set), | |
785 | code_(code) | |
786 | { | |
787 | } | |
788 | ||
789 | virtual void Output(std::ostream &out) const; | |
790 | }; | |
791 | ||
792 | struct CYProperty : | |
793 | CYNext<CYProperty> | |
794 | { | |
795 | CYPropertyName *name_; | |
796 | CYExpression *value_; | |
797 | ||
798 | CYProperty(CYPropertyName *name, CYExpression *value, CYProperty *next) : | |
799 | CYNext<CYProperty>(next), | |
800 | name_(name), | |
801 | value_(value) | |
802 | { | |
803 | } | |
804 | ||
805 | virtual void Output(std::ostream &out) const; | |
806 | }; | |
807 | ||
808 | struct CYObject : | |
809 | CYLiteral | |
810 | { | |
811 | CYProperty *property_; | |
812 | ||
813 | CYObject(CYProperty *property) : | |
814 | property_(property) | |
815 | { | |
816 | } | |
817 | ||
818 | void Output(std::ostream &out, CYFlags flags) const; | |
819 | }; | |
820 | ||
821 | struct CYCatch : | |
822 | CYThing | |
823 | { | |
824 | CYIdentifier *name_; | |
825 | CYStatement *code_; | |
826 | ||
827 | CYCatch(CYIdentifier *name, CYStatement *code) : | |
828 | name_(name), | |
829 | code_(code) | |
830 | { | |
831 | } | |
832 | ||
833 | virtual void Output(std::ostream &out) const; | |
834 | }; | |
835 | ||
836 | struct CYSend : | |
837 | CYExpression | |
838 | { | |
839 | CYExpression *self_; | |
840 | CYArgument *arguments_; | |
841 | ||
842 | CYSend(CYExpression *self, CYArgument *arguments) : | |
843 | self_(self), | |
844 | arguments_(arguments) | |
845 | { | |
846 | } | |
847 | ||
848 | CYPrecedence(0) | |
849 | ||
850 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
851 | }; | |
852 | ||
853 | struct CYMember : | |
854 | CYExpression | |
855 | { | |
856 | CYExpression *object_; | |
857 | CYExpression *property_; | |
858 | ||
859 | CYMember(CYExpression *object, CYExpression *property) : | |
860 | object_(object), | |
861 | property_(property) | |
862 | { | |
863 | } | |
864 | ||
865 | void SetLeft(CYExpression *object) { | |
866 | object_ = object; | |
867 | } | |
868 | }; | |
869 | ||
870 | struct CYDirectMember : | |
871 | CYMember | |
872 | { | |
873 | CYDirectMember(CYExpression *object, CYExpression *property) : | |
874 | CYMember(object, property) | |
875 | { | |
876 | } | |
877 | ||
878 | CYPrecedence(1) | |
879 | ||
880 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
881 | }; | |
882 | ||
883 | struct CYIndirectMember : | |
884 | CYMember | |
885 | { | |
886 | CYIndirectMember(CYExpression *object, CYExpression *property) : | |
887 | CYMember(object, property) | |
888 | { | |
889 | } | |
890 | ||
891 | CYPrecedence(1) | |
892 | ||
893 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
894 | }; | |
895 | ||
896 | struct CYNew : | |
897 | CYExpression | |
898 | { | |
899 | CYExpression *constructor_; | |
900 | CYArgument *arguments_; | |
901 | ||
902 | CYNew(CYExpression *constructor, CYArgument *arguments) : | |
903 | constructor_(constructor), | |
904 | arguments_(arguments) | |
905 | { | |
906 | } | |
907 | ||
908 | CYPrecedence(1) | |
909 | ||
910 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
911 | }; | |
912 | ||
913 | struct CYCall : | |
914 | CYExpression | |
915 | { | |
916 | CYExpression *function_; | |
917 | CYArgument *arguments_; | |
918 | ||
919 | CYCall(CYExpression *function, CYArgument *arguments) : | |
920 | function_(function), | |
921 | arguments_(arguments) | |
922 | { | |
923 | } | |
924 | ||
925 | CYPrecedence(2) | |
926 | ||
927 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
928 | }; | |
929 | ||
930 | struct CYIf : | |
931 | CYStatement | |
932 | { | |
933 | CYExpression *test_; | |
934 | CYStatement *true_; | |
935 | CYStatement *false_; | |
936 | ||
937 | CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) : | |
938 | test_(test), | |
939 | true_(_true), | |
940 | false_(_false) | |
941 | { | |
942 | } | |
943 | ||
944 | virtual void Output(std::ostream &out) const; | |
945 | }; | |
946 | ||
947 | struct CYDoWhile : | |
948 | CYStatement | |
949 | { | |
950 | CYExpression *test_; | |
951 | CYStatement *code_; | |
952 | ||
953 | CYDoWhile(CYExpression *test, CYStatement *code) : | |
954 | test_(test), | |
955 | code_(code) | |
956 | { | |
957 | } | |
958 | ||
959 | virtual void Output(std::ostream &out) const; | |
960 | }; | |
961 | ||
962 | struct CYWhile : | |
963 | CYStatement | |
964 | { | |
965 | CYExpression *test_; | |
966 | CYStatement *code_; | |
967 | ||
968 | CYWhile(CYExpression *test, CYStatement *code) : | |
969 | test_(test), | |
970 | code_(code) | |
971 | { | |
972 | } | |
973 | ||
974 | virtual void Output(std::ostream &out) const; | |
975 | }; | |
976 | ||
977 | struct CYLambda : | |
978 | CYExpression | |
979 | { | |
980 | CYIdentifier *name_; | |
981 | CYFunctionParameter *parameters_; | |
982 | CYSource *body_; | |
983 | ||
984 | CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) : | |
985 | name_(name), | |
986 | parameters_(parameters), | |
987 | body_(body) | |
988 | { | |
989 | } | |
990 | ||
991 | CYPrecedence(0) | |
992 | ||
993 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
994 | }; | |
995 | ||
996 | struct CYFunction : | |
997 | CYLambda, | |
998 | CYSource | |
999 | { | |
1000 | CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) : | |
1001 | CYLambda(name, parameters, body) | |
1002 | { | |
1003 | } | |
1004 | ||
1005 | virtual void Output(std::ostream &out) const; | |
1006 | }; | |
1007 | ||
1008 | struct CYExpress : | |
1009 | CYStatement | |
1010 | { | |
1011 | CYExpression *expression_; | |
1012 | ||
1013 | CYExpress(CYExpression *expression) : | |
1014 | expression_(expression) | |
1015 | { | |
1016 | } | |
1017 | ||
1018 | virtual void Output(std::ostream &out) const; | |
1019 | }; | |
1020 | ||
1021 | struct CYContinue : | |
1022 | CYStatement | |
1023 | { | |
1024 | CYIdentifier *label_; | |
1025 | ||
1026 | CYContinue(CYIdentifier *label) : | |
1027 | label_(label) | |
1028 | { | |
1029 | } | |
1030 | ||
1031 | virtual void Output(std::ostream &out) const; | |
1032 | }; | |
1033 | ||
1034 | struct CYBreak : | |
1035 | CYStatement | |
1036 | { | |
1037 | CYIdentifier *label_; | |
1038 | ||
1039 | CYBreak(CYIdentifier *label) : | |
1040 | label_(label) | |
1041 | { | |
1042 | } | |
1043 | ||
1044 | virtual void Output(std::ostream &out) const; | |
1045 | }; | |
1046 | ||
1047 | struct CYReturn : | |
1048 | CYStatement | |
1049 | { | |
1050 | CYExpression *value_; | |
1051 | ||
1052 | CYReturn(CYExpression *value) : | |
1053 | value_(value) | |
1054 | { | |
1055 | } | |
1056 | ||
1057 | virtual void Output(std::ostream &out) const; | |
1058 | }; | |
1059 | ||
1060 | struct CYEmpty : | |
1061 | CYStatement | |
1062 | { | |
1063 | virtual void Output(std::ostream &out) const; | |
1064 | virtual void Output(std::ostream &out, bool block) const; | |
1065 | }; | |
1066 | ||
1067 | struct CYTry : | |
1068 | CYStatement | |
1069 | { | |
1070 | CYStatement *try_; | |
1071 | CYCatch *catch_; | |
1072 | CYStatement *finally_; | |
1073 | ||
1074 | CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) : | |
1075 | try_(_try), | |
1076 | catch_(_catch), | |
1077 | finally_(finally) | |
1078 | { | |
1079 | } | |
1080 | ||
1081 | virtual void Output(std::ostream &out) const; | |
1082 | }; | |
1083 | ||
1084 | struct CYThrow : | |
1085 | CYStatement | |
1086 | { | |
1087 | CYExpression *value_; | |
1088 | ||
1089 | CYThrow(CYExpression *value) : | |
1090 | value_(value) | |
1091 | { | |
1092 | } | |
1093 | ||
1094 | virtual void Output(std::ostream &out) const; | |
1095 | }; | |
1096 | ||
1097 | struct CYWith : | |
1098 | CYStatement | |
1099 | { | |
1100 | CYExpression *scope_; | |
1101 | CYStatement *code_; | |
1102 | ||
1103 | CYWith(CYExpression *scope, CYStatement *code) : | |
1104 | scope_(scope), | |
1105 | code_(code) | |
1106 | { | |
1107 | } | |
1108 | ||
1109 | virtual void Output(std::ostream &out) const; | |
1110 | }; | |
1111 | ||
1112 | struct CYSwitch : | |
1113 | CYStatement | |
1114 | { | |
1115 | CYExpression *value_; | |
1116 | CYClause *clauses_; | |
1117 | ||
1118 | CYSwitch(CYExpression *value, CYClause *clauses) : | |
1119 | value_(value), | |
1120 | clauses_(clauses) | |
1121 | { | |
1122 | } | |
1123 | ||
1124 | virtual void Output(std::ostream &out) const; | |
1125 | }; | |
1126 | ||
1127 | struct CYCondition : | |
1128 | CYExpression | |
1129 | { | |
1130 | CYExpression *test_; | |
1131 | CYExpression *true_; | |
1132 | CYExpression *false_; | |
1133 | ||
1134 | CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) : | |
1135 | test_(test), | |
1136 | true_(_true), | |
1137 | false_(_false) | |
1138 | { | |
1139 | } | |
1140 | ||
1141 | CYPrecedence(15) | |
1142 | ||
1143 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
1144 | }; | |
1145 | ||
1146 | struct CYAddressOf : | |
1147 | CYPrefix | |
1148 | { | |
1149 | CYAddressOf(CYExpression *rhs) : | |
1150 | CYPrefix(rhs) | |
1151 | { | |
1152 | } | |
1153 | ||
1154 | virtual const char *Operator() const { | |
1155 | return "&"; | |
1156 | } | |
1157 | ||
1158 | CYAlphabetic(false) | |
1159 | CYPrecedence(2) | |
1160 | ||
1161 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
1162 | }; | |
1163 | ||
1164 | struct CYIndirect : | |
1165 | CYPrefix | |
1166 | { | |
1167 | CYIndirect(CYExpression *rhs) : | |
1168 | CYPrefix(rhs) | |
1169 | { | |
1170 | } | |
1171 | ||
1172 | virtual const char *Operator() const { | |
1173 | return "*"; | |
1174 | } | |
1175 | ||
1176 | CYAlphabetic(false) | |
1177 | CYPrecedence(1) | |
1178 | ||
1179 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
1180 | }; | |
1181 | ||
1182 | #define CYPostfix_(op, name) \ | |
1183 | struct CY ## name : \ | |
1184 | CYPostfix \ | |
1185 | { \ | |
1186 | CY ## name(CYExpression *lhs) : \ | |
1187 | CYPostfix(lhs) \ | |
1188 | { \ | |
1189 | } \ | |
1190 | \ | |
1191 | CYPrecedence(3) \ | |
1192 | \ | |
1193 | virtual const char *Operator() const { \ | |
1194 | return op; \ | |
1195 | } \ | |
1196 | }; | |
1197 | ||
1198 | #define CYPrefix_(alphabetic, op, name) \ | |
1199 | struct CY ## name : \ | |
1200 | CYPrefix \ | |
1201 | { \ | |
1202 | CY ## name(CYExpression *rhs) : \ | |
1203 | CYPrefix(rhs) \ | |
1204 | { \ | |
1205 | } \ | |
1206 | \ | |
1207 | CYAlphabetic(alphabetic) \ | |
1208 | CYPrecedence(4) \ | |
1209 | \ | |
1210 | virtual const char *Operator() const { \ | |
1211 | return op; \ | |
1212 | } \ | |
1213 | }; | |
1214 | ||
1215 | #define CYInfix_(alphabetic, precedence, op, name) \ | |
1216 | struct CY ## name : \ | |
1217 | CYInfix \ | |
1218 | { \ | |
1219 | CY ## name(CYExpression *lhs, CYExpression *rhs) : \ | |
1220 | CYInfix(lhs, rhs) \ | |
1221 | { \ | |
1222 | } \ | |
1223 | \ | |
1224 | CYAlphabetic(alphabetic) \ | |
1225 | CYPrecedence(precedence) \ | |
1226 | \ | |
1227 | virtual const char *Operator() const { \ | |
1228 | return op; \ | |
1229 | } \ | |
1230 | }; | |
1231 | ||
1232 | #define CYAssignment_(op, name) \ | |
1233 | struct CY ## name ## Assign : \ | |
1234 | CYAssignment \ | |
1235 | { \ | |
1236 | CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \ | |
1237 | CYAssignment(lhs, rhs) \ | |
1238 | { \ | |
1239 | } \ | |
1240 | \ | |
1241 | CYPrecedence(16) \ | |
1242 | \ | |
1243 | virtual const char *Operator() const { \ | |
1244 | return op; \ | |
1245 | } \ | |
1246 | }; | |
1247 | ||
1248 | CYPostfix_("++", PostIncrement) | |
1249 | CYPostfix_("--", PostDecrement) | |
1250 | ||
1251 | CYPrefix_(true, "delete", Delete) | |
1252 | CYPrefix_(true, "void", Void) | |
1253 | CYPrefix_(true, "typeof", TypeOf) | |
1254 | CYPrefix_(false, "++", PreIncrement) | |
1255 | CYPrefix_(false, "--", PreDecrement) | |
1256 | CYPrefix_(false, "-", Negate) | |
1257 | CYPrefix_(false, "~", BitwiseNot) | |
1258 | CYPrefix_(false, "!", LogicalNot) | |
1259 | ||
1260 | CYInfix_(false, 5, "*", Multiply) | |
1261 | CYInfix_(false, 5, "/", Divide) | |
1262 | CYInfix_(false, 5, "%", Modulus) | |
1263 | CYInfix_(false, 6, "+", Add) | |
1264 | CYInfix_(false, 6, "-", Subtract) | |
1265 | CYInfix_(false, 7, "<<", ShiftLeft) | |
1266 | CYInfix_(false, 7, ">>", ShiftRightSigned) | |
1267 | CYInfix_(false, 7, ">>>", ShiftRightUnsigned) | |
1268 | CYInfix_(false, 8, "<", Less) | |
1269 | CYInfix_(false, 8, ">", Greater) | |
1270 | CYInfix_(false, 8, "<=", LessOrEqual) | |
1271 | CYInfix_(false, 8, ">=", GreaterOrEqual) | |
1272 | CYInfix_(true, 8, "instanceof", InstanceOf) | |
1273 | CYInfix_(true, 8, "in", In) | |
1274 | CYInfix_(false, 9, "==", Equal) | |
1275 | CYInfix_(false, 9, "!=", NotEqual) | |
1276 | CYInfix_(false, 9, "===", Identical) | |
1277 | CYInfix_(false, 9, "!==", NotIdentical) | |
1278 | CYInfix_(false, 10, "&", BitwiseAnd) | |
1279 | CYInfix_(false, 11, "^", BitwiseXOr) | |
1280 | CYInfix_(false, 12, "|", BitwiseOr) | |
1281 | CYInfix_(false, 13, "&&", LogicalAnd) | |
1282 | CYInfix_(false, 14, "||", LogicalOr) | |
1283 | ||
1284 | CYAssignment_("=", ) | |
1285 | CYAssignment_("*=", Multiply) | |
1286 | CYAssignment_("/=", Divide) | |
1287 | CYAssignment_("%=", Modulus) | |
1288 | CYAssignment_("+=", Add) | |
1289 | CYAssignment_("-=", Subtract) | |
1290 | CYAssignment_("<<=", ShiftLeft) | |
1291 | CYAssignment_(">>=", ShiftRightSigned) | |
1292 | CYAssignment_(">>>=", ShiftRightUnsigned) | |
1293 | CYAssignment_("&=", BitwiseAnd) | |
1294 | CYAssignment_("^=", BitwiseXOr) | |
1295 | CYAssignment_("|=", BitwiseOr) | |
1296 | ||
1297 | #endif/*CYPARSER_HPP*/ |