diff --git a/types/ctypes.go b/types/ctypes.go index b980978..47e730f 100644 --- a/types/ctypes.go +++ b/types/ctypes.go @@ -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), diff --git a/types/main.go b/types/main.go index 29cb05c..f2e4f5f 100644 --- a/types/main.go +++ b/types/main.go @@ -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