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