iowo/crates/json-pawarser/src/grammar.rs

126 lines
3.9 KiB
Rust
Raw Normal View History

2024-10-23 11:01:39 +00:00
use array::array;
2024-10-18 12:05:27 +00:00
use enumset::{enum_set, EnumSet};
use crate::{syntax_error::SyntaxError, syntax_kind::SyntaxKind};
use self::object::object;
pub(crate) type Parser<'src> = pawarser::Parser<'src, SyntaxKind, SyntaxError>;
pub(crate) type CompletedMarker = pawarser::CompletedMarker<SyntaxKind, SyntaxError>;
2024-10-18 12:05:27 +00:00
const BASIC_VALUE_TOKENS: EnumSet<SyntaxKind> =
enum_set!(SyntaxKind::BOOL | SyntaxKind::NULL | SyntaxKind::NUMBER | SyntaxKind::STRING);
pub fn value(p: &mut Parser) -> bool {
if BASIC_VALUE_TOKENS.contains(p.current()) {
p.do_bump();
return true;
} else {
2024-10-23 11:01:39 +00:00
object(p).or_else(|| array(p)).is_some()
2024-10-18 12:05:27 +00:00
}
}
mod object {
use crate::{grammar::value, syntax_error::SyntaxError, syntax_kind::SyntaxKind};
use super::{CompletedMarker, Parser, BASIC_VALUE_TOKENS};
pub(super) fn object(p: &mut Parser) -> Option<CompletedMarker> {
let obj_start = p.start("object");
if !p.eat(SyntaxKind::BRACE_OPEN) {
2024-10-18 12:05:27 +00:00
obj_start.abandon(p);
return None;
}
member(p);
while p.at(SyntaxKind::COMMA) {
// not always an error, later configurable
2024-10-23 11:01:39 +00:00
let potential_trailing_comma = p.start("potential_trailing_comma");
p.eat(SyntaxKind::COMMA);
if member(p).is_none() {
2024-10-23 11:01:39 +00:00
potential_trailing_comma.complete(p, SyntaxKind::TRAILING_COMMA);
} else {
2024-10-23 11:01:39 +00:00
potential_trailing_comma.abandon(p);
}
}
Some(if !p.eat(SyntaxKind::BRACE_CLOSE) {
obj_start.error(p, SyntaxError::UnclosedObject)
} else {
obj_start.complete(p, SyntaxKind::OBJECT)
})
2024-10-18 12:05:27 +00:00
}
fn member(p: &mut Parser) -> Option<CompletedMarker> {
let member_start = p.start("member");
if p.at(SyntaxKind::BRACE_CLOSE) {
member_start.abandon(p);
return None;
} else if p.at(SyntaxKind::STRING) {
let member_name_start = p.start("member_name");
p.eat(SyntaxKind::STRING);
member_name_start.complete(p, SyntaxKind::MEMBER_NAME);
} else {
return todo!("handle other tokens: {:?}", p.current());
2024-10-18 12:05:27 +00:00
}
if !p.eat(SyntaxKind::COLON) {
todo!("handle wrong tokens")
}
let member_value_start = p.start("member_value_start");
2024-10-18 12:05:27 +00:00
if value(p) {
member_value_start.complete(p, SyntaxKind::MEMBER_VALUE);
2024-10-18 12:05:27 +00:00
Some(member_start.complete(p, SyntaxKind::MEMBER))
} else {
member_value_start.abandon(p);
2024-10-18 12:05:27 +00:00
let e = member_start.error(p, SyntaxError::MemberMissingValue);
Some(
e.precede(p, "member but failed already")
.complete(p, SyntaxKind::MEMBER),
)
}
}
}
2024-10-23 11:01:39 +00:00
mod array {
use crate::{syntax_error::SyntaxError, syntax_kind::SyntaxKind};
use super::{value, CompletedMarker, Parser};
pub(super) fn array(p: &mut Parser) -> Option<CompletedMarker> {
let array_start = p.start("array");
if !p.eat(SyntaxKind::BRACKET_OPEN) {
array_start.abandon(p);
return None;
}
let el = p.start("arr_el");
value(p);
el.complete(p, SyntaxKind::ELEMENT);
while p.at(SyntaxKind::COMMA) {
let potential_trailing_comma = p.start("potential_trailing_comma");
p.eat(SyntaxKind::COMMA);
let maybe_el = p.start("arr_el");
if !value(p) {
maybe_el.abandon(p);
potential_trailing_comma.complete(p, SyntaxKind::TRAILING_COMMA);
} else {
maybe_el.complete(p, SyntaxKind::ELEMENT);
potential_trailing_comma.abandon(p);
}
}
Some(if !p.eat(SyntaxKind::BRACKET_CLOSE) {
array_start.error(p, SyntaxError::UnclosedArray)
} else {
array_start.complete(p, SyntaxKind::ARRAY)
})
}
}