iv-phonic 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,12 @@ class AstNodeBase<iv::phonic::AstFactory>
20
20
  begin_ = begin;
21
21
  end_ = end;
22
22
  }
23
+ std::size_t begin_position() const {
24
+ return begin_;
25
+ }
26
+ std::size_t end_position() const {
27
+ return end_;
28
+ }
23
29
  private:
24
30
  std::size_t begin_;
25
31
  std::size_t end_;
@@ -36,6 +36,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
36
36
  rb_ary_push(array, ret_);
37
37
  }
38
38
  rb_hash_aset(hash, SYM("body"), array);
39
+ SetLocation(hash, stmt);
39
40
  ret_ = hash;
40
41
  }
41
42
 
@@ -44,6 +45,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
44
45
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FunctionStatement"));
45
46
  Visit(stmt->function());
46
47
  rb_hash_aset(hash, SYM("body"), ret_);
48
+ SetLocation(hash, stmt);
47
49
  ret_ = hash;
48
50
  }
49
51
 
@@ -52,6 +54,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
52
54
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FunctionDeclaration"));
53
55
  Visit(decl->function());
54
56
  rb_hash_aset(hash, SYM("body"), ret_);
57
+ SetLocation(hash, decl);
55
58
  ret_ = hash;
56
59
  }
57
60
 
@@ -62,23 +65,31 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
62
65
  VALUE array = rb_ary_new();
63
66
  for (Declarations::const_iterator it = stmt->decls().begin(),
64
67
  last = stmt->decls().end(); it != last; ++it) {
65
- VALUE decl = rb_hash_new();
66
- rb_hash_aset(decl, SYM("type"), rb_str_new_cstr("Declaration"));
67
- Visit((*it)->name());
68
- rb_hash_aset(decl, SYM("name"), ret_);
69
- if ((*it)->expr()) {
70
- (*it)->expr()->Accept(this);
71
- rb_hash_aset(decl, SYM("expr"), ret_);
72
- }
73
- rb_ary_push(array, decl);
68
+ Visit(*it);
69
+ rb_ary_push(array, ret_);
74
70
  }
75
71
  rb_hash_aset(hash, SYM("body"), array);
72
+ SetLocation(hash, stmt);
73
+ ret_ = hash;
74
+ }
75
+
76
+ void Visit(const Declaration* decl) {
77
+ VALUE hash = rb_hash_new();
78
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Declaration"));
79
+ Visit(decl->name());
80
+ rb_hash_aset(hash, SYM("name"), ret_);
81
+ if (decl->expr()) {
82
+ decl->expr()->Accept(this);
83
+ rb_hash_aset(hash, SYM("expr"), ret_);
84
+ }
85
+ SetLocation(hash, decl);
76
86
  ret_ = hash;
77
87
  }
78
88
 
79
89
  void Visit(const EmptyStatement* stmt) {
80
90
  VALUE hash = rb_hash_new();
81
91
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("EmptyStatement"));
92
+ SetLocation(hash, stmt);
82
93
  ret_ = hash;
83
94
  }
84
95
 
@@ -93,6 +104,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
93
104
  stmt->else_statement()->Accept(this);
94
105
  rb_hash_aset(hash, SYM("else"), ret_);
95
106
  }
107
+ SetLocation(hash, stmt);
96
108
  ret_ = hash;
97
109
  }
98
110
 
@@ -103,6 +115,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
103
115
  rb_hash_aset(hash, SYM("cond"), ret_);
104
116
  stmt->body()->Accept(this);
105
117
  rb_hash_aset(hash, SYM("body"), ret_);
118
+ SetLocation(hash, stmt);
106
119
  ret_ = hash;
107
120
  }
108
121
 
@@ -113,6 +126,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
113
126
  rb_hash_aset(hash, SYM("cond"), ret_);
114
127
  stmt->body()->Accept(this);
115
128
  rb_hash_aset(hash, SYM("body"), ret_);
129
+ SetLocation(hash, stmt);
116
130
  ret_ = hash;
117
131
  }
118
132
 
@@ -133,6 +147,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
133
147
  }
