JOJ3/internal/parser/elf/parser.go
张泊明518370910136 a4232884c5
All checks were successful
build / build (push) Successful in 3m15s
build / build (pull_request) Successful in 3m14s
build / trigger-build-image (push) Has been skipped
build / trigger-build-image (pull_request) Has been skipped
refactor(parser/elf): modern structure
2025-05-24 04:18:16 -04:00

89 lines
2.6 KiB
Go

package elf
import (
"encoding/json"
"fmt"
"log/slog"
"github.com/joint-online-judge/JOJ3/internal/stage"
"github.com/mitchellh/mapstructure"
)
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
stdout := executorResult.Files["stdout"]
stderr := executorResult.Files["stderr"]
if executorResult.Status != stage.StatusAccepted {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf(
"Unexpected executor status: %s.\nStderr: %s",
executorResult.Status, stderr,
),
}
}
var topLevel Toplevel
err := json.Unmarshal([]byte(stdout), &topLevel)
if err != nil {
return stage.ParserResult{
Score: 0,
Comment: fmt.Sprintf("Failed to parse result: %s", err),
}
}
for _, module := range topLevel.Modules {
for _, entry := range module.Entries {
kind := entry[0].(string)
report := Report{}
err := mapstructure.Decode(entry[1], &report)
if err != nil {
slog.Error("elf parse", "mapstructure decode err", err)
}
slog.Debug("elf parse", "report file", report.File)
slog.Debug("elf parse", "report name", report.Name)
slog.Debug("elf parse", "report kind", kind)
for _, caseObj := range report.Cases {
switch kind {
case "ParenDep":
slog.Debug("elf parse", "binders", caseObj.Binders)
slog.Debug("elf parse", "context", caseObj.Context)
slog.Debug("elf parse", "depths", caseObj.Depths)
slog.Debug("elf parse", "code", caseObj.Code)
case "CodeLen":
slog.Debug("elf parse", "binders", caseObj.Binders)
slog.Debug("elf parse", "context", caseObj.Context)
slog.Debug("elf parse", "plain", caseObj.Plain)
slog.Debug("elf parse", "weighed", caseObj.Weighed)
slog.Debug("elf parse", "code", caseObj.Code)
case "OverArity":
slog.Debug("elf parse", "binders", caseObj.Binders)
slog.Debug("elf parse", "context", caseObj.Context)
slog.Debug("elf parse", "detail", caseObj.Detail)
slog.Debug("elf parse", "code", caseObj.Code)
case "CodeDup":
slog.Debug("elf parse", "similarity rate", caseObj.SimilarityRate)
for _, source := range caseObj.Sources {
slog.Debug("elf parse", "context", source.Context, "code", source.Code)
}
}
}
}
}
return stage.ParserResult{
Score: conf.Score,
Comment: conf.Comment,
}
}
func (*Elf) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
res := make([]stage.ParserResult, 0, len(results))
for _, result := range results {
res = append(res, Parse(result, *conf))
}
return res, false, nil
}