rustfst/parsers/
nom_utils.rs

1use nom::bytes::complete::take_while;
2use nom::character::complete::digit1;
3use nom::combinator::map_res;
4use nom::IResult;
5use std::str::FromStr;
6
7use nom::error::ErrorKind;
8use nom::error::{FromExternalError, ParseError};
9
10#[derive(Debug, PartialEq)]
11pub enum NomCustomError<I> {
12    SymbolTableError(String),
13    Nom(I, ErrorKind),
14}
15
16impl<I> ParseError<I> for NomCustomError<I> {
17    fn from_error_kind(input: I, kind: ErrorKind) -> Self {
18        NomCustomError::Nom(input, kind)
19    }
20
21    fn append(_: I, _: ErrorKind, other: Self) -> Self {
22        other
23    }
24}
25
26impl<I, E> FromExternalError<I, E> for NomCustomError<I> {
27    fn from_external_error(input: I, kind: ErrorKind, _e: E) -> Self {
28        NomCustomError::Nom(input, kind)
29    }
30}
31
32pub fn num<V: FromStr>(i: &str) -> IResult<&str, V> {
33    map_res(digit1, |s: &str| s.parse())(i)
34}
35
36pub fn word(i: &str) -> IResult<&str, String> {
37    let (i, letters) = take_while(|c: char| (c != ' ') && (c != '\t') && (c != '\n'))(i)?;
38    Ok((i, letters.to_string()))
39}