Created
July 3, 2019 11:57
-
-
Save kali/dad3c1b4b6fa69782f3edab9d0a22b28 to your computer and use it in GitHub Desktop.
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
// should recognize: | |
// input-node name=foo dim=12 | |
// i want to assume both are here, but not necessarily in this order | |
// i have similar case with more numrous foo=bar cases | |
fn parse_input_node_line(i: &str) -> IResult<&str, (String, usize)> { | |
use crate::parser::spaced; | |
use std::cell::Cell; | |
let name: Cell<Option<String>> = Cell::new(None); | |
let dim: Cell<Option<usize>> = Cell::new(None); | |
let (i, _) = tag("input-node")(i)?; | |
let (i, _) = many1(spaced(alt(( | |
map(preceded(tag("name="), identifier), |n| { | |
name.set(Some(n.to_string())); | |
}), | |
map(preceded(tag("dim="), digit1), |n: &str| { | |
dim.set(Some(n.parse().unwrap())); | |
}), | |
))))(i)?; | |
match (name.into_inner(), dim.into_inner()) { | |
(Some(name), Some(dim)) => Ok((i, (name, dim))), | |
(None, _) => panic!("expect name"), | |
(_, None) => panic!("expect dim"), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment