From 59a945b474a74ac8a7a72f28cd417c9dc1f25b6a Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 11 Oct 2020 23:20:07 -0700 Subject: [PATCH] for some reason can't parse a damn line --- resources/scripts/scratch | 3 +- src/main.rs | 1 - src/parser/parser.rs | 105 ++++++++++++++++++++++++-------------- 3 files changed, 68 insertions(+), 41 deletions(-) diff --git a/resources/scripts/scratch b/resources/scripts/scratch index 16f09096..377806fb 100644 --- a/resources/scripts/scratch +++ b/resources/scripts/scratch @@ -1,6 +1,5 @@ - # this is a comment -elem table : globalTableFormatting { +elem table { } diff --git a/src/main.rs b/src/main.rs index abe3a789..00c9b93b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,7 +93,6 @@ pub fn main() { parse_script::<(&str, ErrorKind)>(&input_string); - return; //hprof::start_frame(); diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 7f9b4bd0..fc22741e 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -1,27 +1,30 @@ +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::number::complete::be_u16; -use nom::bytes::complete::{take, is_not, take_while, escaped}; -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}; +use nom::sequence::{delimited, preceded, terminated, tuple}; -pub fn length_value(input: &[u8]) -> IResult<&[u8],&[u8]> { +pub fn length_value(input: &[u8]) -> IResult<&[u8], &[u8]> { let (input, length) = be_u16(input)?; take(length)(input) } -#[derive(Debug,PartialEq)] +#[derive(Debug, PartialEq)] pub struct Color { - pub red: u8, - pub green: u8, - pub blue: u8, + pub red: u8, + pub green: u8, + pub blue: u8, } -pub struct ScriptMeta { - +pub enum ScriptMeta { + Comment(String), + Element(String), + Meta(String), } pub fn from_hex(input: &str) -> Result { @@ -35,7 +38,7 @@ pub fn is_hex_digit(c: char) -> bool { pub fn hex_primary(input: &str) -> IResult<&str, u8> { map_res( take_while_m_n(2, 2, is_hex_digit), - from_hex + from_hex, )(input) } @@ -46,19 +49,36 @@ pub fn hex_color(input: &str) -> IResult<&str, Color> { 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 = sp(input)?; - Ok((input.0, 0)) + Ok((input.0, input.0)) +} + +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) } -pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, u8, E> { +pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> { - preceded(char('#'), cut(terminated(escaped( - 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)?; - // let (input, _) = tag("#")(input)?; - Ok(("", 0)) + + println!("{:?}", v); + + Ok((v.0, v.0)) } 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> { - - if let Ok(v) = elem_tag(input) { - println!("Found elem tag"); - if let Ok(v) = sp(v.0) { - preceded(sp, char(':')); - println!("ate some spaces"); - } - else { - println!("didn't eat spaces?"); - } - } - if let Ok(v) = comment(input) { - println!("Found comment tag") - } - - return Ok(("", ScriptMeta{})) + let x = delimited( + sp, + alt((map(comment, |s| ScriptMeta::Comment(String::from(s))), + map(elem_tag, |s| ScriptMeta::Element(String::from(s))) + )), + opt(sp), + )(input); + + // if let Ok(v) = elem_tag(input) { + // println!("Found elem tag"); + // if let Ok(v) = sp(v.0) { + // + // println!("ate some spaces"); + // } + // else { + // 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()))); }