Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(368)

Side by Side Diff: pkg/yaml/lib/yaml.dart

Issue 689513002: Rewrite the pkg/yaml parser. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update string_scanner dependency. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/yaml_document.dart';
15 export 'src/yaml_exception.dart'; 15 export 'src/yaml_exception.dart';
16 export 'src/yaml_node.dart'; 16 export 'src/yaml_node.dart' hide setSpan;
17 17
18 /// Loads a single document from a YAML string. 18 /// Loads a single document from a YAML string.
19 /// 19 ///
20 /// If the string contains more than one document, this throws a 20 /// If the string contains more than one document, this throws a
21 /// [YamlException]. In future releases, this will become an [ArgumentError]. 21 /// [YamlException]. In future releases, this will become an [ArgumentError].
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 [sourceUrl] is passed, it's used as the URL from which the YAML 32 /// 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`. 33 /// originated for error reporting. It can be a [String], a [Uri], or `null`.
34 loadYaml(String yaml, {sourceUrl}) => 34 loadYaml(String yaml, {sourceUrl}) =>
35 loadYamlNode(yaml, sourceUrl: sourceUrl).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, {sourceUrl}) { 42 YamlNode loadYamlNode(String yaml, {sourceUrl}) =>
43 var stream = loadYamlStream(yaml, sourceUrl: sourceUrl); 43 loadYamlDocument(yaml, sourceUrl: sourceUrl).contents;
44 if (stream.length != 1) { 44
45 throw new YamlException("Expected 1 document, were ${stream.length}.", 45 /// Loads a single document from a YAML string as a [YamlDocument].
46 stream.span); 46 ///
47 /// This is just like [loadYaml], except that where [loadYaml] would return a
48 /// normal Dart value this returns a [YamlDocument] instead. This allows the
49 /// caller to access document metadata.
50 YamlDocument loadYamlDocument(String yaml, {sourceUrl}) {
51 var loader = new Loader(yaml, sourceUrl: sourceUrl);
52 var document = loader.load();
53 if (document == null) {
54 return new YamlDocument.internal(
55 new YamlScalar.internal(null, loader.span, ScalarStyle.ANY),
56 loader.span, null, const []);
47 } 57 }
48 return stream.nodes[0]; 58
59 var nextDocument = loader.load();
60 if (nextDocument != null) {
61 throw new YamlException("Only expected one document.", nextDocument.span);
62 }
63
64 return document;
49 } 65 }
50 66
51 /// Loads a stream of documents from a YAML string. 67 /// Loads a stream of documents from a YAML string.
52 /// 68 ///
53 /// The return value is mostly normal Dart objects. However, since YAML mappings 69 /// 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 70 /// 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. 71 /// (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 72 /// These have a few small behavioral differences from the default Map
57 /// implementation; for details, see the [YamlMap] class. 73 /// implementation; for details, see the [YamlMap] class.
58 /// 74 ///
59 /// In future versions, maps will instead be [HashMap]s with a custom equality 75 /// In future versions, maps will instead be [HashMap]s with a custom equality
60 /// operation. 76 /// operation.
61 /// 77 ///
62 /// If [sourceUrl] is passed, it's used as the URL from which the YAML 78 /// 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`. 79 /// originated for error reporting. It can be a [String], a [Uri], or `null`.
64 YamlList loadYamlStream(String yaml, {sourceUrl}) { 80 YamlList loadYamlStream(String yaml, {sourceUrl}) {
65 var pair; 81 var loader = new Loader(yaml, sourceUrl: sourceUrl);
66 try { 82
67 pair = new Parser(yaml, sourceUrl).l_yamlStream(); 83 var documents = [];
68 } on StringScannerException catch (error) { 84 var document = loader.load();
69 throw new YamlException(error.message, error.span); 85 while (document != null) {
86 documents.add(document);
87 document = loader.load();
70 } 88 }
71 89
72 var nodes = pair.first 90 return new YamlList.internal(
73 .map((doc) => new Constructor(new Composer(doc).compose()).construct()) 91 documents.map((document) => document.contents).toList(),
74 .toList(); 92 loader.span,
75 return new YamlList.internal(nodes, pair.last); 93 CollectionStyle.ANY);
76 } 94 }
95
96 /// Loads a stream of documents from a YAML string.
97 ///
98 /// This is like [loadYamlStream], except that it returns [YamlDocument]s with
99 /// metadata wrapping the document contents.
100 List<YamlDocument> loadYamlDocuments(String yaml, {sourceUrl}) {
101 var loader = new Loader(yaml, sourceUrl: sourceUrl);
102
103 var documents = [];
104 var document = loader.load();
105 while (document != null) {
106 documents.add(document);
107 document = loader.load();
108 }
109
110 return documents;
111 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698