From 5442a8fc8770a7389fda83e76c7b5850fbbdb266 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 20 May 2023 13:19:51 -0700 Subject: [PATCH] Multiline arrays, better debugging --- demo/demo.rpy | 9 ++++++++- src/lib.rs | 36 +++++++++--------------------------- src/rpy.pest | 5 ++++- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/demo/demo.rpy b/demo/demo.rpy index d39ef89..6d2ff52 100644 --- a/demo/demo.rpy +++ b/demo/demo.rpy @@ -8,6 +8,13 @@ multiple lines" 'this is a single quote string' 'this also has escaped \'quotes\'' this is cool # comment -[ "this", "is", "an", "array" ] +any empty array [] +[ + "this", # test + "is", + "an", + "array" +] +["this","is","an","array"] huh \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 5ca3dba..0f42168 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,14 @@ fn describe_token(pair: pest::iterators::Pair) { _ => unreachable!(), } }, - _ => contents.into_inner().as_str().to_owned(), + Rule::array => { + println!("array: Start array"); + for token in contents.into_inner() { + describe_token(token); + } + "End array".to_string() + } + _ => contents.as_str().to_owned(), }; println!("{:?}: {}", contents_rule, str); } @@ -37,33 +44,8 @@ pub fn parse(file_path: &str) { for line in file.into_inner() { match line.as_rule() { Rule::line => { - println!("Line:"); for token in line.into_inner() { - match token.as_rule() { - Rule::token => { - let token = token.into_inner().next().unwrap(); - match token.as_rule() { - Rule::string => { - let string_data = token.into_inner().next().unwrap(); - let str = string_data.as_str(); - println!("string: {}", match string_data.as_rule() { - Rule::single_quote_string_data => str.replace("\\'", "'"), - Rule::double_quote_string_data => str.replace("\\\"", "\""), - _ => unreachable!(), - }); - }, - Rule::array => { - println!("Array:"); - for element in token.into_inner() { - describe_token(element); - } - } - Rule::keyword => println!("keyword: {}", token.as_str()), - _ => unreachable!(), - }; - }, - _ => unreachable!(), - } + describe_token(token); } println!() }, diff --git a/src/rpy.pest b/src/rpy.pest index b7b5bf7..060253c 100644 --- a/src/rpy.pest +++ b/src/rpy.pest @@ -29,7 +29,10 @@ string = ${ } // ARRAY -array = { "[" ~ token ~ ("," ~ token)* ~ "]"} +array = { + "[" ~ "]" + | "[" ~ NEWLINE* ~ token ~ ("," ~ NEWLINE* ~ token)* ~ NEWLINE* ~ "]" +} // comments are a # followed by // any number of non-newline characters