some pretty bad parsing

looking-at-specs
mitchellhansen 4 years ago
parent 59a945b474
commit b80a87dd18

@ -1,11 +1,13 @@
use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, take, take_till, take_until, take_while};
use nom::bytes::complete::{tag, take_while1, take_while_m_n};
use nom::character::complete::{char, one_of, newline, not_line_ending, anychar};
use nom::character::complete::{anychar, char, line_ending, newline, not_line_ending, one_of};
use nom::character::complete::alphanumeric1 as alphanumeric;
use nom::character::is_alphabetic;
use nom::combinator::{cut, map, map_res, opt};
use nom::error::ParseError;
use nom::IResult;
use nom::multi::many0;
use nom::number::complete::be_u16;
use nom::sequence::{delimited, preceded, terminated, tuple};
@ -50,30 +52,24 @@ pub fn hex_color(input: &str) -> IResult<&str, Color> {
}
pub fn elem_tag(input: &str) -> IResult<&str, &str> {
let (input, _) = tag("elem")(input)?;
let input = sp(input)?;
Ok((input.0, input.0))
let (input, _) = delimited(opt(sp), tag("elem"), sp)(input)?;
Ok((input, input))
}
fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let chars = "\n";
escaped(anychar, '\\', one_of(""))(i)
escaped(alphanumeric, '\\', one_of(""))(i)
}
pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> {
// if let Ok(t) = preceded(sp, char('#'))(input) {
// let v = take_until("\n")(t.0).unwrap();
// println!("{:?}", t);
// }
let v = preceded(char('#'),
cut(
terminated(
parse_str,
newline,
)
),
cut(terminated(
is_not("\n"),
newline,
)),
)(input)?;
println!("{:?}", v);
@ -97,7 +93,6 @@ fn sp<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
}
pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ScriptMeta, E> {
let x = delimited(
sp,
alt((map(comment, |s| ScriptMeta::Comment(String::from(s))),
@ -106,6 +101,19 @@ pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a s
opt(sp),
)(input);
let x = x.unwrap().0;
println!("{:?}", x);
let x = delimited(
sp,
alt((map(comment, |s| ScriptMeta::Comment(String::from(s))),
map(elem_tag, |s| ScriptMeta::Element(String::from(s)))
)),
opt(sp),
)(x);
let x = x.unwrap().0;
println!("{:?}", x);
// if let Ok(v) = elem_tag(input) {
// println!("Found elem tag");
// if let Ok(v) = sp(v.0) {
@ -120,7 +128,6 @@ pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a s
// println!("Found comment tag")
// }
let x = x.unwrap().0;
return Ok((x, ScriptMeta::Comment(String::default())));
}

Loading…
Cancel
Save