feat: keyword parser (#23) #24
							
								
								
									
										4
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							|  | @ -18,3 +18,7 @@ | ||||||
| 	path = examples/dummy/error | 	path = examples/dummy/error | ||||||
| 	url = ssh://git@focs.ji.sjtu.edu.cn:2222/FOCS-dev/JOJ3-examples.git | 	url = ssh://git@focs.ji.sjtu.edu.cn:2222/FOCS-dev/JOJ3-examples.git | ||||||
| 	branch = dummy/error | 	branch = dummy/error | ||||||
|  | [submodule "examples/keyword/sillycode"] | ||||||
|  | 	path = examples/keyword/sillycode | ||||||
|  | 	url = ssh://git@focs.ji.sjtu.edu.cn:2222/FOCS-dev/JOJ3-examples.git | ||||||
|  | 	branch = keyword/sillycode | ||||||
|  |  | ||||||
|  | @ -1 +1 @@ | ||||||
| Subproject commit e6504575379ef49c1495dc635f7aba36f57dddc8 | Subproject commit a7001564a22f9807119efb7b8f4cf6f74d4c12fc | ||||||
							
								
								
									
										1
									
								
								examples/keyword/sillycode
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
								
							
						
						
									
										1
									
								
								examples/keyword/sillycode
									
									
									
									
									
										Submodule
									
								
							|  | @ -0,0 +1 @@ | ||||||
|  | Subproject commit 09cfdedda32061a03b4335e9a0f162c7891301c9 | ||||||
|  | @ -4,6 +4,7 @@ import ( | ||||||
| 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/cpplint" | 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/cpplint" | ||||||
| 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/diff" | 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/diff" | ||||||
| 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/dummy" | 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/dummy" | ||||||
|  | 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/keyword" | ||||||
| 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/resultstatus" | 	_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/resultstatus" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										9
									
								
								internal/parsers/keyword/meta.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								internal/parsers/keyword/meta.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | ||||||
|  | package keyword | ||||||
|  | 
 | ||||||
|  | import "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | ||||||
|  | 
 | ||||||
|  | var name = "keyword" | ||||||
|  | 
 | ||||||
|  | func init() { | ||||||
|  | 	stage.RegisterParser(name, &Keyword{}) | ||||||
|  | } | ||||||
							
								
								
									
										67
									
								
								internal/parsers/keyword/parser.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								internal/parsers/keyword/parser.go
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,67 @@ | ||||||
|  | package keyword | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"strings" | ||||||
|  | 
 | ||||||
|  | 	"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type Match struct { | ||||||
|  | 	Keyword string | ||||||
|  | 	Score   int | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type Conf struct { | ||||||
|  | 	FullScore  int | ||||||
|  | 	MinScore   int | ||||||
|  | 	Files      []string | ||||||
|  | 	EndOnMatch bool | ||||||
|  | 	Matches    []Match | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type Keyword struct{} | ||||||
|  | 
 | ||||||
|  | func Parse(executorResult stage.ExecutorResult, conf Conf) ( | ||||||
|  | 	stage.ParserResult, bool, | ||||||
|  | ) { | ||||||
|  | 	score := conf.FullScore | ||||||
|  | 	comment := "" | ||||||
|  | 	matched := false | ||||||
|  | 	for _, file := range conf.Files { | ||||||
|  | 		content := executorResult.Files[file] | ||||||
|  | 		for _, match := range conf.Matches { | ||||||
|  | 			count := strings.Count(content, match.Keyword) | ||||||
|  | 			if count > 0 { | ||||||
|  | 				matched = true | ||||||
|  | 				score -= count * match.Score | ||||||
|  | 				comment += fmt.Sprintf( | ||||||
|  | 					"Matched keyword %d time(s): %s\n", | ||||||
|  | 					count, match.Keyword) | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return stage.ParserResult{ | ||||||
|  | 		Score:   max(score, conf.MinScore), | ||||||
|  | 		Comment: comment, | ||||||
|  | 	}, matched | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (*Keyword) 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 | ||||||
|  | 	end := false | ||||||
|  | 	for _, result := range results { | ||||||
|  | 		tmp, matched := Parse(result, *conf) | ||||||
|  | 		if matched && conf.EndOnMatch { | ||||||
|  | 			end = true | ||||||
|  | 		} | ||||||
|  | 		res = append(res, tmp) | ||||||
|  | 	} | ||||||
|  | 	return res, end, nil | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user