fix(tpv): 支持 PEP 701 同引号嵌套 f-string 并记录解析错误位置

- lexer.vp: _read_string 增加 f-string {} 深度跟踪,跳过表达式块内嵌套字符串字面量(含 {{/}} 转义、跨行报错)
- parser.vp: _parse_fstring 表达式扫描同步跳过块内嵌套字符串,三条 error_count 路径补 _record_parse_err(消除'第 0 行 0 列'不可读错误)
This commit is contained in:
TermiNexus committed 2026-08-28 12:50:38 +08:00
1 parent bc017d0a61
commit 90a74e4e2c
2 files changed
+64

No files matched your search

+18
View File
@@ -1286,6 +1286,21 @@ def _parse_fstring(ps: Parser | t.CPtr, content: str,
fmt_pos: t.CSizeT = 0
while j < content_len and depth > 0:
c: t.CChar = content[j]
if c == '\'' or c == '"':
# 跳过表达式块内的嵌套字符串字面量(PEP 701 同引号嵌套),
# 避免其内部字符干扰 {} 深度/转换/格式分隔符定位
nq: t.CChar = c
j += 1
while j < content_len:
c2: t.CChar = content[j]
if c2 == '\\':
j += 2
continue
if c2 == nq:
j += 1
break
j += 1
continue
if c == '{':
depth += 1
elif c == '}':
@@ -1307,6 +1322,7 @@ def _parse_fstring(ps: Parser | t.CPtr, content: str,
fmt_pos = j
j += 1
if depth != 0:
_record_parse_err(ps)
ps.error_count += 1
break
# Extract expression text
@@ -1317,6 +1333,7 @@ def _parse_fstring(ps: Parser | t.CPtr, content: str,
expr_end = fmt_pos
expr_len: t.CSizeT = expr_end - i - 1
if expr_len == 0:
_record_parse_err(ps)
ps.error_count += 1
i = j + 1
continue
@@ -1356,6 +1373,7 @@ def _parse_fstring(ps: Parser | t.CPtr, content: str,
lit_pos += 1
i += 2
else:
_record_parse_err(ps)
ps.error_count += 1
i += 1
else: