From 152a1670c518acea64e006179b03ee0814e3dcb2 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 18 Oct 2020 00:21:04 -0700 Subject: [PATCH] sync, parser work --- notes/layout-scripts.txt | 2 +- src/parser/parser.rs | 152 +++++++++++++++++++++------------------ 2 files changed, 83 insertions(+), 71 deletions(-) diff --git a/notes/layout-scripts.txt b/notes/layout-scripts.txt index 5df71552..dfd38bb4 100644 --- a/notes/layout-scripts.txt +++ b/notes/layout-scripts.txt @@ -30,7 +30,7 @@ meta globalTableFormatting : { **There is no way around a tree structure in the markup.** -elem div k{ +elem div { } diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 2a5ed51a..f7c7f69c 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -1,21 +1,16 @@ 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::{anychar, char, line_ending, newline, not_line_ending, one_of, multispace1}; +use nom::character::complete::{anychar, char, line_ending, multispace1, 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, value, verify, map_opt}; -use nom::error::{ParseError, FromExternalError}; +use nom::combinator::{cut, map, map_opt, map_res, opt, value, verify}; +use nom::error::{FromExternalError, ParseError}; use nom::IResult; -use nom::multi::{many0, fold_many0}; +use nom::multi::{fold_many0, many0}; use nom::number::complete::be_u16; use nom::sequence::{delimited, preceded, terminated, tuple}; -pub fn length_value(input: &[u8]) -> IResult<&[u8], &[u8]> { - let (input, length) = be_u16(input)?; - take(length)(input) -} - #[derive(Debug, PartialEq)] pub struct Color { pub red: u8, @@ -23,36 +18,58 @@ pub struct Color { pub blue: u8, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum StringFragment<'a> { + Literal(&'a str), + EscapedChar(char), + EscapedWS, +} + pub enum ScriptMeta { Comment(String), Element(String), Meta(String), } -pub fn from_hex(input: &str) -> Result { - u8::from_str_radix(input, 16) -} +// I think this is the space for petgraph... +pub enum ElementGraph { -pub fn is_hex_digit(c: char) -> bool { - c.is_digit(16) } -pub fn hex_primary(input: &str) -> IResult<&str, u8> { - map_res( - take_while_m_n(2, 2, is_hex_digit), - from_hex, - )(input) -} +// ENTRY +pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ScriptMeta, E> { + println!("Full input string : {:?}\n", input); -pub fn hex_color(input: &str) -> IResult<&str, Color> { - let (input, _) = tag("#")(input)?; - let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?; + let mut remaining_str = input; + let mut scope = Vec::new(); - Ok((input, Color { red, green, blue })) + // While there is text left in the string + while remaining_str.len() > 0 { + println!("Remaining Length : {:?}", remaining_str.len()); + println!("Remaining String: {:?}", remaining_str); + + // Consume whitespace and test + let match_type = delimited( + sp, + alt(( + map(comment, |s| ScriptMeta::Comment(String::from(s))), + // consume an element by pulling all metadata + map(elem::<'a, E>, |s| ScriptMeta::Element(String::from(s))), + )), + opt(sp), + )(remaining_str); + + + + + + remaining_str = match_type.unwrap().0; + } + + return Ok((remaining_str, ScriptMeta::Comment(String::default()))); } pub fn scope<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str> { - let (input, _) = delimited(opt(sp), delimited(char('{'), is_not("}"), char('}')), opt(sp))(input)?; //let (input, _) = delimited(char('{'), is_not("}"), char('}'))(input)?; @@ -60,7 +77,6 @@ pub fn scope<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a } pub fn elem<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str> { - let (input, _) = delimited(opt(sp), tag("elem"), sp)(input)?; let (input, elem_name) = parse_token(input)?; @@ -70,7 +86,6 @@ pub fn elem<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a println!("elem , name : {:?} || scope : {:?}", elem_name, input); Ok((input, elem_name)) - } // Parse a single alphanumeric token delimited by spaces @@ -79,11 +94,24 @@ fn parse_token<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a s escaped(alphanumeric, '\\', one_of(""))(i) } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum StringFragment<'a> { - Literal(&'a str), - EscapedChar(char), - EscapedWS, +// Parse from a # to a newline character +pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> { + let v = preceded(char('#'), + cut(terminated( + is_not("\n"), + newline, + )), + )(input)?; + + println!("comment : # {:?}", v.1); + + Ok((v.0, v.0)) +} + +// Eat up whitespace characters +fn sp<'a>(i: &'a str) -> IResult<&'a str, &'a str> { + let chars = " \t\r\n"; + take_while(move |c| chars.contains(c))(i) } fn parse_unicode<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>>(input: &'a str) @@ -115,8 +143,8 @@ fn parse_unicode<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::nu } /// Parse an escaped character: \n, \t, \r, \u{00AC}, etc. -fn parse_escaped_char<'a, E: ParseError<&'a str>+ FromExternalError<&'a str, std::num::ParseIntError>>(input: &'a str) - -> IResult<&'a str, char, E> { +fn parse_escaped_char<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>>(input: &'a str) + -> IResult<&'a str, char, E> { preceded( char('\\'), // `alt` tries each parser in sequence, returning the result of @@ -162,7 +190,7 @@ fn parse_literal<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, /// Combine parse_literal, parse_escaped_whitespace, and parse_escaped_char /// into a StringFragment. -fn parse_fragment<'a, E: ParseError<&'a str>+ FromExternalError<&'a str, std::num::ParseIntError>>( +fn parse_fragment<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>>( input: &'a str, ) -> IResult<&'a str, StringFragment<'a>, E> { alt(( @@ -199,50 +227,34 @@ fn parse_string<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::num delimited(char('"'), build_string, char('"'))(input) } -// Parse from a # to a newline character -pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> { - - let v = preceded(char('#'), - cut(terminated( - is_not("\n"), - newline, - )), - )(input)?; - - println!("comment : # {:?}", v.1); - - Ok((v.0, v.0)) +// Unused stuff +pub fn length_value(input: &[u8]) -> IResult<&[u8], &[u8]> { + let (input, length) = be_u16(input)?; + take(length)(input) } - -// Eat up whitespace characters -fn sp<'a>(i: &'a str) -> IResult<&'a str, &'a str> { - let chars = " \t\r\n"; - take_while(move |c| chars.contains(c))(i) +pub fn from_hex(input: &str) -> Result { + u8::from_str_radix(input, 16) } -pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ScriptMeta, E> { +pub fn is_hex_digit(c: char) -> bool { + c.is_digit(16) +} - println!("Full input string : {:?}\n", input); +pub fn hex_primary(input: &str) -> IResult<&str, u8> { + map_res( + take_while_m_n(2, 2, is_hex_digit), + from_hex, + )(input) +} - let mut remaining_str = input; - while remaining_str.len() > 0 { - println!("Remaining Length : {:?}", remaining_str.len()); - println!("Remaining String: {:?}", remaining_str); - let x = delimited( - sp, - alt((map(comment, |s| ScriptMeta::Comment(String::from(s))), - map(elem::<'a, E>, |s| ScriptMeta::Element(String::from(s))) - )), - opt(sp), - )(remaining_str); - remaining_str = x.unwrap().0; - } +pub fn hex_color(input: &str) -> IResult<&str, Color> { + let (input, _) = tag("#")(input)?; + let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?; - return Ok((remaining_str, ScriptMeta::Comment(String::default()))); + Ok((input, Color { red, green, blue })) } - /* // ( and any amount of bytes ). Returns the bytes between the () fn parens(input: &str) -> IResult<&str, &str> {