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

124 lines
3.8 KiB
Go

package languages
import (
"regexp"
"strings"
"github.com/zzet/gortex/internal/graph"
"github.com/zzet/gortex/internal/parser"
)
// Mojo is Python-flavored with Rust-like `fn` and `struct`. Bodies
// are indent-delimited. We capture `fn`/`def` functions, `struct`
// and `trait` types, plus `from ... import ...` and `import ...`.
var (
mojoFuncRe = regexp.MustCompile(`(?m)^\s*(?:async\s+)?(?:fn|def)\s+(\w+)\s*\(`)
mojoTypeRe = regexp.MustCompile(`(?m)^\s*(?:struct|trait)\s+(\w+)`)
mojoFromRe = regexp.MustCompile(`(?m)^\s*from\s+([\w.]+)\s+import\s+`)
mojoImportRe = regexp.MustCompile(`(?m)^\s*import\s+([\w.]+)`)
mojoCallRe = regexp.MustCompile(`\b([a-zA-Z_]\w*)\s*\(`)
)
// MojoExtractor extracts Mojo source using regex.
type MojoExtractor struct{}
func NewMojoExtractor() *MojoExtractor { return &MojoExtractor{} }
func (e *MojoExtractor) Language() string { return "mojo" }
func (e *MojoExtractor) Extensions() []string { return []string{".mojo", ".🔥"} }
func (e *MojoExtractor) 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: "mojo",
}
result.Nodes = append(result.Nodes, fileNode)
seen := make(map[string]bool)
add := func(name string, kind graph.NodeKind, start, end int) {
if name == "" || isMojoKeyword(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: "mojo",
})
result.Edges = append(result.Edges, &graph.Edge{
From: fileNode.ID, To: id, Kind: graph.EdgeDefines,
FilePath: filePath, Line: start,
})
}
for _, m := range mojoFuncRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
add(name, graph.KindFunction, line, findIndentedBlockEnd(lines, line))
}
for _, m := range mojoTypeRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
line := lineAt(src, m[0])
add(name, graph.KindType, line, findIndentedBlockEnd(lines, line))
}
for _, m := range mojoFromRe.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,
})
}
for _, m := range mojoImportRe.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 mojoCallRe.FindAllSubmatchIndex(src, -1) {
name := string(src[m[2]:m[3]])
if isMojoKeyword(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 isMojoKeyword(s string) bool {
switch s {
case "if", "elif", "else", "while", "for", "in", "not", "and", "or",
"fn", "def", "struct", "trait", "alias", "var", "let",
"return", "yield", "raise", "try", "except", "finally",
"import", "from", "as", "with", "pass", "break", "continue",
"True", "False", "None", "self", "async", "await", "owned",
"borrowed", "inout", "mut", "ref":
return true
}
return false
}
var _ parser.Extractor = (*MojoExtractor)(nil)