generated from ElnuDev/rust-project
Add single quote strings, quote escaping
This commit is contained in:
parent
8faf2eb636
commit
a518097ff0
3 changed files with 24 additions and 6 deletions
|
@ -5,6 +5,8 @@ what the heck
|
||||||
"this is a string over
|
"this is a string over
|
||||||
multiple lines"
|
multiple lines"
|
||||||
"this is \"escaped\""
|
"this is \"escaped\""
|
||||||
|
'this is a single quote string'
|
||||||
|
'this also has escaped \'quotes\''
|
||||||
this is cool # comment
|
this is cool # comment
|
||||||
|
|
||||||
huh
|
huh
|
12
src/lib.rs
12
src/lib.rs
|
@ -19,9 +19,17 @@ pub fn parse(file_path: &str) {
|
||||||
for token in line.into_inner() {
|
for token in line.into_inner() {
|
||||||
match token.as_rule() {
|
match token.as_rule() {
|
||||||
Rule::token => {
|
Rule::token => {
|
||||||
let token = token.into_inner().next().unwrap();
|
let token = token.into_inner().next().unwrap();
|
||||||
match token.as_rule() {
|
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()),
|
Rule::keyword => println!("keyword: {}", token.as_str()),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
16
src/rpy.pest
16
src/rpy.pest
|
@ -13,10 +13,18 @@ token = { string | keyword }
|
||||||
// has to be atomic for no implicit separate (spaces)
|
// has to be atomic for no implicit separate (spaces)
|
||||||
keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ }
|
keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ }
|
||||||
|
|
||||||
// strings cannot contain quotes
|
single_quote_string_data = @{ (
|
||||||
// TODO: escaped quotes
|
"\\'" // Escaped single quotes
|
||||||
string_data = @{ (!"\"" ~ ANY)* }
|
| (!"'" ~ ANY)
|
||||||
string = ${ "\"" ~ string_data ~ "\"" }
|
)* }
|
||||||
|
double_quote_string_data = @{ (
|
||||||
|
"\\\"" // Escaped double quotes
|
||||||
|
| (!"\"" ~ ANY)
|
||||||
|
)* }
|
||||||
|
string = ${
|
||||||
|
("'" ~ single_quote_string_data ~ "'")
|
||||||
|
| ("\"" ~ double_quote_string_data ~ "\"")
|
||||||
|
}
|
||||||
|
|
||||||
// comments are a # followed by
|
// comments are a # followed by
|
||||||
// any number of non-newline characters
|
// any number of non-newline characters
|
||||||
|
|
Loading…
Add table
Reference in a new issue