]>
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 | ||
283e7e33 | 315 | extern CYRange DigitRange_; |
dea834b0 JF |
316 | extern CYRange WordStartRange_; |
317 | extern CYRange WordEndRange_; | |
318 | ||
cf7d4c69 JF |
319 | struct CYString : |
320 | CYLiteral, | |
321 | CYName | |
322 | { | |
323 | const char *value_; | |
5999c315 | 324 | size_t size_; |
cf7d4c69 | 325 | |
5999c315 JF |
326 | CYString(const char *value, size_t size) : |
327 | value_(value), | |
328 | size_(size) | |
cf7d4c69 JF |
329 | { |
330 | } | |
331 | ||
332 | CYString(const CYIdentifier *identifier) : | |
5999c315 JF |
333 | value_(identifier->Value()), |
334 | size_(strlen(value_)) | |
cf7d4c69 JF |
335 | { |
336 | } | |
337 | ||
5999c315 | 338 | const char *Value() const { |
cf7d4c69 JF |
339 | return value_; |
340 | } | |
341 | ||
342 | virtual const char *Name() const { | |
5999c315 | 343 | return Value(); |
63b4c5a8 | 344 | } |
5999c315 | 345 | |
dea834b0 JF |
346 | virtual const char *Word() const { |
347 | if (size_ == 0 || !WordStartRange_[value_[0]]) | |
348 | return NULL; | |
349 | for (size_t i(1); i != size_; ++i) | |
350 | if (!WordEndRange_[value_[i]]) | |
351 | return NULL; | |
352 | return Value(); | |
353 | } | |
354 | ||
b09da87b JF |
355 | virtual void Output(std::ostream &out) const { |
356 | return Output(out, CYNoFlags); | |
357 | } | |
358 | ||
359 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
63b4c5a8 JF |
360 | }; |
361 | ||
cf7d4c69 | 362 | struct CYNumber : |
cf7d4c69 JF |
363 | CYLiteral, |
364 | CYName | |
365 | { | |
5999c315 JF |
366 | double value_; |
367 | ||
368 | CYNumber(double value) : | |
369 | value_(value) | |
370 | { | |
371 | } | |
372 | ||
373 | double Value() const { | |
374 | return value_; | |
cf7d4c69 JF |
375 | } |
376 | ||
377 | virtual const char *Name() const { | |
378 | throw; | |
379 | } | |
5999c315 | 380 | |
b09da87b JF |
381 | virtual void Output(std::ostream &out) const { |
382 | return Output(out, CYNoFlags); | |
383 | } | |
384 | ||
385 | virtual void Output(std::ostream &out, CYFlags flags) const; | |
cf7d4c69 JF |
386 | }; |
387 | ||
388 | struct CYNull : | |
389 | CYWord, | |
390 | CYLiteral | |
391 | { | |
392 | CYNull() : | |
393 | CYWord("null") | |
394 | { | |
395 | } | |
5999c315 | 396 | |
b09da87b | 397 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
398 | }; |
399 | ||
400 | struct CYThis : | |
401 | CYWord, | |
478d4ed0 | 402 | CYMagic |
cf7d4c69 JF |
403 | { |
404 | CYThis() : | |
405 | CYWord("this") | |
406 | { | |
407 | } | |
5999c315 | 408 | |
b09da87b | 409 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
410 | }; |
411 | ||
412 | struct CYBoolean : | |
413 | CYLiteral | |
414 | { | |
5999c315 | 415 | virtual bool Value() const = 0; |
b09da87b | 416 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
417 | }; |
418 | ||
419 | struct CYFalse : | |
420 | CYWord, | |
421 | CYBoolean | |
422 | { | |
423 | CYFalse() : | |
424 | CYWord("false") | |
425 | { | |
426 | } | |
5999c315 | 427 | |
b09da87b | 428 | virtual bool Value() const; |
cf7d4c69 JF |
429 | }; |
430 | ||
431 | struct CYTrue : | |
432 | CYWord, | |
433 | CYBoolean | |
434 | { | |
435 | CYTrue() : | |
436 | CYWord("true") | |
437 | { | |
438 | } | |
5999c315 | 439 | |
b09da87b | 440 | virtual bool Value() const; |
cf7d4c69 JF |
441 | }; |
442 | ||
443 | struct CYVariable : | |
444 | CYExpression | |
445 | { | |
446 | CYIdentifier *name_; | |
447 | ||
448 | CYVariable(CYIdentifier *name) : | |
449 | name_(name) | |
450 | { | |
451 | } | |
5999c315 | 452 | |
d35a3b07 JF |
453 | CYPrecedence(0) |
454 | ||
b09da87b | 455 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
456 | }; |
457 | ||
458 | struct CYPrefix : | |
63b4c5a8 JF |
459 | CYExpression |
460 | { | |
461 | CYExpression *rhs_; | |
462 | ||
cf7d4c69 | 463 | CYPrefix(CYExpression *rhs) : |
63b4c5a8 JF |
464 | rhs_(rhs) |
465 | { | |
466 | } | |
5999c315 | 467 | |
b09da87b | 468 | virtual bool Alphabetic() const = 0; |
5999c315 JF |
469 | virtual const char *Operator() const = 0; |
470 | ||
b09da87b | 471 | virtual void Output(std::ostream &out, CYFlags flags) const; |
63b4c5a8 JF |
472 | }; |
473 | ||
cf7d4c69 | 474 | struct CYInfix : |
63b4c5a8 JF |
475 | CYExpression |
476 | { | |
477 | CYExpression *lhs_; | |
478 | CYExpression *rhs_; | |
479 | ||
cf7d4c69 | 480 | CYInfix(CYExpression *lhs, CYExpression *rhs) : |
63b4c5a8 JF |
481 | lhs_(lhs), |
482 | rhs_(rhs) | |
483 | { | |
484 | } | |
5999c315 | 485 | |
b09da87b | 486 | virtual bool Alphabetic() const = 0; |
5999c315 JF |
487 | virtual const char *Operator() const = 0; |
488 | ||
b09da87b | 489 | virtual void Output(std::ostream &out, CYFlags flags) const; |
63b4c5a8 JF |
490 | }; |
491 | ||
cf7d4c69 | 492 | struct CYPostfix : |
63b4c5a8 JF |
493 | CYExpression |
494 | { | |
495 | CYExpression *lhs_; | |
496 | ||
cf7d4c69 | 497 | CYPostfix(CYExpression *lhs) : |
63b4c5a8 JF |
498 | lhs_(lhs) |
499 | { | |
500 | } | |
5999c315 JF |
501 | |
502 | virtual const char *Operator() const = 0; | |
503 | ||
b09da87b | 504 | virtual void Output(std::ostream &out, CYFlags flags) const; |
63b4c5a8 JF |
505 | }; |
506 | ||
cf7d4c69 | 507 | struct CYAssignment : |
d35a3b07 | 508 | CYExpression |
cf7d4c69 | 509 | { |
d35a3b07 JF |
510 | CYExpression *lhs_; |
511 | CYExpression *rhs_; | |
512 | ||
cf7d4c69 | 513 | CYAssignment(CYExpression *lhs, CYExpression *rhs) : |
d35a3b07 JF |
514 | lhs_(lhs), |
515 | rhs_(rhs) | |
cf7d4c69 JF |
516 | { |
517 | } | |
5999c315 JF |
518 | |
519 | virtual const char *Operator() const = 0; | |
d35a3b07 | 520 | |
b09da87b | 521 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
522 | }; |
523 | ||
62014ea9 JF |
524 | struct CYArgument : |
525 | CYNext<CYArgument> | |
526 | { | |
cf7d4c69 JF |
527 | CYWord *name_; |
528 | CYExpression *value_; | |
cf7d4c69 JF |
529 | |
530 | CYArgument(CYWord *name, CYExpression *value, CYArgument *next = NULL) : | |
62014ea9 | 531 | CYNext<CYArgument>(next), |
cf7d4c69 | 532 | name_(name), |
62014ea9 | 533 | value_(value) |
cf7d4c69 JF |
534 | { |
535 | } | |
5999c315 | 536 | |
d35a3b07 | 537 | void Output(std::ostream &out) const; |
cf7d4c69 JF |
538 | }; |
539 | ||
540 | struct CYBlank : | |
541 | public CYWord | |
542 | { | |
543 | CYBlank() : | |
544 | CYWord("") | |
545 | { | |
546 | } | |
547 | }; | |
548 | ||
5999c315 JF |
549 | struct CYClause : |
550 | CYThing, | |
551 | CYNext<CYClause> | |
552 | { | |
cf7d4c69 JF |
553 | CYExpression *case_; |
554 | CYStatement *code_; | |
cf7d4c69 JF |
555 | |
556 | CYClause(CYExpression *_case, CYStatement *code) : | |
557 | case_(_case), | |
558 | code_(code) | |
559 | { | |
560 | } | |
561 | ||
5999c315 | 562 | virtual void Output(std::ostream &out) const; |
cf7d4c69 JF |
563 | }; |
564 | ||
62014ea9 JF |
565 | struct CYElement : |
566 | CYNext<CYElement> | |
567 | { | |
cf7d4c69 | 568 | CYExpression *value_; |
cf7d4c69 JF |
569 | |
570 | CYElement(CYExpression *value, CYElement *next) : | |
62014ea9 JF |
571 | CYNext<CYElement>(next), |
572 | value_(value) | |
cf7d4c69 JF |
573 | { |
574 | } | |
5999c315 | 575 | |
5befe15e JF |
576 | void Output(std::ostream &out) const; |
577 | }; | |
578 | ||
579 | struct CYArray : | |
580 | CYLiteral | |
581 | { | |
582 | CYElement *elements_; | |
583 | ||
584 | CYArray(CYElement *elements) : | |
585 | elements_(elements) | |
586 | { | |
587 | } | |
588 | ||
b09da87b | 589 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
590 | }; |
591 | ||
592 | struct CYDeclaration : | |
5999c315 | 593 | CYThing, |
cf7d4c69 JF |
594 | CYForInInitialiser |
595 | { | |
596 | CYIdentifier *identifier_; | |
597 | CYExpression *initialiser_; | |
598 | ||
599 | CYDeclaration(CYIdentifier *identifier, CYExpression *initialiser) : | |
600 | identifier_(identifier), | |
601 | initialiser_(initialiser) | |
602 | { | |
603 | } | |
5999c315 JF |
604 | |
605 | virtual void Part(std::ostream &out) const; | |
606 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
607 | }; |
608 | ||
609 | struct CYDeclarations : | |
610 | CYStatement, | |
611 | CYForInitialiser | |
612 | { | |
613 | CYDeclaration *declaration_; | |
614 | CYDeclarations *next_; | |
615 | ||
616 | CYDeclarations(CYDeclaration *declaration, CYDeclarations *next) : | |
617 | declaration_(declaration), | |
618 | next_(next) | |
619 | { | |
620 | } | |
5999c315 JF |
621 | |
622 | virtual void Part(std::ostream &out) const; | |
623 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
624 | }; |
625 | ||
b09da87b JF |
626 | struct CYField : |
627 | CYNext<CYField> | |
628 | { | |
629 | virtual void Output(std::ostream &out) const; | |
630 | }; | |
631 | ||
632 | struct CYMessageParameter : | |
633 | CYNext<CYMessageParameter> | |
634 | { | |
635 | CYWord *tag_; | |
636 | CYExpression *type_; | |
637 | CYIdentifier *name_; | |
638 | ||
639 | CYMessageParameter(CYWord *tag, CYExpression *type, CYIdentifier *name) : | |
640 | tag_(tag), | |
641 | type_(type), | |
642 | name_(name) | |
643 | { | |
644 | } | |
645 | }; | |
646 | ||
647 | struct CYMessage : | |
648 | CYSource | |
649 | { | |
650 | bool instance_; | |
651 | CYExpression *type_; | |
652 | CYMessageParameter *parameter_; | |
653 | CYSource *body_; | |
654 | ||
655 | CYMessage(bool instance, CYExpression *type, CYMessageParameter *parameter, CYSource *body) : | |
656 | instance_(instance), | |
657 | type_(type), | |
658 | parameter_(parameter), | |
659 | body_(body) | |
660 | { | |
661 | } | |
662 | ||
663 | virtual void Output(std::ostream &out) const; | |
664 | }; | |
665 | ||
666 | struct CYClass : | |
667 | CYSource | |
668 | { | |
669 | CYIdentifier *name_; | |
670 | CYExpression *super_; | |
671 | CYField *fields_; | |
672 | CYMessage *messages_; | |
673 | ||
674 | CYClass(CYIdentifier *name, CYExpression *super, CYField *fields, CYMessage *messages) : | |
675 | name_(name), | |
676 | super_(super), | |
677 | fields_(fields), | |
678 | messages_(messages) | |
679 | { | |
680 | } | |
681 | ||
682 | virtual void Output(std::ostream &out) const; | |
683 | }; | |
684 | ||
685 | struct CYFunctionParameter : | |
686 | CYNext<CYFunctionParameter>, | |
5999c315 JF |
687 | CYThing |
688 | { | |
cf7d4c69 | 689 | CYIdentifier *name_; |
cf7d4c69 | 690 | |
b09da87b JF |
691 | CYFunctionParameter(CYIdentifier *name, CYFunctionParameter *next) : |
692 | CYNext<CYFunctionParameter>(next), | |
62014ea9 | 693 | name_(name) |
cf7d4c69 JF |
694 | { |
695 | } | |
5999c315 JF |
696 | |
697 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
698 | }; |
699 | ||
700 | struct CYFor : | |
701 | CYStatement | |
702 | { | |
703 | CYForInitialiser *initialiser_; | |
704 | CYExpression *test_; | |
705 | CYExpression *increment_; | |
706 | CYStatement *code_; | |
707 | ||
708 | CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : | |
709 | initialiser_(initialiser), | |
710 | test_(test), | |
711 | increment_(increment), | |
712 | code_(code) | |
713 | { | |
714 | } | |
5999c315 JF |
715 | |
716 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
717 | }; |
718 | ||
719 | struct CYForIn : | |
720 | CYStatement | |
721 | { | |
722 | CYForInInitialiser *initialiser_; | |
723 | CYExpression *set_; | |
724 | CYStatement *code_; | |
725 | ||
726 | CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : | |
727 | initialiser_(initialiser), | |
728 | set_(set), | |
729 | code_(code) | |
730 | { | |
731 | } | |
5999c315 JF |
732 | |
733 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
734 | }; |
735 | ||
62014ea9 JF |
736 | struct CYProperty : |
737 | CYNext<CYProperty> | |
738 | { | |
cf7d4c69 JF |
739 | CYName *name_; |
740 | CYExpression *value_; | |
cf7d4c69 JF |
741 | |
742 | CYProperty(CYName *name, CYExpression *value, CYProperty *next) : | |
62014ea9 | 743 | CYNext<CYProperty>(next), |
cf7d4c69 | 744 | name_(name), |
62014ea9 | 745 | value_(value) |
cf7d4c69 JF |
746 | { |
747 | } | |
5999c315 | 748 | |
5999c315 | 749 | virtual void Output(std::ostream &out) const; |
cf7d4c69 JF |
750 | }; |
751 | ||
693d501b JF |
752 | struct CYObject : |
753 | CYLiteral | |
754 | { | |
755 | CYProperty *property_; | |
756 | ||
757 | CYObject(CYProperty *property) : | |
758 | property_(property) | |
759 | { | |
760 | } | |
761 | ||
b09da87b | 762 | void Output(std::ostream &out, CYFlags flags) const; |
693d501b JF |
763 | }; |
764 | ||
5999c315 JF |
765 | struct CYCatch : |
766 | CYThing | |
767 | { | |
cf7d4c69 JF |
768 | CYIdentifier *name_; |
769 | CYStatement *code_; | |
770 | ||
771 | CYCatch(CYIdentifier *name, CYStatement *code) : | |
772 | name_(name), | |
773 | code_(code) | |
774 | { | |
775 | } | |
5999c315 JF |
776 | |
777 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
778 | }; |
779 | ||
b09da87b | 780 | struct CYSend : |
63b4c5a8 JF |
781 | CYExpression |
782 | { | |
cf7d4c69 JF |
783 | CYExpression *self_; |
784 | CYArgument *arguments_; | |
63b4c5a8 | 785 | |
b09da87b | 786 | CYSend(CYExpression *self, CYArgument *arguments) : |
cf7d4c69 JF |
787 | self_(self), |
788 | arguments_(arguments) | |
63b4c5a8 JF |
789 | { |
790 | } | |
5999c315 | 791 | |
d35a3b07 JF |
792 | CYPrecedence(0) |
793 | ||
b09da87b | 794 | virtual void Output(std::ostream &out, CYFlags flags) const; |
63b4c5a8 JF |
795 | }; |
796 | ||
cf7d4c69 JF |
797 | struct CYMember : |
798 | CYExpression | |
799 | { | |
800 | CYExpression *object_; | |
801 | CYExpression *property_; | |
802 | ||
803 | CYMember(CYExpression *object, CYExpression *property) : | |
804 | object_(object), | |
805 | property_(property) | |
806 | { | |
807 | } | |
5999c315 | 808 | |
d35a3b07 JF |
809 | CYPrecedence(1) |
810 | ||
b09da87b | 811 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
812 | }; |
813 | ||
814 | struct CYNew : | |
815 | CYExpression | |
816 | { | |
817 | CYExpression *constructor_; | |
818 | CYArgument *arguments_; | |
819 | ||
820 | CYNew(CYExpression *constructor, CYArgument *arguments) : | |
821 | constructor_(constructor), | |
822 | arguments_(arguments) | |
823 | { | |
824 | } | |
5999c315 | 825 | |
d35a3b07 JF |
826 | CYPrecedence(1) |
827 | ||
b09da87b | 828 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
829 | }; |
830 | ||
831 | struct CYCall : | |
832 | CYExpression | |
833 | { | |
834 | CYExpression *function_; | |
835 | CYArgument *arguments_; | |
836 | ||
837 | CYCall(CYExpression *function, CYArgument *arguments) : | |
838 | function_(function), | |
839 | arguments_(arguments) | |
840 | { | |
841 | } | |
5999c315 | 842 | |
d35a3b07 JF |
843 | CYPrecedence(2) |
844 | ||
b09da87b | 845 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
846 | }; |
847 | ||
848 | struct CYIf : | |
849 | CYStatement | |
850 | { | |
851 | CYExpression *test_; | |
852 | CYStatement *true_; | |
853 | CYStatement *false_; | |
854 | ||
855 | CYIf(CYExpression *test, CYStatement *_true, CYStatement *_false) : | |
856 | test_(test), | |
857 | true_(_true), | |
858 | false_(_false) | |
859 | { | |
860 | } | |
5999c315 JF |
861 | |
862 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
863 | }; |
864 | ||
865 | struct CYDoWhile : | |
866 | CYStatement | |
867 | { | |
868 | CYExpression *test_; | |
869 | CYStatement *code_; | |
870 | ||
871 | CYDoWhile(CYExpression *test, CYStatement *code) : | |
872 | test_(test), | |
873 | code_(code) | |
874 | { | |
875 | } | |
5999c315 JF |
876 | |
877 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
878 | }; |
879 | ||
880 | struct CYWhile : | |
881 | CYStatement | |
882 | { | |
883 | CYExpression *test_; | |
884 | CYStatement *code_; | |
885 | ||
886 | CYWhile(CYExpression *test, CYStatement *code) : | |
887 | test_(test), | |
888 | code_(code) | |
889 | { | |
890 | } | |
5999c315 JF |
891 | |
892 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
893 | }; |
894 | ||
895 | struct CYLambda : | |
896 | CYExpression | |
897 | { | |
898 | CYIdentifier *name_; | |
b09da87b | 899 | CYFunctionParameter *parameters_; |
cf7d4c69 JF |
900 | CYSource *body_; |
901 | ||
b09da87b | 902 | CYLambda(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) : |
cf7d4c69 JF |
903 | name_(name), |
904 | parameters_(parameters), | |
905 | body_(body) | |
906 | { | |
907 | } | |
5999c315 | 908 | |
d35a3b07 JF |
909 | CYPrecedence(0) |
910 | ||
b09da87b | 911 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
912 | }; |
913 | ||
914 | struct CYFunction : | |
5999c315 JF |
915 | CYLambda, |
916 | CYSource | |
cf7d4c69 | 917 | { |
b09da87b | 918 | CYFunction(CYIdentifier *name, CYFunctionParameter *parameters, CYSource *body) : |
cf7d4c69 JF |
919 | CYLambda(name, parameters, body) |
920 | { | |
921 | } | |
5999c315 JF |
922 | |
923 | virtual void Output(std::ostream &out) const; | |
924 | }; | |
925 | ||
926 | struct CYExpress : | |
927 | CYStatement | |
928 | { | |
929 | CYExpression *expression_; | |
930 | ||
931 | CYExpress(CYExpression *expression) : | |
932 | expression_(expression) | |
933 | { | |
934 | } | |
935 | ||
936 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
937 | }; |
938 | ||
939 | struct CYContinue : | |
940 | CYStatement | |
941 | { | |
942 | CYIdentifier *label_; | |
943 | ||
944 | CYContinue(CYIdentifier *label) : | |
945 | label_(label) | |
946 | { | |
947 | } | |
5999c315 JF |
948 | |
949 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
950 | }; |
951 | ||
952 | struct CYBreak : | |
953 | CYStatement | |
954 | { | |
955 | CYIdentifier *label_; | |
956 | ||
957 | CYBreak(CYIdentifier *label) : | |
958 | label_(label) | |
959 | { | |
960 | } | |
5999c315 JF |
961 | |
962 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
963 | }; |
964 | ||
965 | struct CYReturn : | |
966 | CYStatement | |
967 | { | |
968 | CYExpression *value_; | |
969 | ||
970 | CYReturn(CYExpression *value) : | |
971 | value_(value) | |
972 | { | |
973 | } | |
5999c315 JF |
974 | |
975 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
976 | }; |
977 | ||
978 | struct CYEmpty : | |
979 | CYStatement | |
980 | { | |
5999c315 JF |
981 | virtual void Output(std::ostream &out) const; |
982 | virtual void Output(std::ostream &out, bool block) const; | |
cf7d4c69 JF |
983 | }; |
984 | ||
985 | struct CYTry : | |
986 | CYStatement | |
987 | { | |
988 | CYStatement *try_; | |
989 | CYCatch *catch_; | |
990 | CYStatement *finally_; | |
991 | ||
992 | CYTry(CYStatement *_try, CYCatch *_catch, CYStatement *finally) : | |
993 | try_(_try), | |
994 | catch_(_catch), | |
995 | finally_(finally) | |
996 | { | |
997 | } | |
5999c315 JF |
998 | |
999 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
1000 | }; |
1001 | ||
1002 | struct CYThrow : | |
1003 | CYStatement | |
1004 | { | |
1005 | CYExpression *value_; | |
1006 | ||
1007 | CYThrow(CYExpression *value) : | |
1008 | value_(value) | |
1009 | { | |
1010 | } | |
5999c315 JF |
1011 | |
1012 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
1013 | }; |
1014 | ||
1015 | struct CYWith : | |
1016 | CYStatement | |
1017 | { | |
1018 | CYExpression *scope_; | |
1019 | CYStatement *code_; | |
1020 | ||
1021 | CYWith(CYExpression *scope, CYStatement *code) : | |
1022 | scope_(scope), | |
1023 | code_(code) | |
1024 | { | |
1025 | } | |
5999c315 JF |
1026 | |
1027 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
1028 | }; |
1029 | ||
1030 | struct CYSwitch : | |
1031 | CYStatement | |
1032 | { | |
1033 | CYExpression *value_; | |
1034 | CYClause *clauses_; | |
1035 | ||
1036 | CYSwitch(CYExpression *value, CYClause *clauses) : | |
1037 | value_(value), | |
1038 | clauses_(clauses) | |
1039 | { | |
1040 | } | |
5999c315 JF |
1041 | |
1042 | virtual void Output(std::ostream &out) const; | |
cf7d4c69 JF |
1043 | }; |
1044 | ||
1045 | struct CYCondition : | |
1046 | CYExpression | |
1047 | { | |
1048 | CYExpression *test_; | |
1049 | CYExpression *true_; | |
1050 | CYExpression *false_; | |
1051 | ||
1052 | CYCondition(CYExpression *test, CYExpression *_true, CYExpression *_false) : | |
91a416e4 | 1053 | test_(test), |
cf7d4c69 JF |
1054 | true_(_true), |
1055 | false_(_false) | |
1056 | { | |
1057 | } | |
5999c315 | 1058 | |
d35a3b07 JF |
1059 | CYPrecedence(15) |
1060 | ||
b09da87b | 1061 | virtual void Output(std::ostream &out, CYFlags flags) const; |
5999c315 JF |
1062 | }; |
1063 | ||
1064 | struct CYAddressOf : | |
1065 | CYPrefix | |
1066 | { | |
1067 | CYAddressOf(CYExpression *rhs) : | |
1068 | CYPrefix(rhs) | |
1069 | { | |
1070 | } | |
1071 | ||
1072 | virtual const char *Operator() const { | |
1073 | return "&"; | |
1074 | } | |
1075 | ||
b09da87b | 1076 | CYAlphabetic(false) |
d35a3b07 JF |
1077 | CYPrecedence(2) |
1078 | ||
b09da87b | 1079 | virtual void Output(std::ostream &out, CYFlags flags) const; |
5999c315 JF |
1080 | }; |
1081 | ||
1082 | struct CYIndirect : | |
1083 | CYPrefix | |
1084 | { | |
1085 | CYIndirect(CYExpression *rhs) : | |
1086 | CYPrefix(rhs) | |
1087 | { | |
1088 | } | |
1089 | ||
1090 | virtual const char *Operator() const { | |
1091 | return "*"; | |
1092 | } | |
1093 | ||
b09da87b | 1094 | CYAlphabetic(false) |
d35a3b07 JF |
1095 | CYPrecedence(1) |
1096 | ||
b09da87b | 1097 | virtual void Output(std::ostream &out, CYFlags flags) const; |
cf7d4c69 JF |
1098 | }; |
1099 | ||
1100 | #define CYPostfix_(op, name) \ | |
1101 | struct CY ## name : \ | |
1102 | CYPostfix \ | |
1103 | { \ | |
1104 | CY ## name(CYExpression *lhs) : \ | |
1105 | CYPostfix(lhs) \ | |
1106 | { \ | |
1107 | } \ | |
d35a3b07 JF |
1108 | \ |
1109 | CYPrecedence(3) \ | |
5999c315 JF |
1110 | \ |
1111 | virtual const char *Operator() const { \ | |
1112 | return op; \ | |
1113 | } \ | |
cf7d4c69 JF |
1114 | }; |
1115 | ||
b09da87b | 1116 | #define CYPrefix_(alphabetic, op, name) \ |
cf7d4c69 JF |
1117 | struct CY ## name : \ |
1118 | CYPrefix \ | |
1119 | { \ | |
1120 | CY ## name(CYExpression *rhs) : \ | |
1121 | CYPrefix(rhs) \ | |
1122 | { \ | |
1123 | } \ | |
d35a3b07 | 1124 | \ |
b09da87b | 1125 | CYAlphabetic(alphabetic) \ |
d35a3b07 | 1126 | CYPrecedence(4) \ |
5999c315 JF |
1127 | \ |
1128 | virtual const char *Operator() const { \ | |
1129 | return op; \ | |
1130 | } \ | |
cf7d4c69 JF |
1131 | }; |
1132 | ||
b09da87b | 1133 | #define CYInfix_(alphabetic, precedence, op, name) \ |
cf7d4c69 JF |
1134 | struct CY ## name : \ |
1135 | CYInfix \ | |
1136 | { \ | |
1137 | CY ## name(CYExpression *lhs, CYExpression *rhs) : \ | |
1138 | CYInfix(lhs, rhs) \ | |
1139 | { \ | |
1140 | } \ | |
d35a3b07 | 1141 | \ |
b09da87b | 1142 | CYAlphabetic(alphabetic) \ |
d35a3b07 | 1143 | CYPrecedence(precedence) \ |
5999c315 JF |
1144 | \ |
1145 | virtual const char *Operator() const { \ | |
1146 | return op; \ | |
1147 | } \ | |
cf7d4c69 JF |
1148 | }; |
1149 | ||
1150 | #define CYAssignment_(op, name) \ | |
1151 | struct CY ## name ## Assign : \ | |
1152 | CYAssignment \ | |
1153 | { \ | |
1154 | CY ## name ## Assign(CYExpression *lhs, CYExpression *rhs) : \ | |
1155 | CYAssignment(lhs, rhs) \ | |
1156 | { \ | |
1157 | } \ | |
d35a3b07 JF |
1158 | \ |
1159 | CYPrecedence(16) \ | |
5999c315 JF |
1160 | \ |
1161 | virtual const char *Operator() const { \ | |
1162 | return op; \ | |
1163 | } \ | |
cf7d4c69 JF |
1164 | }; |
1165 | ||
1166 | CYPostfix_("++", PostIncrement) | |
1167 | CYPostfix_("--", PostDecrement) | |
1168 | ||
b09da87b JF |
1169 | CYPrefix_(true, "delete", Delete) |
1170 | CYPrefix_(true, "void", Void) | |
1171 | CYPrefix_(true, "typeof", TypeOf) | |
1172 | CYPrefix_(false, "++", PreIncrement) | |
1173 | CYPrefix_(false, "--", PreDecrement) | |
1174 | CYPrefix_(false, "-", Negate) | |
1175 | CYPrefix_(false, "~", BitwiseNot) | |
1176 | CYPrefix_(false, "!", LogicalNot) | |
1177 | ||
1178 | CYInfix_(false, 5, "*", Multiply) | |
1179 | CYInfix_(false, 5, "/", Divide) | |
1180 | CYInfix_(false, 5, "%", Modulus) | |
1181 | CYInfix_(false, 6, "+", Add) | |
1182 | CYInfix_(false, 6, "-", Subtract) | |
1183 | CYInfix_(false, 7, "<<", ShiftLeft) | |
1184 | CYInfix_(false, 7, ">>", ShiftRightSigned) | |
1185 | CYInfix_(false, 7, ">>>", ShiftRightUnsigned) | |
1186 | CYInfix_(false, 8, "<", Less) | |
1187 | CYInfix_(false, 8, ">", Greater) | |
1188 | CYInfix_(false, 8, "<=", LessOrEqual) | |
1189 | CYInfix_(false, 8, ">=", GreaterOrEqual) | |
1190 | CYInfix_(true, 8, "instanceof", InstanceOf) | |
1191 | CYInfix_(true, 8, "in", In) | |
1192 | CYInfix_(false, 9, "==", Equal) | |
1193 | CYInfix_(false, 9, "!=", NotEqual) | |
1194 | CYInfix_(false, 9, "===", Identical) | |
1195 | CYInfix_(false, 9, "!==", NotIdentical) | |
1196 | CYInfix_(false, 10, "&", BitwiseAnd) | |
1197 | CYInfix_(false, 11, "^", BitwiseXOr) | |
1198 | CYInfix_(false, 12, "|", BitwiseOr) | |
1199 | CYInfix_(false, 13, "&&", LogicalAnd) | |
1200 | CYInfix_(false, 14, "||", LogicalOr) | |
cf7d4c69 JF |
1201 | |
1202 | CYAssignment_("=", ) | |
1203 | CYAssignment_("*=", Multiply) | |
1204 | CYAssignment_("/=", Divide) | |
1205 | CYAssignment_("%=", Modulus) | |
1206 | CYAssignment_("+=", Add) | |
1207 | CYAssignment_("-=", Subtract) | |
1208 | CYAssignment_("<<=", ShiftLeft) | |
1209 | CYAssignment_(">>=", ShiftRightSigned) | |
1210 | CYAssignment_(">>>=", ShiftRightUnsigned) | |
1211 | CYAssignment_("&=", BitwiseAnd) | |
1212 | CYAssignment_("^=", BitwiseXOr) | |
1213 | CYAssignment_("|=", BitwiseOr) | |
1214 | ||
63b4c5a8 | 1215 | #endif/*CYPARSER_HPP*/ |