134
148
  stmt->body()->Accept(this);
135
149
  rb_hash_aset(hash, SYM("body"), ret_);
150
+ SetLocation(hash, stmt);
136
151
  ret_ = hash;
137
152
  }
138
153
 
@@ -145,6 +160,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
145
160
  rb_hash_aset(hash, SYM("enum"), ret_);
146
161
  stmt->body()->Accept(this);
147
162
  rb_hash_aset(hash, SYM("body"), ret_);
163
+ SetLocation(hash, stmt);
148
164
  ret_ = hash;
149
165
  }
150
166
 
@@ -155,6 +171,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
155
171
  stmt->label()->Accept(this);
156
172
  rb_hash_aset(hash, SYM("label"), ret_);
157
173
  }
174
+ SetLocation(hash, stmt);
158
175
  ret_ = hash;
159
176
  }
160
177
 
@@ -165,6 +182,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
165
182
  stmt->label()->Accept(this);
166
183
  rb_hash_aset(hash, SYM("label"), ret_);
167
184
  }
185
+ SetLocation(hash, stmt);
168
186
  ret_ = hash;
169
187
  }
170
188
 
@@ -173,6 +191,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
173
191
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ReturnStatement"));
174
192
  stmt->expr()->Accept(this);
175
193
  rb_hash_aset(hash, SYM("expr"), ret_);
194
+ SetLocation(hash, stmt);
176
195
  ret_ = hash;
177
196
  }
178
197
 
@@ -183,6 +202,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
183
202
  rb_hash_aset(hash, SYM("context"), ret_);
184
203
  stmt->body()->Accept(this);
185
204
  rb_hash_aset(hash, SYM("body"), ret_);
205
+ SetLocation(hash, stmt);
186
206
  ret_ = hash;
187
207
  }
188
208
 
@@ -193,6 +213,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
193
213
  rb_hash_aset(hash, SYM("label"), ret_);
194
214
  stmt->body()->Accept(this);
195
215
  rb_hash_aset(hash, SYM("body"), ret_);
216
+ SetLocation(hash, stmt);
196
217
  ret_ = hash;
197
218
  }
198
219
 
@@ -204,42 +225,52 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
204
225
  VALUE array = rb_ary_new();
205
226
  for (CaseClauses::const_iterator it = stmt->clauses().begin(),
206
227
  last = stmt->clauses().end(); it != last; ++it) {
207
- VALUE clause = rb_hash_new();
208
- if ((*it)->IsDefault()) {
209
- rb_hash_aset(clause, SYM("type"), rb_str_new_cstr("Default"));
210
- } else {
211
- rb_hash_aset(clause, SYM("type"), rb_str_new_cstr("Case"));
212
- (*it)->expr()->Accept(this);
213
- rb_hash_aset(clause, SYM("expr"), ret_);
214
- }
215
- VALUE stmts = rb_ary_new();
216
- for (Statements::const_iterator st = (*it)->body().begin(),
217
- stlast = (*it)->body().end(); st != stlast; ++st) {
218
- (*st)->Accept(this);
219
- rb_ary_push(stmts, ret_);
220
- }
221
- rb_hash_aset(clause, SYM("body"), stmts);
228
+ Visit(*it);
222
229
  rb_ary_push(array, ret_);
223
230
  }
224
231
  rb_hash_aset(hash, SYM("clauses"), array);
232
+ SetLocation(hash, stmt);
233
+ ret_ = hash;
234
+ }
235
+
236
+ void Visit(const CaseClause* cl) {
237
+ VALUE hash = rb_hash_new();
238
+ if (cl->IsDefault()) {
239
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Default"));
240
+ } else {
241
+ rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Case"));
242
+ cl->expr()->Accept(this);
243
+ rb_hash_aset(hash, SYM("expr"), ret_);
244
+ }
245
+ VALUE stmts = rb_ary_new();
246
+ for (Statements::const_iterator st = cl->body().begin(),
247
+ stlast = cl->body().end(); st != stlast; ++st) {
248
+ (*st)->Accept(this);
249
+ rb_ary_push(stmts, ret_);
250
+ }
251
+ rb_hash_aset(hash, SYM("body"), stmts);
252
+ SetLocation(hash, cl);
225
253
  ret_ = hash;
226
254
  }
