From a518097ff0c2f600f7100036117b8959edfd2a15 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Fri, 19 May 2023 22:44:00 -0700 Subject: [PATCH] Add single quote strings, quote escaping --- demo/demo.rpy | 2 ++ src/lib.rs | 12 ++++++++++-- src/rpy.pest | 16 ++++++++++++---- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/demo/demo.rpy b/demo/demo.rpy index 37049f5..f41016c 100644 --- a/demo/demo.rpy +++ b/demo/demo.rpy @@ -5,6 +5,8 @@ what the heck "this is a string over multiple lines" "this is \"escaped\"" +'this is a single quote string' +'this also has escaped \'quotes\'' this is cool # comment huh \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index ecba74c..d6fb698 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,9 +19,17 @@ pub fn parse(file_path: &str) { for token in line.into_inner() { match token.as_rule() { Rule::token => { - let token = token.into_inner().next().unwrap(); + let token = token.into_inner().next().unwrap(); match token.as_rule() { - Rule::string => println!("string: {}", token.as_str()), + 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::keyword => println!("keyword: {}", token.as_str()), _ => unreachable!(), }; diff --git a/src/rpy.pest b/src/rpy.pest index 5fdaf5d..6ffbdbf 100644 --- a/src/rpy.pest +++ b/src/rpy.pest @@ -13,10 +13,18 @@ token = { string | keyword } // has to be atomic for no implicit separate (spaces) keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ } -// strings cannot contain quotes -// TODO: escaped quotes -string_data = @{ (!"\"" ~ ANY)* } -string = ${ "\"" ~ string_data ~ "\"" } +single_quote_string_data = @{ ( + "\\'" // Escaped single quotes + | (!"'" ~ ANY) +)* } +double_quote_string_data = @{ ( + "\\\"" // Escaped double quotes + | (!"\"" ~ ANY) +)* } +string = ${ + ("'" ~ single_quote_string_data ~ "'") + | ("\"" ~ double_quote_string_data ~ "\"") +} // comments are a # followed by // any number of non-newline characters