refactor parsing of quoted strings with interpolation containing strings

This commit is contained in:
ConnorSkees 2020-04-19 22:41:37 -04:00
parent 7579a6e9b5
commit 64e2632de4
2 changed files with 15 additions and 22 deletions

View File

@ -164,40 +164,28 @@ pub(crate) fn read_until_closing_quote<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>,
q: char,
) -> Vec<Token> {
let mut is_escaped = false;
let mut t = Vec::new();
for tok in toks {
while let Some(tok) = toks.next() {
match tok.kind {
'"' if !is_escaped && q == '"' => {
'"' if q == '"' => {
t.push(tok);
break;
}
'"' if is_escaped => {
t.push(tok);
is_escaped = false;
continue;
}
'\'' if !is_escaped && q == '\'' => {
'\'' if q == '\'' => {
t.push(tok);
break;
}
'\'' if is_escaped => {
t.push(tok);
is_escaped = false;
continue;
}
'\\' if !is_escaped => {
t.push(tok);
is_escaped = true
}
'\\' => {
is_escaped = false;
t.push(tok);
continue;
t.push(toks.next().unwrap());
}
_ if is_escaped => {
is_escaped = false;
'#' => {
t.push(tok);
let next = toks.peek().unwrap();
if next.kind == '{' {
t.push(toks.next().unwrap());
t.append(&mut read_until_closing_curly_brace(toks));
}
}
_ => t.push(tok),
}

View File

@ -33,3 +33,8 @@ test!(
"a {\n color: '#{foo}';\n}\n",
"a {\n color: \"foo\";\n}\n"
);
test!(
double_quotes_inside_double_quoted_string,
"a {\n color: #{\"#{'\"'}\"};\n}\n",
"a {\n color: \";\n}\n"
);