JOJ3/internal/parser/clangtidy/parser.go
张泊明518370910136 a29fccbfa4
Some checks failed
build / trigger-build-image (push) Blocked by required conditions
build / build (push) Has been cancelled
submodules sync / sync (push) Has been cancelled
feat(parser): unify parsers conf (#74)
Reviewed-on: #74
Co-authored-by: Boming Zhang <bomingzh@sjtu.edu.cn>
Co-committed-by: Boming Zhang <bomingzh@sjtu.edu.cn>
2024-11-02 16:35:07 +08:00

70 lines
1.7 KiB
Go

package clangtidy
import (
"fmt"
"strings"
"github.com/criyle/go-judge/envexec"
"github.com/joint-online-judge/JOJ3/internal/stage"
)
type Match struct {
Keywords []string
Score int
}
type Conf struct {
Score int
RootDir string `default:"/w"`
Matches []Match
Stdout string `default:"stdout"`
Stderr string `default:"stderr"`
ForceQuitOnDeduct bool `default:"false"`
}
type ClangTidy struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
stdout := executorResult.Files[conf.Stdout]
stderr := executorResult.Files[conf.Stderr]
if executorResult.Status != stage.Status(envexec.StatusAccepted) {
if !((executorResult.Status == stage.Status(envexec.StatusNonzeroExitStatus)) &&
(executorResult.ExitStatus == 1)) {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected executor status: %s.\nStderr: %s",
executorResult.Status, stderr,
),
}
}
}
lines := strings.SplitAfter(stdout, "\n")
messages := ParseLines(lines, conf)
formattedMessages := Format(messages)
score, comment := GetResult(formattedMessages, conf)
return stage.ParserResult{
Score: score,
Comment: comment,
}
}
func (*ClangTidy) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
var res []stage.ParserResult
forceQuit := false
for _, result := range results {
parseRes := Parse(result, *conf)
if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score {
forceQuit = true
}
res = append(res, parseRes)
}
return res, forceQuit, nil
}