34 lines
603 B
Go
34 lines
603 B
Go
package tokenize
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
func callerPanic(name, f string, data ...interface{}) {
|
|
filepos := callerBefore(name)
|
|
m := fmt.Sprintf(f, data...)
|
|
m = strings.Replace(m, "{caller}", filepos, -1)
|
|
m = strings.Replace(m, "{name}", name, -1)
|
|
panic(m)
|
|
}
|
|
|
|
func callerBefore(name string) string {
|
|
found := false
|
|
for i := 1; ; i++ {
|
|
pc, file, line, ok := runtime.Caller(i)
|
|
if found {
|
|
return fmt.Sprintf("%s:%d", file, line)
|
|
}
|
|
if !ok {
|
|
return "unknown caller"
|
|
}
|
|
f := runtime.FuncForPC(pc)
|
|
|
|
if strings.HasSuffix(f.Name(), "."+name) {
|
|
found = true
|
|
}
|
|
}
|
|
}
|