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