Skip to content

Commit

Permalink
Add async/await/sync/yield and toImport/ExportBuilder (dart-archive/c…
Browse files Browse the repository at this point in the history
…ode_builder#46)

* Various changes.

* Update presubmit.

* Add async, await, yield

* .

* .

* Update CHANGELOG.md

* Update pubspec.yaml
  • Loading branch information
matanlurey authored Dec 20, 2016
1 parent 3a28214 commit 75c8c69
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 26 deletions.
6 changes: 6 additions & 0 deletions pkgs/code_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.0.0-alpha+9

- Add support for `async`, `sync`, `sync*` functions
- Add support for expression `asAwait`, `asYield`, `asYieldStar`
- Add `toExportBuilder` and `toImportBuilder` to types and references

## 1.0.0-alpha+8

- Fix an import scoping bug in `return` statements and named constructor invocations.
Expand Down
1 change: 1 addition & 0 deletions pkgs/code_builder/lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export 'src/builders/method.dart'
named,
ConstructorBuilder,
MethodBuilder,
MethodModifier,
ValidMethodMember;
export 'src/builders/parameter.dart' show parameter, ParameterBuilder;
export 'src/pretty_printer.dart' show prettyToSource;
Expand Down
11 changes: 11 additions & 0 deletions pkgs/code_builder/lib/src/builders/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:code_builder/dart/core.dart';
import 'package:code_builder/src/builders/annotation.dart';
import 'package:code_builder/src/builders/field.dart';
import 'package:code_builder/src/builders/file.dart';
import 'package:code_builder/src/builders/method.dart';
import 'package:code_builder/src/builders/shared.dart';
import 'package:code_builder/src/builders/type.dart';
Expand Down Expand Up @@ -254,6 +255,16 @@ class _ClassBuilderImpl extends Object
void setExtends(TypeBuilder extend) {
_extends = extend;
}

@override
ExportBuilder toExportBuilder() {
throw new UnsupportedError('Not supported for ClassBuilder');
}

@override
ImportBuilder toImportBuilder({bool deferred: false, String prefix}) {
throw new UnsupportedError('Not supported for ClassBuilder');
}
}

class _TypeNameWrapper implements ValidClassMember {
Expand Down
27 changes: 25 additions & 2 deletions pkgs/code_builder/lib/src/builders/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import 'package:code_builder/src/tokens.dart';

part 'expression/assert.dart';
part 'expression/assign.dart';
part 'expression/await.dart';
part 'expression/cascade.dart';
part 'expression/invocation.dart';
part 'expression/negate.dart';
part 'expression/operators.dart';
part 'expression/return.dart';
part 'expression/yield.dart';

final _false =
astFactory.booleanLiteral(new KeywordToken(Keyword.FALSE, 0), true);
Expand Down Expand Up @@ -141,6 +143,9 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
}) =>
new _AsAssign(this, variable, nullAware, target);

@override
ExpressionBuilder asAwait() => new _AsAwait(this);

@override
StatementBuilder asConst(String variable, [TypeBuilder type]) {
return new _AsAssignNew(this, variable, type, $const);
Expand All @@ -165,6 +170,12 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder {
return new _AsAssignNew(this, variable, type, $var);
}

@override
StatementBuilder asYield() => new _AsYield(this, false);

@override
StatementBuilder asYieldStar() => new _AsYield(this, true);

@override
Statement buildStatement([Scope scope]) {
return asStatement().buildStatement(scope);
Expand Down Expand Up @@ -273,8 +284,14 @@ abstract class ExpressionBuilder
/// Returns as a [StatementBuilder] that assigns to an existing [variable].
///
/// If [target] is specified, determined to be `{target}.{variable}`.
StatementBuilder asAssign(String variable,
{ExpressionBuilder target, bool nullAware});
StatementBuilder asAssign(
String variable, {
ExpressionBuilder target,
bool nullAware,
});

/// Returns as an expression `await`-ing this one.
ExpressionBuilder asAwait() => new _AsAwait(this);

/// Returns as a [StatementBuilder] that assigns to a new `const` [variable].
StatementBuilder asConst(String variable, [TypeBuilder type]);
Expand All @@ -300,6 +317,12 @@ abstract class ExpressionBuilder
/// If [type] is supplied, the resulting statement is `{type} {variable} =`.
StatementBuilder asVar(String variable, [TypeBuilder type]);

/// Returns as a [StatementBuilder] yielding this one.
StatementBuilder asYield();

/// Returns as a [StatementBuilder] yielding this one.
StatementBuilder asYieldStar();

/// Returns an [Expression] AST representing the builder.
Expression buildExpression([Scope scope]);

Expand Down
22 changes: 22 additions & 0 deletions pkgs/code_builder/lib/src/builders/expression/await.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _AsAwait extends AbstractExpressionMixin with TopLevelMixin {
final ExpressionBuilder _expression;

_AsAwait(this._expression);

@override
AstNode buildAst([Scope scope]) => buildExpression(scope);

@override
Expression buildExpression([Scope scope]) {
return new AwaitExpression(
$await,
_expression.buildExpression(scope),
);
}
}
25 changes: 25 additions & 0 deletions pkgs/code_builder/lib/src/builders/expression/yield.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

part of code_builder.src.builders.expression;

class _AsYield extends TopLevelMixin implements StatementBuilder {
final ExpressionBuilder _expression;
final bool _isStar;

_AsYield(this._expression, this._isStar);

@override
Statement buildAst([Scope scope]) => buildStatement(scope);

@override
Statement buildStatement([Scope scope]) {
return new YieldStatement(
$yield,
_isStar ? $star : null,
_expression.buildExpression(scope),
$semicolon,
);
}
}
Loading

0 comments on commit 75c8c69

Please sign in to comment.