Skip to content

Commit 8147776

Browse files
committed
add Parser && YamlLoader
1 parent c32ea8e commit 8147776

File tree

15 files changed

+2324
-216
lines changed

15 files changed

+2324
-216
lines changed

LICENSE

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ SOFTWARE.
2424

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

27-
denoland/std
28-
2927
The original license is as follows:
3028

31-
MIT License
29+
The MIT License (MIT)
3230

33-
Copyright 2018-2022 the Deno authors.
31+
Copyright (c) 2015 Chen Yuheng
32+
Copyright (c) 2023 Ethiraric
3433

3534
Permission is hereby granted, free of charge, to any person obtaining a copy
3635
of this software and associated documentation files (the "Software"), to deal

README.mbt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# YAML
22

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

55
This library is ported from yaml-rust2.

moon.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "myfreess/yaml",
33
"version": "0.1.0",
44
"readme": "README.md",
5-
"repository": "",
5+
"repository": "https://github.com/moonbit-community/yaml.mbt",
66
"license": "MIT",
77
"keywords": [],
88
"description": "",

src/error.mbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
///|
2+
pub suberror YamlError {
3+
/// - `mark`: The position at which the error happened in the source.
4+
/// - `info`: Human-readable details about the error.
5+
YamlError(mark~ : Marker, info~ : String)
6+
} derive(Show)

src/event.mbt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)