sync, parser work

looking-at-specs
mitchellhansen 4 years ago
parent 8ceb805a52
commit 152a1670c5

@ -30,7 +30,7 @@ meta globalTableFormatting : {
**There is no way around a tree structure in the markup.** **There is no way around a tree structure in the markup.**
elem div k{ elem div {
} }

@ -1,21 +1,16 @@
use nom::branch::alt; use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, take, take_till, take_until, take_while}; 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::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::complete::alphanumeric1 as alphanumeric;
use nom::character::is_alphabetic; use nom::character::is_alphabetic;
use nom::combinator::{cut, map, map_res, opt, value, verify, map_opt}; use nom::combinator::{cut, map, map_opt, map_res, opt, value, verify};
use nom::error::{ParseError, FromExternalError}; use nom::error::{FromExternalError, ParseError};
use nom::IResult; use nom::IResult;
use nom::multi::{many0, fold_many0}; use nom::multi::{fold_many0, many0};
use nom::number::complete::be_u16; use nom::number::complete::be_u16;
use nom::sequence::{delimited, preceded, terminated, tuple}; 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)] #[derive(Debug, PartialEq)]
pub struct Color { pub struct Color {
pub red: u8, pub red: u8,
@ -23,36 +18,58 @@ pub struct Color {
pub blue: u8, pub blue: u8,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StringFragment<'a> {
Literal(&'a str),
EscapedChar(char),
EscapedWS,
}
pub enum ScriptMeta { pub enum ScriptMeta {
Comment(String), Comment(String),
Element(String), Element(String),
Meta(String), Meta(String),
} }
pub fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> { // I think this is the space for petgraph...
u8::from_str_radix(input, 16) pub enum ElementGraph {
}
pub fn is_hex_digit(c: char) -> bool {
c.is_digit(16)
} }
pub fn hex_primary(input: &str) -> IResult<&str, u8> { // ENTRY
map_res( pub fn parse_script<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, ScriptMeta, E> {
take_while_m_n(2, 2, is_hex_digit), println!("Full input string : {:?}\n", input);
from_hex,
)(input)
}
pub fn hex_color(input: &str) -> IResult<&str, Color> { let mut remaining_str = input;
let (input, _) = tag("#")(input)?; let mut scope = Vec::new();
let (input, (red, green, blue)) = tuple((hex_primary, hex_primary, hex_primary))(input)?;
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;
} }
pub fn scope<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str> { 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(opt(sp), delimited(char('{'), is_not("}"), char('}')), opt(sp))(input)?;
//let (input, _) = delimited(char('{'), is_not("}"), char('}'))(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> { 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, _) = delimited(opt(sp), tag("elem"), sp)(input)?;
let (input, elem_name) = parse_token(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); println!("elem , name : {:?} || scope : {:?}", elem_name, input);
Ok((input, elem_name)) Ok((input, elem_name))
} }
// Parse a single alphanumeric token delimited by spaces // 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) escaped(alphanumeric, '\\', one_of(""))(i)
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] // Parse from a # to a newline character
enum StringFragment<'a> { pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> {
Literal(&'a str), let v = preceded(char('#'),
EscapedChar(char), cut(terminated(
EscapedWS, 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) fn parse_unicode<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::num::ParseIntError>>(input: &'a str)
@ -199,49 +227,33 @@ fn parse_string<'a, E: ParseError<&'a str> + FromExternalError<&'a str, std::num
delimited(char('"'), build_string, char('"'))(input) delimited(char('"'), build_string, char('"'))(input)
} }
// Parse from a # to a newline character // Unused stuff
pub fn comment<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, &'a str, E> { pub fn length_value(input: &[u8]) -> IResult<&[u8], &[u8]> {
let (input, length) = be_u16(input)?;
let v = preceded(char('#'), take(length)(input)
cut(terminated(
is_not("\n"),
newline,
)),
)(input)?;
println!("comment : # {:?}", v.1);
Ok((v.0, v.0))
} }
pub fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> {
// Eat up whitespace characters u8::from_str_radix(input, 16)
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 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);
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;
} }
return Ok((remaining_str, ScriptMeta::Comment(String::default()))); pub fn hex_primary(input: &str) -> IResult<&str, u8> {
map_res(
take_while_m_n(2, 2, is_hex_digit),
from_hex,
)(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)?;
Ok((input, Color { red, green, blue }))
}
/* /*
// ( and any amount of bytes ). Returns the bytes between the () // ( and any amount of bytes ). Returns the bytes between the ()

Loading…
Cancel
Save