-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8617216
Showing
6 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
target/ | ||
Cargo.lock | ||
.DS_Store | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "binator_nom" | ||
authors = ["Stargateur"] | ||
version = "0.0.0" | ||
description = "binator nom adaptater" | ||
license = "Zlib" | ||
repository = "https://github.com/binator/nom" | ||
readme = "readme.md" | ||
keywords = ["parsing", "combinator", "ascii"] | ||
categories = ["parsing"] | ||
edition = "2021" | ||
|
||
include = [ | ||
"readme.md", | ||
"license.md", | ||
"Cargo.toml", | ||
"rustfmt.toml", | ||
"src/**/*.rs", | ||
] | ||
|
||
[dependencies] | ||
nom = '7' | ||
binator_core = "0.0.2" | ||
|
||
[dev-dependencies] | ||
binator_context = { git = "https://github.com/binator/context.git" } | ||
pretty_assertions = "1" | ||
derive-new = "0.5" | ||
derive_more = "0.99" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[zlib License](https://choosealicense.com/licenses/zlib/) | ||
|
||
(C) 2022-2024 Stargateur | ||
|
||
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. | ||
|
||
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: | ||
|
||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Simple binator parser to be able to call nom parser. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
condense_wildcard_suffixes = true | ||
fn_params_layout = "Compressed" | ||
format_code_in_doc_comments = true | ||
format_macro_matchers = true | ||
format_strings = true | ||
hex_literal_case = "Upper" | ||
imports_layout = "Vertical" | ||
merge_derives = false | ||
imports_granularity = "Crate" | ||
newline_style = "Unix" | ||
normalize_comments = true | ||
reorder_impl_items = true | ||
group_imports = "StdExternalCrate" | ||
tab_spaces = 2 | ||
use_field_init_shorthand = true | ||
version = "Two" | ||
use_try_shorthand = true | ||
wrap_comments = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
#![doc = include_str!("../readme.md")] | ||
#![cfg_attr(not(test), no_std)] | ||
#![warn(missing_docs)] | ||
#![deny(clippy::default_numeric_fallback)] | ||
|
||
use core::{ | ||
fmt::{ | ||
Display, | ||
Formatter, | ||
}, | ||
marker::PhantomData, | ||
}; | ||
|
||
use binator_core::{ | ||
Contexting, | ||
Parse, | ||
Parsed, | ||
}; | ||
use nom::{ | ||
Err, | ||
Needed, | ||
}; | ||
|
||
/// nom error context | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum NomAtom<Error> { | ||
/// When nom parser reported incomplete | ||
Incomplete { | ||
/// How many octet nom parser need to complete its work | ||
needed: Needed, | ||
}, | ||
/// When nom parser reported error | ||
Error { | ||
/// Error produced by Nom parser. | ||
error: Error, | ||
}, | ||
} | ||
|
||
impl<Error: Display> Display for NomAtom<Error> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
Self::Incomplete { needed } => write!(f, "Nom: Incomplete {:?}", needed), | ||
Self::Error { error } => write!(f, "Nom: Error {}", error), | ||
} | ||
} | ||
} | ||
|
||
/// Parser for nom trait. | ||
#[derive(Clone)] | ||
pub struct NomParser<Token, Parser, Error> { | ||
error: PhantomData<Error>, | ||
parser: Parser, | ||
token: PhantomData<Token>, | ||
} | ||
|
||
/// Nom trait allow to transform any Nom parser into Binator parser. | ||
pub trait Nom<Token, Stream, Parser, Error>: Sized | ||
where | ||
Parser: nom::Parser<Stream, Token, Error>, | ||
{ | ||
/// nom method transform any Nom parser into Binator parser. | ||
/// If nom return incomplete it's considered as irrocoverable error | ||
/// if you want to try to ask the stream to buffer more data you will | ||
/// need to do it yourself after catching the error. | ||
fn nom(self) -> NomParser<Token, Self, Error>; | ||
} | ||
|
||
impl<Token, Stream, Context, Parser, Error> Parse<Stream, Context> | ||
for NomParser<Token, Parser, Error> | ||
where | ||
Parser: nom::Parser<Stream, Token, Error>, | ||
Context: Contexting<NomAtom<Error>>, | ||
{ | ||
type Token = Token; | ||
|
||
fn parse(&mut self, stream: Stream) -> Parsed<Token, Stream, Context> { | ||
match self.parser.parse(stream) { | ||
Ok((stream, token)) => Parsed::Success { token, stream }, | ||
Err(Err::Error(error)) => Parsed::Failure(Context::new(NomAtom::Error { error })), | ||
Err(Err::Incomplete(needed)) => Parsed::Error(Context::new(NomAtom::Incomplete { needed })), | ||
Err(Err::Failure(error)) => Parsed::Error(Context::new(NomAtom::Error { error })), | ||
} | ||
} | ||
} | ||
|
||
impl<Token, Stream, Parser, Error> Nom<Token, Stream, Parser, Error> for Parser | ||
where | ||
Parser: nom::Parser<Stream, Token, Error>, | ||
{ | ||
fn nom(self) -> NomParser<Token, Self, Error> { | ||
NomParser { | ||
error: PhantomData::default(), | ||
parser: self, | ||
token: PhantomData::default(), | ||
} | ||
} | ||
} | ||
|
||
/// Take a nom parser and return a Binator parser | ||
pub fn nom<Token, Stream, Context, Parser, Error>( | ||
parser: Parser, | ||
) -> impl Parse<Stream, Context, Token = Token> | ||
where | ||
Parser: nom::Parser<Stream, Token, Error>, | ||
Context: Contexting<NomAtom<Error>>, | ||
{ | ||
parser.nom() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use binator_context::{ | ||
First, | ||
Keep, | ||
}; | ||
use binator_core::{ | ||
Contexting, | ||
Parse, | ||
Parsed, | ||
}; | ||
use derive_more::{ | ||
Display, | ||
From, | ||
}; | ||
use nom::{ | ||
bytes::complete::{ | ||
tag, | ||
take_while_m_n, | ||
}, | ||
combinator::map_res, | ||
error::{ | ||
Error, | ||
ErrorKind, | ||
}, | ||
sequence::tuple, | ||
IResult, | ||
}; | ||
|
||
use crate::{Nom, NomAtom}; | ||
|
||
#[derive(Display, Debug, PartialEq, From)] | ||
enum FromAtom { | ||
Nom(NomAtom<Error<&'static str>>), | ||
} | ||
|
||
type Context = Keep<First, FromAtom>; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Color { | ||
pub red: u8, | ||
pub green: u8, | ||
pub blue: u8, | ||
} | ||
|
||
fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> { | ||
u8::from_str_radix(input, 16) | ||
} | ||
|
||
fn is_hex_digit(c: char) -> bool { | ||
c.is_digit(16) | ||
} | ||
|
||
fn hex_primary(input: &str) -> IResult<&str, u8> { | ||
map_res(take_while_m_n(2, 2, is_hex_digit), from_hex)(input) | ||
} | ||
|
||
fn hex_color(input: &str) -> IResult<&str, Color> { | ||
let (input, _) = tag("#")(input)?; | ||
let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?; | ||
|
||
Ok((input, Color { red, green, blue })) | ||
} | ||
|
||
#[test] | ||
fn parse_color() { | ||
assert_eq!( | ||
super::nom::<_, _, Context, _, _>(hex_color).parse("#2F14DF"), | ||
Parsed::Success { | ||
stream: "", | ||
token: Color { | ||
red: 47, | ||
green: 20, | ||
blue: 223, | ||
} | ||
} | ||
); | ||
|
||
assert_eq!( | ||
hex_color.nom().parse("#2F14D"), | ||
Parsed::Failure(Context::new(NomAtom::Error { | ||
error: Error { | ||
input: "D", | ||
code: ErrorKind::TakeWhileMN, | ||
} | ||
})) | ||
); | ||
} | ||
} |