take_until
45 | pub fn alt<I: Clone, O, E: ParseError<I>, List: Alt<I, O, E>>(l: List) -> impl Fn(I) -> IResult<I, O, E> {
| ------------ required by this bound in `nom::branch::alt`
|
= note: expected enum `std::result::Result<(&str, &str), nom::internal::Err<(&str, nom::error::ErrorKind)>>`
found enum `std::result::Result<(&str, screen::settings::administration::parse_search::Criteria<'_>), nom::internal::Err<(&str, nom::error::ErrorKind)>>`
tuple
that returns a single slice
recognize
looks like it'll help
fn ident(s: &[u8]) -> IResult<&[u8], &[u8]> {
recognize(preceded(
|i| if let Some(&l) = s.get(0) {
if letter(l) {
Ok((&s[1..], ()))
} else {
Err(Err::Error(ParseError::from_error_kind(s, ErrorKind::OneOf)))
}
} else {
Err(Err::Incomplete(Needed::Size(1)))
},
take_while(|b| matches!(b, b'A'..=b'Z' | b'a'..=b'z' | b'_' | b'0'..=b'9'))
))(s)
}
alt
it says "with a custom error type, it is possible to have alt return the error of the parser that went the farthest in the input data" but there seems to not be any documentation on how to do this
Incomplete
, not an error
. How would I get my desired behaivior?
let (input, defs) = complete(many1(annotated_terminal(alt((
p_fn_named,
p_prim,
p_struct,
p_enum,
)))))(input)?;
alt((float_literal, int_literal))
.number::complete::double
to parse the float literal, but this caused trouble because double also accepts integers double("1") == 1.0
. Is there an easy way to fix this?
fn parse_date(input: &str) -> IResult<&str, DateTime<Utc>> {
DateTime::parse_from_str(input, "%F")
.map_err(|_| nom::Err::Error((input, /* here */)))
}
want to adapt this chrono parser so i dont have to DIY it. the %f may become more complex
I have some problems and I do not really understand what is wrong with my code:
use nom::IResult;
use crate::types::JavaVersion;
use nom::bits::complete::take;
use crate::constants::MAGIC_NUMBER;
use nom::error::context;
use crate::constants::sizes::{MINOR_SIZE, MAJOR_SIZE}; // both are assigned a value of 2 respectively
use nom::sequence::tuple;
use nom::combinator::map;
pub fn parse_version_info(i: &[u8]) -> IResult<&[u8], JavaVersion> {
map(
tuple((
take(MINOR_SIZE),
take(MAJOR_SIZE)
)),
|(minor, major)| {
JavaVersion {
major,
minor,
}
}
)(i)
}
#[cfg(test)]
mod tests {
use crate::parser::{parse_class_file, parse_version_info};
use byteorder::{WriteBytesExt, BigEndian};
fn int2bytes(i: i32) -> Vec<u8> {
let mut bs = [0u8; std::mem::size_of::<i32>()];
bs.as_mut()
.write_i32::<BigEndian>(i)
.expect("Failed to convert to byte array");
bs.into_vec()
}
#[test]
fn test_version_info() {
let i = 0x3200;
let i = int2bytes(i).as_slice();
let version = parse_version_info(i);
}
}
|
I get the following error:
error[E0308]: mismatched types
--> java-class-file\src\parser.rs:22:7
|
22 | )(i)
| ^ expected tuple, found `&[u8]`
|
= note: expected tuple `(_, usize)`
found reference `&[u8]`
error[E0308]: mismatched types
--> java-class-file\src\parser.rs:11:5
|
11 | / map(
12 | | tuple((
13 | | take(MINOR_SIZE),
14 | | take(MAJOR_SIZE)
... |
21 | | }
22 | | )(i)
| |________^ expected `&[u8]`, found tuple
|
= note: expected enum `std::result::Result<(&[u8], types::JavaVersion), nom::internal::Err<(&[u8], nom::error::ErrorKind)>>`
found enum `std::result::Result<((_, usize), types::JavaVersion), nom::internal::Err<_>>`
error: aborting due to 2 previous errors
Hi there! A newbie from Uruguay here.
I've been working on a modsecurity parser for some weeks and I'm really glad I chose nom for this project. But I'm quite stucked right now.
I want this parser to be able to parse comments. This should be a very simple task, just reading from '#' until EOL.
What is the recommended way to achieve this?
tag("\x01")
or tag([0x01])
for the time being?hey folks, I'm trying to write an s-expression parser and I have a function that looks like this:
delimited(
char('('),
list_content,
preceded(multispace0, char(')')),
)(input)
I want it to ignore whitespace and apparently that's causing issues. this function fails when called with the input ( 123 456 )
but doesn't when the input is ( 123 456 )
(note the extra space at the end!). any obvious reason why this might be? the error I get is: Err(Error(("", Char)))