From 3794a1d607e945fda11c85746fd658a08dee5207 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 20 May 2023 13:00:23 -0700 Subject: [PATCH] Implement naive arrays, better debugging --- demo/demo.rpy | 1 + src/lib.rs | 28 ++++++++++++++++++++++++++++ src/rpy.pest | 7 ++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/demo/demo.rpy b/demo/demo.rpy index f41016c..d39ef89 100644 --- a/demo/demo.rpy +++ b/demo/demo.rpy @@ -8,5 +8,6 @@ multiple lines" 'this is a single quote string' 'this also has escaped \'quotes\'' this is cool # comment +[ "this", "is", "an", "array" ] huh \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d6fb698..5ca3dba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,28 @@ use pest_derive::Parser; #[grammar = "rpy.pest"] struct RpyParser; +fn describe_token(pair: pest::iterators::Pair) { + let token = pair.as_rule(); + match token { + Rule::token => {}, + _ => panic!("Not a token!"), + }; + let contents = pair.into_inner().next().unwrap(); + let contents_rule = contents.as_rule(); + let str = match contents_rule { + Rule::string => { + let data = contents.into_inner().next().unwrap(); + match data.as_rule() { + Rule::single_quote_string_data => data.as_str().replace("\\'", "'"), + Rule::double_quote_string_data => data.as_str().replace("\\\"", "\""), + _ => unreachable!(), + } + }, + _ => contents.into_inner().as_str().to_owned(), + }; + println!("{:?}: {}", contents_rule, str); +} + pub fn parse(file_path: &str) { let unparsed_file = fs::read_to_string(file_path).expect("cannot find file"); let file = RpyParser::parse(Rule::file, &unparsed_file) @@ -30,6 +52,12 @@ pub fn parse(file_path: &str) { _ => unreachable!(), }); }, + Rule::array => { + println!("Array:"); + for element in token.into_inner() { + describe_token(element); + } + } Rule::keyword => println!("keyword: {}", token.as_str()), _ => unreachable!(), }; diff --git a/src/rpy.pest b/src/rpy.pest index 6ffbdbf..b7b5bf7 100644 --- a/src/rpy.pest +++ b/src/rpy.pest @@ -8,11 +8,13 @@ char = { !NEWLINE ~ ANY } // http://pest.rs/book/grammars/syntax.html#atomic inner = @{ char* } -token = { string | keyword } +token = { string | array | keyword } +// KEYWORDS // has to be atomic for no implicit separate (spaces) keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ } +// STRING single_quote_string_data = @{ ( "\\'" // Escaped single quotes | (!"'" ~ ANY) @@ -26,6 +28,9 @@ string = ${ | ("\"" ~ double_quote_string_data ~ "\"") } +// ARRAY +array = { "[" ~ token ~ ("," ~ token)* ~ "]"} + // comments are a # followed by // any number of non-newline characters COMMENT = _{ "#" ~ char* }