iv-phonic 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. data/.autotest +24 -0
  2. data/Manifest.txt +49 -0
  3. data/README.rdoc +32 -0
  4. data/Rakefile +54 -0
  5. data/ext/include/iv/algorithm.h +23 -0
  6. data/ext/include/iv/alloc.h +200 -0
  7. data/ext/include/iv/any.h +71 -0
  8. data/ext/include/iv/ast-factory.h +277 -0
  9. data/ext/include/iv/ast-fwd.h +92 -0
  10. data/ext/include/iv/ast-serializer.h +579 -0
  11. data/ext/include/iv/ast-visitor.h +121 -0
  12. data/ext/include/iv/ast.h +1127 -0
  13. data/ext/include/iv/chars.h +83 -0
  14. data/ext/include/iv/cmdline.h +830 -0
  15. data/ext/include/iv/conversions.h +308 -0
  16. data/ext/include/iv/dtoa.h +20 -0
  17. data/ext/include/iv/enable_if.h +18 -0
  18. data/ext/include/iv/errors.h +15 -0
  19. data/ext/include/iv/fixedcontainer.h +42 -0
  20. data/ext/include/iv/functor.h +29 -0
  21. data/ext/include/iv/lexer.h +1281 -0
  22. data/ext/include/iv/location.h +23 -0
  23. data/ext/include/iv/mt19937.h +175 -0
  24. data/ext/include/iv/noncopyable.h +30 -0
  25. data/ext/include/iv/none.h +10 -0
  26. data/ext/include/iv/parser.h +2150 -0
  27. data/ext/include/iv/source.h +27 -0
  28. data/ext/include/iv/space.h +178 -0
  29. data/ext/include/iv/static_assert.h +30 -0
  30. data/ext/include/iv/stringpiece.h +385 -0
  31. data/ext/include/iv/token.h +311 -0
  32. data/ext/include/iv/ucdata.h +58 -0
  33. data/ext/include/iv/uchar.h +8 -0
  34. data/ext/include/iv/ustring.h +28 -0
  35. data/ext/include/iv/ustringpiece.h +9 -0
  36. data/ext/include/iv/utils.h +83 -0
  37. data/ext/include/iv/xorshift.h +74 -0
  38. data/ext/iv/phonic/ast-fwd.h +21 -0
  39. data/ext/iv/phonic/ast.h +10 -0
  40. data/ext/iv/phonic/creator.h +530 -0
  41. data/ext/iv/phonic/encoding.h +110 -0
  42. data/ext/iv/phonic/extconf.rb +5 -0
  43. data/ext/iv/phonic/factory.h +247 -0
  44. data/ext/iv/phonic/parser.h +12 -0
  45. data/ext/iv/phonic/phonic.cc +69 -0
  46. data/ext/iv/phonic/rnode.h +15 -0
  47. data/ext/iv/phonic/rparser.h +48 -0
  48. data/ext/iv/phonic/source.h +146 -0
  49. data/test/test_iv_phonic.rb +32 -0
  50. metadata +159 -0
