1
Fork 0

Update nom #14

Merged
lucas merged 3 commits from feat/dependencies into master 2025-04-21 22:35:43 +02:00
6 changed files with 87 additions and 65 deletions

View file

@ -4,6 +4,10 @@
## [Unreleased] - ReleaseDate ## [Unreleased] - ReleaseDate
### Changed
- update [nom](https://crates.io/crates/nom) to v8
## [1.2.0] - 2024-03-21 ## [1.2.0] - 2024-03-21
### Added ### Added

View file

@ -16,12 +16,12 @@ exclude = [
] ]
[dependencies] [dependencies]
nom = "7" nom = "8"
nom_locate = "4.1" nom_locate = "5"
serde = { version = "1.0", default-features = false } serde = { version = "1.0", default-features = false }
[dev-dependencies] [dev-dependencies]
serde = { version = "1.0.194", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
[badges] [badges]
maintenance = { status = "passively-maintained" } maintenance = { status = "passively-maintained" }

View file

@ -11,6 +11,10 @@ run *ARGS:
test *ARGS: test *ARGS:
cargo test {{ARGS}} cargo test {{ARGS}}
check:
cargo clippy -- -D warnings
cargo test
doc: doc:
cargo doc --no-deps cargo doc --no-deps
cargo readme > README.md cargo readme > README.md
@ -18,8 +22,13 @@ doc:
serve-doc port='8000': doc serve-doc port='8000': doc
python3 -m http.server {{port}} --directory target/doc python3 -m http.server {{port}} --directory target/doc
release version execute='': release version execute='': check build doc
cargo release --sign --allow-branch master {{ if execute != "" { '-x' } else { '' } }} {{version}} git fetch --all
[ "$(git rev-parse master)" = "$(git rev-parse origin/master)" ] \
|| (echo "error: master and origin/master differ" >&2; exit 1)
git branch -f release master
git checkout release
cargo release --sign --allow-branch release {{ if execute != "" { '-x' } else { '' } }} {{version}}
coverage *ARGS: coverage *ARGS:
RUSTFLAGS="-C instrument-coverage" cargo test --tests {{ARGS}} || true RUSTFLAGS="-C instrument-coverage" cargo test --tests {{ARGS}} || true

View file

@ -81,7 +81,7 @@ where
} }
} }
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { impl<'de> serde::de::Deserializer<'de> for &mut Deserializer<'de> {
type Error = Error; type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>

View file

@ -4,8 +4,8 @@ use nom::character::complete::{char, digit1, none_of, not_line_ending, one_of};
use nom::combinator::{cut, eof, map, map_res, opt, recognize, value}; use nom::combinator::{cut, eof, map, map_res, opt, recognize, value};
use nom::multi::many1_count; use nom::multi::many1_count;
use nom::number::complete::double; use nom::number::complete::double;
use nom::sequence::{delimited, preceded, terminated, tuple}; use nom::sequence::{delimited, preceded, terminated};
use nom::{IResult, Slice}; use nom::{IResult, Input as _, Parser as _};
use nom_locate::LocatedSpan; use nom_locate::LocatedSpan;
pub(crate) type Span<'a> = LocatedSpan<&'a str>; pub(crate) type Span<'a> = LocatedSpan<&'a str>;
@ -35,23 +35,25 @@ fn whitespace(input: Span) -> IResult<Span, char> {
} }
fn null(input: Span) -> IResult<Span, ()> { fn null(input: Span) -> IResult<Span, ()> {
value((), tag("null"))(input) value((), tag("null")).parse(input)
} }
fn separator(input: Span) -> IResult<Span, &str> { fn separator(input: Span) -> IResult<Span, &str> {
map(alt((tag(","), tag("\n"), tag("\r\n"))), |val: Span| { map(alt((tag(","), tag("\n"), tag("\r\n"))), |val: Span| {
*val.fragment() *val.fragment()
})(input) })
.parse(input)
} }
fn bool(input: Span) -> IResult<Span, bool> { fn bool(input: Span) -> IResult<Span, bool> {
alt((value(true, tag("true")), value(false, tag("false"))))(input) alt((value(true, tag("true")), value(false, tag("false")))).parse(input)
} }
fn integer(input: Span) -> IResult<Span, i64> { fn integer(input: Span) -> IResult<Span, i64> {
map_res(recognize(tuple((opt(char('-')), digit1))), |val: Span| { map_res(recognize((opt(char('-')), digit1)), |val: Span| {
val.fragment().parse::<i64>() val.fragment().parse::<i64>()
})(input) })
.parse(input)
} }
fn float(input: Span) -> IResult<Span, f64> { fn float(input: Span) -> IResult<Span, f64> {
@ -61,14 +63,16 @@ fn float(input: Span) -> IResult<Span, f64> {
fn identifier(input: Span) -> IResult<Span, &str> { fn identifier(input: Span) -> IResult<Span, &str> {
map(recognize(many1_count(none_of("\" \t\n=:"))), |val: Span| { map(recognize(many1_count(none_of("\" \t\n=:"))), |val: Span| {
*val.fragment() *val.fragment()
})(input) })
.parse(input)
} }
fn literal_string(input: Span) -> IResult<Span, &str> { fn literal_string(input: Span) -> IResult<Span, &str> {
map( map(
delimited(tag("\"\"\""), take_until("\"\"\""), tag("\"\"\"")), delimited(tag("\"\"\""), take_until("\"\"\""), tag("\"\"\"")),
|val: Span| *val.fragment(), |val: Span| *val.fragment(),
)(input) )
.parse(input)
} }
fn string_content(input: Span) -> IResult<Span, &str> { fn string_content(input: Span) -> IResult<Span, &str> {
@ -84,49 +88,51 @@ fn string_content(input: Span) -> IResult<Span, &str> {
} }
'\n' if !escaped => { '\n' if !escaped => {
let err = nom::error::Error { let err = nom::error::Error {
input: input.slice(j..), input: input.take_from(j),
code: nom::error::ErrorKind::Char, code: nom::error::ErrorKind::Char,
}; };
return Err(nom::Err::Error(err)); return Err(nom::Err::Error(err));
} }
'"' if !escaped => { '"' if !escaped => {
return Ok((input.slice(j..), &buf[0..j])); return Ok((input.take_from(j), &buf[0..j]));
} }
_ => escaped = false, _ => escaped = false,
} }
} }
let err = nom::error::Error { let err = nom::error::Error {
input: input.slice((i + 1)..), input: input.take_from(i + 1),
code: nom::error::ErrorKind::Char, code: nom::error::ErrorKind::Char,
}; };
Err(nom::Err::Failure(err)) Err(nom::Err::Failure(err))
} }
fn delimited_string(input: Span) -> IResult<Span, &str> { fn delimited_string(input: Span) -> IResult<Span, &str> {
preceded(char('"'), cut(terminated(string_content, char('"'))))(input) preceded(char('"'), cut(terminated(string_content, char('"')))).parse(input)
} }
fn string(input: Span) -> IResult<Span, &str> { fn string(input: Span) -> IResult<Span, &str> {
alt((identifier, literal_string, delimited_string))(input) alt((identifier, literal_string, delimited_string)).parse(input)
} }
fn line_comment(input: Span) -> IResult<Span, &str> { fn line_comment(input: Span) -> IResult<Span, &str> {
map( map(
preceded(tag("//"), alt((not_line_ending, eof))), preceded(tag("//"), alt((not_line_ending, eof))),
|val: Span| *val.fragment(), |val: Span| *val.fragment(),
)(input) )
.parse(input)
} }
fn block_comment(input: Span) -> IResult<Span, &str> { fn block_comment(input: Span) -> IResult<Span, &str> {
map( map(
delimited(tag("/*"), take_until("*/"), tag("*/")), delimited(tag("/*"), take_until("*/"), tag("*/")),
|val: Span| *val.fragment(), |val: Span| *val.fragment(),
)(input) )
.parse(input)
} }
fn comment(input: Span) -> IResult<Span, &str> { fn comment(input: Span) -> IResult<Span, &str> {
alt((line_comment, block_comment))(input) alt((line_comment, block_comment)).parse(input)
} }
fn optional(input: Span) -> IResult<Span, ()> { fn optional(input: Span) -> IResult<Span, ()> {
@ -135,7 +141,7 @@ fn optional(input: Span) -> IResult<Span, ()> {
let empty = value((), tag("")); let empty = value((), tag(""));
let content = value((), many1_count(alt((whitespace, comment)))); let content = value((), many1_count(alt((whitespace, comment))));
alt((content, empty))(input) alt((content, empty)).parse(input)
} }
pub(crate) fn parse_next_token(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_next_token(input: Span) -> IResult<Span, Token> {
@ -159,45 +165,48 @@ pub(crate) fn parse_next_token(input: Span) -> IResult<Span, Token> {
map(float, Token::Float), map(float, Token::Float),
map(string, |val| Token::String(val.to_string())), map(string, |val| Token::String(val.to_string())),
)), )),
)(input) )
.parse(input)
} }
pub(crate) fn parse_trailing_characters(input: Span) -> IResult<Span, ()> { pub(crate) fn parse_trailing_characters(input: Span) -> IResult<Span, ()> {
value((), optional)(input) value((), optional).parse(input)
} }
pub(crate) fn parse_null(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_null(input: Span) -> IResult<Span, Token> {
preceded(optional, value(Token::Null, null))(input) preceded(optional, value(Token::Null, null)).parse(input)
} }
pub(crate) fn parse_separator(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_separator(input: Span) -> IResult<Span, Token> {
preceded( preceded(
opt(horizontal_whitespace), opt(horizontal_whitespace),
value(Token::Separator, separator), value(Token::Separator, separator),
)(input) )
.parse(input)
} }
pub(crate) fn parse_bool(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_bool(input: Span) -> IResult<Span, Token> {
preceded(optional, map(bool, Token::Boolean))(input) preceded(optional, map(bool, Token::Boolean)).parse(input)
} }
pub(crate) fn parse_integer(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_integer(input: Span) -> IResult<Span, Token> {
preceded(optional, map(integer, Token::Integer))(input) preceded(optional, map(integer, Token::Integer)).parse(input)
} }
pub(crate) fn parse_float(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_float(input: Span) -> IResult<Span, Token> {
preceded(optional, map(float, Token::Float))(input) preceded(optional, map(float, Token::Float)).parse(input)
} }
pub(crate) fn parse_identifier(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_identifier(input: Span) -> IResult<Span, Token> {
preceded( preceded(
optional, optional,
map(identifier, |val| Token::String(val.to_string())), map(identifier, |val| Token::String(val.to_string())),
)(input) )
.parse(input)
} }
pub(crate) fn parse_string(input: Span) -> IResult<Span, Token> { pub(crate) fn parse_string(input: Span) -> IResult<Span, Token> {
preceded(optional, map(string, |val| Token::String(val.to_string())))(input) preceded(optional, map(string, |val| Token::String(val.to_string()))).parse(input)
} }
#[cfg(test)] #[cfg(test)]

