@ -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, FromExternal Error} ;
use nom ::combinator ::{ cut , map , map_ opt, map_ res, opt , value , verify } ;
use nom ::error ::{ FromExternalError, Parse Error} ;
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 , std ::num ::ParseIntError > {
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 ;
}
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(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 )
@ -199,49 +227,33 @@ 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 , std ::num ::ParseIntError > {
u8 ::from_str_radix ( input , 16 )
}
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 ;
pub fn is_hex_digit ( c : char ) -> bool {
c . is_digit ( 16 )
}
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 ()