OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 yaml; | 5 library yaml; |
6 | 6 |
7 import 'package:string_scanner/string_scanner.dart'; | 7 import 'package:string_scanner/string_scanner.dart'; |
8 | 8 |
9 import 'src/composer.dart'; | 9 import 'src/composer.dart'; |
10 import 'src/constructor.dart'; | 10 import 'src/constructor.dart'; |
(...skipping 11 matching lines...) Expand all Loading... |
22 /// | 22 /// |
23 /// The return value is mostly normal Dart objects. However, since YAML mappings | 23 /// The return value is mostly normal Dart objects. However, since YAML mappings |
24 /// support some key types that the default Dart map implementation doesn't | 24 /// support some key types that the default Dart map implementation doesn't |
25 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. | 25 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
26 /// These have a few small behavioral differences from the default Map | 26 /// These have a few small behavioral differences from the default Map |
27 /// implementation; for details, see the [YamlMap] class. | 27 /// implementation; for details, see the [YamlMap] class. |
28 /// | 28 /// |
29 /// In future versions, maps will instead be [HashMap]s with a custom equality | 29 /// In future versions, maps will instead be [HashMap]s with a custom equality |
30 /// operation. | 30 /// operation. |
31 /// | 31 /// |
32 /// If [sourceName] is passed, it's used as the name of the file or URL from | 32 /// If [sourceUrl] is passed, it's used as the URL from which the YAML |
33 /// which the YAML originated for error reporting. | 33 /// originated for error reporting. It can be a [String], a [Uri], or `null`. |
34 loadYaml(String yaml, {String sourceName}) => | 34 loadYaml(String yaml, {sourceUrl}) => |
35 loadYamlNode(yaml, sourceName: sourceName).value; | 35 loadYamlNode(yaml, sourceUrl: sourceUrl).value; |
36 | 36 |
37 /// Loads a single document from a YAML string as a [YamlNode]. | 37 /// Loads a single document from a YAML string as a [YamlNode]. |
38 /// | 38 /// |
39 /// This is just like [loadYaml], except that where [loadYaml] would return a | 39 /// This is just like [loadYaml], except that where [loadYaml] would return a |
40 /// normal Dart value this returns a [YamlNode] instead. This allows the caller | 40 /// normal Dart value this returns a [YamlNode] instead. This allows the caller |
41 /// to be confident that the return value will always be a [YamlNode]. | 41 /// to be confident that the return value will always be a [YamlNode]. |
42 YamlNode loadYamlNode(String yaml, {String sourceName}) { | 42 YamlNode loadYamlNode(String yaml, {sourceUrl}) { |
43 var stream = loadYamlStream(yaml, sourceName: sourceName); | 43 var stream = loadYamlStream(yaml, sourceUrl: sourceUrl); |
44 if (stream.length != 1) { | 44 if (stream.length != 1) { |
45 throw new YamlException("Expected 1 document, were ${stream.length}.", | 45 throw new YamlException("Expected 1 document, were ${stream.length}.", |
46 stream.span); | 46 stream.span); |
47 } | 47 } |
48 return stream.nodes[0]; | 48 return stream.nodes[0]; |
49 } | 49 } |
50 | 50 |
51 /// Loads a stream of documents from a YAML string. | 51 /// Loads a stream of documents from a YAML string. |
52 /// | 52 /// |
53 /// The return value is mostly normal Dart objects. However, since YAML mappings | 53 /// The return value is mostly normal Dart objects. However, since YAML mappings |
54 /// support some key types that the default Dart map implementation doesn't | 54 /// support some key types that the default Dart map implementation doesn't |
55 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. | 55 /// (NaN, lists, and maps), all maps in the returned document are [YamlMap]s. |
56 /// These have a few small behavioral differences from the default Map | 56 /// These have a few small behavioral differences from the default Map |
57 /// implementation; for details, see the [YamlMap] class. | 57 /// implementation; for details, see the [YamlMap] class. |
58 /// | 58 /// |
59 /// In future versions, maps will instead be [HashMap]s with a custom equality | 59 /// In future versions, maps will instead be [HashMap]s with a custom equality |
60 /// operation. | 60 /// operation. |
61 /// | 61 /// |
62 /// If [sourceName] is passed, it's used as the name of the file or URL from | 62 /// If [sourceUrl] is passed, it's used as the URL from which the YAML |
63 /// which the YAML originated for error reporting. | 63 /// originated for error reporting. It can be a [String], a [Uri], or `null`. |
64 YamlList loadYamlStream(String yaml, {String sourceName}) { | 64 YamlList loadYamlStream(String yaml, {sourceUrl}) { |
65 var pair; | 65 var pair; |
66 try { | 66 try { |
67 pair = new Parser(yaml, sourceName).l_yamlStream(); | 67 pair = new Parser(yaml, sourceUrl).l_yamlStream(); |
68 } on StringScannerException catch (error) { | 68 } on StringScannerException catch (error) { |
69 throw new YamlException(error.message, error.span); | 69 throw new YamlException(error.message, error.span); |
70 } | 70 } |
71 | 71 |
72 var nodes = pair.first | 72 var nodes = pair.first |
73 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) | 73 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) |
74 .toList(); | 74 .toList(); |
75 return new YamlList.internal(nodes, pair.last); | 75 return new YamlList.internal(nodes, pair.last); |
76 } | 76 } |
OLD | NEW |