From 98cc1886c461714a064ecd0e3b25b0bfdd9f19f9 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Wed, 3 May 2023 19:37:44 -0500 Subject: [PATCH] parser into metasource file --- src/parser/meta.pest | 4 +++- src/parser/parse.rs | 14 ++++++++++++-- src/tests/test_parser.rs | 1 - src/tests/test_source.meta | 4 ++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/parser/meta.pest b/src/parser/meta.pest index 6ae0337..b951124 100644 --- a/src/parser/meta.pest +++ b/src/parser/meta.pest @@ -6,7 +6,9 @@ sigil = _{ ("$" | "@" | "&") ~ "{" } raw_char = _{ !(sigil) ~ ANY } char_seq = { raw_char+ } -string = @{ "\"" ~ char+ ~ "\"" } +string = { escaped_string | raw_string } +escaped_string = @{ "\"" ~ char+ ~ "\"" } +raw_string = @{ "'" ~ (!("'") ~ ANY)+ ~ "'" } char = _{ !("\"" | "\\") ~ ANY | "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t") diff --git a/src/parser/parse.rs b/src/parser/parse.rs index 82ff15a..b314c40 100644 --- a/src/parser/parse.rs +++ b/src/parser/parse.rs @@ -84,7 +84,14 @@ fn parse_assign(pair: Pair) -> (&'_ str, &'_ str) { key = pair.as_str(); } if Rule::value == pair.as_rule() { - val = pair.as_str(); + let tmp = pair.as_str(); + // blank and default shoud be handled by whoever is getting the value + // set it to empty strings do remove it from the HashMap + if tmp == "BLANK" || tmp == "DEFAULT" { + return ("", ""); + } + // remove surrounding quotes from values + val = &tmp[1..tmp.len() - 1]; } } @@ -111,7 +118,10 @@ fn parse_array(pairs: Pairs) -> Vec<&'_ str> { for pair in pairs { if Rule::string == pair.as_rule() { - vec.push(pair.as_str()); + let tmp = pair.as_str(); + // remove surrounding quotes from values + let val = &tmp[1..tmp.len() - 1]; + vec.push(val); } } vec diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index c404f84..db62c24 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -24,7 +24,6 @@ fn build_meta_file() -> Result<()> { &vec!["stuff", "with", "spaces"] ); assert_eq!(source.arrays.get("not_defined"), None); - assert_eq!(source.arrays.get("blank"), None); assert_eq!(source.patterns.get("pat").unwrap(), &"pattern"); assert_eq!(source.patterns.get("pat.sub_pat"), None); diff --git a/src/tests/test_source.meta b/src/tests/test_source.meta index 3d346be..818a5f9 100644 --- a/src/tests/test_source.meta +++ b/src/tests/test_source.meta @@ -1,12 +1,12 @@ ${ var = "good" + single = 'quotes' blank = BLANK } @{ sub.array = ["sub","value"] - arr = ["split","up","values"] - blank = BLANK -{ post line comment } + arr = ["split",'up',"values"] with_spaces = [ "stuff", "with" , "spaces" ] } -- 2.45.2