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.
@@ -17,35 +17,55 @@ class AstFactory : public core::Space<2> {
17
17
  public:
18
18
  AstFactory()
19
19
  : core::Space<2>(),
20
- undefined_instance_(new(this)Undefined()),
21
- empty_statement_instance_(new(this)EmptyStatement()),
22
- debugger_statement_instance_(new(this)DebuggerStatement()),
23
- this_instance_(new(this)ThisLiteral()),
24
- null_instance_(new(this)NullLiteral()),
25
- true_instance_(new(this)TrueLiteral()),
26
- false_instance_(new(this)FalseLiteral()) {
20
+ undefined_instance_(new(this)Undefined()) {
21
+ }
22
+
23
+ Scope* NewScope(FunctionLiteral::DeclType type) {
24
+ return new (this) Scope(this, type == FunctionLiteral::GLOBAL);
27
25
  }
28
26
 
29
27
  template<typename Range>
30
- Identifier* NewIdentifier(const Range& range) {
28
+ Identifier* NewIdentifier(const Range& range,
29
+ std::size_t begin,
30
+ std::size_t end) {
31
31
  Identifier* ident = new(this)Identifier(range, this);
32
+ ident->Location(begin, end);
32
33
  return ident;
33
34
  }
34
35
 
35
- NumberLiteral* NewNumberLiteral(const double& val) {
36
- return new (this) NumberLiteral(val);
36
+ NumberLiteral* NewNumberLiteral(const double& val,
37
+ std::size_t begin,
38
+ std::size_t end) {
39
+ NumberLiteral* num = new (this) NumberLiteral(val);
40
+ num->Location(begin, end);
41
+ return num;
37
42
  }
38
43
 
39
- StringLiteral* NewStringLiteral(const std::vector<uc16>& buffer) {
40
- return new (this) StringLiteral(buffer, this);
44
+ NumberLiteral* NewReducedNumberLiteral(const double& val) {
45
+ UNREACHABLE();
46
+ return NULL;
41
47
  }
42
48
 
43
- Directivable* NewDirectivable(const std::vector<uc16>& buffer) {
44
- return new (this) Directivable(buffer, this);
49
+ StringLiteral* NewStringLiteral(const std::vector<uc16>& buffer,
50
+ std::size_t begin,
51
+ std::size_t end) {
52
+ StringLiteral* str = new (this) StringLiteral(buffer, this);
53
+ str->Location(begin, end);
54
+ return str;
55
+ }
56
+
57
+ Directivable* NewDirectivable(const std::vector<uc16>& buffer,
58
+ std::size_t begin,
59
+ std::size_t end) {
60
+ Directivable* directive = new (this) Directivable(buffer, this);
61
+ directive->Location(begin, end);
62
+ return directive;
45
63
  }
46
64
 
47
65
  RegExpLiteral* NewRegExpLiteral(const std::vector<uc16>& content,
48
- const std::vector<uc16>& flags) {
66
+ const std::vector<uc16>& flags,
67
+ std::size_t begin,
68
+ std::size_t end) {
49
69
  // OnigErrorInfo einfo;
50
70
  // regex_t* reg;
51
71
  // // TODO(Constellation) Little Endian?
@@ -60,19 +80,49 @@ class AstFactory : public core::Space<2> {
60
80
  // return NULL;
61
81
  // }
62
82
  // onig_free(reg);
63
- return new (this) RegExpLiteral(content, flags, this);
64
- }
65
-
66
- FunctionLiteral* NewFunctionLiteral(FunctionLiteral::DeclType type) {
67
- return new (this) FunctionLiteral(type, this);
68
- }
69
-
70
- ArrayLiteral* NewArrayLiteral(Expressions* items) {
71
- return new (this) ArrayLiteral(items);
72
- }
73
-
74
- ObjectLiteral* NewObjectLiteral(ObjectLiteral::Properties* properties) {
75
- return new (this) ObjectLiteral(properties);
83
+ RegExpLiteral* reg = new (this) RegExpLiteral(content, flags, this);
84
+ reg->Location(begin, end);
85
+ return reg;
86
+ }
87
+
88
+ FunctionLiteral* NewFunctionLiteral(FunctionLiteral::DeclType type,
89
+ Identifier* name,
90
+ Identifiers* params,
91
+ Statements* body,
92
+ Scope* scope,
93
+ bool strict,
94
+ std::size_t begin_block_position,
95
+ std::size_t end_block_position,
96
+ std::size_t begin,
97
+ std::size_t end) {
98
+ FunctionLiteral* func = new (this)
99
+ FunctionLiteral(type,
100
+ name,
101
+ params,
102
+ body,
103
+ scope,
104
+ strict,
105
+ begin_block_position,
106
+ end_block_position);
107
+ func->Location(begin, end);
108
+ return func;
109
+ }
110
+
111
+ ArrayLiteral* NewArrayLiteral(Expressions* items,
112
+ std::size_t begin, std::size_t end) {
113
+ ArrayLiteral* ary = new (this) ArrayLiteral(items);
114
+ ary->Location(begin, end);
115
+ return ary;
116
+ }
117
+
118
+
119
+ ObjectLiteral*
120
+ NewObjectLiteral(ObjectLiteral::Properties* properties,
121
+ std::size_t begin,
122
+ std::size_t end) {
123
+ ObjectLiteral* obj = new (this) ObjectLiteral(properties);
124
+ obj->Location(begin, end);
125
+ return obj;
76
126
  }
77
127
 
78
128
  template<typename T>
@@ -86,189 +136,293 @@ class AstFactory : public core::Space<2> {
86
136
  return new (New(sizeof(Vector))) Vector(typename Vector::allocator_type(this));
87
137
  }
88
138
 
89
- NullLiteral* NewNullLiteral() {
90
- return null_instance_;
139
+ NullLiteral* NewNullLiteral(std::size_t begin,
140
+ std::size_t end) {
141
+ NullLiteral* null = new (this) NullLiteral();
142
+ null->Location(begin, end);
143
+ return null;
91
144
  }
92
145
 
93
- EmptyStatement* NewEmptyStatement() {
94
- return empty_statement_instance_;
146
+ ThisLiteral* NewThisLiteral(std::size_t begin, std::size_t end) {
147
+ ThisLiteral* th = new (this) ThisLiteral();
148
+ th->Location(begin, end);
149
+ return th;
95
150
  }
96
151
 
97
- DebuggerStatement* NewDebuggerStatement() {
98
- return debugger_statement_instance_;
152
+ Undefined* NewUndefined() {
153
+ return undefined_instance_;
99
154
  }
100
155
 
101
- ThisLiteral* NewThisLiteral() {
102
- return this_instance_;
156
+ TrueLiteral* NewTrueLiteral(std::size_t begin, std::size_t end) {
157
+ TrueLiteral* tr = new (this) TrueLiteral();
158
+ tr->Location(begin, end);
159
+ return tr;
103
160
  }
104
161
 
105
- Undefined* NewUndefined() {
106
- return undefined_instance_;
162
+ FalseLiteral* NewFalseLiteral(std::size_t begin, std::size_t end) {
163
+ FalseLiteral* fal = new (this) FalseLiteral();
164
+ fal->Location(begin, end);
165
+ return fal;
107
166
  }
108
167
 
109
- TrueLiteral* NewTrueLiteral() {
110
- return true_instance_;
168
+ EmptyStatement* NewEmptyStatement(std::size_t begin, std::size_t end) {
169
+ EmptyStatement* empty = new (this) EmptyStatement();
170
+ empty->Location(begin, end);
171
+ return empty;
111
172
  }
112
173
 
113
- FalseLiteral* NewFalseLiteral() {
114
- return false_instance_;
174
+ DebuggerStatement* NewDebuggerStatement(std::size_t begin, std::size_t end) {
175
+ DebuggerStatement* debug = new (this) DebuggerStatement();
176
+ debug->Location(begin, end);
177
+ return debug;
115
178
  }
116
179
 
117
180
  FunctionStatement* NewFunctionStatement(FunctionLiteral* func) {
118
- return new (this) FunctionStatement(func);
181
+ FunctionStatement* stmt = new (this) FunctionStatement(func);
182
+ stmt->Location(func->begin_position(), func->end_position());
183
+ return stmt;
119
184
  }
120
185
 
121
186
  FunctionDeclaration* NewFunctionDeclaration(FunctionLiteral* func) {
122
- return new (this) FunctionDeclaration(func);
187
+ FunctionDeclaration* decl = new (this) FunctionDeclaration(func);
188
+ decl->Location(func->begin_position(), func->end_position());
189
+ return decl;
123
190
  }
124
191
 
125
- Block* NewBlock(Statements* body) {
126
- return new (this) Block(body);
192
+ Block* NewBlock(Statements* body, std::size_t begin, std::size_t end) {
193
+ Block* block = new (this) Block(body);
194
+ block->Location(begin, end);
195
+ return block;
127
196
  }
128
197
 
129
198
  VariableStatement* NewVariableStatement(core::Token::Type token,
130
- Declarations* decls) {
131
- return new (this) VariableStatement(token, decls);
199
+ Declarations* decls,
200
+ std::size_t begin,
201
+ std::size_t end) {
202
+ VariableStatement* var = new (this) VariableStatement(token, decls);
203
+ assert(!decls->empty());
204
+ var->Location(begin, end);
205
+ return var;
132
206
  }
133
207
 
134
208
  Declaration* NewDeclaration(Identifier* name, Expression* expr) {
135
- return new (this) Declaration(name, expr);
209
+ assert(name && expr);
210
+ Declaration* decl = new (this) Declaration(name, expr);
211
+ const std::size_t end = (expr->AsUndefined()) ?
212
+ name->end_position() : expr->end_position();
213
+ decl->Location(name->begin_position(), end);
214
+ return decl;
136
215
  }
137
216
 
138
217
  IfStatement* NewIfStatement(Expression* cond,
139
218
  Statement* then_statement,
140
- Statement* else_statement) {
141
- return new (this) IfStatement(cond,
142
- then_statement,
143
- else_statement);
219
+ Statement* else_statement,
220
+ std::size_t begin) {
221
+ IfStatement* stmt = new (this) IfStatement(cond,
222
+ then_statement, else_statement);
223
+ assert(then_statement);
224
+ const std::size_t end = (else_statement) ?
225
+ else_statement->end_position() : then_statement->end_position();
226
+ stmt->Location(begin, end);
227
+ return stmt;
144
228
  }
145
229
 
146
230
  DoWhileStatement* NewDoWhileStatement(Statement* body,
147
- Expression* cond) {
148
- return new (this) DoWhileStatement(body, cond);
231
+ Expression* cond,
232
+ std::size_t begin,
233
+ std::size_t end) {
234
+ DoWhileStatement* stmt = new (this) DoWhileStatement(body, cond);
235
+ stmt->Location(begin, end);
236
+ return stmt;
149
237
  }
150
238
 
151
239
  WhileStatement* NewWhileStatement(Statement* body,
152
- Expression* cond) {
153
- return new (this) WhileStatement(body, cond);
240
+ Expression* cond,
241
+ std::size_t begin) {
242
+ assert(body && cond);
243
+ WhileStatement* stmt = new (this) WhileStatement(body, cond);
244
+ stmt->Location(begin, body->end_position());
245
+ return stmt;
154
246
  }
155
247
 
156
248
  ForInStatement* NewForInStatement(Statement* body,
157
249
  Statement* each,
158
- Expression* enumerable) {
159
- return new (this) ForInStatement(body, each, enumerable);
160
- }
161
-
162
- ExpressionStatement* NewExpressionStatement(Expression* expr) {
163
- return new (this) ExpressionStatement(expr);
250
+ Expression* enumerable,
251
+ std::size_t begin) {
252
+ assert(body);
253
+ ForInStatement* stmt = new (this) ForInStatement(body, each, enumerable);
254
+ stmt->Location(begin, body->end_position());
255
+ return stmt;
164
256
  }
165
257
 
166
258
  ForStatement* NewForStatement(Statement* body,
167
259
  Statement* init,
168
260
  Expression* cond,
169
- Statement* next) {
170
- return new (this) ForStatement(body, init, cond, next);
261
+ Statement* next,
262
+ std::size_t begin) {
263
+ assert(body);
264
+ ForStatement* stmt = new (this) ForStatement(body, init, cond, next);
265
+ stmt->Location(begin, body->end_position());
266
+ return stmt;
171
267
  }
172
268
 
269
+ ExpressionStatement* NewExpressionStatement(Expression* expr, std::size_t end) {
270
+ ExpressionStatement* stmt = new (this) ExpressionStatement(expr);
271
+ stmt->Location(expr->begin_position(), end);
272
+ return stmt;
273
+ }
173
274
 
174
275
  ContinueStatement* NewContinueStatement(Identifier* label,
175
- IterationStatement** target) {
176
- return new (this) ContinueStatement(label, target);
276
+ IterationStatement** target,
277
+ std::size_t begin,
278
+ std::size_t end) {
279
+ ContinueStatement* stmt = new (this) ContinueStatement(label, target);
280
+ stmt->Location(begin, end);
281
+ return stmt;
177
282
  }
178
283
 
179
284
  BreakStatement* NewBreakStatement(Identifier* label,
180
- BreakableStatement** target) {
181
- return new (this) BreakStatement(label, target);
285
+ BreakableStatement** target,
286
+ std::size_t begin,
287
+ std::size_t end) {
288
+ BreakStatement* stmt = new (this) BreakStatement(label, target);
289
+ stmt->Location(begin, end);
290
+ return stmt;
182
291
  }
183
292
 
184
- ReturnStatement* NewReturnStatement(Expression* expr) {
185
- return new (this) ReturnStatement(expr);
293
+ ReturnStatement* NewReturnStatement(Expression* expr,
294
+ std::size_t begin,
295
+ std::size_t end) {
296
+ assert(expr);
297
+ ReturnStatement* stmt = new (this) ReturnStatement(expr);
298
+ stmt->Location(begin, end);
299
+ return stmt;
186
300
  }
187
301
 
188
- WithStatement* NewWithStatement(Expression* expr, Statement* stmt) {
189
- return new (this) WithStatement(expr, stmt);
302
+ WithStatement* NewWithStatement(Expression* expr,
303
+ Statement* body, std::size_t begin) {
304
+ assert(body);
305
+ WithStatement* stmt = new (this) WithStatement(expr, body);
306
+ stmt->Location(begin, body->end_position());
307
+ return stmt;
190
308
  }
191
309
 
192
- SwitchStatement* NewSwitchStatement(Expression* expr, CaseClauses* clauses) {
193
- return new (this) SwitchStatement(expr, clauses);
310
+ SwitchStatement* NewSwitchStatement(Expression* expr, CaseClauses* clauses,
311
+ std::size_t begin, std::size_t end) {
312
+ SwitchStatement* stmt = new (this) SwitchStatement(expr, clauses);
313
+ stmt->Location(begin, end);
314
+ return stmt;
194
315
  }
195
316
 
196
317
  CaseClause* NewCaseClause(bool is_default,
197
- Expression* expr, Statements* body) {
198
- return new (this) CaseClause(is_default, expr, body);
318
+ Expression* expr, Statements* body,
319
+ std::size_t begin,
320
+ std::size_t end) {
321
+ CaseClause* clause = new (this) CaseClause(is_default, expr, body);
322
+ clause->Location(begin, end);
323
+ return clause;
199
324
  }
200
325
 
201
326
 
202
- ThrowStatement* NewThrowStatement(Expression* expr) {
203
- return new (this) ThrowStatement(expr);
327
+ ThrowStatement* NewThrowStatement(Expression* expr,
328
+ std::size_t begin,
329
+ std::size_t end) {
330
+ assert(expr);
331
+ ThrowStatement* stmt = new (this) ThrowStatement(expr);
332
+ stmt->Location(begin, end);
333
+ return stmt;
204
334
  }
205
335
 
206
336
  TryStatement* NewTryStatement(Block* try_block,
207
337
  Identifier* catch_name,
208
338
  Block* catch_block,
209
- Block* finally_block) {
210
- return new (this) TryStatement(try_block,
211
- catch_name,
212
- catch_block,
213
- finally_block);
339
+ Block* finally_block,
340
+ std::size_t begin) {
341
+ TryStatement* stmt = new (this) TryStatement(try_block,
342
+ catch_name,
343
+ catch_block,
344
+ finally_block);
345
+ assert(catch_block || finally_block);
346
+ const std::size_t end = (finally_block) ?
347
+ finally_block->end_position() : catch_block->end_position();
348
+ stmt->Location(begin, end);
349
+ return stmt;
214
350
  }
215
351
 
216
352
  LabelledStatement* NewLabelledStatement(Expression* expr, Statement* stmt) {
217
- return new (this) LabelledStatement(expr, stmt);
353
+ LabelledStatement* label = new (this) LabelledStatement(expr, stmt);
354
+ label->Location(expr->begin_position(), stmt->end_position());
355
+ return label;
218
356
  }
219
357
 
220
358
  BinaryOperation* NewBinaryOperation(core::Token::Type op,
221
359
  Expression* result,
222
360
  Expression* right) {
223
- return new (this) BinaryOperation(op, result, right);
361
+ BinaryOperation* expr = new (this) BinaryOperation(op, result, right);
362
+ expr->Location(result->begin_position(), right->end_position());
363
+ return expr;
224
364
  }
225
365
 
226
366
  Assignment* NewAssignment(core::Token::Type op,
227
367
  Expression* left,
228
368
  Expression* right) {
229
- return new (this) Assignment(op, left, right);
369
+ Assignment* expr = new (this) Assignment(op, left, right);
370
+ expr->Location(left->begin_position(), right->end_position());
371
+ return expr;
230
372
  }
231
373
 
232
374
  ConditionalExpression* NewConditionalExpression(Expression* cond,
233
375
  Expression* left,
234
376
  Expression* right) {
235
- return new (this) ConditionalExpression(cond, left, right);
377
+ ConditionalExpression* expr = new (this) ConditionalExpression(cond, left, right);
378
+ expr->Location(cond->begin_position(), right->end_position());
379
+ return expr;
236
380
  }
237
381
 
238
382
  UnaryOperation* NewUnaryOperation(core::Token::Type op,
239
- Expression* expr) {
240
- return new (this) UnaryOperation(op, expr);
383
+ Expression* expr, std::size_t begin) {
384
+ assert(expr);
385
+ UnaryOperation* unary = new (this) UnaryOperation(op, expr);
386
+ unary->Location(begin, expr->end_position());
387
+ return unary;
241
388
  }
242
389
 
243
390
  PostfixExpression* NewPostfixExpression(core::Token::Type op,
244
- Expression* expr) {
245
- return new (this) PostfixExpression(op, expr);
391
+ Expression* expr, std::size_t end) {
392
+ assert(expr);
393
+ PostfixExpression* post = new (this) PostfixExpression(op, expr);
394
+ post->Location(expr->begin_position(), end);
395
+ return post;
246
396
  }
247
397
 
248
- FunctionCall* NewFunctionCall(Expression* expr, Expressions* args) {
249
- return new (this) FunctionCall(expr, args);
398
+ FunctionCall* NewFunctionCall(Expression* expr,
399
+ Expressions* args, std::size_t end) {
400
+ FunctionCall* call = new (this) FunctionCall(expr, args);
401
+ call->Location(expr->begin_position(), end);
402
+ return call;
250
403
  }
251
404
 
252
- ConstructorCall* NewConstructorCall(Expression* target, Expressions* args) {
253
- return new (this) ConstructorCall(target, args);
405
+ ConstructorCall* NewConstructorCall(Expression* target,
406
+ Expressions* args, std::size_t end) {
407
+ ConstructorCall* call = new (this) ConstructorCall(target, args);
408
+ call->Location(target->begin_position(), end);
409
+ return call;
254
410
  }
255
411
 
256
412
  IndexAccess* NewIndexAccess(Expression* expr, Expression* index) {
257
- return new (this) IndexAccess(expr, index);
413
+ IndexAccess* access = new (this) IndexAccess(expr, index);
414
+ access->Location(expr->begin_position(), index->end_position());
415
+ return access;
258
416
  }
259
417
 
260
418
  IdentifierAccess* NewIdentifierAccess(Expression* expr, Identifier* ident) {
261
- return new (this) IdentifierAccess(expr, ident);
419
+ IdentifierAccess* access = new (this) IdentifierAccess(expr, ident);
420
+ access->Location(expr->begin_position(), ident->end_position());
421
+ return access;
262
422
  }
263
423
 
264
424
  private:
265
425
  Undefined* undefined_instance_;
266
- EmptyStatement* empty_statement_instance_;
267
- DebuggerStatement* debugger_statement_instance_;
268
- ThisLiteral* this_instance_;
269
- NullLiteral* null_instance_;
270
- TrueLiteral* true_instance_;
271
- FalseLiteral* false_instance_;
272
426
  };
273
427
 
274
428
 
@@ -23,10 +23,17 @@ class TestPhonic < Test::Unit::TestCase
23
23
  assert(IV::Phonic::parse("T"))
24
24
  assert(IV::Phonic::parse("var test = \"おはようございます\";"))
25
25
  assert_raise(IV::Phonic::ParseError) {
26
- IV::Phonic::parse("var test = var;")
26
+ IV::Phonic::parse("var test =var;")
27
27
  }
28
28
  assert_nothing_raised {
29
29
  IV::Phonic::parse("var test = /test/;")
30
30
  }
31
+ pp IV::Phonic::parse("var i = 20;")
32
+ # assert_nothing_raised {
33
+ # IV::Phonic::parse(File.read('tmp/jquery.js'))
34
+ # }
35
+ # assert_nothing_raised {
36
+ # IV::Phonic::parse(File.read('tmp/dojo.js'))
37
+ # }
31
38
  end
32
39
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: iv-phonic
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Constellation
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-08 00:00:00 +09:00
13
+ date: 2011-02-09 00:00:00 +09:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency