| 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 source.analysis_options_provider; | 5 library source.analysis_options_provider; |
| 6 | 6 |
| 7 import 'package:analyzer/file_system/file_system.dart'; | 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart'; | 8 import 'package:analyzer/src/generated/engine.dart'; |
| 9 import 'package:source_span/source_span.dart'; | 9 import 'package:source_span/source_span.dart'; |
| 10 import 'package:yaml/yaml.dart'; | 10 import 'package:yaml/yaml.dart'; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 return getOptionsFromString(optionsSource); | 26 return getOptionsFromString(optionsSource); |
| 27 } | 27 } |
| 28 | 28 |
| 29 /// Provide the options found in [optionsSource]. | 29 /// Provide the options found in [optionsSource]. |
| 30 /// Return an empty options map if the source is null. | 30 /// Return an empty options map if the source is null. |
| 31 Map<String, YamlNode> getOptionsFromString(String optionsSource) { | 31 Map<String, YamlNode> getOptionsFromString(String optionsSource) { |
| 32 var options = <String, YamlNode>{}; | 32 var options = <String, YamlNode>{}; |
| 33 if (optionsSource == null) { | 33 if (optionsSource == null) { |
| 34 return options; | 34 return options; |
| 35 } | 35 } |
| 36 YamlNode doc = loadYamlNode(optionsSource); | 36 |
| 37 YamlNode doc; |
| 38 try { |
| 39 doc = loadYamlNode(optionsSource); |
| 40 } on YamlException catch (e) { |
| 41 throw new OptionsFormatException(e.message, e.span); |
| 42 } catch (e) { |
| 43 throw new OptionsFormatException('Unable to parse YAML document.'); |
| 44 } |
| 45 |
| 37 // Empty options. | 46 // Empty options. |
| 38 if (doc is YamlScalar && doc.value == null) { | 47 if (doc is YamlScalar && doc.value == null) { |
| 39 return options; | 48 return options; |
| 40 } | 49 } |
| 41 if ((doc != null) && (doc is! YamlMap)) { | 50 if ((doc != null) && (doc is! YamlMap)) { |
| 42 throw new OptionsFormatException( | 51 throw new OptionsFormatException( |
| 43 'Bad options file format (expected map, got ${doc.runtimeType})', | 52 'Bad options file format (expected map, got ${doc.runtimeType})', |
| 44 doc.span); | 53 doc.span); |
| 45 } | 54 } |
| 46 if (doc is YamlMap) { | 55 if (doc is YamlMap) { |
| 47 doc.forEach((k, v) { | 56 (doc as YamlMap).forEach((k, v) { |
| 48 if (k is! String) { | 57 if (k is! String) { |
| 49 throw new OptionsFormatException( | 58 throw new OptionsFormatException( |
| 50 'Bad options file format (expected String scope key, ' | 59 'Bad options file format (expected String scope key, ' |
| 51 'got ${k.runtimeType})', | 60 'got ${k.runtimeType})', |
| 52 k != null ? k.span : doc.span); | 61 k != null ? k.span : doc.span); |
| 53 } | 62 } |
| 54 options[k] = v; | 63 options[k] = v; |
| 55 }); | 64 }); |
| 56 } | 65 } |
| 57 return options; | 66 return options; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 72 /// Thrown on options format exceptions. | 81 /// Thrown on options format exceptions. |
| 73 class OptionsFormatException implements Exception { | 82 class OptionsFormatException implements Exception { |
| 74 final String message; | 83 final String message; |
| 75 final SourceSpan span; | 84 final SourceSpan span; |
| 76 OptionsFormatException(this.message, [this.span]); | 85 OptionsFormatException(this.message, [this.span]); |
| 77 | 86 |
| 78 @override | 87 @override |
| 79 String toString() => | 88 String toString() => |
| 80 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; | 89 'OptionsFormatException: ${message?.toString()}, ${span?.toString()}'; |
| 81 } | 90 } |
| OLD | NEW |