fix(语法错误): 有模板的题报错行号多 1;else 后面写了条件被说成「少了冒号」,a+b=c 被说成「判断相等要用 ==」

全库 9182 条 Python 编译错误逐条过了一遍翻译表,1014 条的说明变了:

- 行号:判题跑的是「模板 + 代码」,运行时错误扣了模板行数、编译错误没扣,
  「第 N 行」和标红都落在下一行(691 条)。提交和运行例子两条路都扣上,
  gcc 的 main.c:N: 和代码摘录左边的行号一起扣。报错前先打了一条警告时
  File 那行是缩进的,行号原来解析不出来。
- 同一句报错要看出错那一行才分得开:expected ':' 九成是 else a<60:(行尾有冒号),
  「Maybe you meant '=='」只有 if / while 行才是真想判相等,a+b=c、a=96,b=92、
  print(1+1=2) 各有各的说法。
- 选了 Python 却交了 C 代码(294 条),直接说换语言。只凭 printf 不算:
  Python 里 print 手误成 printf 的不少。
- 单独说清楚的:elif: 没写条件、esle / else if、=< 写反、条件之间用逗号、
  漏了 +、√ ² ≤ ≥、顿号、全角 #、emoji(正则少了 u 标志)。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
xuyueandClaude Opus 5.5 committed 2026-09-30 10:44:09 +08:00
1 parent f413518a85
commit 96ce9725b2
6 files changed
+132 -20

No files matched your search

+4 -2
View File
@@ -14,7 +14,7 @@ import { asRecord } from "../routes/helpers"
import type { JudgeJobData } from "./job"
import { judgeConfigFor } from "./languages"
import { isAccepted, JudgeStatus, type JudgeStatusValue } from "./status"
import { parseProblemTemplate } from "./template"
import { parseProblemTemplate, shiftCompileLines } from "./template"
import { runSqlCase } from "./sql"
import {
nativeRuntimeError,
@@ -536,9 +536,11 @@ export async function judgeSubmission(job: JudgeJobData) {
let statisticInfo: Record<string, unknown> = {}
if (response.err) {
const errInfo =
typeof response.data === "string" ? response.data : JSON.stringify(response.data)
result = JudgeStatus.COMPILE_ERROR
statisticInfo = {
err_info: typeof response.data === "string" ? response.data : JSON.stringify(response.data),
err_info: shiftCompileLines(errInfo, template ? template.prepend.split("\n").length : 0),
score: 0,
}
} else {
+22
View File
@@ -8,3 +8,25 @@ export function parseProblemTemplate(template: string) {
append: section("APPEND"),
}
}
/**
* 编译错误里的行号扣掉模板拼在前面的行数,和运行时错误同一个口径(那边在
* runtime-diagnosis.ts 里扣)。不扣的话有模板的题「第 N 行」和编辑器标红都落在下一行
* —— 全库 691 条 Python 编译错误是这样。
*
* 认三种写法:Python 的 `File "…/solution.py", line N` 和 `(solution.py, line N)`,
* gcc 的 `main.c:N:M:` 以及它摘录代码时左边的 ` N | `。落在模板里的行(N 不大于
* 模板行数)是模板自己的错,原样留着。
*/
export function shiftCompileLines(message: string, prependLines: number) {
if (prependLines <= 0) return message
const shift = (line: string) => {
const n = Number(line)
return n > prependLines ? String(n - prependLines) : line
}
return message
.replace(/(solution\.py", line )(\d+)/g, (_, head: string, n: string) => head + shift(n))
.replace(/(\(solution\.py, line )(\d+)/g, (_, head: string, n: string) => head + shift(n))
.replace(/(main\.c(?:pp)?:)(\d+)(?=:)/g, (_, head: string, n: string) => head + shift(n))
.replace(/^(\s*)(\d+)( \|)/gm, (_, pad: string, n: string, bar: string) => pad + shift(n) + bar)
}
+5 -1
View File
@@ -4,6 +4,7 @@ import { config } from "../config"
import { postJudge, statusValue, type JudgeCase, type JudgeResponse } from "./run"
import { nativeRuntimeError, parsePythonTraceback } from "./runtime-diagnosis"
import { JudgeStatus, type JudgeStatusValue } from "./status"
import { shiftCompileLines } from "./template"
/**
* 试运行:「运行例子」「自己输入」、后台「生成测试点」。
@@ -148,7 +149,10 @@ async function judgeTrial(options: Parameters<typeof runTrial>[0]): Promise<Tria
if (parsed.err === "CompileError") {
return {
status: "compile-error",
message: typeof parsed.data === "string" ? parsed.data : "",
message:
typeof parsed.data === "string"
? shiftCompileLines(parsed.data, options.prependLines)
: "",
}
}
throw new Error(`JudgeServer error ${parsed.err}: ${JSON.stringify(parsed.data)}`)