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 /// A parser for [YAML](http://www.yaml.org/). | 5 /// A parser for [YAML](http://www.yaml.org/). |
6 /// | 6 /// |
7 /// Use [loadYaml] to load a single document, or [loadYamlStream] to load a | 7 /// Use [loadYaml] to load a single document, or [loadYamlStream] to load a |
8 /// stream of documents. For example: | 8 /// stream of documents. For example: |
9 /// | 9 /// |
10 /// import 'package:yaml/yaml.dart'; | 10 /// import 'package:yaml/yaml.dart'; |
11 /// main() { | 11 /// main() { |
12 /// var doc = loadYaml("YAML: YAML Ain't Markup Language"); | 12 /// var doc = loadYaml("YAML: YAML Ain't Markup Language"); |
13 /// print(doc['YAML']); | 13 /// print(doc['YAML']); |
14 /// } | 14 /// } |
15 /// | 15 /// |
16 /// This library currently doesn't support dumping to YAML. You should use | 16 /// This library currently doesn't support dumping to YAML. You should use |
17 /// `stringify` from `dart:json` instead: | 17 /// `stringify` from `dart:json` instead: |
18 /// | 18 /// |
19 /// import 'dart:json' as json; | 19 /// import 'dart:json' as json; |
20 /// import 'package:yaml/yaml.dart'; | 20 /// import 'package:yaml/yaml.dart'; |
21 /// main() { | 21 /// main() { |
22 /// var doc = loadYaml("YAML: YAML Ain't Markup Language"); | 22 /// var doc = loadYaml("YAML: YAML Ain't Markup Language"); |
23 /// print(json.stringify(doc)); | 23 /// print(json.stringify(doc)); |
24 /// } | 24 /// } |
25 library yaml; | 25 library yaml; |
26 | 26 |
27 import 'dart:math' as Math; | 27 import 'src/composer.dart'; |
28 import 'dart:collection' show Queue; | 28 import 'src/constructor.dart'; |
| 29 import 'src/parser.dart'; |
| 30 import 'src/yaml_exception.dart'; |
29 | 31 |
30 import 'deep_equals.dart'; | 32 export 'src/yaml_exception.dart'; |
31 | 33 export 'src/yaml_map.dart'; |
32 part 'yaml_map.dart'; | |
33 part 'model.dart'; | |
34 part 'parser.dart'; | |
35 part 'visitor.dart'; | |
36 part 'composer.dart'; | |
37 part 'constructor.dart'; | |
38 | 34 |
39 /// Loads a single document from a YAML string. If the string contains more than | 35 /// Loads a single document from a YAML string. If the string contains more than |
40 /// one document, this throws an error. | 36 /// one document, this throws an error. |
41 /// | 37 /// |
42 /// The return value is mostly normal Dart objects. However, since YAML mappings | 38 /// The return value is mostly normal Dart objects. However, since YAML mappings |
43 /// support some key types that the default Dart map implementation doesn't | 39 /// support some key types that the default Dart map implementation doesn't |
44 /// (null, NaN, booleans, lists, and maps), all maps in the returned document | 40 /// (null, NaN, booleans, lists, and maps), all maps in the returned document |
45 /// are YamlMaps. These have a few small behavioral differences from the default | 41 /// are [YamlMap]s. These have a few small behavioral differences from the |
46 /// Map implementation; for details, see the YamlMap class. | 42 /// default Map implementation; for details, see the [YamlMap] class. |
47 loadYaml(String yaml) { | 43 loadYaml(String yaml) { |
48 var stream = loadYamlStream(yaml); | 44 var stream = loadYamlStream(yaml); |
49 if (stream.length != 1) { | 45 if (stream.length != 1) { |
50 throw new YamlException("Expected 1 document, were ${stream.length}"); | 46 throw new YamlException("Expected 1 document, were ${stream.length}"); |
51 } | 47 } |
52 return stream[0]; | 48 return stream[0]; |
53 } | 49 } |
54 | 50 |
55 /// Loads a stream of documents from a YAML string. | 51 /// Loads a stream of documents from a YAML string. |
56 /// | 52 /// |
57 /// 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 |
58 /// 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 |
59 /// (null, NaN, booleans, lists, and maps), all maps in the returned document | 55 /// (null, NaN, booleans, lists, and maps), all maps in the returned document |
60 /// are YamlMaps. These have a few small behavioral differences from the default | 56 /// are [YamlMap]s. These have a few small behavioral differences from the |
61 /// Map implementation; for details, see the YamlMap class. | 57 /// default Map implementation; for details, see the [YamlMap] class. |
62 List loadYamlStream(String yaml) { | 58 List loadYamlStream(String yaml) { |
63 return new _Parser(yaml).l_yamlStream() | 59 return new Parser(yaml).l_yamlStream() |
64 .map((doc) => new _Constructor(new _Composer(doc).compose()).construct()) | 60 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) |
65 .toList(); | 61 .toList(); |
66 } | 62 } |
67 | |
68 /// An error thrown by the YAML processor. | |
69 class YamlException implements Exception { | |
70 String msg; | |
71 | |
72 YamlException(this.msg); | |
73 | |
74 String toString() => msg; | |
75 } | |
OLD | NEW |