🌐 AI搜索 & 代理 主页
Skip to content

Commit 083bf47

Browse files
committed
Minor changes to tiny_run.py, make the error source snippet more useful by adding line numbering and a ">" pointer to the erroneous line, and skip a line before the exception explanation
1 parent 20b3455 commit 083bf47

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

examples/tiny/tiny_run.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,18 @@ def main(argv: list[str] | None = None) -> int:
4040
parsed = parse_tiny(source_text)
4141
except pp.ParseBaseException as exc:
4242
# Print helpful location info
43-
error_line = exc.lineno
44-
fragment = "\n".join(source_text.splitlines()[error_line - 3 : error_line + 1])
43+
error_lineno = exc.lineno
44+
lineno_len = len(str(error_lineno + 1))
45+
*prelude, error_line, postlude = source_text.splitlines()[max(error_lineno - 3, 0) : error_lineno + 1]
46+
fragment = "\n".join(
47+
[
48+
*(f"{prelineno:>{lineno_len}}: {line}" for prelineno, line in enumerate(prelude, start = error_lineno - len(prelude))),
49+
f"{error_lineno:>{lineno_len}}: >{error_line}",
50+
f"{error_lineno + 1:>{lineno_len}}: {postlude}",
51+
]
52+
)
4553
print(fragment)
54+
print()
4655
print(exc.explain(depth=0))
4756
return 3
4857

0 commit comments

Comments
 (0)