From f23d8932ead7a58e4f8a384c085a0de672e33488 Mon Sep 17 00:00:00 2001 From: Boming Zhang Date: Sat, 2 Nov 2024 15:53:37 +0800 Subject: [PATCH] feat(parser/keyword): force quit on deduct (#75) Reviewed-on: https://focs.ji.sjtu.edu.cn/git/JOJ/JOJ3/pulls/75 Co-authored-by: Boming Zhang Co-committed-by: Boming Zhang --- internal/parser/keyword/parser.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/internal/parser/keyword/parser.go b/internal/parser/keyword/parser.go index a5f9736..f811010 100644 --- a/internal/parser/keyword/parser.go +++ b/internal/parser/keyword/parser.go @@ -14,21 +14,18 @@ type Match struct { } type Conf struct { - Score int - FullScore int // TODO: remove me - Files []string - ForceQuitOnMatch bool - Matches []Match + Score int + FullScore int // TODO: remove me + Files []string + ForceQuitOnDeduct bool `default:"false"` + Matches []Match } type Keyword struct{} -func Parse(executorResult stage.ExecutorResult, conf Conf) ( - stage.ParserResult, bool, -) { +func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult { score := conf.Score comment := "" - matched := false for _, file := range conf.Files { content := executorResult.Files[file] for _, match := range conf.Matches { @@ -37,7 +34,6 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) ( count = min(count, match.MaxMatchCount) } if count > 0 { - matched = true score -= count * match.Score comment += fmt.Sprintf( "Matched keyword %d time(s): %s\n", @@ -48,7 +44,7 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) ( return stage.ParserResult{ Score: score, Comment: comment, - }, matched + } } func (*Keyword) Run(results []stage.ExecutorResult, confAny any) ( @@ -65,11 +61,11 @@ func (*Keyword) Run(results []stage.ExecutorResult, confAny any) ( var res []stage.ParserResult forceQuit := false for _, result := range results { - tmp, matched := Parse(result, *conf) - if matched && conf.ForceQuitOnMatch { + parseRes := Parse(result, *conf) + if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score { forceQuit = true } - res = append(res, tmp) + res = append(res, parseRes) } return res, forceQuit, nil }