Memoize types/TypeName() for performance.

types/Parse() returns an error if the parse is incomplete.
This commit is contained in:
Greg 2019-04-18 14:40:02 -04:00
parent da2008c597
commit 4867bb80f4
2 changed files with 23 additions and 4 deletions

View File

@ -2,7 +2,23 @@ package types
// Parsers for recognizing type names in C/Objective-C
func TypeName(s string, n *Node) (string, *Node) {
var TypeName func(s string, n *Node) (string, *Node)
func init() {
cache := map[string]*Node{}
TypeName = func(s string, n *Node) (string, *Node) {
if n2,ok := cache[s]; ok {
return "",n2
}
s2,n2 := _TypeName(s,n)
if s2 == "" {
cache[s] = n2
}
return s2,n2
}
}
func _TypeName(s string, n *Node) (string, *Node) {
return ChildOf(NewNode("TypeName"),Seq(
SpecifierQualifierList,
Opt(AbstractDeclarator),

View File

@ -15,9 +15,12 @@ func dbg(f string, xs ...interface{}) {
}
func Parse(s string) *Node {
_, n2 := TypeName(s,NewNode("AST"))
return n2
func Parse(s string) (*Node, error) {
s2, n := TypeName(s,NewNode("AST"))
if s2 != "" {
return n,fmt.Errorf("Parse failed or incomplete. Remainder: %s",s2)
}
return n, nil
}
//Evaluate a node to determine if it is a pointer or array