OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.parser; | 5 library yaml.parser; |
6 | 6 |
7 import 'package:source_span/source_span.dart'; | 7 import 'package:source_span/source_span.dart'; |
8 import 'package:string_scanner/string_scanner.dart'; | 8 import 'package:string_scanner/string_scanner.dart'; |
9 | 9 |
10 import 'event.dart'; | 10 import 'event.dart'; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 return _parseFlowMappingValue(empty: true); | 108 return _parseFlowMappingValue(empty: true); |
109 default: | 109 default: |
110 throw "Unreachable"; | 110 throw "Unreachable"; |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 /// Parses the production: | 114 /// Parses the production: |
115 /// | 115 /// |
116 /// stream ::= | 116 /// stream ::= |
117 /// STREAM-START implicit_document? explicit_document* STREAM-END | 117 /// STREAM-START implicit_document? explicit_document* STREAM-END |
118 /// ************ | 118 /// ************ |
119 Event _parseStreamStart() { | 119 Event _parseStreamStart() { |
120 var token = _scanner.scan(); | 120 var token = _scanner.scan(); |
121 assert(token.type == TokenType.STREAM_START); | 121 assert(token.type == TokenType.STREAM_START); |
122 | 122 |
123 _state = _State.DOCUMENT_START; | 123 _state = _State.DOCUMENT_START; |
124 return new Event(EventType.STREAM_START, token.span); | 124 return new Event(EventType.STREAM_START, token.span); |
125 } | 125 } |
126 | 126 |
127 /// Parses the productions: | 127 /// Parses the productions: |
128 /// | 128 /// |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 return new DocumentStartEvent(start.expand(token.span), | 176 return new DocumentStartEvent(start.expand(token.span), |
177 versionDirective: versionDirective, | 177 versionDirective: versionDirective, |
178 tagDirectives: tagDirectives, | 178 tagDirectives: tagDirectives, |
179 isImplicit: false); | 179 isImplicit: false); |
180 } | 180 } |
181 | 181 |
182 /// Parses the productions: | 182 /// Parses the productions: |
183 /// | 183 /// |
184 /// explicit_document ::= | 184 /// explicit_document ::= |
185 /// DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* | 185 /// DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END* |
186 /// *********** | 186 /// *********** |
187 Event _parseDocumentContent() { | 187 Event _parseDocumentContent() { |
188 var token = _scanner.peek(); | 188 var token = _scanner.peek(); |
189 | 189 |
190 switch (token.type) { | 190 switch (token.type) { |
191 case TokenType.VERSION_DIRECTIVE: | 191 case TokenType.VERSION_DIRECTIVE: |
192 case TokenType.TAG_DIRECTIVE: | 192 case TokenType.TAG_DIRECTIVE: |
193 case TokenType.DOCUMENT_START: | 193 case TokenType.DOCUMENT_START: |
194 case TokenType.DOCUMENT_END: | 194 case TokenType.DOCUMENT_END: |
195 case TokenType.STREAM_END: | 195 case TokenType.STREAM_END: |
196 _state = _states.removeLast(); | 196 _state = _states.removeLast(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 253 |
254 if (token is AliasToken) { | 254 if (token is AliasToken) { |
255 _scanner.scan(); | 255 _scanner.scan(); |
256 _state = _states.removeLast(); | 256 _state = _states.removeLast(); |
257 return new AliasEvent(token.span, token.name); | 257 return new AliasEvent(token.span, token.name); |
258 } | 258 } |
259 | 259 |
260 var anchor; | 260 var anchor; |
261 var tagToken; | 261 var tagToken; |
262 var span = token.span.start.pointSpan(); | 262 var span = token.span.start.pointSpan(); |
263 parseAnchor() { | 263 parseAnchor(token) { |
264 anchor = token.name; | 264 anchor = token.name; |
265 span = span.expand(token.span); | 265 span = span.expand(token.span); |
266 token = _scanner.advance(); | 266 return _scanner.advance(); |
267 } | 267 } |
268 | 268 |
269 parseTag() { | 269 parseTag(token) { |
270 tagToken = token; | 270 tagToken = token; |
271 span = span.expand(token.span); | 271 span = span.expand(token.span); |
272 token = _scanner.advance(); | 272 return _scanner.advance(); |
273 } | 273 } |
274 | 274 |
275 if (token is AnchorToken) { | 275 if (token is AnchorToken) { |
276 parseAnchor(); | 276 token = parseAnchor(token); |
277 if (token is TagToken) parseTag(); | 277 if (token is TagToken) token = parseTag(token); |
278 } else if (token is TagToken) { | 278 } else if (token is TagToken) { |
279 parseTag(); | 279 token = parseTag(token); |
280 if (token is AnchorToken) parseAnchor(); | 280 if (token is AnchorToken) token = parseAnchor(token); |
281 } | 281 } |
282 | 282 |
283 var tag; | 283 var tag; |
284 if (tagToken != null) { | 284 if (tagToken != null) { |
285 if (tagToken.handle == null) { | 285 if (tagToken.handle == null) { |
286 tag = tagToken.suffix; | 286 tag = tagToken.suffix; |
287 } else { | 287 } else { |
288 var tagDirective = _tagDirectives[tagToken.handle]; | 288 var tagDirective = _tagDirectives[tagToken.handle]; |
289 if (tagDirective == null) { | 289 if (tagDirective == null) { |
290 throw new YamlException("Undefined tag handle.", tagToken.span); | 290 throw new YamlException("Undefined tag handle.", tagToken.span); |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 } | 455 } |
456 | 456 |
457 /// Parses the productions: | 457 /// Parses the productions: |
458 /// | 458 /// |
459 /// block_mapping ::= BLOCK-MAPPING_START | 459 /// block_mapping ::= BLOCK-MAPPING_START |
460 /// | 460 /// |
461 /// ((KEY block_node_or_indentless_sequence?)? | 461 /// ((KEY block_node_or_indentless_sequence?)? |
462 /// | 462 /// |
463 /// (VALUE block_node_or_indentless_sequence?)?)* | 463 /// (VALUE block_node_or_indentless_sequence?)?)* |
464 /// ***** * | 464 /// ***** * |
465 /// BLOCK-END | 465 /// BLOCK-END |
466 /// | 466 /// |
467 Event _parseBlockMappingValue() { | 467 Event _parseBlockMappingValue() { |
468 var token = _scanner.peek(); | 468 var token = _scanner.peek(); |
469 | 469 |
470 if (token.type != TokenType.VALUE) { | 470 if (token.type != TokenType.VALUE) { |
471 _state = _State.BLOCK_MAPPING_KEY; | 471 _state = _State.BLOCK_MAPPING_KEY; |
472 return _processEmptyScalar(token.span.start); | 472 return _processEmptyScalar(token.span.start); |
473 } | 473 } |
474 | 474 |
475 var start = token.span.start; | 475 var start = token.span.start; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 token.span, CollectionStyle.FLOW); | 520 token.span, CollectionStyle.FLOW); |
521 } else if (token.type != TokenType.FLOW_SEQUENCE_END) { | 521 } else if (token.type != TokenType.FLOW_SEQUENCE_END) { |
522 _states.add(_State.FLOW_SEQUENCE_ENTRY); | 522 _states.add(_State.FLOW_SEQUENCE_ENTRY); |
523 return _parseNode(); | 523 return _parseNode(); |
524 } | 524 } |
525 } | 525 } |
526 | 526 |
527 _scanner.scan(); | 527 _scanner.scan(); |
528 _state = _states.removeLast(); | 528 _state = _states.removeLast(); |
529 return new Event(EventType.SEQUENCE_END, token.span); | 529 return new Event(EventType.SEQUENCE_END, token.span); |
530 } | 530 } |
531 | 531 |
532 /// Parses the productions: | 532 /// Parses the productions: |
533 /// | 533 /// |
534 /// flow_sequence_entry ::= | 534 /// flow_sequence_entry ::= |
535 /// flow_node | KEY flow_node? (VALUE flow_node?)? | 535 /// flow_node | KEY flow_node? (VALUE flow_node?)? |
536 /// *** * | 536 /// *** * |
537 Event _parseFlowSequenceEntryMappingKey() { | 537 Event _parseFlowSequenceEntryMappingKey() { |
538 var token = _scanner.peek(); | 538 var token = _scanner.peek(); |
539 | 539 |
540 if (token.type == TokenType.VALUE || | 540 if (token.type == TokenType.VALUE || |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 | 663 |
664 /// Generate an empty scalar event. | 664 /// Generate an empty scalar event. |
665 Event _processEmptyScalar(SourceLocation location) => | 665 Event _processEmptyScalar(SourceLocation location) => |
666 new ScalarEvent(location.pointSpan(), '', ScalarStyle.PLAIN); | 666 new ScalarEvent(location.pointSpan(), '', ScalarStyle.PLAIN); |
667 | 667 |
668 /// Parses directives. | 668 /// Parses directives. |
669 Pair<VersionDirective, List<TagDirective>> _processDirectives() { | 669 Pair<VersionDirective, List<TagDirective>> _processDirectives() { |
670 var token = _scanner.peek(); | 670 var token = _scanner.peek(); |
671 | 671 |
672 var versionDirective; | 672 var versionDirective; |
673 var tagDirectives = []; | 673 var tagDirectives = <TagDirective>[]; |
674 while (token.type == TokenType.VERSION_DIRECTIVE || | 674 while (token.type == TokenType.VERSION_DIRECTIVE || |
675 token.type == TokenType.TAG_DIRECTIVE) { | 675 token.type == TokenType.TAG_DIRECTIVE) { |
676 if (token is VersionDirectiveToken) { | 676 if (token is VersionDirectiveToken) { |
677 if (versionDirective != null) { | 677 if (versionDirective != null) { |
678 throw new YamlException("Duplicate %YAML directive.", token.span); | 678 throw new YamlException("Duplicate %YAML directive.", token.span); |
679 } | 679 } |
680 | 680 |
681 if (token.major != 1 || token.minor == 0) { | 681 if (token.major != 1 || token.minor == 0) { |
682 throw new YamlException( | 682 throw new YamlException( |
683 "Incompatible YAML document. This parser only supports YAML 1.1 " | 683 "Incompatible YAML document. This parser only supports YAML 1.1 " |
684 "and 1.2.", | 684 "and 1.2.", |
685 token.span); | 685 token.span); |
686 } else if (token.minor > 2) { | 686 } else if (token.minor > 2) { |
687 // TODO(nweiz): Print to stderr when issue 6943 is fixed and dart:io | 687 // TODO(nweiz): Print to stderr when issue 6943 is fixed and dart:io |
688 // is available. | 688 // is available. |
689 warn("Warning: this parser only supports YAML 1.1 and 1.2.", | 689 warn("Warning: this parser only supports YAML 1.1 and 1.2.", |
690 token.span); | 690 token.span); |
691 } | 691 } |
692 | 692 |
693 versionDirective = new VersionDirective(token.major, token.minor); | 693 versionDirective = new VersionDirective(token.major, token.minor); |
694 } else if (token is TagDirectiveToken) { | 694 } else if (token is TagDirectiveToken) { |
695 var tagDirective = new TagDirective(token.handle, token.prefix); | 695 var tagDirective = new TagDirective(token.handle, token.prefix); |
696 _appendTagDirective(tagDirective, token.span); | 696 _appendTagDirective(tagDirective, token.span); |
697 tagDirectives.add(tagDirective); | 697 tagDirectives.add(tagDirective); |
698 } | 698 } |
699 | 699 |
700 token = _scanner.advance(); | 700 token = _scanner.advance(); |
701 } | 701 } |
702 | 702 |
703 _appendTagDirective( | 703 _appendTagDirective( |
704 new TagDirective("!", "!"), | 704 new TagDirective("!", "!"), |
705 token.span.start.pointSpan(), | 705 token.span.start.pointSpan(), |
706 allowDuplicates: true); | 706 allowDuplicates: true); |
707 _appendTagDirective( | 707 _appendTagDirective( |
708 new TagDirective("!!", "tag:yaml.org,2002:"), | 708 new TagDirective("!!", "tag:yaml.org,2002:"), |
709 token.span.start.pointSpan(), | 709 token.span.start.pointSpan(), |
710 allowDuplicates: true); | 710 allowDuplicates: true); |
711 | 711 |
712 return new Pair(versionDirective, tagDirectives); | 712 return new Pair(versionDirective, tagDirectives); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 | 807 |
808 /// Expect nothing. | 808 /// Expect nothing. |
809 static const END = const _State("END"); | 809 static const END = const _State("END"); |
810 | 810 |
811 final String name; | 811 final String name; |
812 | 812 |
813 const _State(this.name); | 813 const _State(this.name); |
814 | 814 |
815 String toString() => name; | 815 String toString() => name; |
816 } | 816 } |
OLD | NEW |