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 'src/loader.dart'; |
8 | 8 import 'src/style.dart'; |
9 import 'src/composer.dart'; | 9 import 'src/yaml_document.dart'; |
10 import 'src/constructor.dart'; | |
11 import 'src/parser.dart'; | |
12 import 'src/yaml_exception.dart'; | 10 import 'src/yaml_exception.dart'; |
13 import 'src/yaml_node.dart'; | 11 import 'src/yaml_node.dart'; |
14 | 12 |
| 13 export 'src/style.dart'; |
| 14 export 'src/utils.dart' show YamlWarningCallback, yamlWarningCallback; |
| 15 export 'src/yaml_document.dart'; |
15 export 'src/yaml_exception.dart'; | 16 export 'src/yaml_exception.dart'; |
16 export 'src/yaml_node.dart'; | 17 export 'src/yaml_node.dart' hide setSpan; |
17 | 18 |
18 /// Loads a single document from a YAML string. | 19 /// Loads a single document from a YAML string. |
19 /// | 20 /// |
20 /// If the string contains more than one document, this throws a | 21 /// If the string contains more than one document, this throws a |
21 /// [YamlException]. In future releases, this will become an [ArgumentError]. | 22 /// [YamlException]. In future releases, this will become an [ArgumentError]. |
22 /// | 23 /// |
23 /// The return value is mostly normal Dart objects. However, since YAML mappings | 24 /// 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 | 25 /// 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. | 26 /// (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 | 27 /// These have a few small behavioral differences from the default Map |
27 /// implementation; for details, see the [YamlMap] class. | 28 /// implementation; for details, see the [YamlMap] class. |
28 /// | 29 /// |
29 /// In future versions, maps will instead be [HashMap]s with a custom equality | 30 /// In future versions, maps will instead be [HashMap]s with a custom equality |
30 /// operation. | 31 /// operation. |
31 /// | 32 /// |
32 /// If [sourceUrl] is passed, it's used as the URL from which the YAML | 33 /// If [sourceUrl] is passed, it's used as the URL from which the YAML |
33 /// originated for error reporting. It can be a [String], a [Uri], or `null`. | 34 /// originated for error reporting. It can be a [String], a [Uri], or `null`. |
34 loadYaml(String yaml, {sourceUrl}) => | 35 loadYaml(String yaml, {sourceUrl}) => |
35 loadYamlNode(yaml, sourceUrl: sourceUrl).value; | 36 loadYamlNode(yaml, sourceUrl: sourceUrl).value; |
36 | 37 |
37 /// Loads a single document from a YAML string as a [YamlNode]. | 38 /// Loads a single document from a YAML string as a [YamlNode]. |
38 /// | 39 /// |
39 /// This is just like [loadYaml], except that where [loadYaml] would return a | 40 /// 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 | 41 /// 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]. | 42 /// to be confident that the return value will always be a [YamlNode]. |
42 YamlNode loadYamlNode(String yaml, {sourceUrl}) { | 43 YamlNode loadYamlNode(String yaml, {sourceUrl}) => |
43 var stream = loadYamlStream(yaml, sourceUrl: sourceUrl); | 44 loadYamlDocument(yaml, sourceUrl: sourceUrl).contents; |
44 if (stream.length != 1) { | 45 |
45 throw new YamlException("Expected 1 document, were ${stream.length}.", | 46 /// Loads a single document from a YAML string as a [YamlDocument]. |
46 stream.span); | 47 /// |
| 48 /// This is just like [loadYaml], except that where [loadYaml] would return a |
| 49 /// normal Dart value this returns a [YamlDocument] instead. This allows the |
| 50 /// caller to access document metadata. |
| 51 YamlDocument loadYamlDocument(String yaml, {sourceUrl}) { |
| 52 var loader = new Loader(yaml, sourceUrl: sourceUrl); |
| 53 var document = loader.load(); |
| 54 if (document == null) { |
| 55 return new YamlDocument.internal( |
| 56 new YamlScalar.internal(null, loader.span, ScalarStyle.ANY), |
| 57 loader.span, null, const []); |
47 } | 58 } |
48 return stream.nodes[0]; | 59 |
| 60 var nextDocument = loader.load(); |
| 61 if (nextDocument != null) { |
| 62 throw new YamlException("Only expected one document.", nextDocument.span); |
| 63 } |
| 64 |
| 65 return document; |
49 } | 66 } |
50 | 67 |
51 /// Loads a stream of documents from a YAML string. | 68 /// Loads a stream of documents from a YAML string. |
52 /// | 69 /// |
53 /// The return value is mostly normal Dart objects. However, since YAML mappings | 70 /// 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 | 71 /// 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. | 72 /// (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 | 73 /// These have a few small behavioral differences from the default Map |
57 /// implementation; for details, see the [YamlMap] class. | 74 /// implementation; for details, see the [YamlMap] class. |
58 /// | 75 /// |
59 /// In future versions, maps will instead be [HashMap]s with a custom equality | 76 /// In future versions, maps will instead be [HashMap]s with a custom equality |
60 /// operation. | 77 /// operation. |
61 /// | 78 /// |
62 /// If [sourceUrl] is passed, it's used as the URL from which the YAML | 79 /// If [sourceUrl] is passed, it's used as the URL from which the YAML |
63 /// originated for error reporting. It can be a [String], a [Uri], or `null`. | 80 /// originated for error reporting. It can be a [String], a [Uri], or `null`. |
64 YamlList loadYamlStream(String yaml, {sourceUrl}) { | 81 YamlList loadYamlStream(String yaml, {sourceUrl}) { |
65 var pair; | 82 var loader = new Loader(yaml, sourceUrl: sourceUrl); |
66 try { | 83 |
67 pair = new Parser(yaml, sourceUrl).l_yamlStream(); | 84 var documents = []; |
68 } on StringScannerException catch (error) { | 85 var document = loader.load(); |
69 throw new YamlException(error.message, error.span); | 86 while (document != null) { |
| 87 documents.add(document); |
| 88 document = loader.load(); |
70 } | 89 } |
71 | 90 |
72 var nodes = pair.first | 91 return new YamlList.internal( |
73 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) | 92 documents.map((document) => document.contents).toList(), |
74 .toList(); | 93 loader.span, |
75 return new YamlList.internal(nodes, pair.last); | 94 CollectionStyle.ANY); |
76 } | 95 } |
| 96 |
| 97 /// Loads a stream of documents from a YAML string. |
| 98 /// |
| 99 /// This is like [loadYamlStream], except that it returns [YamlDocument]s with |
| 100 /// metadata wrapping the document contents. |
| 101 List<YamlDocument> loadYamlDocuments(String yaml, {sourceUrl}) { |
| 102 var loader = new Loader(yaml, sourceUrl: sourceUrl); |
| 103 |
| 104 var documents = []; |
| 105 var document = loader.load(); |
| 106 while (document != null) { |
| 107 documents.add(document); |
| 108 document = loader.load(); |
| 109 } |
| 110 |
| 111 return documents; |
| 112 } |
OLD | NEW |