View file

@ -84,7 +84,7 @@ where
} }
} }
impl<'a, W> serde::ser::Serializer for &'a mut Serializer<W> impl<W> serde::ser::Serializer for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
@ -216,9 +216,9 @@ where
self.serialize_unit() self.serialize_unit()
} }
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok> fn serialize_some<T>(self, value: &T) -> Result<Self::Ok>
where where
T: serde::Serialize, T: serde::Serialize + ?Sized,
{ {
self.ensure_top_level_struct()?; self.ensure_top_level_struct()?;
@ -245,9 +245,9 @@ where
self.serialize_str(variant) self.serialize_str(variant)
} }
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> Result<Self::Ok> fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Self::Ok>
where where
T: serde::Serialize, T: serde::Serialize + ?Sized,
{ {
self.ensure_top_level_struct()?; self.ensure_top_level_struct()?;
@ -255,7 +255,7 @@ where
} }
// Serialize an externally tagged enum: `{ NAME = VALUE }`. // Serialize an externally tagged enum: `{ NAME = VALUE }`.
fn serialize_newtype_variant<T: ?Sized>( fn serialize_newtype_variant<T>(
self, self,
_name: &'static str, _name: &'static str,
_variant_index: u32, _variant_index: u32,
@ -263,7 +263,7 @@ where
value: &T, value: &T,
) -> Result<Self::Ok> ) -> Result<Self::Ok>
where where
T: serde::Serialize, T: serde::Serialize + ?Sized,
{ {
self.ensure_top_level_struct()?; self.ensure_top_level_struct()?;
@ -345,24 +345,24 @@ where
Ok(self) Ok(self)
} }
fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok> fn collect_str<T>(self, value: &T) -> Result<Self::Ok>
where where
T: std::fmt::Display, T: std::fmt::Display + ?Sized,
{ {
self.serialize_str(&value.to_string()) self.serialize_str(&value.to_string())
} }
} }
impl<'a, W> serde::ser::SerializeSeq for &'a mut Serializer<W> impl<W> serde::ser::SerializeSeq for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()> fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
value.serialize(&mut **self)?; value.serialize(&mut **self)?;
@ -376,16 +376,16 @@ where
} }
} }
impl<'a, W> serde::ser::SerializeTuple for &'a mut Serializer<W> impl<W> serde::ser::SerializeTuple for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()> fn serialize_element<T>(&mut self, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
value.serialize(&mut **self)?; value.serialize(&mut **self)?;
@ -399,16 +399,16 @@ where
} }
} }
impl<'a, W> serde::ser::SerializeTupleStruct for &'a mut Serializer<W> impl<W> serde::ser::SerializeTupleStruct for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()> fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
value.serialize(&mut **self)?; value.serialize(&mut **self)?;
@ -422,16 +422,16 @@ where
} }
} }
impl<'a, W> serde::ser::SerializeTupleVariant for &'a mut Serializer<W> impl<W> serde::ser::SerializeTupleVariant for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()> fn serialize_field<T>(&mut self, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
value.serialize(&mut **self)?; value.serialize(&mut **self)?;
@ -454,24 +454,24 @@ where
} }
} }
impl<'a, W> serde::ser::SerializeMap for &'a mut Serializer<W> impl<W> serde::ser::SerializeMap for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()> fn serialize_key<T>(&mut self, key: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
key.serialize(&mut **self) key.serialize(&mut **self)
} }
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<()> fn serialize_value<T>(&mut self, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
// It doesn't make a difference where the `=` is added. But doing it here // It doesn't make a difference where the `=` is added. But doing it here
// means `serialize_key` is only a call to a different function, which should // means `serialize_key` is only a call to a different function, which should
@ -491,16 +491,16 @@ where
} }
} }
impl<'a, W> serde::ser::SerializeStruct for &'a mut Serializer<W> impl<W> serde::ser::SerializeStruct for &mut Serializer<W>
where where
W: io::Write, W: io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<()> fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
key.serialize(&mut **self)?; key.serialize(&mut **self)?;
@ -521,16 +521,16 @@ where
} }
} }
impl<'a, W> serde::ser::SerializeStructVariant for &'a mut Serializer<W> impl<W> serde::ser::SerializeStructVariant for &mut Serializer<W>
where where
W: std::io::Write, W: std::io::Write,
{ {
type Ok = (); type Ok = ();
type Error = Error; type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<()> fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
where where
T: Serialize, T: Serialize + ?Sized,
{ {
self.add_indent()?; self.add_indent()?;
key.serialize(&mut **self)?; key.serialize(&mut **self)?;