Performance improvements (do not parse addresses or locations unless
location tracking is enabled). Allow profiling option. Add some comments and clean-ups to types/*.go.
This commit is contained in:
+20
-46
@@ -1,7 +1,6 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
@@ -17,26 +16,12 @@ func init() {
|
||||
reservedwords = regexp.MustCompile("^(void|char|short|int|long|float|double|signed|unsigned|_Bool|_Complex|const|restrict|volatile|struct|union|enum)$")
|
||||
}
|
||||
|
||||
//Parser is a function that takes the string to be parsed plus an input Node
|
||||
//and returns a new Node and the unparsed remainder string. If the parser fails
|
||||
//to parse anything in the input, it should return a nil Node.
|
||||
type Parser func(string, *Node) (string, *Node)
|
||||
|
||||
// Adders
|
||||
|
||||
//Child takes a parser and adds its output node (if non-nil) to the tree.
|
||||
//FIXME -- broken?
|
||||
func Child(p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
dbg("Child(%s %p)\n",n.Kind,n)
|
||||
s2,n2 := p(s,n)
|
||||
if n2 == nil {
|
||||
return s,nil
|
||||
}
|
||||
if n2 != n {
|
||||
dbg("Child(%p): AddChild()\n",p)
|
||||
n.AddChild(n2)
|
||||
}
|
||||
return s2,n
|
||||
}
|
||||
}
|
||||
// Adders -- add elements to the Node tree
|
||||
|
||||
//ChildOf takes a node and adds results of a parser to it as a child
|
||||
func ChildOf(ret *Node, p Parser) Parser {
|
||||
@@ -100,9 +85,10 @@ func NodeNamed(k string, p Parser) Parser {
|
||||
}
|
||||
}
|
||||
|
||||
// Combinators
|
||||
// Combinators -- combine one or more Parsers into a new Parser.
|
||||
|
||||
//Opt optionally runs a Parser, returning the input node if it fails
|
||||
//Opt optionally runs a Parser, returning the input Node (instead of nil)
|
||||
//if it fails
|
||||
func Opt(p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
s2,n2 := p(s,n)
|
||||
@@ -127,29 +113,6 @@ func OneOf(ps ...Parser) Parser {
|
||||
}
|
||||
}
|
||||
|
||||
//Doesn't work? May have side effects that do not get unwound.
|
||||
func Longest(ps ...Parser) Parser {
|
||||
dbg("Longest(%p)\n",ps)
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
ss := make([]string,len(ps))
|
||||
ns := make([]*Node,len(ps))
|
||||
//An arbitrarily large number so I don't have to import "math"
|
||||
minrem := 10000
|
||||
mini := 0
|
||||
for i,p := range ps {
|
||||
ss[i],ns[i] = p(s,n)
|
||||
if ns[i] != nil && len(ss[i]) < minrem {
|
||||
minrem = len(ss[i])
|
||||
mini = i
|
||||
}
|
||||
}
|
||||
if minrem < 10000 {
|
||||
return ss[mini],ns[mini]
|
||||
}
|
||||
return s,nil
|
||||
}
|
||||
}
|
||||
|
||||
//Seq applies parsers in sequence, adding results as children to the input
|
||||
//node. Returns nil and the input string unless the entire sequence succeeds
|
||||
func Seq(ps ...Parser) Parser {
|
||||
@@ -172,7 +135,8 @@ func Seq(ps ...Parser) Parser {
|
||||
return Children(p)
|
||||
}
|
||||
|
||||
//Like Seq but subsequent children are nested inside their earlier siblings.
|
||||
//Nest is like Seq but subsequent children are nested inside their earlier
|
||||
//siblings.
|
||||
func Nest(ps ...Parser) Parser {
|
||||
dbg("Nest(%p)\n",ps)
|
||||
p := func(s string, n *Node) (string, *Node) {
|
||||
@@ -212,32 +176,41 @@ func ZeroOrMore(p Parser) Parser {
|
||||
return Children(ret)
|
||||
}
|
||||
|
||||
//OneOrMore is ZeroOrMore, but fails (returns nil) if the input parser does
|
||||
//not match any elements.
|
||||
func OneOrMore(p Parser) Parser {
|
||||
return Seq(p,ZeroOrMore(p))
|
||||
}
|
||||
|
||||
//Parenthesized matches the input parser surrounded by literal parenthesis.
|
||||
func Parenthesized(p Parser) Parser {
|
||||
return Children(Seq(Lit("("),p,Lit(")")))
|
||||
}
|
||||
|
||||
//Bracketed matches the input parser surrounded by literal square brackets.
|
||||
func Bracketed(p Parser) Parser {
|
||||
return Seq(Lit("["),p,Lit("]"))
|
||||
}
|
||||
|
||||
//AngBracketed matches the input parser surrounded by literal angled brackets.
|
||||
func AngBracketed(p Parser) Parser {
|
||||
return Children(Seq(Lit("<"),p,Lit(">")))
|
||||
}
|
||||
|
||||
//CurlyBracketed matches the input parser surrounded by literal curly brackets.
|
||||
func CurlyBracketed(p Parser) Parser {
|
||||
return Children(Seq(Lit("{"),p,Lit("}")))
|
||||
}
|
||||
|
||||
// Recognizers
|
||||
// Recognizers -- these functions return parsers that match tokens in the input
|
||||
// stream. There is no separate tokenizer.
|
||||
|
||||
//Word matches an element with a word boundary after its end
|
||||
func Word(f string) Parser {
|
||||
return Lit(f,true)
|
||||
}
|
||||
|
||||
//Lit matches a literal string
|
||||
func Lit(f string, ws ...bool) Parser {
|
||||
word := false
|
||||
if len(ws) > 0 {
|
||||
@@ -261,6 +234,7 @@ func Lit(f string, ws ...bool) Parser {
|
||||
}
|
||||
}
|
||||
|
||||
//Regexp matches a regular expression at the beginning of the input string
|
||||
func Regexp(f string) Parser {
|
||||
f = "^" + f
|
||||
r := regexp.MustCompile(f)
|
||||
|
||||
+9
-18
@@ -33,7 +33,6 @@ var TypeParameters map[string]map[string]string
|
||||
var typedefs map[string]*Type
|
||||
|
||||
func (t *Type) Typedef() *Type {
|
||||
//return typedefs[t.BaseType().CType()]
|
||||
return typedefs[t.CType()]
|
||||
}
|
||||
|
||||
@@ -78,7 +77,6 @@ func AddTypedef(n,t string) {
|
||||
type Type struct {
|
||||
Node *Node
|
||||
Class string
|
||||
ctype string
|
||||
Variadic bool
|
||||
}
|
||||
|
||||
@@ -96,7 +94,7 @@ func clean(n *Node,c string) (*Node,bool) {
|
||||
}
|
||||
ret := NewNode(n.Kind,n.Content)
|
||||
ret.Children = n.Children
|
||||
//fmt.Printf("clean(%s,%s)\n",n.Ctype(),c)
|
||||
//fmt.Printf("clean(%s,%s)\n",n.CType(),c)
|
||||
recur := false
|
||||
if TypeParameters[c] != nil {
|
||||
for k,v := range TypeParameters[c] {
|
||||
@@ -127,7 +125,7 @@ func NewTypeFromString(t,c string) *Type {
|
||||
}
|
||||
if n2,ok := clean(n, c); ok {
|
||||
//found type parameters, re-parse
|
||||
return NewTypeFromString(n2.Ctype(),c)
|
||||
return NewTypeFromString(n2.CType(),c)
|
||||
}
|
||||
return &Type{
|
||||
Node: n,
|
||||
@@ -216,24 +214,16 @@ func (t *Type) _CType(attrib bool) string {
|
||||
//fmt.Println("nil sent to _CType()")
|
||||
return ""
|
||||
}
|
||||
//if !attrib && t.ctype != "" { // cache
|
||||
// return t.ctype
|
||||
//}
|
||||
var ct string
|
||||
if attrib {
|
||||
ignore := map[string]bool { "GenericList": true }
|
||||
ct = t.Node._Ctype(ignore)
|
||||
ct = t.Node._CType(ignore)
|
||||
} else {
|
||||
ct = t.Node.CtypeSimplified()
|
||||
ct = t.Node.CTypeSimplified()
|
||||
}
|
||||
ct = r_id.ReplaceAllString(ct,"NSObject*")
|
||||
ct = r_instancename.ReplaceAllString(ct,t.Class)
|
||||
ct = r_instancetype.ReplaceAllString(ct,t.Class + "*")
|
||||
if attrib {
|
||||
t._CType(false)
|
||||
} else {
|
||||
t.ctype = ct
|
||||
}
|
||||
return ct
|
||||
}
|
||||
|
||||
@@ -295,7 +285,7 @@ func (t *Type) IsFunctionPtr() bool {
|
||||
if td := t.Typedef(); td != nil {
|
||||
return td.IsFunctionPtr()
|
||||
}
|
||||
for pt := t.PointsTo(); pt != nil; pt = pt.PointsTo() {
|
||||
if pt := t.PointsTo(); pt != nil {
|
||||
return pt.IsFunction()
|
||||
}
|
||||
return false
|
||||
@@ -326,7 +316,8 @@ func (t *Type) IsPointer() bool {
|
||||
return t.Node.IsPointer()
|
||||
}
|
||||
|
||||
func (t *Type) CToGo(cval string) string { // cast C value to CGo
|
||||
// cast C value to CGo
|
||||
func (t *Type) CToGo(cval string) string {
|
||||
if t.IsPointer() {
|
||||
cval = "unsafe.Pointer(" + cval + ")"
|
||||
}
|
||||
@@ -334,7 +325,7 @@ func (t *Type) CToGo(cval string) string { // cast C value to CGo
|
||||
}
|
||||
|
||||
// Call a C function from Go with a given return type and parameter types
|
||||
func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
|
||||
func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type, fun bool) string {
|
||||
if rtype == nil {
|
||||
fmt.Println("nil sent to GoToC")
|
||||
return ""
|
||||
@@ -367,7 +358,7 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
|
||||
switch {
|
||||
case pt.Variadic:
|
||||
p = "unsafe.Pointer(&" + p + ")"
|
||||
case pt.IsPointer():
|
||||
case pt.IsPointer() && !fun:
|
||||
p = "unsafe.Pointer(" + pn + ")"
|
||||
default:
|
||||
p = "(" + pt.CGoType() + ")(" + pn + ")"
|
||||
|
||||
+4
-91
@@ -1,85 +1,6 @@
|
||||
package types
|
||||
|
||||
/* Parsers for recognizing type names in C/Objective-C
|
||||
|
||||
type-name:
|
||||
specifier-qualifier-list abstract-declarator<opt>
|
||||
abstract-declarator:
|
||||
pointer
|
||||
pointer<opt> direct-abstract-declarator
|
||||
direct-abstract-declarator:
|
||||
( abstract-declarator )
|
||||
direct-abstract-declarator<opt> [ type-qualifier-list<opt> assignment-expression<opt> ]
|
||||
direct-abstract-declarator<opt> [ static type-qualifier-list<opt> assignment-expression ]
|
||||
direct-abstract-declarator<opt> [ type-qualifier-list static assignment-expression ]
|
||||
direct-abstract-declarator<opt> [ * ]
|
||||
direct-abstract-declarator<opt> ( parameter-type-list<opt> )
|
||||
pointer:
|
||||
* type-qualifier-list<opt>
|
||||
* type-qualifier-list<opt> pointer
|
||||
parameter-type-list:
|
||||
parameter-list
|
||||
parameter-list , ...
|
||||
parameter-list:
|
||||
parameter-declaration
|
||||
parameter-list , parameter-declaration
|
||||
parameter-declaration:
|
||||
declaration-specifiers declarator
|
||||
declaration-specifiers abstract-declarator<opt>
|
||||
type-qualifier-list:
|
||||
type-qualifier
|
||||
type-qualifier-list type-qualifier
|
||||
specifier-qualifier-list:
|
||||
type-specifier specifier-qualifier-list<opt>
|
||||
type-qualifier specifier-qualifier-list<opt>
|
||||
type-specifier:
|
||||
void
|
||||
char
|
||||
short
|
||||
int
|
||||
long
|
||||
float
|
||||
double
|
||||
signed
|
||||
unsigned
|
||||
_Bool
|
||||
_Complex
|
||||
struct-or-union-specifier
|
||||
enum-specifier
|
||||
typedef-name
|
||||
type-qualifier:
|
||||
const
|
||||
restrict
|
||||
volatile
|
||||
struct-or-union-specifier:
|
||||
// DON'T DO struct-or-union identifier<opt> { struct-declaration-list }
|
||||
struct-or-union identifier
|
||||
struct-or-union:
|
||||
struct
|
||||
union
|
||||
struct-declaration-list:
|
||||
struct-declaration
|
||||
struct-declaration-list struct-declaration
|
||||
struct-declaration:
|
||||
specifier-qualifier-list struct-declarator-list ;
|
||||
struct-declarator-list:
|
||||
struct-declarator
|
||||
struct-declarator-list , struct-declarator
|
||||
struct-declarator:
|
||||
declarator
|
||||
declarator<opt>: constant-expression
|
||||
identifier:
|
||||
identifier-non-digit
|
||||
identifier identifier-nondigit
|
||||
identifier digit
|
||||
identifier-nondigit:
|
||||
nondigit
|
||||
universal-character-name
|
||||
nondigit:
|
||||
_ [a-zA-Z]
|
||||
digit:
|
||||
[0-9]
|
||||
*/
|
||||
// A parser to recognize type names in C/Objective-C
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
@@ -94,6 +15,7 @@ func init() {
|
||||
return !instancetype.MatchString(s) && !instancename.MatchString(s)
|
||||
}
|
||||
|
||||
//memoize the TypeName function for performance
|
||||
cache := map[string]*Node{}
|
||||
TypeName = func(s string, n *Node) (string, *Node) {
|
||||
if n2,ok := cache[s]; ok {
|
||||
@@ -105,6 +27,8 @@ func init() {
|
||||
}
|
||||
return s2,n2
|
||||
}
|
||||
//for debug purposes, the following line can be uncommented, which will
|
||||
//memoization memoization
|
||||
//TypeName = _TypeName
|
||||
}
|
||||
|
||||
@@ -120,7 +44,6 @@ func AbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||
Opt(Pointer),
|
||||
OneOrMore(DirectAbstractDeclarator)),
|
||||
Pointer,
|
||||
//Id,
|
||||
Block,
|
||||
)(s,n)
|
||||
}
|
||||
@@ -227,14 +150,6 @@ func NullableAnnotation(s string, n *Node) (string, *Node) {
|
||||
))(s,n)
|
||||
}
|
||||
|
||||
func Id(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
NodeNamed("Id",Lit("id")),
|
||||
Opt(TypeQualifierList),
|
||||
Opt(NullableAnnotation),
|
||||
)(s,n)
|
||||
}
|
||||
|
||||
func Pointer(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
NodeNamed("Pointer",Lit("*")),
|
||||
@@ -280,9 +195,7 @@ func TypeSpecifier(s string, n *Node) (string, *Node) {
|
||||
Word("unsigned"),
|
||||
Word("_Bool"),
|
||||
Word("_Complex"),
|
||||
//StructOrUnionSpecifier,
|
||||
EnumSpecifier,
|
||||
//TypedefName,
|
||||
))(s,n)
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -36,15 +36,15 @@ func Parse(s string) (*Node, error) {
|
||||
}
|
||||
|
||||
//Evaluate a node to determine if it is a pointer or array
|
||||
func (n *Node) isAbstract(k string) bool {
|
||||
if n.stripAbstract(k) == nil {
|
||||
func (n *Node) isIndirect(k string) bool {
|
||||
if n.stripIndirect(k) == nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//Strip one level of pointer or array indirection from a node
|
||||
func (n *Node) stripAbstract(k string) *Node {
|
||||
func (n *Node) stripIndirect(k string) *Node {
|
||||
if n == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -55,14 +55,14 @@ func (n *Node) stripAbstract(k string) *Node {
|
||||
ret := NewNode(n.Kind)
|
||||
cs := append([]*Node{},n.Children...)
|
||||
|
||||
dbg("stripAbstract(): i = %d\n",i)
|
||||
dbg("stripIndirect(): i = %d\n",i)
|
||||
//Scan backwords skipping TypeQualifier and NullableAnnotation tags
|
||||
for ;i > 0 &&
|
||||
(cs[i].Kind == "TypeQualifier" ||
|
||||
cs[i].Kind == "NullableAnnotation") ; i-- { }
|
||||
|
||||
if cs[i].Kind == k {
|
||||
dbg("stripAbstract(): last node is %s\n",k)
|
||||
dbg("stripIndirect(): last node is %s\n",k)
|
||||
ret.Children = cs[:i]
|
||||
return ret
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func (n *Node) stripAbstract(k string) *Node {
|
||||
//pointed to. Otherwise returns nil when called on non-pointer types.
|
||||
func (n *Node) PointsTo() *Node {
|
||||
dbg("PointsTo()\n")
|
||||
return n.stripAbstract("Pointer")
|
||||
return n.stripIndirect("Pointer")
|
||||
}
|
||||
|
||||
//IsPointer returns true if the node is a pointer
|
||||
@@ -108,7 +108,7 @@ func (n *Node) IsPointer() bool {
|
||||
//non-array types.
|
||||
func (n *Node) ArrayOf() *Node {
|
||||
dbg("ArrayOf()\n")
|
||||
return n.stripAbstract("Array")
|
||||
return n.stripIndirect("Array")
|
||||
}
|
||||
|
||||
//IsArray returns true if the node is an array
|
||||
|
||||
+8
-7
@@ -13,6 +13,8 @@ type Node struct {
|
||||
Children []*Node
|
||||
}
|
||||
|
||||
//NewNode returns a new node of kind k with an optional content string as its
|
||||
//second parameter.
|
||||
func NewNode(k string,cs ...string) *Node {
|
||||
c := ""
|
||||
if len(cs) > 0 {
|
||||
@@ -56,7 +58,6 @@ func (n *Node) AddChild(c *Node) *Node {
|
||||
// Skip literals
|
||||
if c.Kind == "Lit" { return n }
|
||||
|
||||
// Do we already have this child? (FIXME: Not needed?)
|
||||
for _,d := range n.Children {
|
||||
if c == d {
|
||||
return n
|
||||
@@ -85,21 +86,21 @@ func (n *Node) renameTypedefs(a,b string) (ret bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (n *Node) CtypeSimplified() string {
|
||||
func (n *Node) CTypeSimplified() string {
|
||||
ignore := map[string]bool{
|
||||
"NullableAnnotation": true,
|
||||
"KindQualifier": true,
|
||||
"TypeQualifier": true,
|
||||
"GenericList": true,
|
||||
}
|
||||
return n._Ctype(ignore)
|
||||
return n._CType(ignore)
|
||||
}
|
||||
|
||||
func (n *Node) Ctype() string {
|
||||
return n._Ctype(map[string]bool{})
|
||||
func (n *Node) CType() string {
|
||||
return n._CType(map[string]bool{})
|
||||
}
|
||||
|
||||
func (n *Node) _Ctype(ignore map[string]bool) string {
|
||||
func (n *Node) _CType(ignore map[string]bool) string {
|
||||
if n == nil || ignore[n.Kind] {
|
||||
return ""
|
||||
}
|
||||
@@ -108,7 +109,7 @@ func (n *Node) _Ctype(ignore map[string]bool) string {
|
||||
ret := []string{}
|
||||
if n == nil { return ret }
|
||||
for _,c := range n.Children {
|
||||
if x := c._Ctype(ignore); x != "" {
|
||||
if x := c._CType(ignore); x != "" {
|
||||
ret = append(ret, x)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user