-
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
1 parent
e9c8fc0
commit 14d8b86
Showing
5 changed files
with
112 additions
and
44 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
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
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,31 @@ | ||
use nom::character::complete::space1; | ||
use nom::combinator::map; | ||
use nom::multi::separated_list1; | ||
use nom::*; | ||
use nom::error::{Error, ParseError}; | ||
use nom_parse_trait::{ParseFrom, ParseFromExt}; | ||
|
||
struct Numbers(Vec<u32>); | ||
|
||
impl<I, E: ParseError<I>> ParseFrom<I, E> for Numbers | ||
where | ||
// From separated_list1 | ||
I: Clone + InputLength, | ||
// From space1 | ||
I: InputTakeAtPosition, | ||
<I as InputTakeAtPosition>::Item: AsChar + Clone, | ||
// From u32::parse | ||
I: InputIter + Slice<std::ops::RangeFrom<usize>> + InputLength, | ||
<I as InputIter>::Item: AsChar, | ||
{ | ||
fn parse(input: I) -> IResult<I, Self, E> { | ||
map(separated_list1(space1, u32::parse), |v| Numbers(v)).parse(input) | ||
} | ||
} | ||
|
||
fn main() { | ||
let input = "1 2 3 4 5"; | ||
if let Ok::<_, Error<_>>(numbers) = Numbers::parse_complete(input) { | ||
println!("Parsed \"{}\" into {:?}", input, numbers.0); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
use nom::character::complete::space1; | ||
use nom::combinator::map; | ||
use nom::error::Error; | ||
use nom::multi::separated_list1; | ||
use nom::{IResult, Parser}; | ||
use nom_parsable::Parsable; | ||
use nom::{ | ||
IResult, | ||
Parser, | ||
}; | ||
use nom_parse_trait::{ParseFrom, ParseFromExt}; | ||
|
||
struct Numbers(Vec<u32>); | ||
|
||
impl Parsable for Numbers { | ||
fn parse(input: &str) -> IResult<&str, Self, Error<&str>> { | ||
impl<'a> ParseFrom<&'a str> for Numbers { | ||
fn parse(input: &'a str) -> IResult<&'a str, Self> { | ||
map(separated_list1(space1, u32::parse), |v| Numbers(v)).parse(input) | ||
} | ||
} | ||
|
||
fn main() { | ||
let input = "1 2 3 4 5"; | ||
if let Ok( numbers) = Numbers::parse_complete(input) { | ||
if let Ok(numbers) = Numbers::parse_complete(input) { | ||
println!("Parsed \"{}\" into {:?}", input, numbers.0); | ||
} | ||
} |
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