2021-01-05 14:43:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type entityTracker struct {
|
2021-01-05 14:44:31 +00:00
|
|
|
entityDetector *regexp.Regexp
|
|
|
|
openBracketDetector *regexp.Regexp
|
2021-01-05 14:43:12 +00:00
|
|
|
closeBracketDetector *regexp.Regexp
|
|
|
|
|
|
|
|
currentEntity string
|
2021-01-05 14:44:31 +00:00
|
|
|
lastEntity string
|
2021-01-05 14:43:12 +00:00
|
|
|
openBracketCount map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEntityTracker() (*entityTracker, error) {
|
|
|
|
ed, err := regexp.Compile("(func.+\\(.*\\).*|type .+) \\{")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
obd, err := regexp.Compile("\\{")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cbd, err := regexp.Compile("\\}")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &entityTracker{
|
2021-01-05 14:44:31 +00:00
|
|
|
entityDetector: ed,
|
|
|
|
openBracketDetector: obd,
|
2021-01-05 14:43:12 +00:00
|
|
|
closeBracketDetector: cbd,
|
2021-01-05 14:44:31 +00:00
|
|
|
openBracketCount: map[string]int{},
|
2021-01-05 14:43:12 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (et *entityTracker) Track(line string) {
|
|
|
|
ft := et.entityDetector.FindStringSubmatch(line)
|
|
|
|
if ft != nil {
|
|
|
|
// If a struct is declared inside a function
|
|
|
|
if et.openBracketCount[et.currentEntity] > 0 {
|
|
|
|
et.lastEntity = et.currentEntity
|
|
|
|
}
|
|
|
|
|
|
|
|
et.currentEntity = ft[1]
|
|
|
|
et.openBracketCount[et.currentEntity]++
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ob := et.openBracketDetector.FindAllString(line, -1)
|
|
|
|
if ob != nil {
|
|
|
|
et.openBracketCount[et.currentEntity] += len(ob)
|
|
|
|
}
|
|
|
|
|
|
|
|
cb := et.closeBracketDetector.FindAllString(line, -1)
|
|
|
|
if cb != nil {
|
|
|
|
et.openBracketCount[et.currentEntity] -= len(cb)
|
|
|
|
|
|
|
|
// If current entity has closed its final bracket delete the tracking from map
|
|
|
|
// then check if there is an active lastEntity, if so replace currentEntity with lastEntity
|
|
|
|
if et.openBracketCount[et.currentEntity] == 0 {
|
|
|
|
delete(et.openBracketCount, et.currentEntity)
|
|
|
|
|
|
|
|
if et.lastEntity != "" {
|
|
|
|
et.currentEntity = et.lastEntity
|
|
|
|
et.lastEntity = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (et entityTracker) Current() string {
|
|
|
|
if et.openBracketCount[et.currentEntity] == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return et.currentEntity
|
|
|
|
}
|