1- library code_builder.src.builders.expression;
2-
31// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
42// for details. All rights reserved. Use of this source code is governed by a
53// BSD-style license that can be found in the LICENSE file.
4+ library code_builder.src.builders.expression;
65
76import 'package:analyzer/analyzer.dart' ;
87import 'package:analyzer/dart/ast/token.dart' ;
@@ -32,7 +31,37 @@ final _true = new BooleanLiteral(new KeywordToken(Keyword.TRUE, 0), true);
3231/// Returns a pre-defined literal expression of [value] .
3332///
3433/// Only primitive values are allowed.
35- ExpressionBuilder literal (value) => new _LiteralExpression (_literal (value));
34+ ExpressionBuilder literal (value) {
35+ if (value is List ) {
36+ return list (value);
37+ }
38+ if (value is Map ) {
39+ return map (value);
40+ }
41+ return new _LiteralExpression (_literal (value));
42+ }
43+
44+ /// Returns a literal `List` expression from [values] .
45+ ///
46+ /// Optionally specify [asConst] or with a generic [type] .
47+ ExpressionBuilder list (
48+ Iterable values, {
49+ bool asConst: false ,
50+ TypeBuilder type,
51+ }) =>
52+ new _TypedListExpression (values, asConst: asConst, type: type);
53+
54+ /// Returns a literal `Map` expression from [values] .
55+ ///
56+ /// Optionally specify [asConst] or with a generic [keyType] or [valueType] .
57+ ExpressionBuilder map (
58+ Map values, {
59+ bool asConst: false ,
60+ TypeBuilder keyType,
61+ TypeBuilder valueType,
62+ }) =>
63+ new _TypedMapExpression (values,
64+ asConst: asConst, keyType: keyType, valueType: valueType);
3665
3766Literal _literal (value) {
3867 if (value == null ) {
@@ -45,24 +74,6 @@ Literal _literal(value) {
4574 return new IntegerLiteral (stringToken ('$value ' ), value);
4675 } else if (value is double ) {
4776 return new DoubleLiteral (stringToken ('$value ' ), value);
48- } else if (value is List ) {
49- return new ListLiteral (
50- null ,
51- null ,
52- $openBracket,
53- value.map/*<Literal>*/ (_literal).toList (),
54- $closeBracket,
55- );
56- } else if (value is Map ) {
57- return new MapLiteral (
58- null ,
59- null ,
60- $openBracket,
61- value.keys.map/*<MapLiteralEntry>*/ ((k) {
62- return new MapLiteralEntry (_literal (k), $colon, _literal (value[k]));
63- }).toList (),
64- $closeBracket,
65- );
6677 }
6778 throw new ArgumentError .value (value, 'Unsupported' );
6879}
@@ -357,3 +368,103 @@ class _ParenthesesExpression extends Object with AbstractExpressionMixin {
357368 );
358369 }
359370}
371+
372+ ExpressionBuilder _expressionify (v) {
373+ if (v == null || v is bool || v is String || v is int || v is double ) {
374+ return new _LiteralExpression (_literal (v));
375+ }
376+ if (v is ExpressionBuilder ) {
377+ return v;
378+ }
379+ throw new ArgumentError ('Could not expressionify $v ' );
380+ }
381+
382+ class _TypedListExpression extends Object with AbstractExpressionMixin {
383+ static List <ExpressionBuilder > _toExpression (Iterable values) {
384+ return values.map (_expressionify).toList ();
385+ }
386+
387+ final bool _asConst;
388+ final TypeBuilder _type;
389+ final List <ExpressionBuilder > _values;
390+
391+ _TypedListExpression (Iterable values, {bool asConst, TypeBuilder type})
392+ : _values = _toExpression (values),
393+ _asConst = asConst,
394+ _type = type;
395+
396+ @override
397+ AstNode buildAst ([Scope scope]) => buildExpression (scope);
398+
399+ @override
400+ Expression buildExpression ([Scope scope]) {
401+ return new ListLiteral (
402+ _asConst ? $const : null ,
403+ _type != null
404+ ? new TypeArgumentList (
405+ $openBracket,
406+ [_type.buildType (scope)],
407+ $closeBracket,
408+ )
409+ : null ,
410+ $openBracket,
411+ _values.map ((v) => v.buildExpression (scope)).toList (),
412+ $closeBracket,
413+ );
414+ }
415+ }
416+
417+ class _TypedMapExpression extends Object with AbstractExpressionMixin {
418+ static Map <ExpressionBuilder , ExpressionBuilder > _toExpression (Map values) {
419+ return new Map <ExpressionBuilder , ExpressionBuilder >.fromIterable (
420+ values.keys,
421+ key: (k) => _expressionify (k),
422+ value: (k) => _expressionify (values[k]),
423+ );
424+ }
425+
426+ final bool _asConst;
427+ final TypeBuilder _keyType;
428+ final TypeBuilder _valueType;
429+ final Map <ExpressionBuilder , ExpressionBuilder > _values;
430+
431+ _TypedMapExpression (Map values,
432+ {bool asConst, TypeBuilder keyType, TypeBuilder valueType})
433+ : _values = _toExpression (values),
434+ _asConst = asConst,
435+ _keyType = keyType != null
436+ ? keyType
437+ : valueType != null ? lib$core.$dynamic : null ,
438+ _valueType = valueType != null
439+ ? valueType
440+ : keyType != null ? lib$core.$dynamic : null ;
441+
442+ @override
443+ AstNode buildAst ([Scope scope]) => buildExpression (scope);
444+
445+ @override
446+ Expression buildExpression ([Scope scope]) {
447+ return new MapLiteral (
448+ _asConst ? $const : null ,
449+ _keyType != null
450+ ? new TypeArgumentList (
451+ $openBracket,
452+ [
453+ _keyType.buildType (scope),
454+ _valueType.buildType (scope),
455+ ],
456+ $closeBracket,
457+ )
458+ : null ,
459+ $openBracket,
460+ _values.keys.map ((k) {
461+ return new MapLiteralEntry (
462+ k.buildExpression (scope),
463+ $colon,
464+ _values[k].buildExpression (scope),
465+ );
466+ }).toList (),
467+ $closeBracket,
468+ );
469+ }
470+ }
0 commit comments