for some reason can't parse a damn line

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

@ -1,6 +1,5 @@
# this is a comment # this is a comment
elem table : globalTableFormatting { elem table {
} }

@ -93,7 +93,6 @@ pub fn main() {
parse_script::<(&str, ErrorKind)>(&input_string); parse_script::<(&str, ErrorKind)>(&input_string);
return; return;
//hprof::start_frame(); //hprof::start_frame();

@ -1,12 +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::alphanumeric1 as alphanumeric;
use nom::combinator::{cut, map, map_res, opt};
use nom::error::ParseError;
use nom::IResult; use nom::IResult;
use nom::number::complete::be_u16; use nom::number::complete::be_u16;
use nom::bytes::complete::{take, is_not, take_while, escaped}; use nom::sequence::{delimited, preceded, terminated, tuple};
use nom::character::complete::{char, one_of};
use nom::bytes::complete::{take_while1, tag, take_while_m_n};
use nom::combinator::{map_res, opt, cut};
use nom::sequence::{preceded, tuple, delimited, terminated};
use nom::error::ParseError;
use nom::character::complete::{alphanumeric1 as alphanumeric};
pub fn length_value(input: &[u8]) -> IResult<&[u8], &[u8]> { pub fn length_value(input: &[u8]) -> IResult<&[u8], &[u8]> {
let (input, length) = be_u16(input)?; let (input, length) = be_u16(input)?;
@ -20,8 +21,10 @@ pub struct Color {
pub blue: u8, pub blue: u8,
} }
pub struct ScriptMeta { pub enum ScriptMeta {
Comment(String),
Element(String),
Meta(String),
} }
pub fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> { pub fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> {
@ -35,7 +38,7 @@ pub fn is_hex_digit(c: char) -> bool {
pub fn hex_primary(input: &str) -> IResult<&str, u8> { pub fn hex_primary(input: &str) -> IResult<&str, u8> {
map_res( map_res(
take_while_m_n(2, 2, is_hex_digit), take_while_m_n(2, 2, is_hex_digit),
from_hex from_hex,
)(input) )(input)
} }
@ -46,19 +49,36 @@ pub fn hex_color(input: &str) -> IResult<&str, Color> {
Ok((input, Color { red, green, blue })) Ok((input, Color { red, green, blue }))
} }
pub fn elem_tag(input: &str) -> IResult<&str, u8> { pub fn elem_tag(input: &str) -> IResult<&str, &str> {
let (input, _) = tag("elem")(input)?; let (input, _) = tag("elem")(input)?;
let input = sp(input)?; let input = sp(input)?;
Ok((input.0, 0)) Ok((input.0, input.0))
} }
pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, u8, E> { 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)
}
preceded(char('#'), cut(terminated(escaped( pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> {
alphanumeric, '\\', one_of("\"n\\")), char('\"')))
// 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,
)
),
)(input)?; )(input)?;
// let (input, _) = tag("#")(input)?;
Ok(("", 0)) println!("{:?}", v);
Ok((v.0, v.0))
} }
fn curlies(input: &str) -> IResult<&str, &str> { fn curlies(input: &str) -> IResult<&str, &str> {
@ -78,22 +98,31 @@ 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> { pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ScriptMeta, E> {
let x = delimited(
if let Ok(v) = elem_tag(input) { sp,
println!("Found elem tag"); alt((map(comment, |s| ScriptMeta::Comment(String::from(s))),
if let Ok(v) = sp(v.0) { map(elem_tag, |s| ScriptMeta::Element(String::from(s)))
preceded(sp, char(':')); )),
println!("ate some spaces"); opt(sp),
} )(input);
else {
println!("didn't eat spaces?"); // if let Ok(v) = elem_tag(input) {
} // println!("Found elem tag");
} // if let Ok(v) = sp(v.0) {
if let Ok(v) = comment(input) { //
println!("Found comment tag") // println!("ate some spaces");
} // }
// else {
return Ok(("", ScriptMeta{})) // println!("didn't eat spaces?");
// }
// }
// if let Ok(v) = comment(input) {
// println!("Found comment tag")
// }
let x = x.unwrap().0;
return Ok((x, ScriptMeta::Comment(String::default())));
} }

Loading…
Cancel
Save