@@ -14,15 +14,8 @@ const literalNull = const _LiteralNull();
1414/// Represents an expression value of `true` .
1515const literalTrue = const LiteralBool (true );
1616
17- // Returns wrapped as a [ExpressionFunctionBody] AST.
18- final Token _closeP = new Token (TokenType .CLOSE_PAREN , 0 );
19-
20- // Returns wrapped as a [FunctionExpression] AST.
21- final Token _openP = new Token (TokenType .OPEN_PAREN , 0 );
22-
23- final Token _semicolon = new Token (TokenType .SEMICOLON , 0 );
24-
2517// TODO(matanl): Make this part of the public API. See annotation_builder.dart.
18+ // Returns wrapped as a [ExpressionFunctionBody] AST.
2619ExpressionFunctionBody _asFunctionBody (
2720 CodeBuilder <Expression > expression,
2821 Scope scope,
@@ -31,22 +24,23 @@ ExpressionFunctionBody _asFunctionBody(
3124 null ,
3225 null ,
3326 expression.toAst (scope),
34- _semicolon ,
27+ $semicolon ,
3528 );
3629}
3730
31+ // Returns wrapped as a [FunctionExpression] AST.
3832FunctionExpression _asFunctionExpression (
3933 CodeBuilder <Expression > expression,
4034 Scope scope,
4135) {
4236 return new FunctionExpression (
4337 null ,
4438 new FormalParameterList (
45- _openP ,
39+ $openParen ,
4640 const [],
4741 null ,
4842 null ,
49- _closeP ,
43+ $closeParen ,
5044 ),
5145 _asFunctionBody (expression, scope),
5246 );
@@ -108,6 +102,15 @@ abstract class ExpressionBuilder implements CodeBuilder<Expression> {
108102 );
109103 }
110104
105+ /// Assign [left] to [right] .
106+ ///
107+ /// If [nullAware] is true, the assignment uses the `??=` operator.
108+ factory ExpressionBuilder .assignment (
109+ String left, CodeBuilder <Expression > right,
110+ {bool nullAware: false }) {
111+ return new _AssignmentExpression (left, right, nullAware: nullAware);
112+ }
113+
111114 const ExpressionBuilder ._();
112115
113116 /// Return a new [ExpressionBuilder] invoking the result of this expression.
@@ -136,10 +139,8 @@ abstract class ExpressionBuilder implements CodeBuilder<Expression> {
136139
137140/// Creates a new literal `bool` value.
138141class LiteralBool extends _LiteralExpression <BooleanLiteral > {
139- static final BooleanLiteral _true =
140- new BooleanLiteral (new KeywordToken (Keyword .TRUE , 0 ), true );
141- static final BooleanLiteral _false =
142- new BooleanLiteral (new KeywordToken (Keyword .FALSE , 0 ), false );
142+ static final BooleanLiteral _true = new BooleanLiteral ($true, true );
143+ static final BooleanLiteral _false = new BooleanLiteral ($false, false );
143144
144145 final bool _value;
145146
@@ -159,7 +160,7 @@ class LiteralInt extends _LiteralExpression<IntegerLiteral> {
159160
160161 @override
161162 IntegerLiteral toAst ([_]) => new IntegerLiteral (
162- new StringToken ( TokenType . INT , '$ _value ' , 0 ),
163+ intToken ( _value),
163164 _value,
164165 );
165166}
@@ -173,18 +174,12 @@ class LiteralString extends _LiteralExpression<StringLiteral> {
173174
174175 @override
175176 StringLiteral toAst ([_]) => new SimpleStringLiteral (
176- new StringToken (
177- TokenType .STRING ,
178- "'$_value '" ,
179- 0 ,
180- ),
177+ stringToken ("'$_value '" ),
181178 _value,
182179 );
183180}
184181
185182class _InvokeExpression extends ExpressionBuilder {
186- static final Token _colon = new Token (TokenType .COLON , 0 );
187-
188183 final String _importFrom;
189184 final ExpressionBuilder _target;
190185 final String _name;
@@ -231,18 +226,18 @@ class _InvokeExpression extends ExpressionBuilder {
231226 // TODO(matanl): Move to TypeBuilder.newInstance.
232227 if (_type != null ) {
233228 return new InstanceCreationExpression (
234- new KeywordToken ( Keyword . NEW , 0 ) ,
229+ $ new,
235230 new ConstructorName (
236231 _type.toAst (scope),
237- _name != null ? new Token ( TokenType . PERIOD , 0 ) : null ,
232+ _name != null ? $period : null ,
238233 _name != null ? _stringIdentifier (_name) : null ,
239234 ),
240235 _getArgumentList (scope),
241236 );
242237 }
243238 return new MethodInvocation (
244239 _target? .toAst (scope),
245- _target != null ? new Token ( TokenType . PERIOD , 0 ) : null ,
240+ _target != null ? $period : null ,
246241 _stringIdentifier (_name),
247242 null ,
248243 _getArgumentList (scope),
@@ -254,21 +249,46 @@ class _InvokeExpression extends ExpressionBuilder {
254249
255250 ArgumentList _getArgumentList (Scope scope) {
256251 return new ArgumentList (
257- new Token ( TokenType . OPEN_CURLY_BRACKET , 0 ) ,
252+ $openCurly ,
258253 _positionalArguments.map/*<Expression*/ ((p) => p.toAst (scope)).toList ()
259254 ..addAll (_namedArguments.keys
260255 .map/*<Expression>*/ ((name) => new NamedExpression (
261256 new Label (
262257 _stringIdentifier (name),
263- _colon ,
258+ $colon ,
264259 ),
265260 _namedArguments[name].toAst (scope),
266261 ))),
267- new Token ( TokenType . CLOSE_CURLY_BRACKET , 0 ) ,
262+ $closeCurly ,
268263 );
269264 }
270265}
271266
267+ class _AssignmentExpression extends ExpressionBuilder {
268+ final String left;
269+ final CodeBuilder <Expression > right;
270+ final bool nullAware;
271+
272+ _AssignmentExpression (this .left, this .right, {this .nullAware: false })
273+ : super ._();
274+
275+ @override
276+ ExpressionBuilder invokeSelf (String name,
277+ {Iterable <CodeBuilder <Expression >> positional: const [],
278+ Map <String , CodeBuilder <Expression >> named: const {}}) {
279+ return _invokeSelfImpl (this , name, positional: positional, named: named);
280+ }
281+
282+ @override
283+ Expression toAst ([Scope scope = const Scope .identity ()]) {
284+ return new AssignmentExpression (_stringIdentifier (left),
285+ nullAware ? $nullAwareEquals : $equals, right.toAst (scope));
286+ }
287+
288+ @override
289+ StatementBuilder toStatement () => new _ExpressionStatementBuilder (this );
290+ }
291+
272292abstract class _LiteralExpression <A extends Literal >
273293 implements ExpressionBuilder , CodeBuilder <A > {
274294 const _LiteralExpression ();
@@ -296,7 +316,7 @@ abstract class _LiteralExpression<A extends Literal>
296316}
297317
298318class _LiteralNull extends _LiteralExpression <NullLiteral > {
299- static NullLiteral _null = new NullLiteral (new KeywordToken ( Keyword . NULL , 0 ) );
319+ static final NullLiteral _null = new NullLiteral ($null );
300320
301321 const _LiteralNull ();
302322
0 commit comments