@@ -0,0 +1,1127 @@
1
+ #ifndef _IV_AST_H_
2
+ #define _IV_AST_H_
3
+ #include <vector>
4
+ #include <tr1/unordered_map>
5
+ #include <tr1/tuple>
6
+ #include "uchar.h"
7
+ #include "noncopyable.h"
8
+ #include "utils.h"
9
+ #include "space.h"
10
+ #include "functor.h"
11
+ #include "token.h"
12
+ #include "ast-fwd.h"
13
+ #include "ast-visitor.h"
14
+ #include "source.h"
15
+ #include "ustringpiece.h"
16
+
17
+ namespace iv {
18
+ namespace core {
19
+ namespace ast {
20
+ #define ACCEPT_VISITOR \
21
+ inline void Accept(\
22
+ typename AstVisitor<Factory>::type* visitor) {\
23
+ visitor->Visit(this);\
24
+ }\
25
+ inline void Accept(\
26
+ typename AstVisitor<Factory>::const_type * visitor) const {\
27
+ visitor->Visit(this);\
28
+ }
29
+
30
+ #define DECLARE_NODE_TYPE(type) \
31
+ inline const type<Factory>* As##type() const { return this; }\
32
+ inline type<Factory>* As##type() { return this; }
33
+
34
+ #define DECLARE_NODE_TYPE_BASE(type) \
35
+ inline virtual const type<Factory>* As##type() const { return NULL; }\
36
+ inline virtual type<Factory>* As##type() { return NULL; }
37
+
38
+ template<typename Factory>
39
+ class Scope : public SpaceObject,
40
+ private Noncopyable<Scope<Factory> >::type {
41
+ public:
42
+ typedef std::pair<Identifier<Factory>*, bool> Variable;
43
+ typedef typename SpaceVector<Factory, Variable>::type Variables;
44
+ typedef typename SpaceVector<
45
+ Factory,
46
+ FunctionLiteral<Factory>*>::type FunctionLiterals;
47
+ typedef Scope<Factory> this_type;
48
+
49
+ explicit Scope(Factory* factory)
50
+ : up_(NULL),
51
+ vars_(typename Variables::allocator_type(factory)),
52
+ funcs_(typename FunctionLiterals::allocator_type(factory)) {
53
+ }
54
+ void AddUnresolved(Identifier<Factory>* name, bool is_const) {
55
+ vars_.push_back(std::make_pair(name, is_const));
56
+ }
57
+ void AddFunctionDeclaration(FunctionLiteral<Factory>* func) {
58
+ funcs_.push_back(func);
59
+ }
60
+ void SetUpperScope(this_type* scope) {
61
+ up_ = scope;
62
+ }
63
+ inline const FunctionLiterals& function_declarations() const {
64
+ return funcs_;
65
+ }
66
+ inline const Variables& variables() const {
67
+ return vars_;
68
+ }
69
+ this_type* GetUpperScope() {
70
+ return up_;
71
+ }
72
+ protected:
73
+ this_type* up_;
74
+ Variables vars_;
75
+ FunctionLiterals funcs_;
76
+ };
77
+
78
+ template<typename Factory>
79
+ class AstNode : public SpaceObject,
80
+ private Noncopyable<AstNode<Factory> >::type {
81
+ public:
82
+ virtual ~AstNode() = 0;
83
+
84
+ DECLARE_NODE_TYPE_BASE(Statement)
85
+ DECLARE_NODE_TYPE_BASE(Block)
86
+ DECLARE_NODE_TYPE_BASE(ExpressionStatement)
87
+ DECLARE_NODE_TYPE_BASE(EmptyStatement)
88
+ DECLARE_NODE_TYPE_BASE(VariableStatement)
89
+ DECLARE_NODE_TYPE_BASE(DebuggerStatement)
90
+ DECLARE_NODE_TYPE_BASE(FunctionStatement)
91
+ DECLARE_NODE_TYPE_BASE(IfStatement)
92
+ DECLARE_NODE_TYPE_BASE(IterationStatement)
93
+ DECLARE_NODE_TYPE_BASE(DoWhileStatement)
94
+ DECLARE_NODE_TYPE_BASE(WhileStatement)
95
+ DECLARE_NODE_TYPE_BASE(ForStatement)
96
+ DECLARE_NODE_TYPE_BASE(ForInStatement)
97
+ DECLARE_NODE_TYPE_BASE(ContinueStatement)
98
+ DECLARE_NODE_TYPE_BASE(BreakStatement)
99
+ DECLARE_NODE_TYPE_BASE(ReturnStatement)
100
+ DECLARE_NODE_TYPE_BASE(WithStatement)
101
+ DECLARE_NODE_TYPE_BASE(LabelledStatement)
102
+ DECLARE_NODE_TYPE_BASE(SwitchStatement)
103
+ DECLARE_NODE_TYPE_BASE(ThrowStatement)
104
+ DECLARE_NODE_TYPE_BASE(TryStatement)
105
+ DECLARE_NODE_TYPE_BASE(BreakableStatement)
106
+ DECLARE_NODE_TYPE_BASE(NamedOnlyBreakableStatement)
107
+ DECLARE_NODE_TYPE_BASE(AnonymousBreakableStatement)
108
+
109
+ DECLARE_NODE_TYPE_BASE(Expression)
110
+
111
+ DECLARE_NODE_TYPE_BASE(Literal)
112
+ DECLARE_NODE_TYPE_BASE(ThisLiteral)
113
+ DECLARE_NODE_TYPE_BASE(NullLiteral)
114
+ DECLARE_NODE_TYPE_BASE(FalseLiteral)
115
+ DECLARE_NODE_TYPE_BASE(TrueLiteral)
116
+ DECLARE_NODE_TYPE_BASE(Undefined)
117
+ DECLARE_NODE_TYPE_BASE(NumberLiteral)
118
+ DECLARE_NODE_TYPE_BASE(StringLiteral)
119
+ DECLARE_NODE_TYPE_BASE(Directivable)
120
+ DECLARE_NODE_TYPE_BASE(Identifier)
121
+ DECLARE_NODE_TYPE_BASE(RegExpLiteral)
122
+ DECLARE_NODE_TYPE_BASE(ArrayLiteral)
123
+ DECLARE_NODE_TYPE_BASE(ObjectLiteral)
124
+ DECLARE_NODE_TYPE_BASE(FunctionLiteral)
125
+
126
+ DECLARE_NODE_TYPE_BASE(UnaryOperation)
127
+ DECLARE_NODE_TYPE_BASE(PostfixExpression)
128
+ DECLARE_NODE_TYPE_BASE(Assignment)
129
+ DECLARE_NODE_TYPE_BASE(BinaryOperation)
130
+ DECLARE_NODE_TYPE_BASE(ConditionalExpression)
131
+ DECLARE_NODE_TYPE_BASE(PropertyAccess)
132
+ DECLARE_NODE_TYPE_BASE(IdentifierAccess)
133
+ DECLARE_NODE_TYPE_BASE(IndexAccess)
134
+ DECLARE_NODE_TYPE_BASE(Call)
135
+ DECLARE_NODE_TYPE_BASE(FunctionCall)
136
+ DECLARE_NODE_TYPE_BASE(ConstructorCall)
137
+
138
+ virtual void Accept(
139
+ typename AstVisitor<Factory>::type* visitor) = 0;
140
+ virtual void Accept(
141
+ typename AstVisitor<Factory>::const_type* visitor) const = 0;
142
+ };
143
+
144
+ template<typename Factory>
145
+ inline AstNode<Factory>::~AstNode() { }
146
+
147
+ #undef DECLARE_NODE_TYPE_BASE
148
+ // Statement
149
+ // : Block
150
+ // | FunctionStatement
151
+ // | VariableStatement
152
+ // | EmptyStatement
153
+ // | ExpressionStatement
154
+ // | IfStatement
155
+ // | IterationStatement
156
+ // | ContinueStatement
157
+ // | BreakStatement
158
+ // | ReturnStatement
159
+ // | WithStatement
160
+ // | LabelledStatement
161
+ // | SwitchStatement
162
+ // | ThrowStatement
163
+ // | TryStatement
164
+ // | DebuggerStatement
165
+
166
+ // Expression
167
+ template<typename Factory>
168
+ class Expression : public AstNode<Factory> {
169
+ public:
170
+ inline virtual bool IsValidLeftHandSide() const { return false; }
171
+ DECLARE_NODE_TYPE(Expression)
172
+ };
173
+
174
+ template<typename Factory>
175
+ class Literal : public Expression<Factory> {
176
+ public:
177
+ DECLARE_NODE_TYPE(Literal)
178
+ };
179
+
180
+ template<typename Factory>
181
+ class Statement : public AstNode<Factory> {
182
+ public:
183
+ DECLARE_NODE_TYPE(Statement)
184
+ };
185
+
186
+ template<typename Factory>
187
+ class BreakableStatement : public Statement<Factory> {
188
+ public:
189
+ typedef typename SpaceVector<Factory, Identifier<Factory>*>::type Identifiers;
190
+ BreakableStatement() : labels_(NULL) { }
191
+ void set_labels(Identifiers* labels) {
192
+ labels_ = labels;
193
+ }
194
+ Identifiers* labels() const {
195
+ return labels_;
196
+ }
197
+ DECLARE_NODE_TYPE(BreakableStatement)
198
+ protected:
199
+ Identifiers* labels_;
200
+ };
201
+
202
+ template<typename Factory>
203
+ class NamedOnlyBreakableStatement : public BreakableStatement<Factory> {
204
+ public:
205
+ DECLARE_NODE_TYPE(NamedOnlyBreakableStatement)
206
+ };
207
+
208
+ template<typename Factory>
209
+ class AnonymousBreakableStatement : public BreakableStatement<Factory> {
210
+ public:
211
+ DECLARE_NODE_TYPE(AnonymousBreakableStatement)
212
+ };
213
+
214
+ template<typename Factory>
215
+ class Block : public NamedOnlyBreakableStatement<Factory> {
216
+ public:
217
+ typedef typename SpaceVector<Factory, Statement<Factory>*>::type Statements;
218
+ explicit Block(Factory* factory)
219
+ : body_(typename Statements::allocator_type(factory)) { }
220
+
221
+ void AddStatement(Statement<Factory>* stmt) {
222
+ body_.push_back(stmt);
223
+ }
224
+
225
+ inline const Statements& body() const {
226
+ return body_;
227
+ }
228
+ ACCEPT_VISITOR
229
+ DECLARE_NODE_TYPE(Block)
230
+ private:
231
+ Statements body_;
232
+ };
233
+
234
+ template<typename Factory>
235
+ class FunctionStatement : public Statement<Factory> {
236
+ public:
237
+ explicit FunctionStatement(FunctionLiteral<Factory>* func)
238
+ : function_(func) {
239
+ }
240
+ inline FunctionLiteral<Factory>* function() const {
241
+ return function_;
242
+ }
243
+ ACCEPT_VISITOR
244
+ DECLARE_NODE_TYPE(FunctionStatement)
245
+ private:
246
+ FunctionLiteral<Factory>* function_;
247
+ };
248
+
249
+ template<typename Factory>
250
+ class VariableStatement : public Statement<Factory> {
251
+ public:
252
+ typedef typename SpaceVector<
253
+ Factory,
254
+ Declaration<Factory>*>::type Declarations;
255
+ VariableStatement(Token::Type type, Factory* factory)
256
+ : is_const_(type == Token::CONST),
257
+ decls_(typename Declarations::allocator_type(factory)) { }
258
+
259
+ void AddDeclaration(Declaration<Factory>* decl) {
260
+ decls_.push_back(decl);
261
+ }
262
+
263
+ inline const Declarations& decls() const {
264
+ return decls_;
265
+ }
266
+ inline bool IsConst() const {
267
+ return is_const_;
268
+ }
269
+ ACCEPT_VISITOR
270
+ DECLARE_NODE_TYPE(VariableStatement)
271
+ private:
272
+ const bool is_const_;
273
+ Declarations decls_;
274
+ };
275
+
276
+ template<typename Factory>
277
+ class Declaration : public AstNode<Factory> {
278
+ public:
279
+ Declaration(Identifier<Factory>* name, Expression<Factory>* expr)
280
+ : name_(name),
281
+ expr_(expr) {
282
+ }
283
+ inline Identifier<Factory>* name() const {
284
+ return name_;
285
+ }
286
+ inline Expression<Factory>* expr() const {
287
+ return expr_;
288
+ }
289
+ ACCEPT_VISITOR
290
+ private:
291
+ Identifier<Factory>* name_;
292
+ Expression<Factory>* expr_;
293
+ };
294
+
295
+ template<typename Factory>
296
+ class EmptyStatement : public Statement<Factory> {
297
+ public:
298
+ DECLARE_NODE_TYPE(EmptyStatement)
299
+ ACCEPT_VISITOR
300
+ };
301
+
302
+ template<typename Factory>
303
+ class IfStatement : public Statement<Factory> {
304
+ public:
305
+ IfStatement(Expression<Factory>* cond, Statement<Factory>* then)
306
+ : cond_(cond),
307
+ then_(then),
308
+ else_(NULL) {
309
+ }
310
+ void SetElse(Statement<Factory>* stmt) {
311
+ else_ = stmt;
312
+ }
313
+ inline Expression<Factory>* cond() const { return cond_; }
314
+ inline Statement<Factory>* then_statement() const { return then_; }
315
+ inline Statement<Factory>* else_statement() const { return else_; }
316
+ ACCEPT_VISITOR
317
+ DECLARE_NODE_TYPE(IfStatement)
318
+ private:
319
+ Expression<Factory>* cond_;
320
+ Statement<Factory>* then_;
321
+ Statement<Factory>* else_;
322
+ };
323
+
324
+ template<typename Factory>
325
+ class IterationStatement : public AnonymousBreakableStatement<Factory> {
326
+ public:
327
+ IterationStatement()
328
+ : body_(NULL) {
329
+ }
330
+ inline Statement<Factory>* body() const { return body_; }
331
+ inline void set_body(Statement<Factory>* stmt) {
332
+ body_ = stmt;
333
+ }
334
+ DECLARE_NODE_TYPE(IterationStatement)
335
+ private:
336
+ Statement<Factory>* body_;
337
+ };
338
+
339
+ template<typename Factory>
340
+ class DoWhileStatement : public IterationStatement<Factory> {
341
+ public:
342
+ DoWhileStatement()
343
+ : IterationStatement<Factory>(),
344
+ cond_(NULL) {
345
+ }
346
+ inline Expression<Factory>* cond() const { return cond_; }
347
+ inline void set_cond(Expression<Factory>* expr) {
348
+ cond_ = expr;
349
+ }
350
+ ACCEPT_VISITOR
351
+ DECLARE_NODE_TYPE(DoWhileStatement)
352
+ private:
353
+ Expression<Factory>* cond_;
354
+ };
355
+
356
+ template<typename Factory>
357
+ class WhileStatement : public IterationStatement<Factory> {
358
+ public:
359
+ explicit WhileStatement(Expression<Factory>* cond)
360
+ : IterationStatement<Factory>(),
361
+ cond_(cond) {
362
+ }
363
+ inline Expression<Factory>* cond() const { return cond_; }
364
+ ACCEPT_VISITOR
365
+ DECLARE_NODE_TYPE(WhileStatement)
366
+ private:
367
+ Expression<Factory>* cond_;
368
+ };
369
+
370
+ template<typename Factory>
371
+ class ForStatement : public IterationStatement<Factory> {
372
+ public:
373
+ ForStatement()
374
+ : IterationStatement<Factory>(),
375
+ init_(NULL),
376
+ cond_(NULL),
377
+ next_(NULL) {
378
+ }
379
+ inline void SetInit(Statement<Factory>* init) { init_ = init; }
380
+ inline void SetCondition(Expression<Factory>* cond) { cond_ = cond; }
381
+ inline void SetNext(Statement<Factory>* next) { next_ = next; }
382
+ inline Statement<Factory>* init() const { return init_; }
383
+ inline Expression<Factory>* cond() const { return cond_; }
384
+ inline Statement<Factory>* next() const { return next_; }
385
+ ACCEPT_VISITOR
386
+ DECLARE_NODE_TYPE(ForStatement)
387
+ private:
388
+ Statement<Factory>* init_;
389
+ Expression<Factory>* cond_;
390
+ Statement<Factory>* next_;
391
+ };
392
+
393
+ template<typename Factory>
394
+ class ForInStatement : public IterationStatement<Factory> {
395
+ public:
396
+ ForInStatement(Statement<Factory>* each,
397
+ Expression<Factory>* enumerable)
398
+ : IterationStatement<Factory>(),
399
+ each_(each),
400
+ enumerable_(enumerable) {
401
+ }
402
+ inline Statement<Factory>* each() const { return each_; }
403
+ inline Expression<Factory>* enumerable() const { return enumerable_; }
404
+ ACCEPT_VISITOR
405
+ DECLARE_NODE_TYPE(ForInStatement)
406
+ private:
407
+ Statement<Factory>* each_;
408
+ Expression<Factory>* enumerable_;
409
+ };
410
+
411
+ template<typename Factory>
412
+ class ContinueStatement : public Statement<Factory> {
413
+ public:
414
+ ContinueStatement()
415
+ : label_(NULL) {
416
+ }
417
+
418
+ void SetLabel(Identifier<Factory>* label) {
419
+ label_ = label;
420
+ }
421
+
422
+ void SetTarget(IterationStatement<Factory>* target) {
423
+ target_ = target;
424
+ }
425
+ inline Identifier<Factory>* label() const { return label_; }
426
+ inline IterationStatement<Factory>* target() const { return target_; }
427
+ ACCEPT_VISITOR
428
+ DECLARE_NODE_TYPE(ContinueStatement)
429
+ private:
430
+ Identifier<Factory>* label_;
431
+ IterationStatement<Factory>* target_;
432
+ };
433
+
434
+ template<typename Factory>
435
+ class BreakStatement : public Statement<Factory> {
436
+ public:
437
+ BreakStatement()
438
+ : label_(NULL),
439
+ target_(NULL) {
440
+ }
441
+
442
+ void SetLabel(Identifier<Factory>* label) {
443
+ label_ = label;
444
+ }
445
+
446
+ void SetTarget(BreakableStatement<Factory>* target) {
447
+ target_ = target;
448
+ }
449
+ inline Identifier<Factory>* label() const { return label_; }
450
+ inline BreakableStatement<Factory>* target() const { return target_; }
451
+ ACCEPT_VISITOR
452
+ DECLARE_NODE_TYPE(BreakStatement)
453
+ private:
454
+ Identifier<Factory>* label_;
455
+ BreakableStatement<Factory>* target_;
456
+ };
457
+
458
+ template<typename Factory>
459
+ class ReturnStatement : public Statement<Factory> {
460
+ public:
461
+ explicit ReturnStatement(Expression<Factory>* expr)
462
+ : expr_(expr) {
463
+ }
464
+ inline Expression<Factory>* expr() const { return expr_; }
465
+ ACCEPT_VISITOR
466
+ DECLARE_NODE_TYPE(ReturnStatement)
467
+ private:
468
+ Expression<Factory>* expr_;
469
+ };
470
+
471
+ template<typename Factory>
472
+ class WithStatement : public Statement<Factory> {
473
+ public:
474
+ WithStatement(Expression<Factory>* context,
475
+ Statement<Factory>* body)
476
+ : context_(context),
477
+ body_(body) {
478
+ }
479
+ inline Expression<Factory>* context() const { return context_; }
480
+ inline Statement<Factory>* body() const { return body_; }
481
+ ACCEPT_VISITOR
482
+ DECLARE_NODE_TYPE(WithStatement)
483
+ private:
484
+ Expression<Factory>* context_;
485
+ Statement<Factory>* body_;
486
+ };
487
+
488
+ template<typename Factory>
489
+ class LabelledStatement : public Statement<Factory> {
490
+ public:
491
+ LabelledStatement(Expression<Factory>* expr, Statement<Factory>* body)
492
+ : body_(body) {
493
+ label_ = expr->AsLiteral()->AsIdentifier();
494
+ }
495
+ inline Identifier<Factory>* label() const { return label_; }
496
+ inline Statement<Factory>* body() const { return body_; }
497
+ ACCEPT_VISITOR
498
+ DECLARE_NODE_TYPE(LabelledStatement)
499
+ private:
500
+ Identifier<Factory>* label_;
501
+ Statement<Factory>* body_;
502
+ };
503
+
504
+ template<typename Factory>
505
+ class CaseClause : public AstNode<Factory> {
506
+ public:
507
+ typedef typename SpaceVector<Factory, Statement<Factory>*>::type Statements;
508
+ explicit CaseClause(Factory* factory)
509
+ : expr_(NULL),
510
+ body_(typename Statements::allocator_type(factory)),
511
+ default_(false) {
512
+ }
513
+
514
+ void SetExpression(Expression<Factory>* expr) {
515
+ expr_ = expr;
516
+ }
517
+
518
+ void SetDefault() {
519
+ default_ = true;
520
+ }
521
+
522
+ void AddStatement(Statement<Factory>* stmt) {
523
+ body_.push_back(stmt);
524
+ }
525
+ inline bool IsDefault() const {
526
+ return default_;
527
+ }
528
+ inline Expression<Factory>* expr() const {
529
+ return expr_;
530
+ }
531
+ inline const Statements& body() const {
532
+ return body_;
533
+ }
534
+ ACCEPT_VISITOR
535
+ private:
536
+ Expression<Factory>* expr_;
537
+ Statements body_;
538
+ bool default_;
539
+ };
540
+
541
+ template<typename Factory>
542
+ class SwitchStatement : public AnonymousBreakableStatement<Factory> {
543
+ public:
544
+ typedef typename SpaceVector<Factory, CaseClause<Factory>*>::type CaseClauses;
545
+ SwitchStatement(Expression<Factory>* expr,
546
+ Factory* factory)
547
+ : expr_(expr),
548
+ clauses_(typename CaseClauses::allocator_type(factory)) {
549
+ }
550
+
551
+ void AddCaseClause(CaseClause<Factory>* clause) {
552
+ clauses_.push_back(clause);
553
+ }
554
+ inline Expression<Factory>* expr() const { return expr_; }
555
+ inline const CaseClauses& clauses() const { return clauses_; }
556
+ ACCEPT_VISITOR
557
+ DECLARE_NODE_TYPE(SwitchStatement)
558
+ private:
559
+ Expression<Factory>* expr_;
560
+ CaseClauses clauses_;
561
+ };
562
+
563
+ template<typename Factory>
564
+ class ThrowStatement : public Statement<Factory> {
565
+ public:
566
+ explicit ThrowStatement(Expression<Factory>* expr)
567
+ : expr_(expr) {
568
+ }
569
+ inline Expression<Factory>* expr() const { return expr_; }
570
+ ACCEPT_VISITOR
571
+ DECLARE_NODE_TYPE(ThrowStatement)
572
+ private:
573
+ Expression<Factory>* expr_;
574
+ };
575
+
576
+ template<typename Factory>
577
+ class TryStatement : public Statement<Factory> {
578
+ public:
579
+ explicit TryStatement(Block<Factory>* block)
580
+ : body_(block),
581
+ catch_name_(NULL),
582
+ catch_block_(NULL),
583
+ finally_block_(NULL) {
584
+ }
585
+
586
+ void SetCatch(Identifier<Factory>* name, Block<Factory>* block) {
587
+ catch_name_ = name;
588
+ catch_block_ = block;
589
+ }
590
+
591
+ void SetFinally(Block<Factory>* block) {
592
+ finally_block_ = block;
593
+ }
594
+ inline Block<Factory>* body() const { return body_; }
595
+ inline Identifier<Factory>* catch_name() const { return catch_name_; }
596
+ inline Block<Factory>* catch_block() const { return catch_block_; }
597
+ inline Block<Factory>* finally_block() const { return finally_block_; }
598
+ ACCEPT_VISITOR
599
+ DECLARE_NODE_TYPE(TryStatement)
600
+ private:
601
+ Block<Factory>* body_;
602
+ Identifier<Factory>* catch_name_;
603
+ Block<Factory>* catch_block_;
604
+ Block<Factory>* finally_block_;
605
+ };
606
+
607
+ template<typename Factory>
608
+ class DebuggerStatement : public Statement<Factory> {
609
+ ACCEPT_VISITOR
610
+ DECLARE_NODE_TYPE(DebuggerStatement)
611
+ };
612
+
613
+ template<typename Factory>
614
+ class ExpressionStatement : public Statement<Factory> {
615
+ public:
616
+ explicit ExpressionStatement(Expression<Factory>* expr) : expr_(expr) { }
617
+ inline Expression<Factory>* expr() const { return expr_; }
618
+ ACCEPT_VISITOR
619
+ DECLARE_NODE_TYPE(ExpressionStatement)
620
+ private:
621
+ Expression<Factory>* expr_;
622
+ };
623
+
624
+ template<typename Factory>
625
+ class Assignment : public Expression<Factory> {
626
+ public:
627
+ Assignment(Token::Type op,
628
+ Expression<Factory>* left, Expression<Factory>* right)
629
+ : op_(op),
630
+ left_(left),
631
+ right_(right) {
632
+ }
633
+ inline Token::Type op() const { return op_; }
634
+ inline Expression<Factory>* left() const { return left_; }
635
+ inline Expression<Factory>* right() const { return right_; }
636
+ ACCEPT_VISITOR
637
+ DECLARE_NODE_TYPE(Assignment)
638
+ private:
639
+ Token::Type op_;
640
+ Expression<Factory>* left_;
641
+ Expression<Factory>* right_;
642
+ };
643
+
644
+ template<typename Factory>
645
+ class BinaryOperation : public Expression<Factory> {
646
+ public:
647
+ BinaryOperation(Token::Type op,
648
+ Expression<Factory>* left, Expression<Factory>* right)
649
+ : op_(op),
650
+ left_(left),
651
+ right_(right) {
652
+ }
653
+ inline Token::Type op() const { return op_; }
654
+ inline Expression<Factory>* left() const { return left_; }
655
+ inline Expression<Factory>* right() const { return right_; }
656
+ ACCEPT_VISITOR
657
+ DECLARE_NODE_TYPE(BinaryOperation)
658
+ private:
659
+ Token::Type op_;
660
+ Expression<Factory>* left_;
661
+ Expression<Factory>* right_;
662
+ };
663
+
664
+ template<typename Factory>
665
+ class ConditionalExpression : public Expression<Factory> {
666
+ public:
667
+ ConditionalExpression(Expression<Factory>* cond,
668
+ Expression<Factory>* left,
669
+ Expression<Factory>* right)
670
+ : cond_(cond), left_(left), right_(right) {
671
+ }
672
+ inline Expression<Factory>* cond() const { return cond_; }
673
+ inline Expression<Factory>* left() const { return left_; }
674
+ inline Expression<Factory>* right() const { return right_; }
675
+ ACCEPT_VISITOR
676
+ DECLARE_NODE_TYPE(ConditionalExpression)
677
+ private:
678
+ Expression<Factory>* cond_;
679
+ Expression<Factory>* left_;
680
+ Expression<Factory>* right_;
681
+ };
682
+
683
+ template<typename Factory>
684
+ class UnaryOperation : public Expression<Factory> {
685
+ public:
686
+ UnaryOperation(Token::Type op, Expression<Factory>* expr)
687
+ : op_(op),
688
+ expr_(expr) {
689
+ }
690
+ inline Token::Type op() const { return op_; }
691
+ inline Expression<Factory>* expr() const { return expr_; }
692
+ ACCEPT_VISITOR
693
+ DECLARE_NODE_TYPE(UnaryOperation)
694
+ private:
695
+ Token::Type op_;
696
+ Expression<Factory>* expr_;
697
+ };
698
+
699
+ template<typename Factory>
700
+ class PostfixExpression : public Expression<Factory> {
701
+ public:
702
+ PostfixExpression(Token::Type op, Expression<Factory>* expr)
703
+ : op_(op),
704
+ expr_(expr) {
705
+ }
706
+ inline Token::Type op() const { return op_; }
707
+ inline Expression<Factory>* expr() const { return expr_; }
708
+ ACCEPT_VISITOR
709
+ DECLARE_NODE_TYPE(PostfixExpression)
710
+ private:
711
+ Token::Type op_;
712
+ Expression<Factory>* expr_;
713
+ };
714
+
715
+ template<typename Factory>
716
+ class StringLiteral : public Literal<Factory> {
717
+ public:
718
+ typedef typename SpaceUString<Factory>::type value_type;
719
+ StringLiteral(const std::vector<uc16>& buffer,
720
+ Factory* factory)
721
+ : value_(buffer.data(),
722
+ buffer.size(),
723
+ typename value_type::allocator_type(factory)) {
724
+ }
725
+
726
+ inline const value_type& value() const {
727
+ return value_;
728
+ }
729
+ ACCEPT_VISITOR
730
+ DECLARE_NODE_TYPE(StringLiteral)
731
+ private:
732
+ const value_type value_;
733
+ };
734
+
735
+ template<typename Factory>
736
+ class Directivable : public StringLiteral<Factory> {
737
+ public:
738
+ explicit Directivable(const std::vector<uc16>& buffer,
739
+ Factory* factory)
740
+ : StringLiteral<Factory>(buffer, factory) { }
741
+ DECLARE_NODE_TYPE(Directivable)
742
+ };
743
+
744
+ template<typename Factory>
745
+ class NumberLiteral : public Literal<Factory> {
746
+ public:
747
+ explicit NumberLiteral(const double & val)
748
+ : value_(val) {
749
+ }
750
+ inline double value() const { return value_; }
751
+ ACCEPT_VISITOR
752
+ DECLARE_NODE_TYPE(NumberLiteral)
753
+ private:
754
+ double value_;
755
+ };
756
+
757
+ template<typename Factory>
758
+ class Identifier : public Literal<Factory> {
759
+ public:
760
+ typedef typename SpaceUString<Factory>::type value_type;
761
+ Identifier(const UStringPiece& buffer, Factory* factory)
762
+ : value_(buffer.data(),
763
+ buffer.size(),
764
+ typename value_type::allocator_type(factory)) {
765
+ }
766
+ Identifier(const std::vector<uc16>& buffer, Factory* factory)
767
+ : value_(buffer.data(),
768
+ buffer.size(),
769
+ typename value_type::allocator_type(factory)) {
770
+ }
771
+ Identifier(const std::vector<char>& buffer, Factory* factory)
772
+ : value_(buffer.begin(),
773
+ buffer.end(),
774
+ typename value_type::allocator_type(factory)) {
775
+ }
776
+ inline const value_type& value() const {
777
+ return value_;
778
+ }
779
+ inline bool IsValidLeftHandSide() const { return true; }
780
+ ACCEPT_VISITOR
781
+ DECLARE_NODE_TYPE(Identifier)
782
+ protected:
783
+ value_type value_;
784
+ };
785
+
786
+ template<typename Factory>
787
+ class IdentifierKey {
788
+ public:
789
+ typedef Identifier<Factory> value_type;
790
+ typedef IdentifierKey<Factory> this_type;
791
+ IdentifierKey(value_type* ident) // NOLINT
792
+ : ident_(ident) { }
793
+ IdentifierKey(const this_type& rhs) // NOLINT
794
+ : ident_(rhs.ident_) { }
795
+ inline const typename value_type::value_type& value() const {
796
+ return ident_->value();
797
+ }
798
+ inline bool operator==(const this_type& rhs) const {
799
+ if (ident_ == rhs.ident_) {
800
+ return true;
801
+ } else {
802
+ return value() == rhs.value();
803
+ }
804
+ }
805
+ inline bool operator>(const this_type& rhs) const {
806
+ return value() > rhs.value();
807
+ }
808
+ inline bool operator<(const this_type& rhs) const {
809
+ return value() < rhs.value();
810
+ }
811
+ inline bool operator>=(const this_type& rhs) const {
812
+ return value() >= rhs.value();
813
+ }
814
+ inline bool operator<=(const this_type& rhs) const {
815
+ return value() <= rhs.value();
816
+ }
817
+ private:
818
+ value_type* ident_;
819
+ };
820
+
821
+ template<typename Factory>
822
+ class ThisLiteral : public Literal<Factory> {
823
+ public:
824
+ ACCEPT_VISITOR
825
+ DECLARE_NODE_TYPE(ThisLiteral)
826
+ };
827
+
828
+ template<typename Factory>
829
+ class NullLiteral : public Literal<Factory> {
830
+ public:
831
+ ACCEPT_VISITOR
832
+ DECLARE_NODE_TYPE(NullLiteral)
833
+ };
834
+
835
+ template<typename Factory>
836
+ class TrueLiteral : public Literal<Factory> {
837
+ public:
838
+ ACCEPT_VISITOR
839
+ DECLARE_NODE_TYPE(TrueLiteral)
840
+ };
841
+
842
+ template<typename Factory>
843
+ class FalseLiteral : public Literal<Factory> {
844
+ public:
845
+ ACCEPT_VISITOR
846
+ DECLARE_NODE_TYPE(FalseLiteral)
847
+ };
848
+
849
+ template<typename Factory>
850
+ class Undefined : public Literal<Factory> {
851
+ public:
852
+ ACCEPT_VISITOR
853
+ DECLARE_NODE_TYPE(Undefined)
854
+ };
855
+
856
+ template<typename Factory>
857
+ class RegExpLiteral : public Literal<Factory> {
858
+ public:
859
+ typedef typename SpaceUString<Factory>::type value_type;
860
+
861
+ RegExpLiteral(const std::vector<uc16>& buffer,
862
+ const std::vector<uc16>& flags,
863
+ Factory* factory)
864
+ : value_(buffer.data(),
865
+ buffer.size(), typename value_type::allocator_type(factory)),
866
+ flags_(flags.data(),
867
+ flags.size(), typename value_type::allocator_type(factory)) {
868
+ }
869
+ inline const value_type& value() const { return value_; }
870
+ inline const value_type& flags() const { return flags_; }
871
+ ACCEPT_VISITOR
872
+ DECLARE_NODE_TYPE(RegExpLiteral)
873
+ protected:
874
+ value_type value_;
875
+ value_type flags_;
876
+ };
877
+
878
+ template<typename Factory>
879
+ class ArrayLiteral : public Literal<Factory> {
880
+ public:
881
+ typedef typename SpaceVector<Factory, Expression<Factory>*>::type Expressions;
882
+
883
+ explicit ArrayLiteral(Factory* factory)
884
+ : items_(typename Expressions::allocator_type(factory)) {
885
+ }
886
+ inline void AddItem(Expression<Factory>* expr) {
887
+ items_.push_back(expr);
888
+ }
889
+ inline const Expressions& items() const {
890
+ return items_;
891
+ }
892
+ ACCEPT_VISITOR
893
+ DECLARE_NODE_TYPE(ArrayLiteral)
894
+ private:
895
+ Expressions items_;
896
+ };
897
+
898
+ template<typename Factory>
899
+ class ObjectLiteral : public Literal<Factory> {
900
+ public:
901
+ enum PropertyDescriptorType {
902
+ DATA = 1,
903
+ GET = 2,
904
+ SET = 4
905
+ };
906
+ typedef std::tr1::tuple<PropertyDescriptorType,
907
+ Identifier<Factory>*,
908
+ Expression<Factory>*> Property;
909
+ typedef typename SpaceVector<Factory, Property>::type Properties;
910
+ explicit ObjectLiteral(Factory* factory)
911
+ : properties_(typename Properties::allocator_type(factory)) {
912
+ }
913
+
914
+ inline void AddDataProperty(Identifier<Factory>* key,
915
+ Expression<Factory>* val) {
916
+ AddPropertyDescriptor(DATA, key, val);
917
+ }
918
+ inline void AddAccessor(PropertyDescriptorType type,
919
+ Identifier<Factory>* key,
920
+ Expression<Factory>* val) {
921
+ AddPropertyDescriptor(type, key, val);
922
+ }
923
+ inline const Properties& properties() const {
924
+ return properties_;
925
+ }
926
+ ACCEPT_VISITOR
927
+ DECLARE_NODE_TYPE(ObjectLiteral)
928
+ private:
929
+ inline void AddPropertyDescriptor(PropertyDescriptorType type,
930
+ Identifier<Factory>* key,
931
+ Expression<Factory>* val) {
932
+ properties_.push_back(std::tr1::make_tuple(type, key, val));
933
+ }
934
+ Properties properties_;
935
+ };
936
+
937
+ template<typename Factory>
938
+ class FunctionLiteral : public Literal<Factory> {
939
+ public:
940
+ typedef typename SpaceVector<Factory, Statement<Factory>*>::type Statements;
941
+ typedef typename SpaceVector<Factory, Identifier<Factory>*>::type Identifiers;
942
+ enum DeclType {
943
+ DECLARATION,
944
+ STATEMENT,
945
+ EXPRESSION,
946
+ GLOBAL
947
+ };
948
+ enum ArgType {
949
+ GENERAL,
950
+ SETTER,
951
+ GETTER
952
+ };
953
+ inline void SetName(Identifier<Factory>* name) { name_ = name; }
954
+ inline Identifier<Factory>* name() const {
955
+ return name_;
956
+ }
957
+ inline DeclType type() const { return type_; }
958
+ inline const Identifiers& params() const {
959
+ return params_;
960
+ }
961
+ inline const Statements& body() const {
962
+ return body_;
963
+ }
964
+ inline Scope<Factory>* scope() {
965
+ return &scope_;
966
+ }
967
+ inline const Scope<Factory>& scope() const {
968
+ return scope_;
969
+ }
970
+ inline void set_start_position(std::size_t start) {
971
+ start_position_ = start;
972
+ }
973
+ inline void set_end_position(std::size_t end) {
974
+ end_position_ = end;
975
+ }
976
+ inline std::size_t start_position() const {
977
+ return start_position_;
978
+ }
979
+ inline std::size_t end_position() const {
980
+ return end_position_;
981
+ }
982
+ inline bool strict() const {
983
+ return strict_;
984
+ }
985
+ inline void set_strict(bool strict) {
986
+ strict_ = strict;
987
+ }
988
+ inline void SubStringSource(BasicSource* src) {
989
+ source_ = src->SubString(start_position_,
990
+ end_position_ - start_position_ + 1);
991
+ }
992
+ inline UStringPiece GetSource() const {
993
+ return source_;
994
+ }
995
+ FunctionLiteral(DeclType type, Factory* factory)
996
+ : name_(NULL),
997
+ type_(type),
998
+ params_(typename Identifiers::allocator_type(factory)),
999
+ body_(typename Statements::allocator_type(factory)),
1000
+ scope_(factory),
1001
+ strict_(false) {
1002
+ }
1003
+
1004
+ void AddParameter(Identifier<Factory>* param) {
1005
+ params_.push_back(param);
1006
+ }
1007
+
1008
+ void AddStatement(Statement<Factory>* stmt) {
1009
+ body_.push_back(stmt);
1010
+ }
1011
+ ACCEPT_VISITOR
1012
+ DECLARE_NODE_TYPE(FunctionLiteral)
1013
+ private:
1014
+ Identifier<Factory>* name_;
1015
+ DeclType type_;
1016
+ Identifiers params_;
1017
+ Statements body_;
1018
+ Scope<Factory> scope_;
1019
+ bool strict_;
1020
+ std::size_t start_position_;
1021
+ std::size_t end_position_;
1022
+ UStringPiece source_;
1023
+ };
1024
+
1025
+ template<typename Factory>
1026
+ class PropertyAccess : public Expression<Factory> {
1027
+ public:
1028
+ inline bool IsValidLeftHandSide() const { return true; }
1029
+ inline Expression<Factory>* target() const { return target_; }
1030
+ DECLARE_NODE_TYPE(PropertyAccess)
1031
+ protected:
1032
+ explicit PropertyAccess(Expression<Factory>* obj)
1033
+ : target_(obj) {
1034
+ }
1035
+ Expression<Factory>* target_;
1036
+ };
1037
+
1038
+ template<typename Factory>
1039
+ class IdentifierAccess : public PropertyAccess<Factory> {
1040
+ public:
1041
+ IdentifierAccess(Expression<Factory>* obj,
1042
+ Identifier<Factory>* key)
1043
+ : PropertyAccess<Factory>(obj),
1044
+ key_(key) {
1045
+ }
1046
+ inline Identifier<Factory>* key() const { return key_; }
1047
+ ACCEPT_VISITOR
1048
+ DECLARE_NODE_TYPE(IdentifierAccess)
1049
+ private:
1050
+ Identifier<Factory>* key_;
1051
+ };
1052
+
1053
+ template<typename Factory>
1054
+ class IndexAccess : public PropertyAccess<Factory> {
1055
+ public:
1056
+ IndexAccess(Expression<Factory>* obj,
1057
+ Expression<Factory>* key)
1058
+ : PropertyAccess<Factory>(obj),
1059
+ key_(key) {
1060
+ }
1061
+ inline Expression<Factory>* key() const { return key_; }
1062
+ DECLARE_NODE_TYPE(IndexAccess)
1063
+ ACCEPT_VISITOR
1064
+ private:
1065
+ Expression<Factory>* key_;
1066
+ };
1067
+
1068
+ template<typename Factory>
1069
+ class Call : public Expression<Factory> {
1070
+ public:
1071
+ typedef typename SpaceVector<Factory, Expression<Factory>*>::type Expressions;
1072
+ inline bool IsValidLeftHandSide() const { return true; }
1073
+ Call(Expression<Factory>* target,
1074
+ Factory* factory)
1075
+ : target_(target),
1076
+ args_(typename Expressions::allocator_type(factory)) {
1077
+ }
1078
+ void AddArgument(Expression<Factory>* expr) { args_.push_back(expr); }
1079
+ inline Expression<Factory>* target() const { return target_; }
1080
+ inline const Expressions& args() const { return args_; }
1081
+ DECLARE_NODE_TYPE(Call)
1082
+ protected:
1083
+ Expression<Factory>* target_;
1084
+ Expressions args_;
1085
+ };
1086
+
1087
+ template<typename Factory>
1088
+ class FunctionCall : public Call<Factory> {
1089
+ public:
1090
+ FunctionCall(Expression<Factory>* target,
1091
+ Factory* factory)
1092
+ : Call<Factory>(target, factory) {
1093
+ }
1094
+ ACCEPT_VISITOR
1095
+ DECLARE_NODE_TYPE(FunctionCall)
1096
+ };
1097
+
1098
+ template<typename Factory>
1099
+ class ConstructorCall : public Call<Factory> {
1100
+ public:
1101
+ ConstructorCall(Expression<Factory>* target,
1102
+ Factory* factory)
1103
+ : Call<Factory>(target, factory) {
1104
+ }
1105
+ ACCEPT_VISITOR
1106
+ DECLARE_NODE_TYPE(ConstructorCall)
1107
+ };
1108
+
1109
+ } } } // namespace iv::core::ast
1110
+ namespace std {
1111
+ namespace tr1 {
1112
+ // template specialization
1113
+ // for iv::core::Parser::IdentifierWrapper in std::tr1::unordered_map
1114
+ // allowed in section 17.4.3.1
1115
+ template<class Factory>
1116
+ struct hash<iv::core::ast::IdentifierKey<Factory> >
1117
+ : public unary_function<iv::core::ast::IdentifierKey<Factory>, std::size_t> {
1118
+ std::size_t operator()(const iv::core::ast::IdentifierKey<Factory>& x) const {
1119
+ return hash<
1120
+ typename iv::core::ast::Identifier<Factory>::value_type>()(x.value());
1121
+ }
1122
+ };
1123
+ } } // namespace std::tr1
1124
+ #undef ACCEPT_VISITOR
1125
+ #undef DECLARE_NODE_TYPE
1126
+ #undef DECLARE_NODE_TYPE_BASE
1127
+ #endif // _IV_AST_H_