-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_web.go
More file actions
76 lines (68 loc) · 2.26 KB
/
parser_web.go
File metadata and controls
76 lines (68 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package repomap
import (
"regexp"
"strings"
)
// --- Ruby patterns ---
var rubyDecl = regexp.MustCompile(`^(?:def|class|module)\s+(\w+)`)
// --- PHP patterns ---
var (
phpClass = regexp.MustCompile(`^(?:abstract\s+|final\s+)?class\s+(\w+)`)
phpInterface = regexp.MustCompile(`^interface\s+(\w+)`)
phpTrait = regexp.MustCompile(`^trait\s+(\w+)`)
phpEnum = regexp.MustCompile(`^enum\s+(\w+)`)
phpFunction = regexp.MustCompile(`^(?:public\s+|protected\s+|private\s+)?(?:static\s+)?function\s+(\w+)`)
phpConst = regexp.MustCompile(`^(?:public\s+|protected\s+|private\s+)?const\s+(\w+)`)
phpUse = regexp.MustCompile(`^use\s+([^;{]+)`)
phpNamespace = regexp.MustCompile(`^namespace\s+([^;]+)`)
)
// parsePHP processes PHP lines.
func parsePHP(lines []string, fs *FileSymbols) {
scanLines(lines, func(e lineEntry) bool {
if e.trimmed == "<?php" || e.trimmed == "?>" || e.trimmed == "<?" {
return true
}
if strings.HasPrefix(e.trimmed, "//") || strings.HasPrefix(e.trimmed, "#") {
return true
}
if m := phpNamespace.FindStringSubmatch(e.trimmed); m != nil {
fs.Package = strings.TrimSpace(m[1])
return true
}
if tryAppendImport(phpUse, e, fs) {
return true
}
if m := phpFunction.FindStringSubmatch(e.trimmed); m != nil {
if strings.HasPrefix(m[1], "__") {
return true
}
kind := "function"
if len(e.line) > len(e.trimmed) {
kind = "method"
}
fs.Symbols = append(fs.Symbols, Symbol{Name: m[1], Kind: kind, Exported: true, Line: e.idx + 1})
return true
}
if tryAppendSymbol(phpClass, e, "class", true, fs) ||
tryAppendSymbol(phpInterface, e, "interface", true, fs) ||
tryAppendSymbol(phpTrait, e, "trait", true, fs) ||
tryAppendSymbol(phpEnum, e, "enum", true, fs) ||
tryAppendSymbol(phpConst, e, "constant", true, fs) {
return true
}
return true
})
}
// parseRuby processes Ruby lines.
func parseRuby(lines []string, fs *FileSymbols) {
for lineIdx, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "#") {
continue
}
if m := rubyDecl.FindStringSubmatch(trimmed); m != nil {
kind := strings.Fields(trimmed)[0] // def / class / module
fs.Symbols = append(fs.Symbols, Symbol{Name: m[1], Kind: kind, Exported: true, Line: lineIdx + 1})
}
}
}