Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library yaml.event; | |
| 6 | |
| 7 import 'package:source_span/source_span.dart'; | |
| 8 | |
| 9 import 'style.dart'; | |
| 10 import 'yaml_document.dart'; | |
| 11 | |
| 12 /// An event emitted by a [Parser]. | |
| 13 class Event { | |
| 14 /// The event type. | |
| 15 final EventType type; | |
| 16 | |
| 17 /// The span associated with the event. | |
| 18 final FileSpan span; | |
| 19 | |
| 20 Event(this.type, this.span); | |
| 21 | |
| 22 String toString() => type.toString(); | |
| 23 } | |
| 24 | |
| 25 /// An event indicating the beginning of a YAML document. | |
| 26 class DocumentStartEvent implements Event { | |
| 27 final type = EventType.DOCUMENT_START; | |
|
Bob Nystrom
2014/10/31 20:03:27
It's more efficient to use getters, here and below
nweiz
2014/11/04 22:19:35
Done.
| |
| 28 final FileSpan span; | |
| 29 | |
| 30 /// The document's `%YAML` directive, or `null` if there was none. | |
| 31 final VersionDirective versionDirective; | |
| 32 | |
| 33 /// The document's `%TAG` directives, if any. | |
| 34 final List<TagDirective> tagDirectives; | |
| 35 | |
| 36 /// Whether the document started implicitly (that is, without an explicit | |
| 37 /// `===` sequence). | |
| 38 final bool implicit; | |
|
Bob Nystrom
2014/10/31 20:03:27
Maybe "isImplicit"?
nweiz
2014/11/04 22:19:35
Done.
| |
| 39 | |
| 40 DocumentStartEvent(this.span, {this.versionDirective, | |
| 41 List<TagDirective> tagDirectives, this.implicit: true}) | |
| 42 : tagDirectives = tagDirectives == null ? [] : tagDirectives; | |
| 43 | |
| 44 String toString() => "DOCUMENT_START"; | |
|
Bob Nystrom
2014/10/31 20:03:26
Why not just extend Event and use the inherited to
nweiz
2014/11/04 22:19:35
Because then we'd have to pass a type to its super
| |
| 45 } | |
| 46 | |
| 47 /// An event indicating the end of a YAML document. | |
| 48 class DocumentEndEvent implements Event { | |
| 49 final type = EventType.DOCUMENT_END; | |
| 50 final FileSpan span; | |
| 51 | |
| 52 /// Whether the document ended implicitly (that is, without an explicit | |
| 53 /// `...` sequence). | |
| 54 final bool implicit; | |
| 55 | |
| 56 DocumentEndEvent(this.span, {this.implicit: true}); | |
| 57 | |
| 58 String toString() => "DOCUMENT_END"; | |
| 59 } | |
| 60 | |
| 61 /// An event indicating that an alias was referenced. | |
| 62 class AliasEvent implements Event { | |
| 63 final type = EventType.ALIAS; | |
| 64 final FileSpan span; | |
| 65 | |
| 66 /// The name of the anchor. | |
| 67 final String name; | |
| 68 | |
| 69 AliasEvent(this.span, this.name); | |
| 70 | |
| 71 String toString() => "ALIAS $name"; | |
| 72 } | |
| 73 | |
| 74 /// A base class for events that can have anchor and tag properties associated | |
| 75 /// with them. | |
| 76 abstract class _ValueEvent implements Event { | |
| 77 /// The name of the value's anchor, or `null` if it wasn't anchored. | |
| 78 String get anchor; | |
| 79 | |
| 80 /// The text of the value's tag, or `null` if it wasn't tagged. | |
| 81 String get tag; | |
| 82 | |
| 83 String toString() { | |
| 84 var buffer = new StringBuffer('$type'); | |
| 85 if (anchor != null) buffer.write(" &$anchor"); | |
| 86 if (tag != null) buffer.write(" $tag"); | |
| 87 return buffer.toString(); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 /// An event indicating a single scalar value. | |
| 92 class ScalarEvent extends _ValueEvent { | |
| 93 final type = EventType.SCALAR; | |
| 94 final FileSpan span; | |
| 95 final String anchor; | |
| 96 final String tag; | |
| 97 | |
| 98 /// The contents of the scalar. | |
| 99 final String value; | |
| 100 | |
| 101 /// The style of the scalar in the original source. | |
| 102 final ScalarStyle style; | |
| 103 | |
| 104 ScalarEvent(this.span, this.value, this.style, {this.anchor, this.tag}); | |
| 105 | |
| 106 String toString() => "${super.toString()} \"$value\""; | |
| 107 } | |
| 108 | |
| 109 /// An event indicating the beginning of a sequence. | |
| 110 class SequenceStartEvent extends _ValueEvent { | |
| 111 final type = EventType.SEQUENCE_START; | |
| 112 final FileSpan span; | |
| 113 final String anchor; | |
| 114 final String tag; | |
| 115 | |
| 116 /// The style of the collection in the original source. | |
| 117 final CollectionStyle style; | |
| 118 | |
| 119 SequenceStartEvent(this.span, this.style, {this.anchor, this.tag}); | |
| 120 } | |
| 121 | |
| 122 /// An event indicating the beginning of a mapping. | |
| 123 class MappingStartEvent extends _ValueEvent { | |
| 124 final type = EventType.MAPPING_START; | |
| 125 final FileSpan span; | |
| 126 final String anchor; | |
| 127 final String tag; | |
| 128 | |
| 129 /// The style of the collection in the original source. | |
| 130 final CollectionStyle style; | |
| 131 | |
| 132 MappingStartEvent(this.span, this.style, {this.anchor, this.tag}); | |
| 133 } | |
| 134 | |
| 135 /// An enum of types of [Event] object. | |
| 136 class EventType { | |
| 137 static const STREAM_START = const EventType._("STREAM_START"); | |
| 138 static const STREAM_END = const EventType._("STREAM_END"); | |
| 139 | |
| 140 static const DOCUMENT_START = const EventType._("DOCUMENT_START"); | |
| 141 static const DOCUMENT_END = const EventType._("DOCUMENT_END"); | |
| 142 | |
| 143 static const ALIAS = const EventType._("ALIAS"); | |
| 144 static const SCALAR = const EventType._("SCALAR"); | |
| 145 | |
| 146 static const SEQUENCE_START = const EventType._("SEQUENCE_START"); | |
| 147 static const SEQUENCE_END = const EventType._("SEQUENCE_END"); | |
| 148 | |
| 149 static const MAPPING_START = const EventType._("MAPPING_START"); | |
| 150 static const MAPPING_END = const EventType._("MAPPING_END"); | |
| 151 | |
| 152 final String name; | |
| 153 | |
| 154 const EventType._(this.name); | |
| 155 | |
| 156 String toString() => name; | |
| 157 } | |
| OLD | NEW |