227
255
 
228
256
  void Visit(const ThrowStatement* stmt) {
229
257
  VALUE hash = rb_hash_new();
230
258
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ThrowStatement"));
259
+ SetLocation(hash, stmt);
231
260
  ret_ = hash;
232
261
  }
233
262
 
234
263
  void Visit(const TryStatement* stmt) {
235
264
  VALUE hash = rb_hash_new();
236
265
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("TryStatement"));
266
+ SetLocation(hash, stmt);
237
267
  ret_ = hash;
238
268
  }
239
269
 
240
270
  void Visit(const DebuggerStatement* stmt) {
241
271
  VALUE hash = rb_hash_new();
242
272
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("DebuggerStatement"));
273
+ SetLocation(hash, stmt);
243
274
  ret_ = hash;
244
275
  }
245
276
 
@@ -248,6 +279,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
248
279
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ExpressionStatement"));
249
280
  stmt->expr()->Accept(this);
250
281
  rb_hash_aset(hash, SYM("body"), ret_);
282
+ SetLocation(hash, stmt);
251
283
  ret_ = hash;
252
284
  }
253
285
 
@@ -260,6 +292,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
260
292
  rb_hash_aset(hash, SYM("left"), ret_);
261
293
  expr->right()->Accept(this);
262
294
  rb_hash_aset(hash, SYM("right"), ret_);
295
+ SetLocation(hash, expr);
263
296
  ret_ = hash;
264
297
  }
265
298
 
@@ -272,6 +305,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
272
305
  rb_hash_aset(hash, SYM("left"), ret_);
273
306
  expr->right()->Accept(this);
274
307
  rb_hash_aset(hash, SYM("right"), ret_);
308
+ SetLocation(hash, expr);
275
309
  ret_ = hash;
276
310
  }
277
311
 
@@ -284,6 +318,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
284
318
  rb_hash_aset(hash, SYM("left"), ret_);
285
319
  expr->right()->Accept(this);
286
320
  rb_hash_aset(hash, SYM("right"), ret_);
321
+ SetLocation(hash, expr);
287
322
  ret_ = hash;
288
323
  }
289
324
 
@@ -294,6 +329,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
294
329
  rb_str_new_cstr(core::Token::ToString(expr->op())));
295
330
  expr->expr()->Accept(this);
296
331
  rb_hash_aset(hash, SYM("expr"), ret_);
332
+ SetLocation(hash, expr);
297
333
  ret_ = hash;
298
334
  }
299
335
 
@@ -304,6 +340,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
304
340
  rb_str_new_cstr(core::Token::ToString(expr->op())));
305
341
  expr->expr()->Accept(this);
306
342
  rb_hash_aset(hash, SYM("expr"), ret_);
343
+ SetLocation(hash, expr);
307
344
  ret_ = hash;
308
345
  }
309
346
 
@@ -317,6 +354,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
317
354
  reinterpret_cast<const char*>(str.data()),
318
355
  str.size()*2,
319
356
  rb_to_encoding(Encoding::UTF16Encoding()))));
357
+ SetLocation(hash, literal);
320
358
  ret_ = hash;
321
359
  }
322
360
 
@@ -324,6 +362,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
324
362
  VALUE hash = rb_hash_new();
325
363
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("NumberLiteral"));
326
364
  rb_hash_aset(hash, SYM("value"), rb_float_new(literal->value()));
365
+ SetLocation(hash, literal);
327
366
  ret_ = hash;
328
367
  }
329
368
 
@@ -337,34 +376,40 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
337
376
  reinterpret_cast<const char*>(str.data()),
338
377
  str.size()*2,
339
378
  rb_to_encoding(Encoding::UTF16Encoding()))));
379
+ SetLocation(hash, literal);
340
380
  ret_ = hash;
341
381
  }
342
382
 
