使用带有 nom_locate 的 nom 有简短的错误消息

Have short error message using nom with nom_locate

我正在尝试在 this tutorial 之后使用带有 nom_locate 的 nom 进行解析。我只想要以下格式的输出:

Error: line 5, column 1

但目前我得到:

Error: Parsing Failure: ParseError { span: LocatedSpan { offset: 37, line: 5, fragment: "{{G]\nK(1)key [J]\n", extra: () }, message: "Error: line 5, column 1" }

相关代码:


pub type Span<'a> = LocatedSpan<&'a str>;
pub type IResult<'a, O> = nom::IResult<Span<'a>, O, ParseError<'a>>;


#[derive(Debug, PartialEq)]
pub struct ParseError<'a> {
    span: Span<'a>,
    message: String,
}

impl<'a> ParseError<'a> {
    pub fn new(message: String, span: Span<'a>) -> Self {
        Self {
            span,
            message,
        }
    }
}

impl<'a> nom::error::ParseError<Span<'a>> for ParseError<'a> {
    fn from_error_kind(input: Span<'a>, _: nom::error::ErrorKind) -> Self {
        Self::new(format!("Error: line {}, column {}", input.location_line(), input.location_line()), input)
    }

    fn append(input: Span<'a>, _: nom::error::ErrorKind, _: Self) -> Self {
        Self::new(format!("Error: line {}, column {}", input.location_line(), input.location_line()), input)
    }

    fn from_char(input: Span<'a>, _: char) -> Self {
        Self::new(format!("Error: line {}, column {}", input.location_line(), input.location_line()), input)
    }
}

pub fn job(s: Span) -> IResult<Vec<entities::Step>> {
    let (s, steps) = many1(job_lines)(s)?;
    Ok((s, steps))
}

fn job_lines(s: Span) -> IResult<entities::Step> {
    let (s, name) = match step_name(s) {
        Ok((s, name)) => (s, name),
        Err(nom::Err::Error(_)) => {
            let line = s.location_line();
            let column = s.get_column();
            return Err(nom::Err::Failure(ParseError::new(
                format!("Error: line {}, column {}", line, column),
                s,
            )));
        },
        Err(e) => return Err(e),
    };

    Ok((s, name))
}

main中的相关错误代码:

    let steps = match input::parsers::job(input::parsers::Span::new(&input)) {
        Ok((_, steps)) => steps,
        Err(error) => {
            eprintln!("{}", error);
            std::process::exit(1)
        }
    };

我需要做什么才能获得简短的错误消息而不是广泛的错误消息?

编辑: 我从自定义 ParseError 中删除了跨度 属性,然后错误输出变为:

Parsing Failure: ParseError { message: "Error: line 5, column 1" }

好多了,但问题仍然存在:除了消息本身,我可以得到所有内容吗?

正如您推测的那样,error 是一个 nom::Err::Err,它又是一个 enum,因此您需要匹配它才能得到内部错误:

    let steps = match input::parsers::job(input::parsers::Span::new(&input)) {
        Ok((_, steps)) => steps,
        Err(nom::Err::Failure (error)) => {
            eprintln!("{}", error.message);
            std::process::exit(1)
        },
        Err(error) => {
            eprintln!("Unknown error: {}", error);
            std::process::exit(1)
        },
    };