Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

114 lines
3.4 KiB
Go

package languages
import (
"regexp"
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
)
// Perl: `sub Name { ... }` for functions, `package Name;` for modules,
// `use Name` / `require Name` for imports. Calls come in many shapes
// (`&name`, `name()`, `Name->method`); we only chase the `name(`
// variety to keep noise down.
var (
perlSubRe = regexp.MustCompile(`(?m)^\s*sub\s+(\w+)`)
perlPackageRe = regexp.MustCompile(`(?m)^\s*package\s+([\w:]+)`)
perlUseRe = regexp.MustCompile(`(?m)^\s*(?:use|require)\s+([\w:]+)`)
perlCallRe = regexp.MustCompile(`\b([A-Za-z_]\w*)\s*\(`)
)
// PerlExtractor extracts Perl source using regex.
type PerlExtractor struct{}
func NewPerlExtractor() *PerlExtractor { return &PerlExtractor{} }
func (e *PerlExtractor) Language() string { return "perl" }
func (e *PerlExtractor) Extensions() []string { return []string{".pl", ".pm", ".t", ".pod"} }
func (e *PerlExtractor) Extract(filePath string, src []byte) (*parser.ExtractionResult, error) {
lines := strings.Split(string(src), "\n")
result := &parser.ExtractionResult{}
fileNode := &graph.Node{
ID: filePath, Kind: graph.KindFile, Name: filePath,
FilePath: filePath, StartLine: 1, EndLine: len(lines),
Language: "perl",
}
result.Nodes = append(result.Nodes, fileNode)
seen := make(map[string]bool)
add := func(name string, kind graph.NodeKind, start, end int) {
if name == "" || isPerlKeyword(name) {
return
}
id := filePath + "::" + name
if seen[id] {
return
}
seen[id] = true
result.Nodes = append(result.Nodes, &graph.Node{
ID: id, Kind: kind, Name: name,
FilePath: filePath, StartLine: start, EndLine: end,
Language: "perl",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: start,
})
}
for _, m := range perlPackageRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
add(name, graph.KindType, line, len(lines))
}
for _, m := range perlSubRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
add(name, graph.KindFunction, line, findBlockEnd(lines, line))
}
for _, m := range perlUseRe.FindAllSubmatchIndex(src, -1) {
mod := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: "unresolved::import::" + mod,
Kind: graph.EdgeImports, FilePath: filePath, Line: line,
})
}
funcRanges := buildFuncRanges(result)
for _, m := range perlCallRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
if isPerlKeyword(name) {
continue
}
line := lineAt(src, m[0])
callerID := findEnclosingFunc(funcRanges, line)
if callerID == "" || strings.HasSuffix(callerID, "::"+name) {
continue
}
result.Edges = append(result.Edges, &graph.Edge{
From: callerID, To: "unresolved::" + name,
Kind: graph.EdgeCalls, FilePath: filePath, Line: line,
})
}
return result, nil
}
func isPerlKeyword(s string) bool {
switch s {
case "if", "elsif", "else", "unless", "while", "until", "for", "foreach",
"do", "last", "next", "redo", "return", "my", "our", "local", "state",
"sub", "package", "use", "no", "require", "defined", "undef", "and",
"or", "not", "xor", "eq", "ne", "lt", "gt", "le", "ge", "cmp":
return true
}
return false
}
var _ parser.Extractor = (*PerlExtractor)(nil)