Add alternative string syntax with single quotes and backticks
This commit is contained in:
parent
e698fd315e
commit
01c7ba6e2a
4 changed files with 33 additions and 5 deletions
|
@ -40,7 +40,10 @@ pub enum Token {
|
||||||
Ident(String),
|
Ident(String),
|
||||||
#[regex(r"[+-]?(?:[0-9]+[.])?[0-9]+", |x| x.slice().to_string())]
|
#[regex(r"[+-]?(?:[0-9]+[.])?[0-9]+", |x| x.slice().to_string())]
|
||||||
NumLit(String),
|
NumLit(String),
|
||||||
|
|
||||||
#[regex(r#""(?:[^"\\]|\\.)*""#, |x| ESCAPE_REPLACE_REGEX.replace_all(x.slice(), "$1").to_string())]
|
#[regex(r#""(?:[^"\\]|\\.)*""#, |x| ESCAPE_REPLACE_REGEX.replace_all(x.slice(), "$1").to_string())]
|
||||||
|
#[regex(r#"`(?:[^`\\]|\\.)*`"#, |x| ESCAPE_REPLACE_REGEX.replace_all(x.slice(), "$1").to_string())]
|
||||||
|
#[regex(r#"'(?:[^'\\]|\\.)*'"#, |x| ESCAPE_REPLACE_REGEX.replace_all(x.slice(), "$1").to_string())]
|
||||||
StrLit(String),
|
StrLit(String),
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -70,6 +70,8 @@ regex_rules! {
|
||||||
r"true" => |_| Token::True,
|
r"true" => |_| Token::True,
|
||||||
r"false" => |_| Token::False,
|
r"false" => |_| Token::False,
|
||||||
r#""(?:[^"\\]|\\.)*""# => |x| Token::StrLit(ESCAPE_REPLACE_REGEX.replace_all(&x, "$1").to_string()),
|
r#""(?:[^"\\]|\\.)*""# => |x| Token::StrLit(ESCAPE_REPLACE_REGEX.replace_all(&x, "$1").to_string()),
|
||||||
|
r#"`(?:[^`\\]|\\.)*`"# => |x| Token::StrLit(ESCAPE_REPLACE_REGEX.replace_all(&x, "$1").to_string()),
|
||||||
|
r#"'(?:[^'\\]|\\.)*'"# => |x| Token::StrLit(ESCAPE_REPLACE_REGEX.replace_all(&x, "$1").to_string()),
|
||||||
r#"[+-]?(?:[0-9]+[.])?[0-9]+"# => |x| Token::NumLit(x),
|
r#"[+-]?(?:[0-9]+[.])?[0-9]+"# => |x| Token::NumLit(x),
|
||||||
r#":[^\s\)\]}]+"# => |x| Token::Keyword(x),
|
r#":[^\s\)\]}]+"# => |x| Token::Keyword(x),
|
||||||
r#"[a-zA-Z_!\?<>/\.\*-\+\-][^\s{}\(\)\[\](){}]*"# => |x| Token::Symbol(x),
|
r#"[a-zA-Z_!\?<>/\.\*-\+\-][^\s{}\(\)\[\](){}]*"# => |x| Token::Symbol(x),
|
||||||
|
@ -102,7 +104,7 @@ impl Iterator for Lexer {
|
||||||
|
|
||||||
if string.starts_with('{') {
|
if string.starts_with('{') {
|
||||||
let expr_start = self.pos;
|
let expr_start = self.pos;
|
||||||
let mut in_string = false;
|
let mut in_string = None;
|
||||||
loop {
|
loop {
|
||||||
if self.pos >= self.source.len() {
|
if self.pos >= self.source.len() {
|
||||||
return None;
|
return None;
|
||||||
|
@ -112,13 +114,19 @@ impl Iterator for Lexer {
|
||||||
}
|
}
|
||||||
let string = &self.source[self.pos..];
|
let string = &self.source[self.pos..];
|
||||||
|
|
||||||
if string.starts_with('}') && !in_string {
|
if string.starts_with('}') && in_string.is_none() {
|
||||||
self.pos += 1;
|
self.pos += 1;
|
||||||
let tok_str = &self.source[expr_start..self.pos];
|
let tok_str = &self.source[expr_start..self.pos];
|
||||||
return Some(Ok((expr_start, Token::SimplExpr(tok_str.to_string()), self.pos - 1)));
|
return Some(Ok((expr_start, Token::SimplExpr(tok_str.to_string()), self.pos - 1)));
|
||||||
} else if string.starts_with('"') {
|
} else if string.starts_with('"') || string.starts_with('\'') || string.starts_with('`') {
|
||||||
|
if let Some(quote) = in_string {
|
||||||
|
if string.starts_with(quote) {
|
||||||
|
in_string = None;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
in_string = Some(string.chars().next().unwrap());
|
||||||
|
}
|
||||||
self.pos += 1;
|
self.pos += 1;
|
||||||
in_string = !in_string;
|
|
||||||
} else if string.starts_with("\\\"") {
|
} else if string.starts_with("\\\"") {
|
||||||
self.pos += 2;
|
self.pos += 2;
|
||||||
} else {
|
} else {
|
||||||
|
@ -165,4 +173,5 @@ fn test_yuck_lexer() {
|
||||||
insta::assert_debug_snapshot!(Lexer::new(0, r#"{ bla "} \" }" " \" "}"#.to_string()).collect_vec());
|
insta::assert_debug_snapshot!(Lexer::new(0, r#"{ bla "} \" }" " \" "}"#.to_string()).collect_vec());
|
||||||
insta::assert_debug_snapshot!(Lexer::new(0, r#""< \" >""#.to_string()).collect_vec());
|
insta::assert_debug_snapshot!(Lexer::new(0, r#""< \" >""#.to_string()).collect_vec());
|
||||||
insta::assert_debug_snapshot!(Lexer::new(0, r#"{ " " + music}"#.to_string()).collect_vec());
|
insta::assert_debug_snapshot!(Lexer::new(0, r#"{ " " + music}"#.to_string()).collect_vec());
|
||||||
|
insta::assert_debug_snapshot!(Lexer::new(0, r#"{ " } ' }" }"#.to_string()).collect_vec());
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
---
|
||||||
|
source: crates/yuck/src/parser/lexer.rs
|
||||||
|
expression: "Lexer::new(0, r#\"{ \" } ' }\" }\"#.to_string()).collect_vec()"
|
||||||
|
|
||||||
|
---
|
||||||
|
[
|
||||||
|
Ok(
|
||||||
|
(
|
||||||
|
0,
|
||||||
|
SimplExpr(
|
||||||
|
"{ \" } ' }\" }",
|
||||||
|
),
|
||||||
|
11,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
|
@ -24,7 +24,7 @@
|
||||||
|
|
||||||
(defwidget music []
|
(defwidget music []
|
||||||
(box :class "music" :orientation "h" :space-evenly false :halign "center"
|
(box :class "music" :orientation "h" :space-evenly false :halign "center"
|
||||||
{ " " + music}))
|
{ ' ' + music}))
|
||||||
|
|
||||||
(defwidget slider-vol []
|
(defwidget slider-vol []
|
||||||
(box :class "slider-vol" :orientation "h" :space-evenly "false"
|
(box :class "slider-vol" :orientation "h" :space-evenly "false"
|
||||||
|
|
Loading…
Add table
Reference in a new issue