OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dev_compiler.src.codegen.ast_builder; | 5 library dev_compiler.src.codegen.ast_builder; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/scanner.dart'; | 8 import 'package:analyzer/src/generated/scanner.dart'; |
9 import 'package:analyzer/src/generated/utilities_dart.dart'; | 9 import 'package:analyzer/src/generated/utilities_dart.dart'; |
10 import 'package:logging/logging.dart' as logger; | 10 import 'package:logging/logging.dart' as logger; |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 return RawAstBuilder.namedFormalParameter(fp); | 321 return RawAstBuilder.namedFormalParameter(fp); |
322 } | 322 } |
323 | 323 |
324 static NamedExpression namedParameter(String s, Expression e) { | 324 static NamedExpression namedParameter(String s, Expression e) { |
325 return namedExpression(s, e); | 325 return namedExpression(s, e); |
326 } | 326 } |
327 | 327 |
328 static NamedExpression namedExpression(String s, Expression e) { | 328 static NamedExpression namedExpression(String s, Expression e) { |
329 return RawAstBuilder.namedExpression(identifierFromString(s), e); | 329 return RawAstBuilder.namedExpression(identifierFromString(s), e); |
330 } | 330 } |
| 331 |
| 332 /// Declares a single variable `var <name> = <init>` with the type and name |
| 333 /// specified by the VariableElement. See also [variableStatement]. |
| 334 static VariableDeclarationList declareVariable(SimpleIdentifier name, |
| 335 [Expression init]) { |
| 336 var eqToken = init != null ? new Token(TokenType.EQ, 0) : null; |
| 337 var varToken = new KeywordToken(Keyword.VAR, 0); |
| 338 return new VariableDeclarationList(null, null, varToken, null, |
| 339 [new VariableDeclaration(name, eqToken, init)]); |
| 340 } |
| 341 |
| 342 static VariableDeclarationStatement variableStatement(SimpleIdentifier name, |
| 343 [Expression init]) { |
| 344 return RawAstBuilder |
| 345 .variableDeclarationStatement(declareVariable(name, init)); |
| 346 } |
| 347 |
| 348 static InstanceCreationExpression instanceCreation( |
| 349 ConstructorName ctor, List<Expression> args) { |
| 350 var newToken = new KeywordToken(Keyword.NEW, 0); |
| 351 return new InstanceCreationExpression( |
| 352 newToken, ctor, RawAstBuilder.argumentList(args)); |
| 353 } |
331 } | 354 } |
332 | 355 |
333 // This class provides a low-level wrapper around the constructors for | 356 // This class provides a low-level wrapper around the constructors for |
334 // the AST. It mostly simply abstracts from the lexical tokens. | 357 // the AST. It mostly simply abstracts from the lexical tokens. |
335 class RawAstBuilder { | 358 class RawAstBuilder { |
| 359 static ConstructorName constructorName(TypeName type, |
| 360 [SimpleIdentifier name]) { |
| 361 Token period = name != null ? new Token(TokenType.PERIOD, 0) : null; |
| 362 return new ConstructorName(type, period, name); |
| 363 } |
| 364 |
336 static SimpleIdentifier identifierFromString(String name) { | 365 static SimpleIdentifier identifierFromString(String name) { |
337 StringToken token = new SyntheticStringToken(TokenType.IDENTIFIER, name, 0); | 366 StringToken token = new SyntheticStringToken(TokenType.IDENTIFIER, name, 0); |
338 return new SimpleIdentifier(token); | 367 return new SimpleIdentifier(token); |
339 } | 368 } |
340 | 369 |
341 static PrefixedIdentifier prefixedIdentifier( | 370 static PrefixedIdentifier prefixedIdentifier( |
342 SimpleIdentifier pre, SimpleIdentifier id) { | 371 SimpleIdentifier pre, SimpleIdentifier id) { |
343 Token period = new Token(TokenType.PERIOD, 0); | 372 Token period = new Token(TokenType.PERIOD, 0); |
344 return new PrefixedIdentifier(pre, period, id); | 373 return new PrefixedIdentifier(pre, period, id); |
345 } | 374 } |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 } | 554 } |
526 | 555 |
527 static NamedExpression namedParameter(SimpleIdentifier s, Expression e) { | 556 static NamedExpression namedParameter(SimpleIdentifier s, Expression e) { |
528 return namedExpression(s, e); | 557 return namedExpression(s, e); |
529 } | 558 } |
530 | 559 |
531 static NamedExpression namedExpression(SimpleIdentifier s, Expression e) { | 560 static NamedExpression namedExpression(SimpleIdentifier s, Expression e) { |
532 Label l = new Label(s, new Token(TokenType.COLON, 0)); | 561 Label l = new Label(s, new Token(TokenType.COLON, 0)); |
533 return new NamedExpression(l, e); | 562 return new NamedExpression(l, e); |
534 } | 563 } |
| 564 |
| 565 static VariableDeclarationStatement variableDeclarationStatement( |
| 566 VariableDeclarationList varDecl) { |
| 567 var semi = new Token(TokenType.SEMICOLON, 0); |
| 568 return new VariableDeclarationStatement(varDecl, semi); |
| 569 } |
535 } | 570 } |
OLD | NEW |