diff --git a/changelog.d/+eb-parse-fstring-nested-braces.bugfix.md b/changelog.d/+eb-parse-fstring-nested-braces.bugfix.md new file mode 100644 index 00000000..f8161d47 --- /dev/null +++ b/changelog.d/+eb-parse-fstring-nested-braces.bugfix.md @@ -0,0 +1 @@ +F-string fields holding an inline dict lookup or a quoted closing brace now parse, because field scanning balances nested braces and skips quoted strings. diff --git a/src/eb_parse.rs b/src/eb_parse.rs index 9ca644c5..cbb02079 100644 --- a/src/eb_parse.rs +++ b/src/eb_parse.rs @@ -1186,7 +1186,7 @@ impl<'a> Parser<'a> { // joining across a newline there would swallow the next statement. let join_strings = matches!(left, Value::Str(_)) && (self.depth > 0 || !crossed_line) - && (matches!(self.peek(), Some(b'\'' | b'"')) || self.starts_prefixed_string()); + && self.string_prefix().is_some(); if join_strings { let right = self.parse_postfix()?; left = match (left, right) { @@ -1272,11 +1272,17 @@ impl<'a> Parser<'a> { let Some(c) = self.peek() else { return Err(self.err("expected expression, got EOF")); }; + if let Some((prefix_len, is_f, is_r)) = self.string_prefix() { + return if is_f { + self.parse_fstring() + } else if is_r { + self.pos += prefix_len; + self.parse_string_with_raw(true) + } else { + self.parse_string() + }; + } match c { - b'f' | b'F' | b'r' | b'R' if self.starts_prefixed_string() => { - self.parse_prefixed_string() - } - b'\'' | b'"' => self.parse_string(), b'[' => self.parse_list(), b'(' => self.parse_tuple_or_group(), b'{' => self.parse_dict(), @@ -1380,47 +1386,53 @@ impl<'a> Parser<'a> { sub.parse_expr() } - /// Whether what follows is a prefixed (`r`, `f`, `fr`, `rf`) string literal - /// rather than an identifier beginning with `r` or `f`. - fn starts_prefixed_string(&self) -> bool { - let Some(first) = self.src.get(self.pos) else { - return false; - }; - let is_f = |b: u8| matches!(b, b'f' | b'F'); - let is_r = |b: u8| matches!(b, b'r' | b'R'); - if !(is_f(*first) || is_r(*first)) { - return false; - } - if matches!(self.src.get(self.pos + 1), Some(b'\'' | b'"')) { - return true; + /// Recognize a bare, raw, formatted, or raw-formatted string prefix. + /// + /// Returns `(prefix_len, is_fstring, is_raw)` when a quote follows. + fn string_prefix(&self) -> Option<(usize, bool, bool)> { + let quote = |idx: usize| matches!(self.src.get(idx), Some(b'\'' | b'"')); + match self.src.get(self.pos) { + Some(b'\'' | b'"') => Some((0, false, false)), + Some(b'r' | b'R') => { + if quote(self.pos + 1) { + Some((1, false, true)) + } else if matches!(self.src.get(self.pos + 1), Some(b'f' | b'F')) + && quote(self.pos + 2) + { + Some((2, true, true)) + } else { + None + } + } + Some(b'f' | b'F') => { + if quote(self.pos + 1) { + Some((1, true, false)) + } else if matches!(self.src.get(self.pos + 1), Some(b'r' | b'R')) + && quote(self.pos + 2) + { + Some((2, true, true)) + } else { + None + } + } + _ => None, } - let Some(second) = self.src.get(self.pos + 1) else { - return false; - }; - let valid_pair = (is_f(*first) && is_r(*second)) || (is_r(*first) && is_f(*second)); - valid_pair && matches!(self.src.get(self.pos + 2), Some(b'\'' | b'"')) } - /// Parse `r'...'`, `f'...'`, `fr'...'`, or `rf'...'` string literals. + /// An f-string, with `{expr}` filled from what the file has defined. /// - /// Only a bare name or expression inside `{...}` is substituted in f-strings; - /// raw prefixes preserve backslashes verbatim. - fn parse_prefixed_string(&mut self) -> Result { - let mut is_f = false; - let mut is_r = false; - while let Some(b) = self.peek() { - if matches!(b, b'f' | b'F') { - is_f = true; - self.pos += 1; - } else if matches!(b, b'r' | b'R') { - is_r = true; - self.pos += 1; - } else { - break; - } - } - let Value::Str(raw) = self.parse_string_with_raw(is_r)? else { - return Err(self.err("prefixed string body is not a string")); + /// 221 recipes use one and 47 put it in a field that decides identity or + /// dependencies, so a parser that stops at the `f` reads those as having + /// no version at all. A format spec or a conversion is an error rather + /// than a guess, because a silently mis-rendered version is worse than a + /// recipe that is skipped. + fn parse_fstring(&mut self) -> Result { + let (prefix_len, is_f, is_raw) = self + .string_prefix() + .ok_or_else(|| self.err("expected f-string"))?; + self.pos += prefix_len; + let Value::Str(raw) = self.parse_string_with_raw(is_raw)? else { + return Err(self.err("f-string body is not a string")); }; if !is_f { return Ok(Value::Str(raw)); @@ -1440,7 +1452,7 @@ impl<'a> Parser<'a> { if opener == "}" { return Err(self.err("unmatched } in f-string")); } - let Some(end) = tail.find('}') else { + let Some(end) = find_fstring_field_end(tail) else { return Err(self.err("unterminated { in f-string")); }; let expr = tail[1..end].trim(); @@ -1976,16 +1988,55 @@ fn dict_method(entries: &[(String, Value)], name: &str, args: &[Value]) -> Resul } } -/// Whether `{field}` carries a conversion or a format spec at bracket depth 0. +/// Find the matching `}` for an f-string field starting at index 0 of `tail`. +/// +/// Tracks nested braces and quoted strings so dict literals and string +/// arguments containing `}` do not terminate the field early. +fn find_fstring_field_end(tail: &str) -> Option { + let mut depth = 0usize; + let mut quote: Option = None; + let mut escape = false; + for (idx, c) in tail.char_indices().skip(1) { + if let Some(q) = quote { + if escape { + escape = false; + } else if c == '\\' { + escape = true; + } else if c == q { + quote = None; + } + continue; + } + match c { + '\'' | '"' => quote = Some(c), + '{' => depth += 1, + '}' => { + if depth == 0 { + return Some(idx); + } + depth -= 1; + } + _ => {} + } + } + None +} + +/// Whether an f-string field carries a `!conversion` or `:format_spec`. /// /// `{local_hash[:8]}` is a slice. `{value:.2f}` and `{value!s}` would change /// the text, and guessing at either would put a wrong value in a field. fn field_has_format_spec(field: &str) -> bool { let mut depth = 0i32; let mut quote: Option = None; + let mut escape = false; for c in field.chars() { if let Some(q) = quote { - if c == q { + if escape { + escape = false; + } else if c == '\\' { + escape = true; + } else if c == q { quote = None; } continue; @@ -5640,6 +5691,61 @@ dependencies = [('Java', '21', '', SYSTEM)]\nmoduleclass = 'devel'\n"; assert_eq!(parsed.source_files[1].filename, "INCHI-1-SRC.zip"); } + #[test] + fn raw_strings_and_rf_fstrings_preserve_backslashes_and_parse() { + let rf_src = "name = 'Demo'\nversion = '1.2'\n\ + toolchain = {'name': 'foss', 'version': '2026.1'}\n\ + local_raw = r'{not_interpolated}\\n'\n\ + local_rf = rf'prefix-{version}\\n'\n\ + local_esc = r'a\\'b'\n\ + sources = [rf'demo-{version}.tar.gz', r'raw-' 'pkg.tar.gz']\n"; + let mut parser = Parser::new(rf_src, "x86_64"); + parser.parse_file().expect("parse rf and raw strings"); + assert!( + parser.skipped.is_empty(), + "rf/raw statements should not be skipped: {:?}", + parser.skipped + ); + assert_eq!( + parser.env.get("local_raw").and_then(Value::as_str), + Some(r"{not_interpolated}\n") + ); + assert_eq!( + parser.env.get("local_rf").and_then(Value::as_str), + Some(r"prefix-1.2\n") + ); + assert_eq!( + parser.env.get("local_esc").and_then(Value::as_str), + Some(r"a\'b") + ); + let parsed = resolve_easyconfig_str(rf_src).expect("resolve rf and raw sources"); + assert_eq!(parsed.source_files.len(), 2); + assert_eq!(parsed.source_files[0].filename, "demo-1.2.tar.gz"); + assert_eq!(parsed.source_files[1].filename, "raw-pkg.tar.gz"); + } + + #[test] + fn fstring_fields_support_nested_dicts_and_quoted_braces() { + let nested_dict = "name = 'Demo'\nversion = '1.0'\n\ + toolchain = SYSTEM\n\ + sources = [f\"demo-{ {'x86_64': 'amd64', 'aarch64': 'arm64'}[ARCH] }.tar.gz\"]\n"; + let x86 = resolve_easyconfig_str_for_arch(nested_dict, "x86_64") + .expect("nested dict in f-string on x86_64"); + assert_eq!(x86.source_files.len(), 1); + assert_eq!(x86.source_files[0].filename, "demo-amd64.tar.gz"); + let arm = resolve_easyconfig_str_for_arch(nested_dict, "aarch64") + .expect("nested dict in f-string on aarch64"); + assert_eq!(arm.source_files.len(), 1); + assert_eq!(arm.source_files[0].filename, "demo-arm64.tar.gz"); + + let quoted_brace = "name = 'Demo'\nversion = '1.0'\n\ + toolchain = SYSTEM\n\ + sources = [f\"demo-{version.replace('.', '}')}.tar.gz\"]\n"; + let parsed = resolve_easyconfig_str(quoted_brace).expect("quoted } in f-string"); + assert_eq!(parsed.source_files.len(), 1); + assert_eq!(parsed.source_files[0].filename, "demo-1}0.tar.gz"); + } + /// EvidentialGene indexes a month table with `int()`. Geant4 and PovChem /// build `sources` with a comprehension. SPM unpacks one version into the /// two names the tarballs use.