343
383
  void Visit(const ThisLiteral* literal) {
344
384
  VALUE hash = rb_hash_new();
345
385
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("ThisLiteral"));
386
+ SetLocation(hash, literal);
346
387
  ret_ = hash;
347
388
  }
348
389
 
349
390
  void Visit(const NullLiteral* literal) {
350
391
  VALUE hash = rb_hash_new();
351
392
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("NullLiteral"));
393
+ SetLocation(hash, literal);
352
394
  ret_ = hash;
353
395
  }
354
396
 
355
397
  void Visit(const TrueLiteral* literal) {
356
398
  VALUE hash = rb_hash_new();
357
399
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("TrueLiteral"));
400
+ SetLocation(hash, literal);
358
401
  ret_ = hash;
359
402
  }
360
403
 
361
404
  void Visit(const FalseLiteral* literal) {
362
405
  VALUE hash = rb_hash_new();
363
406
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("FalseLiteral"));
407
+ SetLocation(hash, literal);
364
408
  ret_ = hash;
365
409
  }
366
410
 
367
411
  void Visit(const Undefined* literal) {
412
+ // Undefined has no location
368
413
  VALUE hash = rb_hash_new();
369
414
  rb_hash_aset(hash, SYM("type"), rb_str_new_cstr("Undefined"));
370
415
  ret_ = hash;
@@ -387,6 +432,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
387
432
  reinterpret_cast<const char*>(flags.data()),
388
433
  flags.size()*2,
389
434
  rb_to_encoding(Encoding::UTF16Encoding()))));
435
+ SetLocation(hash, literal);
390
436
  ret_ = hash;
391
437
  }
392
438
 
@@ -400,6 +446,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
400
446
  rb_ary_push(array, ret_);
401
447
  }
402
448
  rb_hash_aset(hash, SYM("value"), array);
449
+ SetLocation(hash, literal);
403
450
  ret_ = hash;
404
451
  }
405
452
 
@@ -441,6 +488,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
441
488
  rb_ary_push(array, item);
442
489
  }
443
490
  rb_hash_aset(hash, SYM("value"), array);
491
+ SetLocation(hash, literal);
444
492
  ret_ = hash;
445
493
  }
446
494
 
@@ -469,6 +517,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
469
517
  }
470
518
  rb_hash_aset(hash, SYM("body"), array);
471
519
  }
520
+ SetLocation(hash, literal);
472
521
  ret_ = hash;
473
522
  }
474
523
 
@@ -479,6 +528,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
479
528
  rb_hash_aset(hash, SYM("target"), ret_);
480
529
  expr->key()->Accept(this);
481
530
  rb_hash_aset(hash, SYM("key"), ret_);
531
+ SetLocation(hash, expr);
482
532
  ret_ = hash;
483
533
  }
484
534
 
@@ -489,6 +539,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
489
539
  rb_hash_aset(hash, SYM("target"), ret_);
490
540
  Visit(expr->key());
491
541
  rb_hash_aset(hash, SYM("key"), ret_);
542
+ SetLocation(hash, expr);
492
543
  ret_ = hash;
493
544
  }
494
545
 
@@ -504,6 +555,7 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
504
555
  rb_ary_push(args, ret_);
505
556
  }
506
557
  rb_hash_aset(hash, SYM("args"), args);
558
+ SetLocation(hash, call);
507
559
  ret_ = hash;
508
560
  }
509
561
 
@@ -519,10 +571,17 @@ class Creator : public iv::core::ast::AstVisitor<AstFactory>::const_type {
519
571
  rb_ary_push(args, ret_);
520
572
  }
521
573
  rb_hash_aset(hash, SYM("args"), args);
574
+ SetLocation(hash, call);
522
575
  ret_ = hash;
523
576
  }
524
577
 
525
578
  private:
579
+
580
+ static void SetLocation(VALUE hash, const AstNode* node) {
581
+ rb_hash_aset(hash, SYM("begin"), INT2NUM(node->begin_position()));
582
+ rb_hash_aset(hash, SYM("end"), INT2NUM(node->end_position()));
583
+ }
584
+
526
585
  VALUE ret_;
527
586
  };
528
587