diff --git a/lib/src/builders/expression.dart b/lib/src/builders/expression.dart index 213c744..4565cb4 100644 --- a/lib/src/builders/expression.dart +++ b/lib/src/builders/expression.dart @@ -29,6 +29,7 @@ part 'expression/invocation.dart'; part 'expression/negate.dart'; part 'expression/operators.dart'; part 'expression/return.dart'; +part 'expression/throw.dart'; part 'expression/yield.dart'; final _false = @@ -205,6 +206,9 @@ abstract class AbstractExpressionMixin implements ExpressionBuilder { @override StatementBuilder asYieldStar() => new _AsYield(this, true); + @override + ExpressionBuilder asThrow() => new _ThrowExpression(this); + @override WhileStatementBuilder asWhile({bool asDo: false}) { return new WhileStatementBuilder(asDo, this); @@ -371,6 +375,9 @@ abstract class ExpressionBuilder /// [Statement] AST instead of an expression. StatementBuilder asStatement(); + /// Returns as an [ExpressionBuilder] that throws this expression as an error. + ExpressionBuilder asThrow(); + /// Returns as a [StatementBuilder] that assigns to a new `var` [variable]. /// /// If [type] is supplied, the resulting statement is `{type} {variable} =`. diff --git a/lib/src/builders/expression/throw.dart b/lib/src/builders/expression/throw.dart new file mode 100644 index 0000000..5db07c6 --- /dev/null +++ b/lib/src/builders/expression/throw.dart @@ -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 _ThrowExpression extends AbstractExpressionMixin with TopLevelMixin { + final ExpressionBuilder _value; + + _ThrowExpression(this._value); + + @override + ExpressionBuilder asThrow() => this; + + @override + AstNode buildAst([Scope scope]) => buildExpression(scope); + + @override + Expression buildExpression([Scope scope]) { + return astFactory.throwExpression($throw, _value.buildExpression(scope)); + } +} diff --git a/lib/src/builders/field.dart b/lib/src/builders/field.dart index 5964e4a..1424463 100644 --- a/lib/src/builders/field.dart +++ b/lib/src/builders/field.dart @@ -106,9 +106,7 @@ abstract class FieldBuilder TopLevelVariableDeclaration buildTopLevel([Scope scope]); } -class _FieldBuilderImpl extends TopLevelMixin - with HasAnnotationsMixin - implements FieldBuilder { +class _FieldBuilderImpl extends HasAnnotationsMixin implements FieldBuilder { final Keyword _keyword; final String _name; final TypeBuilder _type; @@ -157,6 +155,11 @@ class _FieldBuilderImpl extends TopLevelMixin ); } + @override + CompilationUnitMember buildTopLevelAst([Scope scope]) { + return buildTopLevel(scope); + } + VariableDeclarationList _buildVariableList([Scope scope]) { return astFactory.variableDeclarationList( null, diff --git a/lib/src/tokens.dart b/lib/src/tokens.dart index 88687a6..1ed0993 100644 --- a/lib/src/tokens.dart +++ b/lib/src/tokens.dart @@ -187,6 +187,9 @@ final Token $static = new KeywordToken(Keyword.STATIC, 0); /// The `this` token. final Token $this = new KeywordToken(Keyword.THIS, 0); +/// The `throw` token. +final Token $throw = new KeywordToken(Keyword.THROW, 0); + /// The `true` token. final Token $true = new KeywordToken(Keyword.TRUE, 0); diff --git a/test/builders/expression_test.dart b/test/builders/expression_test.dart index 571d0b9..21a4876 100644 --- a/test/builders/expression_test.dart +++ b/test/builders/expression_test.dart @@ -404,4 +404,15 @@ void main() { '''), ); }); + + test('should throw an exception', () { + expect( + new TypeBuilder('StateError') + .newInstance([literal('Hey! No!')]) + .asThrow() + .asStatement(), + equalsSource(''' + throw new StateError('Hey! No!'); + ''')); + }); }