You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Trac3r-rust/src/parser/parser.rs

158 lines
4.5 KiB

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};
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};
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,
pub green: u8,
pub blue: u8,
}
pub enum ScriptMeta {
Comment(String),
Element(String),
Meta(String),
}
pub fn from_hex(input: &str) -> Result<u8, std::num::ParseIntError> {
u8::from_str_radix(input, 16)
}
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)
}
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 }))
}
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)?;
Ok((input, input))
}
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_str(input)?;
let (input, _) = scope::<'a, E>(input)?;
println!("elem , name : {:?} || scope : {:?}", elem_name, input);
Ok((input, elem_name))
}
fn parse_str<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
let chars = "\n";
escaped(alphanumeric, '\\', one_of(""))(i)
}
// 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))
}
/// parser combinators are constructed from the bottom up:
/// first we write parsers for the smallest elements (here a space character),
/// then we'll combine them in larger parsers
fn sp<'a>(i: &'a str) -> IResult<&'a str, &'a str> {
let chars = " \t\r\n";
// nom combinators like `take_while` return a function. That function is the
// parser,to which we can pass the input
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> {
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;
}
//println!("{:?}", x);
// 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")
// }
5 years ago
return Ok((remaining_str, ScriptMeta::Comment(String::default())));
}
/*
// ( and any amount of bytes ). Returns the bytes between the ()
fn parens(input: &str) -> IResult<&str, &str> {
delimited(char('('), is_not(")"), char(')'))(input)
}
// `take_while_m_n` parses between `m` and `n` bytes (inclusive) that match
// a predicate. `parse_hex` here parses between 1 and 6 hexadecimal numerals.
let parse_hex = take_while_m_n(1, 6, |c: char| c.is_ascii_hexdigit());
*/