Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ SOFTWARE.

This project contains code from the following third-party project, which is licensed under the MIT License:

denoland/std

The original license is as follows:

MIT License
The MIT License (MIT)

Copyright 2018-2022 the Deno authors.
Copyright (c) 2015 Chen Yuheng
Copyright (c) 2023 Ethiraric

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.mbt.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# YAML

A comprehensive YAML parsing and stringifying library for MoonBit, supporting YAML 1.2.
A simple YAML parsing and stringifying library for MoonBit, support a simplified YAML subset which can be convert to JSON.

This library is ported from yaml-rust2.
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "myfreess/yaml",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"repository": "https://github.com/moonbit-community/yaml.mbt",
"license": "MIT",
"keywords": [],
"description": "",
Expand Down
6 changes: 6 additions & 0 deletions src/error.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
///|
pub suberror YamlError {
/// - `mark`: The position at which the error happened in the source.
/// - `info`: Human-readable details about the error.
YamlError(mark~ : Marker, info~ : String)
} derive(Show)
92 changes: 92 additions & 0 deletions src/event.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
///|
pub enum Event {
/// Event generated at the very beginning of parsing.
StreamStart
/// Last event that will be generated by the parser. Signals EOF.
StreamEnd
/// The YAML start document directive (`---`).
DocumentStart
/// The YAML end document directive (`...`).
DocumentEnd
/// A YAML Alias.
/// - id : The anchor ID the alias refers to.
Alias(id~ : Int)
/// Value, style, anchor id, tag
Scalar(value~ : String, style~ : TScalarStyle, id~ : Int, tag~ : Tag?)
/// The start of a YAML sequence (array).
/// - id : The anchor ID of the start of the sequence.
/// - tag : An optional tag
SequenceStart(id~ : Int, tag~ : Tag?)
/// The end of a YAML sequence (array).
SequenceEnd
/// The start of a YAML mapping (object, hash).
/// - id : The anchor ID of the start of the mapping.
/// - tag : An optional tag
MappingStart(id~ : Int, tag~ : Tag?)
/// The end of a YAML mapping (object, hash).
MappingEnd
} derive(Eq, Show)

///|
fn Event::empty_scalar() -> Event {
Event::Scalar(value="", style=TScalarStyle::Plain, id=0, tag=None)
}

///|
fn Event::empty_scalar_with_anchor(anchor : Int, tag : Tag?) -> Event {
Event::Scalar(value="", style=TScalarStyle::Plain, id=anchor, tag~)
}

///|
/// Trait to be implemented in order to use the low-level parsing API.
///
/// The low-level parsing API is event-based (a push parser), calling [`EventReceiver::on_event`]
/// for each YAML [`Event`] that occurs.
/// The [`EventReceiver`] trait only receives events. In order to receive both events and their
/// location in the source, use [`MarkedEventReceiver`]. Note that [`EventReceiver`]s implement
/// [`MarkedEventReceiver`] automatically.
///
/// # Event hierarchy
/// The event stream starts with an [`Event::StreamStart`] event followed by an
/// [`Event::DocumentStart`] event. If the YAML document starts with a mapping (an object), an
/// [`Event::MappingStart`] event is emitted. If it starts with a sequence (an array), an
/// [`Event::SequenceStart`] event is emitted. Otherwise, an [`Event::Scalar`] event is emitted.
///
/// In a mapping, key-values are sent as consecutive events. The first event after an
/// [`Event::MappingStart`] will be the key, and following its value. If the mapping contains no
/// sub-mapping or sub-sequence, then even events (starting from 0) will always be keys and odd
/// ones will always be values. The mapping ends when an [`Event::MappingEnd`] event is received.
///
/// In a sequence, values are sent consecutively until the [`Event::SequenceEnd`] event.
///
/// If a value is a sub-mapping or a sub-sequence, an [`Event::MappingStart`] or
/// [`Event::SequenceStart`] event will be sent respectively. Following events until the associated
/// [`Event::MappingStart`] or [`Event::SequenceEnd`] (beware of nested mappings or sequences) will
/// be part of the value and not another key-value pair or element in the sequence.
///
/// For instance, the following yaml:
/// ```yaml
/// a: b
/// c:
/// d: e
/// f:
/// - g
/// - h
/// ```
/// will emit (indented and commented for lisibility):
/// ```text
/// StreamStart, DocumentStart, MappingStart,
/// Scalar("a", ..), Scalar("b", ..)
/// Scalar("c", ..), MappingStart, Scalar("d", ..), Scalar("e", ..), MappingEnd,
/// Scalar("f", ..), SequenceStart, Scalar("g", ..), Scalar("h", ..), SequenceEnd,
/// MappingEnd, DocumentEnd, StreamEnd
/// ```
pub trait EventReceiver {
/// Handler called for each YAML event that is emitted by the parser.
on_event(Self, event : Event) -> Unit
}

///|
pub trait MarkedEventReceiver {
on_event(Self, event : Event, _mark : Marker) -> Unit
}
Loading