use super::*;
use crate::lexer::decode_escape_char;
fn fragment_options(options: crate::lexer::ParseOptions) -> crate::lexer::ParseOptions {
crate::lexer::ParseOptions {
path_construction: false,
..options
}
}
pub(super) fn parse_interp_body(
body: &str,
options: crate::lexer::ParseOptions,
) -> Result<Vec<crate::ast::InterpPart>, String> {
use crate::ast::InterpPart;
let chars: Vec<char> = body.chars().collect();
let mut parts: Vec<InterpPart> = Vec::new();
let mut text = String::new();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if c == '\\' && i + 1 < chars.len() {
i += push_interp_escape(&mut text, &chars, i)?;
continue;
}
if c != '{' {
text.push(c);
i += 1;
continue;
}
let mut depth = 1usize;
let mut inner_quote: Option<char> = None;
let mut in_backtick = false;
let mut j = i + 1;
while j < chars.len() {
let d = chars[j];
if let Some(q) = inner_quote {
if d == '\\' {
j += 2;
continue;
}
if d == q {
inner_quote = None;
}
} else if in_backtick {
if d == '`' {
in_backtick = false;
}
} else {
match d {
'\\' => j += 1,
'`' => in_backtick = true,
'{' | '[' | '(' => depth += 1,
'}' | ']' | ')' => {
if depth == 0 {
break;
}
depth -= 1;
if d == '}' && depth == 0 {
break;
}
}
'\'' | '"' => inner_quote = Some(d),
_ => {}
}
}
j += 1;
}
if depth != 0 {
return Err("unterminated { expression } in interpolated string".into());
}
let expr_src: String = chars[i + 1..j].iter().collect();
let expr = parse_expression_str_with_options(&expr_src, fragment_options(options))
.map_err(|e| format!("invalid expression in interpolated string: {e}"))?;
if !text.is_empty() {
parts.push(InterpPart::Text(std::mem::take(&mut text)));
}
parts.push(InterpPart::Interp(Box::new(expr)));
i = j + 1;
}
if !text.is_empty() {
parts.push(InterpPart::Text(text));
}
Ok(parts)
}
fn push_interp_escape(text: &mut String, chars: &[char], i: usize) -> Result<usize, String> {
let next = chars[i + 1];
let decoded = match next {
'u' => return push_interp_unicode_escape(text, chars, i),
'{' => Some('{'),
'}' => Some('}'),
other => decode_escape_char(other),
};
match decoded {
Some(decoded) => text.push(decoded),
None => {
text.push('\\');
text.push(next);
}
}
Ok(2)
}
fn push_interp_unicode_escape(
text: &mut String,
chars: &[char],
i: usize,
) -> Result<usize, String> {
if i + crate::lexer::UNICODE_ESCAPE_SPAN > chars.len() {
return Err("invalid unicode escape sequence".into());
}
let invalid = || {
Err(format!(
"invalid unicode escape sequence: \\u{}",
chars[i + 2..i + crate::lexer::UNICODE_ESCAPE_SPAN]
.iter()
.collect::<String>()
))
};
let Some((cp, span)) = crate::lexer::unicode_escape_window(&chars[i..]) else {
return invalid();
};
if let Some(pair) = interp_surrogate_pair(cp, chars, i) {
text.push(pair.0);
return Ok(pair.1);
}
match char::from_u32(cp) {
Some(ch) => {
text.push(ch);
Ok(span)
}
None => invalid(),
}
}
fn interp_surrogate_pair(cp: u32, chars: &[char], i: usize) -> Option<(char, usize)> {
let (lo, span) =
crate::lexer::unicode_escape_window(chars.get(i + crate::lexer::UNICODE_ESCAPE_SPAN..)?)?;
crate::lexer::unicode_surrogate_pair(cp, lo).map(|ch| (ch, 2 * span))
}