|
| 1 | +///| |
| 2 | +pub enum Event { |
| 3 | + /// Event generated at the very beginning of parsing. |
| 4 | + StreamStart |
| 5 | + /// Last event that will be generated by the parser. Signals EOF. |
| 6 | + StreamEnd |
| 7 | + /// The YAML start document directive (`---`). |
| 8 | + DocumentStart |
| 9 | + /// The YAML end document directive (`...`). |
| 10 | + DocumentEnd |
| 11 | + /// A YAML Alias. |
| 12 | + /// - id : The anchor ID the alias refers to. |
| 13 | + Alias(id~ : Int) |
| 14 | + /// Value, style, anchor id, tag |
| 15 | + Scalar(value~ : String, style~ : TScalarStyle, id~ : Int, tag~ : Tag?) |
| 16 | + /// The start of a YAML sequence (array). |
| 17 | + /// - id : The anchor ID of the start of the sequence. |
| 18 | + /// - tag : An optional tag |
| 19 | + SequenceStart(id~ : Int, tag~ : Tag?) |
| 20 | + /// The end of a YAML sequence (array). |
| 21 | + SequenceEnd |
| 22 | + /// The start of a YAML mapping (object, hash). |
| 23 | + /// - id : The anchor ID of the start of the mapping. |
| 24 | + /// - tag : An optional tag |
| 25 | + MappingStart(id~ : Int, tag~ : Tag?) |
| 26 | + /// The end of a YAML mapping (object, hash). |
| 27 | + MappingEnd |
| 28 | +} derive(Eq, Show) |
| 29 | + |
| 30 | +///| |
| 31 | +fn Event::empty_scalar() -> Event { |
| 32 | + Event::Scalar(value="", style=TScalarStyle::Plain, id=0, tag=None) |
| 33 | +} |
| 34 | + |
| 35 | +///| |
| 36 | +fn Event::empty_scalar_with_anchor(anchor : Int, tag : Tag?) -> Event { |
| 37 | + Event::Scalar(value="", style=TScalarStyle::Plain, id=anchor, tag~) |
| 38 | +} |
| 39 | + |
| 40 | +///| |
| 41 | +/// Trait to be implemented in order to use the low-level parsing API. |
| 42 | +/// |
| 43 | +/// The low-level parsing API is event-based (a push parser), calling [`EventReceiver::on_event`] |
| 44 | +/// for each YAML [`Event`] that occurs. |
| 45 | +/// The [`EventReceiver`] trait only receives events. In order to receive both events and their |
| 46 | +/// location in the source, use [`MarkedEventReceiver`]. Note that [`EventReceiver`]s implement |
| 47 | +/// [`MarkedEventReceiver`] automatically. |
| 48 | +/// |
| 49 | +/// # Event hierarchy |
| 50 | +/// The event stream starts with an [`Event::StreamStart`] event followed by an |
| 51 | +/// [`Event::DocumentStart`] event. If the YAML document starts with a mapping (an object), an |
| 52 | +/// [`Event::MappingStart`] event is emitted. If it starts with a sequence (an array), an |
| 53 | +/// [`Event::SequenceStart`] event is emitted. Otherwise, an [`Event::Scalar`] event is emitted. |
| 54 | +/// |
| 55 | +/// In a mapping, key-values are sent as consecutive events. The first event after an |
| 56 | +/// [`Event::MappingStart`] will be the key, and following its value. If the mapping contains no |
| 57 | +/// sub-mapping or sub-sequence, then even events (starting from 0) will always be keys and odd |
| 58 | +/// ones will always be values. The mapping ends when an [`Event::MappingEnd`] event is received. |
| 59 | +/// |
| 60 | +/// In a sequence, values are sent consecutively until the [`Event::SequenceEnd`] event. |
| 61 | +/// |
| 62 | +/// If a value is a sub-mapping or a sub-sequence, an [`Event::MappingStart`] or |
| 63 | +/// [`Event::SequenceStart`] event will be sent respectively. Following events until the associated |
| 64 | +/// [`Event::MappingStart`] or [`Event::SequenceEnd`] (beware of nested mappings or sequences) will |
| 65 | +/// be part of the value and not another key-value pair or element in the sequence. |
| 66 | +/// |
| 67 | +/// For instance, the following yaml: |
| 68 | +/// ```yaml |
| 69 | +/// a: b |
| 70 | +/// c: |
| 71 | +/// d: e |
| 72 | +/// f: |
| 73 | +/// - g |
| 74 | +/// - h |
| 75 | +/// ``` |
| 76 | +/// will emit (indented and commented for lisibility): |
| 77 | +/// ```text |
| 78 | +/// StreamStart, DocumentStart, MappingStart, |
| 79 | +/// Scalar("a", ..), Scalar("b", ..) |
| 80 | +/// Scalar("c", ..), MappingStart, Scalar("d", ..), Scalar("e", ..), MappingEnd, |
| 81 | +/// Scalar("f", ..), SequenceStart, Scalar("g", ..), Scalar("h", ..), SequenceEnd, |
| 82 | +/// MappingEnd, DocumentEnd, StreamEnd |
| 83 | +/// ``` |
| 84 | +pub trait EventReceiver { |
| 85 | + /// Handler called for each YAML event that is emitted by the parser. |
| 86 | + on_event(Self, event : Event) -> Unit |
| 87 | +} |
| 88 | +
|
| 89 | +///| |
| 90 | +pub trait MarkedEventReceiver { |
| 91 | + on_event(Self, event : Event, _mark : Marker) -> Unit |
| 92 | +} |
0 commit comments