Run gofmt -w on everything. Add 'gogc' option to use Go garbage
collector for memory management.
This commit is contained in:
+59
-60
@@ -5,8 +5,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
whitespace *regexp.Regexp
|
||||
wordchars *regexp.Regexp
|
||||
whitespace *regexp.Regexp
|
||||
wordchars *regexp.Regexp
|
||||
reservedwords *regexp.Regexp
|
||||
)
|
||||
|
||||
@@ -26,19 +26,19 @@ type Parser func(string, *Node) (string, *Node)
|
||||
//ChildOf takes a node and adds results of a parser to it as a child
|
||||
func ChildOf(ret *Node, p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
dbg("ChildOf(%s %p) %s %p\n",ret.Kind,ret,n.Kind,n)
|
||||
s2,n2 := p(s,ret)
|
||||
dbg("ChildOf(%s %p) %s %p\n", ret.Kind, ret, n.Kind, n)
|
||||
s2, n2 := p(s, ret)
|
||||
if n2 == nil {
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
if n2 == ret {
|
||||
dbg("ChildOf(ret = %p) n2 = %p. WHAT\n",ret,n2)
|
||||
dbg("ChildOf(ret = %p) n2 = %p. WHAT\n", ret, n2)
|
||||
ret.Children = n2.Children
|
||||
} else {
|
||||
dbg("ChildOf(ret = %p) AddChild()\n",ret)
|
||||
dbg("ChildOf(ret = %p) AddChild()\n", ret)
|
||||
ret.AddChild(n2)
|
||||
}
|
||||
return s2,ret
|
||||
return s2, ret
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,18 +51,18 @@ func Children(ps ...Parser) Parser {
|
||||
}
|
||||
p := ps[0]
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
dbg("Children(%s %p)\n",n.Kind,n)
|
||||
s2,n2 := p(s,n)
|
||||
dbg("Children(%s %p)\n", n.Kind, n)
|
||||
s2, n2 := p(s, n)
|
||||
if n2 == nil {
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
for _,c := range n2.Children {
|
||||
dbg("Children(%s %p) AddChild() from %p\n",n.Kind,n,n2)
|
||||
for _, c := range n2.Children {
|
||||
dbg("Children(%s %p) AddChild() from %p\n", n.Kind, n, n2)
|
||||
if c != n {
|
||||
n.AddChild(c)
|
||||
}
|
||||
}
|
||||
return s2,n
|
||||
return s2, n
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,18 +70,18 @@ func Children(ps ...Parser) Parser {
|
||||
//to it as its children.
|
||||
func ChildrenOf(ret *Node, p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
dbg("ChildrenOf(%s %p) %s %p\n",ret.Kind,ret,n.Kind,n)
|
||||
return Children(p)(s,ret)
|
||||
dbg("ChildrenOf(%s %p) %s %p\n", ret.Kind, ret, n.Kind, n)
|
||||
return Children(p)(s, ret)
|
||||
}
|
||||
}
|
||||
|
||||
func NodeNamed(k string, p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
s2,n2 := p(s,n)
|
||||
s2, n2 := p(s, n)
|
||||
if n2 != nil {
|
||||
n2.Kind = k
|
||||
}
|
||||
return s2,n2
|
||||
return s2, n2
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,46 +91,46 @@ func NodeNamed(k string, p Parser) Parser {
|
||||
//if it fails
|
||||
func Opt(p Parser) Parser {
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
s2,n2 := p(s,n)
|
||||
s2, n2 := p(s, n)
|
||||
if n2 == nil {
|
||||
return s,n
|
||||
return s, n
|
||||
}
|
||||
return s2,n2
|
||||
return s2, n2
|
||||
}
|
||||
}
|
||||
|
||||
//OneOf picks the first matching parser and returns its result
|
||||
func OneOf(ps ...Parser) Parser {
|
||||
dbg("OneOf(%p)\n",ps)
|
||||
dbg("OneOf(%p)\n", ps)
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
for _,p := range ps {
|
||||
s2,n2 := p(s,n)
|
||||
for _, p := range ps {
|
||||
s2, n2 := p(s, n)
|
||||
if n2 != nil {
|
||||
return s2,n2
|
||||
return s2, n2
|
||||
}
|
||||
}
|
||||
return s,nil
|
||||
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 {
|
||||
dbg("Seq(%p)\n",ps)
|
||||
dbg("Seq(%p)\n", ps)
|
||||
p := func(s string, n *Node) (string, *Node) {
|
||||
ret := NewNode("Seq")
|
||||
s2, n2 := s,n
|
||||
for _,p := range ps {
|
||||
s2, n2 = p(s2,ret)
|
||||
s2, n2 := s, n
|
||||
for _, p := range ps {
|
||||
s2, n2 = p(s2, ret)
|
||||
if n2 == nil {
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
if n2 != ret {
|
||||
dbg("Seq(%p): AddChild()\n",ps)
|
||||
dbg("Seq(%p): AddChild()\n", ps)
|
||||
ret.AddChild(n2)
|
||||
}
|
||||
}
|
||||
return s2,ret
|
||||
return s2, ret
|
||||
}
|
||||
return Children(p)
|
||||
}
|
||||
@@ -138,21 +138,21 @@ func Seq(ps ...Parser) Parser {
|
||||
//Nest is like Seq but subsequent children are nested inside their earlier
|
||||
//siblings.
|
||||
func Nest(ps ...Parser) Parser {
|
||||
dbg("Nest(%p)\n",ps)
|
||||
dbg("Nest(%p)\n", ps)
|
||||
p := func(s string, n *Node) (string, *Node) {
|
||||
ret := NewNode("Nest")
|
||||
s2,n2 := Seq(ps...)(s,ret)
|
||||
s2, n2 := Seq(ps...)(s, ret)
|
||||
if n2 == nil {
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
ocs := n2.Children
|
||||
ret.Children = []*Node{}
|
||||
n3 := ret
|
||||
for _,c := range ocs {
|
||||
for _, c := range ocs {
|
||||
n3.AddChild(c)
|
||||
n3 = c
|
||||
}
|
||||
return s2,ret
|
||||
return s2, ret
|
||||
}
|
||||
return Children(p)
|
||||
}
|
||||
@@ -161,17 +161,17 @@ func Nest(ps ...Parser) Parser {
|
||||
func ZeroOrMore(p Parser) Parser {
|
||||
ret := func(s string, n *Node) (string, *Node) {
|
||||
ret := NewNode("ZeroOrMore")
|
||||
dbg("ZeroOrMore(%s %p) ret = %p\n",n.Kind,n,ret)
|
||||
dbg("ZeroOrMore(%s %p) ret = %p\n", n.Kind, n, ret)
|
||||
var s2 string
|
||||
var n2 *Node
|
||||
for s2,n2 = p(s,n); n2 != nil; s2,n2 = p(s2,n) {
|
||||
dbg("ZeroOrMore(%p): AddChild()\n",p)
|
||||
for s2, n2 = p(s, n); n2 != nil; s2, n2 = p(s2, n) {
|
||||
dbg("ZeroOrMore(%p): AddChild()\n", p)
|
||||
ret.AddChild(n2)
|
||||
}
|
||||
if len(ret.Children) > 0 {
|
||||
return s2,ret
|
||||
return s2, ret
|
||||
}
|
||||
return s,n
|
||||
return s, n
|
||||
}
|
||||
return Children(ret)
|
||||
}
|
||||
@@ -179,27 +179,27 @@ func ZeroOrMore(p Parser) Parser {
|
||||
//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))
|
||||
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(")")))
|
||||
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("]"))
|
||||
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(">")))
|
||||
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("}")))
|
||||
return Children(Seq(Lit("{"), p, Lit("}")))
|
||||
}
|
||||
|
||||
// Recognizers -- these functions return parsers that match tokens in the input
|
||||
@@ -207,7 +207,7 @@ func CurlyBracketed(p Parser) Parser {
|
||||
|
||||
//Word matches an element with a word boundary after its end
|
||||
func Word(f string) Parser {
|
||||
return Lit(f,true)
|
||||
return Lit(f, true)
|
||||
}
|
||||
|
||||
//Lit matches a literal string
|
||||
@@ -218,19 +218,19 @@ func Lit(f string, ws ...bool) Parser {
|
||||
}
|
||||
lenf := len(f)
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
ret := NewNode("Lit",f)
|
||||
dbg("Lit(%p) %s ret = %p\n",n,f,ret)
|
||||
ret := NewNode("Lit", f)
|
||||
dbg("Lit(%p) %s ret = %p\n", n, f, ret)
|
||||
if len(s) < lenf {
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
if f == s[:lenf] && !(word && len(s) > lenf && wordchars.Match([]byte{s[lenf]})) {
|
||||
adv := lenf
|
||||
if loc := whitespace.FindStringIndex(s[lenf:]); loc != nil {
|
||||
adv += loc[1]
|
||||
}
|
||||
return s[adv:],ret
|
||||
return s[adv:], ret
|
||||
}
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,18 +239,17 @@ func Regexp(f string) Parser {
|
||||
f = "^" + f
|
||||
r := regexp.MustCompile(f)
|
||||
return func(s string, n *Node) (string, *Node) {
|
||||
dbg("Regexp(%p) %s\n",n,f)
|
||||
dbg("Regexp(%p) %s\n", n, f)
|
||||
if loc := r.FindStringIndex(s); loc != nil {
|
||||
lenf := loc[1]
|
||||
adv := lenf
|
||||
if loc := whitespace.FindStringIndex(s[lenf:]); loc != nil {
|
||||
adv += loc[1]
|
||||
}
|
||||
ret := NewNode("Regexp",s[:lenf])
|
||||
dbg("Regexp(%p): ret = %p (%s)\n",n,ret,s[:lenf])
|
||||
return s[adv:],ret
|
||||
ret := NewNode("Regexp", s[:lenf])
|
||||
dbg("Regexp(%p): ret = %p (%s)\n", n, ret, s[:lenf])
|
||||
return s[adv:], ret
|
||||
}
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+58
-50
@@ -24,7 +24,6 @@ func IsGoInterface(gt string) bool {
|
||||
return goInterfaces[gt]
|
||||
}
|
||||
|
||||
|
||||
//TypeParameters maps, for each class, a TypedefName to a type, representing
|
||||
//the Objective-C type parameters for that class
|
||||
var TypeParameters map[string]map[string]string
|
||||
@@ -37,7 +36,7 @@ func (t *Type) Typedef() *Type {
|
||||
}
|
||||
|
||||
var (
|
||||
r_id *regexp.Regexp
|
||||
r_id *regexp.Regexp
|
||||
r_instancename *regexp.Regexp
|
||||
r_instancetype *regexp.Regexp
|
||||
)
|
||||
@@ -75,60 +74,60 @@ func AddTypedef(n string, tp *Type) {
|
||||
}
|
||||
|
||||
type Type struct {
|
||||
Node *Node
|
||||
Class string
|
||||
Node *Node
|
||||
Class string
|
||||
Variadic bool
|
||||
}
|
||||
|
||||
func (t *Type) CloneToClass(c string) *Type {
|
||||
return &Type{
|
||||
Node: t.Node,
|
||||
Class: c,
|
||||
Node: t.Node,
|
||||
Class: c,
|
||||
Variadic: t.Variadic,
|
||||
}
|
||||
}
|
||||
|
||||
func clean(n *Node,c string) (*Node,bool) {
|
||||
func clean(n *Node, c string) (*Node, bool) {
|
||||
if n == nil {
|
||||
return nil,false
|
||||
return nil, false
|
||||
}
|
||||
ret := NewNode(n.Kind,n.Content)
|
||||
ret := NewNode(n.Kind, n.Content)
|
||||
ret.Children = n.Children
|
||||
//fmt.Printf("clean(%s,%s)\n",n.CType(),c)
|
||||
recur := false
|
||||
if TypeParameters[c] != nil {
|
||||
for k,v := range TypeParameters[c] {
|
||||
recur = ret.renameTypedefs(k,v)
|
||||
for k, v := range TypeParameters[c] {
|
||||
recur = ret.renameTypedefs(k, v)
|
||||
}
|
||||
}
|
||||
if recur {
|
||||
clean(n, c)
|
||||
return ret,true
|
||||
return ret, true
|
||||
}
|
||||
return n,false
|
||||
return n, false
|
||||
}
|
||||
|
||||
func NewType(n *Node, c string) *Type {
|
||||
n2,_ := clean(n, c)
|
||||
n2, _ := clean(n, c)
|
||||
return &Type{
|
||||
Node: n2,
|
||||
Node: n2,
|
||||
Class: c,
|
||||
}
|
||||
}
|
||||
|
||||
func NewTypeFromString(t,c string) *Type {
|
||||
func NewTypeFromString(t, c string) *Type {
|
||||
//fmt.Printf("t/c: %s/%s\n",t,c)
|
||||
n,err := Parse(t)
|
||||
n, err := Parse(t)
|
||||
//fmt.Printf("%p %s",n,n.String())
|
||||
if err != nil {
|
||||
return &Type{}
|
||||
}
|
||||
if n2,ok := clean(n, c); ok {
|
||||
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,
|
||||
Node: n,
|
||||
Class: c,
|
||||
}
|
||||
}
|
||||
@@ -156,7 +155,7 @@ func (t *Type) BaseType() *Type {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
ret := NewType(
|
||||
ret := NewType(
|
||||
t.Node.BaseType(),
|
||||
t.Class,
|
||||
)
|
||||
@@ -172,11 +171,11 @@ func swapstars(s string) string {
|
||||
|
||||
func (t *Type) CGoType() string {
|
||||
ct := swapstars("C." + t.CType())
|
||||
ct = strings.ReplaceAll(ct,"unsigned ","u")
|
||||
ct = strings.ReplaceAll(ct,"signed ","u")
|
||||
ct = strings.ReplaceAll(ct,"long ","long")
|
||||
ct = strings.ReplaceAll(ct,"complex ","complex")
|
||||
ct = strings.ReplaceAll(ct," ","_")
|
||||
ct = strings.ReplaceAll(ct, "unsigned ", "u")
|
||||
ct = strings.ReplaceAll(ct, "signed ", "u")
|
||||
ct = strings.ReplaceAll(ct, "long ", "long")
|
||||
ct = strings.ReplaceAll(ct, "complex ", "complex")
|
||||
ct = strings.ReplaceAll(ct, " ", "_")
|
||||
return ct
|
||||
}
|
||||
|
||||
@@ -187,15 +186,15 @@ func (t *Type) GoType() string {
|
||||
func _goType(ct string) string {
|
||||
ct = swapstars(ct)
|
||||
ct = strings.Title(ct)
|
||||
ct = strings.ReplaceAll(ct," ","")
|
||||
ct = strings.ReplaceAll(ct,"Struct","")
|
||||
ct = strings.ReplaceAll(ct, " ", "")
|
||||
ct = strings.ReplaceAll(ct, "Struct", "")
|
||||
if IsGoInterface(ct) {
|
||||
return ct
|
||||
}
|
||||
/*
|
||||
if ct == "Id" {
|
||||
ct = "Id"
|
||||
}*/
|
||||
if ct == "Id" {
|
||||
ct = "Id"
|
||||
}*/
|
||||
if len(ct) > 1 && ShouldWrap(ct[1:]) {
|
||||
return ct[1:]
|
||||
}
|
||||
@@ -220,14 +219,14 @@ func (t *Type) _CType(attrib bool) string {
|
||||
}
|
||||
var ct string
|
||||
if attrib {
|
||||
ignore := map[string]bool { "GenericList": true }
|
||||
ignore := map[string]bool{"GenericList": true}
|
||||
ct = t.Node._CType(ignore)
|
||||
} else {
|
||||
ct = t.Node.CTypeSimplified()
|
||||
}
|
||||
ct = r_id.ReplaceAllString(ct,"NSObject*")
|
||||
ct = r_instancename.ReplaceAllString(ct,t.Class)
|
||||
ct = r_instancetype.ReplaceAllString(ct,t.Class + "*")
|
||||
ct = r_id.ReplaceAllString(ct, "NSObject*")
|
||||
ct = r_instancename.ReplaceAllString(ct, t.Class)
|
||||
ct = r_instancetype.ReplaceAllString(ct, t.Class+"*")
|
||||
return ct
|
||||
}
|
||||
|
||||
@@ -252,7 +251,7 @@ func (t *Type) GoTypeDecl() string {
|
||||
}
|
||||
return fmt.Sprintf(`
|
||||
type %s %s
|
||||
`,gt,cgt)
|
||||
`, gt, cgt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +265,7 @@ func (t *Type) GoInterfaceDecl() string {
|
||||
type %s interface {
|
||||
Ptr() unsafe.Pointer
|
||||
}
|
||||
`,gt)
|
||||
`, gt)
|
||||
}
|
||||
if IsGoInterface(super) {
|
||||
super = "Id"
|
||||
@@ -279,7 +278,7 @@ func (o Id) %s() %s {
|
||||
ret.ptr = o.ptr
|
||||
return ret
|
||||
}
|
||||
`,gt,super,gt,gt,gt,gt)
|
||||
`, gt, super, gt, gt, gt, gt)
|
||||
}
|
||||
|
||||
func (t *Type) IsFunctionPtr() bool {
|
||||
@@ -321,7 +320,7 @@ func (t *Type) ReturnType() *Type {
|
||||
return td.ReturnType()
|
||||
}
|
||||
if rt := t.Node.ReturnType(); rt != nil {
|
||||
return NewType(rt,t.Class)
|
||||
return NewType(rt, t.Class)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -341,11 +340,11 @@ func (t *Type) CToGo(cval string) string {
|
||||
if t.IsPointer() {
|
||||
cval = "unsafe.Pointer(" + cval + ")"
|
||||
}
|
||||
return fmt.Sprintf("(%s)(%s)",t.GoType(),cval)
|
||||
return fmt.Sprintf("(%s)(%s)", t.GoType(), cval)
|
||||
}
|
||||
|
||||
// Call a C function from Go with a given return type and parameter types
|
||||
func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fun bool) string {
|
||||
func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fun, gogc bool) string {
|
||||
if rtype == nil {
|
||||
//fmt.Println("nil sent to GoToC")
|
||||
return ""
|
||||
@@ -360,8 +359,8 @@ func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fu
|
||||
if rt != "void" {
|
||||
if sw {
|
||||
ret.WriteString(fmt.Sprintf(
|
||||
`ret := %s{}
|
||||
ret.ptr = `,rtgt))
|
||||
`ret := %s{}
|
||||
ret.ptr = `, rtgt))
|
||||
} else {
|
||||
if rtgt == "BOOL" {
|
||||
ret.WriteString("ret := (")
|
||||
@@ -377,7 +376,7 @@ func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fu
|
||||
ret.WriteString("C." + name + "(")
|
||||
parms := []string{}
|
||||
for i := 0; i < len(pnames); i++ {
|
||||
pn,pt := pnames[i],ptypes[i]
|
||||
pn, pt := pnames[i], ptypes[i]
|
||||
p := pn
|
||||
if (ShouldWrap(pt.GoType()) || IsGoInterface(pt.GoType())) && !pt.Variadic {
|
||||
p = pn + ".Ptr()"
|
||||
@@ -393,9 +392,9 @@ func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fu
|
||||
p = "(" + pt.CGoType() + ")(" + pn + ")"
|
||||
}
|
||||
}
|
||||
parms = append(parms,p)
|
||||
parms = append(parms, p)
|
||||
}
|
||||
ret.WriteString(strings.Join(parms,", "))
|
||||
ret.WriteString(strings.Join(parms, ", "))
|
||||
ret.WriteString(")")
|
||||
if rt != "void" && !sw {
|
||||
ret.WriteString(")")
|
||||
@@ -406,8 +405,10 @@ func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fu
|
||||
if rt == "BOOL" {
|
||||
ret.WriteString(" != 0")
|
||||
}
|
||||
for i,sname := range snames {
|
||||
if sname == "" { continue }
|
||||
for i, sname := range snames {
|
||||
if sname == "" {
|
||||
continue
|
||||
}
|
||||
ret.WriteString(fmt.Sprintf(`
|
||||
(*%s) = (*%s)[:cap(*%s)]
|
||||
for i := 0; i < len(*%s); i++ {
|
||||
@@ -416,12 +417,19 @@ func GoToC(name string, pnames, snames []string, rtype *Type, ptypes []*Type, fu
|
||||
break
|
||||
}
|
||||
(*%s)[i].ptr = %s[i]
|
||||
}`,pnames[i],pnames[i],pnames[i],pnames[i],sname,pnames[i],pnames[i],pnames[i],sname))
|
||||
}`, pnames[i], pnames[i], pnames[i], pnames[i], sname, pnames[i], pnames[i], pnames[i], sname))
|
||||
}
|
||||
if rt != "void" {
|
||||
if sw && gogc && rtgt != "NSAutoreleasePool" {
|
||||
// if m,_ :=regexp.MatchString(`Alloc(WithZone)?`,name); m {
|
||||
ret.WriteString(fmt.Sprintf(`
|
||||
runtime.SetFinalizer(&ret, func(o *%s) {
|
||||
o.Release()
|
||||
})`,rtgt))
|
||||
// }
|
||||
}
|
||||
ret.WriteString(`
|
||||
return ret`)
|
||||
}
|
||||
return ret.String()
|
||||
}
|
||||
|
||||
|
||||
+127
-125
@@ -9,7 +9,7 @@ import (
|
||||
func isNil(x interface{}) bool {
|
||||
return x == nil ||
|
||||
(reflect.ValueOf(x).Kind() == reflect.Ptr &&
|
||||
reflect.ValueOf(x).IsNil())
|
||||
reflect.ValueOf(x).IsNil())
|
||||
}
|
||||
|
||||
func TestType(t *testing.T) {
|
||||
@@ -20,169 +20,171 @@ func TestType(t *testing.T) {
|
||||
var tp *Type
|
||||
|
||||
chk := func(actual, expected interface{}) {
|
||||
t.Run(fmt.Sprintf("TestType%d",i),func(t *testing.T) {
|
||||
if isNil(actual) && isNil(expected) { return }
|
||||
if !reflect.DeepEqual(actual,expected) {
|
||||
t.Errorf("Test failed for %s\n",str)
|
||||
fmt.Println("Actual:\n",actual)
|
||||
fmt.Println("Expected:\n",expected)
|
||||
t.Run(fmt.Sprintf("TestType%d", i), func(t *testing.T) {
|
||||
if isNil(actual) && isNil(expected) {
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Test failed for %s\n", str)
|
||||
fmt.Println("Actual:\n", actual)
|
||||
fmt.Println("Expected:\n", expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
chk_newtype := func() {
|
||||
tp = NewTypeFromString(str,"")
|
||||
chk(tp,&Type{ Node: n, Class: "" })
|
||||
tp = NewTypeFromString(str, "")
|
||||
chk(tp, &Type{Node: n, Class: ""})
|
||||
}
|
||||
|
||||
//tests on nil Type pointers:
|
||||
chk(tp.BaseType(),nil)
|
||||
chk(tp.CType(),"")
|
||||
chk(tp.IsFunction(),false)
|
||||
chk(tp.IsPointer(),false)
|
||||
chk(tp.IsFunctionPtr(),false)
|
||||
chk(tp.IsValist(),false)
|
||||
chk(tp.BaseType(), nil)
|
||||
chk(tp.CType(), "")
|
||||
chk(tp.IsFunction(), false)
|
||||
chk(tp.IsPointer(), false)
|
||||
chk(tp.IsFunctionPtr(), false)
|
||||
chk(tp.IsValist(), false)
|
||||
|
||||
str = "int"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypeSpecifier", "int", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypeSpecifier", "int", []*Node{}}}}
|
||||
chk_newtype()
|
||||
tint := tp
|
||||
|
||||
str = "NSObject"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypedefName", "NSObject", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypedefName", "NSObject", []*Node{}}}}
|
||||
chk_newtype()
|
||||
nso := tp
|
||||
|
||||
str = "NSString"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypedefName", "NSString", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypedefName", "NSString", []*Node{}}}}
|
||||
chk_newtype()
|
||||
nst := tp
|
||||
|
||||
str = "NSString*"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypedefName", "NSString", []*Node{} },
|
||||
&Node{ "Pointer", "*", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypedefName", "NSString", []*Node{}},
|
||||
&Node{"Pointer", "*", []*Node{}}}}
|
||||
chk_newtype()
|
||||
chk(tp.IsPointer(),true)
|
||||
chk(tp.Typedef(),nil)
|
||||
chk(tp.IsPointer(), true)
|
||||
chk(tp.Typedef(), nil)
|
||||
tpNSString := tp
|
||||
|
||||
str = "myTypedef"
|
||||
AddTypedef("myTypedef",tp)
|
||||
n = &Node { "TypeName", "", []*Node{
|
||||
&Node{ "TypedefName", "myTypedef", []*Node{} }}}
|
||||
AddTypedef("myTypedef", tp)
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypedefName", "myTypedef", []*Node{}}}}
|
||||
chk_newtype()
|
||||
chk(tp.Typedef(),tpNSString)
|
||||
chk(tp.Typedef(), tpNSString)
|
||||
|
||||
str = "const NSArray <ObjectType * _Nonnull> *"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypeQualifier", "const", []*Node{} },
|
||||
&Node{ "TypedefName", "NSArray", []*Node{} },
|
||||
&Node{ "GenericList", "", []*Node{
|
||||
&Node{ "Generic", "", []*Node{
|
||||
&Node{ "TypedefName", "ObjectType", []*Node{} },
|
||||
&Node{ "Pointer", "*", []*Node{} },
|
||||
&Node{ "NullableAnnotation", "_Nonnull", []*Node{} } } } } },
|
||||
&Node{ "Pointer", "*", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypeQualifier", "const", []*Node{}},
|
||||
&Node{"TypedefName", "NSArray", []*Node{}},
|
||||
&Node{"GenericList", "", []*Node{
|
||||
&Node{"Generic", "", []*Node{
|
||||
&Node{"TypedefName", "ObjectType", []*Node{}},
|
||||
&Node{"Pointer", "*", []*Node{}},
|
||||
&Node{"NullableAnnotation", "_Nonnull", []*Node{}}}}}},
|
||||
&Node{"Pointer", "*", []*Node{}}}}
|
||||
chk_newtype()
|
||||
chk(tp.CType(),"NSArray*")
|
||||
chk(tp.CTypeAttrib(),"const NSArray*")
|
||||
chk(tp.CGoType(),"*C.NSArray")
|
||||
chk(tp.GoType(),"*NSArray")
|
||||
chk(tp.CType(), "NSArray*")
|
||||
chk(tp.CTypeAttrib(), "const NSArray*")
|
||||
chk(tp.CGoType(), "*C.NSArray")
|
||||
chk(tp.GoType(), "*NSArray")
|
||||
|
||||
SetTypeParam("MyClass","ObjectType","MyClass")
|
||||
SetTypeParam("MyClass", "ObjectType", "MyClass")
|
||||
str = "id<ObjectType *>"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypedefName", "id", []*Node{} },
|
||||
&Node{ "GenericList", "", []*Node{
|
||||
&Node{ "Generic", "", []*Node{
|
||||
&Node{ "TypedefName", "ObjectType", []*Node{} },
|
||||
&Node{ "Pointer", "*", []*Node{} } } } } } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypedefName", "id", []*Node{}},
|
||||
&Node{"GenericList", "", []*Node{
|
||||
&Node{"Generic", "", []*Node{
|
||||
&Node{"TypedefName", "ObjectType", []*Node{}},
|
||||
&Node{"Pointer", "*", []*Node{}}}}}}}}
|
||||
chk_newtype()
|
||||
tp = NewType(n,"MyClass")
|
||||
chk(tp.CType(),"NSObject*")
|
||||
tp = NewType(n, "MyClass")
|
||||
chk(tp.CType(), "NSObject*")
|
||||
chk(tp.String(),
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'id'
|
||||
-<GenericList> ''
|
||||
--<Generic> ''
|
||||
---<TypedefName> 'MyClass'
|
||||
---<Pointer> '*'
|
||||
`)
|
||||
x,_ := clean(nil,"MyClass")
|
||||
chk(x,nil)
|
||||
SetSuper("NSString","NSObject")
|
||||
chk(Super("NSString"),"NSObject")
|
||||
x, _ := clean(nil, "MyClass")
|
||||
chk(x, nil)
|
||||
SetSuper("NSString", "NSObject")
|
||||
chk(Super("NSString"), "NSObject")
|
||||
|
||||
tp2 := tp.CloneToClass("NSObject")
|
||||
chk(tp2.Class,"NSObject")
|
||||
chk(tp2.Class, "NSObject")
|
||||
|
||||
str = "you can't parse this"
|
||||
tp = NewTypeFromString(str,"")
|
||||
tp = NewTypeFromString(str, "")
|
||||
tp2 = &Type{}
|
||||
chk(tp,tp2)
|
||||
chk(tp, tp2)
|
||||
|
||||
str = "id<ObjectType *>"
|
||||
tp2 = NewTypeFromString(str,"MyClass")
|
||||
chk(tp.BaseType(),tp)
|
||||
tp2 = NewTypeFromString(str, "MyClass")
|
||||
chk(tp.BaseType(), tp)
|
||||
|
||||
str = "id<ObjectType *>*"
|
||||
tp = NewTypeFromString(str,"MyClass")
|
||||
chk(tp.BaseType(),tp2)
|
||||
chk(tp.PointsTo(),tp2)
|
||||
AddTypedef("myTypedef",tp)
|
||||
tp = NewTypeFromString(str, "MyClass")
|
||||
chk(tp.BaseType(), tp2)
|
||||
chk(tp.PointsTo(), tp2)
|
||||
AddTypedef("myTypedef", tp)
|
||||
|
||||
str = "myTypedef"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypedefName", "myTypedef", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypedefName", "myTypedef", []*Node{}}}}
|
||||
chk_newtype()
|
||||
chk(tp.PointsTo(),tp2)
|
||||
chk(tp2.PointsTo(),nil)
|
||||
chk(tp.PointsTo(), tp2)
|
||||
chk(tp2.PointsTo(), nil)
|
||||
|
||||
chk(tp.GoTypeDecl(),`
|
||||
chk(tp.GoTypeDecl(), `
|
||||
type MyTypedef **C.NSObject
|
||||
`)
|
||||
chk(tp2.GoTypeDecl(),"")
|
||||
chk(nst.GoTypeDecl(),`
|
||||
chk(tp2.GoTypeDecl(), "")
|
||||
chk(nst.GoTypeDecl(), `
|
||||
type NSString C.NSString
|
||||
`)
|
||||
str = "void"
|
||||
n = &Node{ "TypeName", "", []*Node{
|
||||
&Node{ "TypeSpecifier", "void", []*Node{} } } }
|
||||
n = &Node{"TypeName", "", []*Node{
|
||||
&Node{"TypeSpecifier", "void", []*Node{}}}}
|
||||
chk_newtype()
|
||||
chk(tp.GoTypeDecl(),"")
|
||||
chk(tp.GoTypeDecl(), "")
|
||||
void := tp
|
||||
|
||||
str = "BOOL"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
bl := tp
|
||||
|
||||
str = "void**"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.GoType(),"*unsafe.Pointer")
|
||||
chk(tp.CToGo("var"),"(*unsafe.Pointer)(unsafe.Pointer(var))")
|
||||
chk(tp.GoType(), "*unsafe.Pointer")
|
||||
chk(tp.CToGo("var"), "(*unsafe.Pointer)(unsafe.Pointer(var))")
|
||||
voidpp := tp
|
||||
|
||||
Wrap("NSObject")
|
||||
str = "NSObject*"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.GoType(),"NSObject")
|
||||
chk(tp.GoType(), "NSObject")
|
||||
|
||||
Wrap("NSString")
|
||||
chk(nso.GoTypeDecl(),`
|
||||
chk(nso.GoTypeDecl(), `
|
||||
type NSObject interface {
|
||||
Ptr() unsafe.Pointer
|
||||
}
|
||||
`)
|
||||
chk(nso.GoType(),"NSObject")
|
||||
chk(nso.GoType(), "NSObject")
|
||||
|
||||
chk(nst.GoTypeDecl(),`
|
||||
chk(nst.GoTypeDecl(), `
|
||||
type NSString struct { Id }
|
||||
func (o NSString) Ptr() unsafe.Pointer { return o.ptr }
|
||||
func (o Id) NSString() NSString {
|
||||
@@ -192,60 +194,60 @@ func (o Id) NSString() NSString {
|
||||
}
|
||||
`)
|
||||
str = "int(void)"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.IsFunction(),true)
|
||||
chk(tp.IsFunctionPtr(),false)
|
||||
chk(tp.ReturnType().CType(),"int")
|
||||
chk(tp.IsFunction(), true)
|
||||
chk(tp.IsFunctionPtr(), false)
|
||||
chk(tp.ReturnType().CType(), "int")
|
||||
|
||||
str = "int *(void)"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.IsFunction(),true)
|
||||
chk(tp.ReturnType().CType(),"int*")
|
||||
chk(tp.ReturnType().GoType(),"*Int")
|
||||
chk(tp.IsFunction(), true)
|
||||
chk(tp.ReturnType().CType(), "int*")
|
||||
chk(tp.ReturnType().GoType(), "*Int")
|
||||
fn := tp
|
||||
|
||||
AddTypedef("myTypedef",fn)
|
||||
AddTypedef("myTypedef", fn)
|
||||
str = "myTypedef"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.IsFunction(),true)
|
||||
chk(tp.IsFunctionPtr(),false)
|
||||
chk(tp.ReturnType().CType(),"int*")
|
||||
chk(tp.IsFunction(), true)
|
||||
chk(tp.IsFunctionPtr(), false)
|
||||
chk(tp.ReturnType().CType(), "int*")
|
||||
|
||||
str = "int (*)(void)"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.IsFunction(),false)
|
||||
chk(tp.IsPointer(),true)
|
||||
chk(tp.IsFunctionPtr(),true)
|
||||
chk(tp.ReturnType(),nil)
|
||||
chk(tp.IsFunction(), false)
|
||||
chk(tp.IsPointer(), true)
|
||||
chk(tp.IsFunctionPtr(), true)
|
||||
chk(tp.ReturnType(), nil)
|
||||
fnptr := tp
|
||||
|
||||
AddTypedef("myTypedef",fnptr)
|
||||
AddTypedef("myTypedef", fnptr)
|
||||
str = "myTypedef"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.IsFunction(),false)
|
||||
chk(tp.IsPointer(),true)
|
||||
chk(tp.IsFunctionPtr(),true)
|
||||
chk(tp.ReturnType(),nil)
|
||||
chk(tp.IsFunction(), false)
|
||||
chk(tp.IsPointer(), true)
|
||||
chk(tp.IsFunctionPtr(), true)
|
||||
chk(tp.ReturnType(), nil)
|
||||
|
||||
chk(tp.IsValist(),false)
|
||||
chk(tp.IsValist(), false)
|
||||
str = "__va_list_tag"
|
||||
n,_ = Parse(str)
|
||||
n, _ = Parse(str)
|
||||
chk_newtype()
|
||||
chk(tp.IsValist(),true)
|
||||
chk(tp.IsValist(), true)
|
||||
|
||||
str = "GoToC"
|
||||
var rtype *Type
|
||||
ptypes := []*Type{ nst, nso, tint, voidpp }
|
||||
pnames := []string{"p1","p2","p3", "p4"}
|
||||
snames := []string{"","","",""}
|
||||
ptypes := []*Type{nst, nso, tint, voidpp}
|
||||
pnames := []string{"p1", "p2", "p3", "p4"}
|
||||
snames := []string{"", "", "", ""}
|
||||
|
||||
chk_gotoc := func(expected string) {
|
||||
chk(GoToC("myFun",pnames,snames,rtype,ptypes,false),expected)
|
||||
chk(GoToC("myFun", pnames, snames, rtype, ptypes, false), expected)
|
||||
}
|
||||
|
||||
chk_gotoc("")
|
||||
@@ -255,36 +257,36 @@ func (o Id) NSString() NSString {
|
||||
|
||||
rtype = bl
|
||||
chk_gotoc(
|
||||
`ret := (C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))) != 0
|
||||
`ret := (C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))) != 0
|
||||
return ret`)
|
||||
|
||||
rtype = voidpp
|
||||
chk_gotoc(
|
||||
`ret := (*unsafe.Pointer)(unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))))
|
||||
`ret := (*unsafe.Pointer)(unsafe.Pointer(C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))))
|
||||
return ret`)
|
||||
|
||||
rtype = nst
|
||||
chk_gotoc(
|
||||
`ret := NSString{}
|
||||
`ret := NSString{}
|
||||
ret.ptr = C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))
|
||||
return ret`)
|
||||
|
||||
rtype = nso
|
||||
chk_gotoc(
|
||||
`ret := Id{}
|
||||
`ret := Id{}
|
||||
ret.ptr = C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))
|
||||
return ret`)
|
||||
|
||||
ptypes[1].Variadic = true
|
||||
chk_gotoc(
|
||||
`ret := Id{}
|
||||
`ret := Id{}
|
||||
ret.ptr = C.myFun(p1.Ptr(), unsafe.Pointer(&p2), (C.int)(p3), unsafe.Pointer(p4))
|
||||
return ret`)
|
||||
ptypes[1].Variadic = false
|
||||
|
||||
snames[1] = "p2p"
|
||||
chk_gotoc(
|
||||
`ret := Id{}
|
||||
`ret := Id{}
|
||||
ret.ptr = C.myFun(p1.Ptr(), p2.Ptr(), (C.int)(p3), unsafe.Pointer(p4))
|
||||
(*p2) = (*p2)[:cap(*p2)]
|
||||
for i := 0; i < len(*p2); i++ {
|
||||
@@ -298,7 +300,7 @@ func (o Id) NSString() NSString {
|
||||
snames[1] = ""
|
||||
snames[2] = "p3p"
|
||||
chk_gotoc(
|
||||
`ret := Id{}
|
||||
`ret := Id{}
|
||||
ret.ptr = C.myFun(p1.Ptr(), p2.Ptr(), unsafe.Pointer(&p3p[0]), unsafe.Pointer(p4))
|
||||
(*p3) = (*p3)[:cap(*p3)]
|
||||
for i := 0; i < len(*p3); i++ {
|
||||
@@ -309,8 +311,8 @@ func (o Id) NSString() NSString {
|
||||
(*p3)[i].ptr = p3p[i]
|
||||
}
|
||||
return ret`)
|
||||
chk(GoToC("myFun",pnames,snames,rtype,ptypes,true),
|
||||
`ret := Id{}
|
||||
chk(GoToC("myFun", pnames, snames, rtype, ptypes, true),
|
||||
`ret := Id{}
|
||||
ret.ptr = C.myFun(p1.Ptr(), p2.Ptr(), unsafe.Pointer(&p3p[0]), (**C.void)(p4))
|
||||
(*p3) = (*p3)[:cap(*p3)]
|
||||
for i := 0; i < len(*p3); i++ {
|
||||
|
||||
+80
-81
@@ -18,14 +18,14 @@ func init() {
|
||||
//memoize the TypeName function for performance
|
||||
cache := map[string]*Node{}
|
||||
TypeName = func(s string, n *Node) (string, *Node) {
|
||||
if n2,ok := cache[s]; ok {
|
||||
return "",n2
|
||||
if n2, ok := cache[s]; ok {
|
||||
return "", n2
|
||||
}
|
||||
s2,n2 := _TypeName(s,n)
|
||||
s2, n2 := _TypeName(s, n)
|
||||
if s2 == "" && cacheable(s) {
|
||||
cache[s] = n2
|
||||
}
|
||||
return s2,n2
|
||||
return s2, n2
|
||||
}
|
||||
//for debug purposes, the following line can be uncommented, which will
|
||||
//memoization memoization
|
||||
@@ -33,57 +33,57 @@ func init() {
|
||||
}
|
||||
|
||||
func MethodSignature(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("MethodSignature"),Seq(
|
||||
return ChildOf(NewNode("MethodSignature"), Seq(
|
||||
Parenthesized(TypeName),
|
||||
Identifier,
|
||||
Opt(MethodParameterList),
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func MethodParameterList(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
FirstMethodParameter,
|
||||
ZeroOrMore(MethodParameter),
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func FirstMethodParameter(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("MethodParameter"),Seq(
|
||||
return ChildOf(NewNode("MethodParameter"), Seq(
|
||||
Lit(":"),
|
||||
Parenthesized(TypeName),
|
||||
Identifier,
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func MethodParameter(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("MethodParameter"),Seq(
|
||||
return ChildOf(NewNode("MethodParameter"), Seq(
|
||||
Identifier,
|
||||
Lit(":"),
|
||||
Parenthesized(TypeName),
|
||||
Identifier,
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func _TypeName(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("TypeName"),Seq(
|
||||
return ChildOf(NewNode("TypeName"), Seq(
|
||||
SpecifierQualifierList,
|
||||
Opt(AbstractDeclarator),
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func AbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||
return OneOf(Seq(
|
||||
Opt(Pointer),
|
||||
OneOrMore(DirectAbstractDeclarator)),
|
||||
Opt(Pointer),
|
||||
OneOrMore(DirectAbstractDeclarator)),
|
||||
Pointer,
|
||||
Block,
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func ParenAbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("Parenthesized"),
|
||||
Parenthesized(AbstractDeclarator),
|
||||
)(s,n)
|
||||
Parenthesized(AbstractDeclarator),
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func ArrayDeclarator(s string, n *Node) (string, *Node) {
|
||||
@@ -95,26 +95,26 @@ func ArrayDeclarator(s string, n *Node) (string, *Node) {
|
||||
ChildOf(NewNode("Array"),
|
||||
Bracketed(Seq(
|
||||
Opt(TypeQualifierList),
|
||||
NodeNamed("Length",Regexp(`[\d]+|\*`))))),
|
||||
NodeNamed("Length", Regexp(`[\d]+|\*`))))),
|
||||
|
||||
ChildOf(NewNode("Array"),
|
||||
Bracketed(Seq(
|
||||
Word("static"),
|
||||
Opt(TypeQualifierList),
|
||||
NodeNamed("Length",Regexp(`[\d]+`))))),
|
||||
NodeNamed("Length", Regexp(`[\d]+`))))),
|
||||
|
||||
ChildOf(NewNode("Array"),
|
||||
Bracketed(Seq(
|
||||
Opt(TypeQualifierList),
|
||||
Word("static"),
|
||||
NodeNamed("Length",Regexp(`[\d]+`))))),
|
||||
)(s,n)
|
||||
NodeNamed("Length", Regexp(`[\d]+`))))),
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func FunctionDeclarator(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("Function"),
|
||||
Parenthesized(Opt(ParameterList)),
|
||||
)(s,n)
|
||||
Parenthesized(Opt(ParameterList)),
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func DirectAbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||
@@ -122,100 +122,100 @@ func DirectAbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||
ParenAbstractDeclarator,
|
||||
ArrayDeclarator,
|
||||
FunctionDeclarator,
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func ParameterList(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
Opt(OneOrMore(Seq(ParameterDeclaration,Lit(",")))),
|
||||
Opt(OneOrMore(Seq(ParameterDeclaration, Lit(",")))),
|
||||
ParameterDeclaration,
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func ParameterDeclaration(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("ParameterDeclaration"),OneOf(
|
||||
NodeNamed("Ellipsis",Lit("...")),
|
||||
Seq(DeclarationSpecifiers,Declarator),
|
||||
Seq(DeclarationSpecifiers,Opt(AbstractDeclarator)),
|
||||
))(s,n)
|
||||
return ChildOf(NewNode("ParameterDeclaration"), OneOf(
|
||||
NodeNamed("Ellipsis", Lit("...")),
|
||||
Seq(DeclarationSpecifiers, Declarator),
|
||||
Seq(DeclarationSpecifiers, Opt(AbstractDeclarator)),
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func DeclarationSpecifiers(s string, n *Node) (string, *Node) {
|
||||
return OneOf(
|
||||
Seq(StorageClassSpecifier,Opt(DeclarationSpecifiers)),
|
||||
Seq(TypeSpecifier,Opt(DeclarationSpecifiers)),
|
||||
Seq(StructOrUnionSpecifier,Opt(DeclarationSpecifiers)),
|
||||
Seq(TypeQualifier,Opt(DeclarationSpecifiers)),
|
||||
Seq(TypedefName,Opt(DeclarationSpecifiers)),
|
||||
Seq(StorageClassSpecifier, Opt(DeclarationSpecifiers)),
|
||||
Seq(TypeSpecifier, Opt(DeclarationSpecifiers)),
|
||||
Seq(StructOrUnionSpecifier, Opt(DeclarationSpecifiers)),
|
||||
Seq(TypeQualifier, Opt(DeclarationSpecifiers)),
|
||||
Seq(TypedefName, Opt(DeclarationSpecifiers)),
|
||||
// Seq(FunctionSpecifier,Opt(DeclarationSpecifiers)),
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func StorageClassSpecifier(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("StorageClassSpecifier",OneOf(
|
||||
return NodeNamed("StorageClassSpecifier", OneOf(
|
||||
Word("typedef"),
|
||||
Word("extern"),
|
||||
Word("static"),
|
||||
Word("auto"),
|
||||
Word("register"),
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func Declarator(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("Declarator"),
|
||||
Seq(ZeroOrMore(Pointer), DirectDeclarator))(s,n)
|
||||
Seq(ZeroOrMore(Pointer), DirectDeclarator))(s, n)
|
||||
}
|
||||
|
||||
func DirectDeclarator(s string, n *Node) (string, *Node) {
|
||||
return OneOf(
|
||||
Identifier,
|
||||
Parenthesized(Declarator),
|
||||
// INCOMPLETE
|
||||
)(s,n)
|
||||
Identifier,
|
||||
Parenthesized(Declarator),
|
||||
// INCOMPLETE
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func NullableAnnotation(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("NullableAnnotation",OneOf(
|
||||
return NodeNamed("NullableAnnotation", OneOf(
|
||||
Word("_Nullable"),
|
||||
Word("_Nonnull"),
|
||||
Word("_Null_unspecified"),
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func Pointer(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
NodeNamed("Pointer",Lit("*")),
|
||||
NodeNamed("Pointer", Lit("*")),
|
||||
Opt(TypeQualifierList),
|
||||
Opt(NullableAnnotation),
|
||||
Opt(Pointer),
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
//FIXME: not sure how correct this is...
|
||||
func Block(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
NodeNamed("Block",Lit("^")),
|
||||
NodeNamed("Block", Lit("^")),
|
||||
Opt(NullableAnnotation),
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func TypeQualifierList(s string, n *Node) (string, *Node) {
|
||||
return OneOrMore(TypeQualifier)(s,n)
|
||||
return OneOrMore(TypeQualifier)(s, n)
|
||||
}
|
||||
|
||||
func SpecifierQualifierList(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("SpecifierQualifierList",
|
||||
OneOf(
|
||||
Seq(TypeSpecifier,Opt(SpecifierQualifierList)),
|
||||
Seq(StructOrUnionSpecifier,Opt(SpecifierQualifierList)),
|
||||
Seq(TypedefName,Opt(SpecifierQualifierList)),
|
||||
Seq(TypeQualifier,Opt(SpecifierQualifierList)),
|
||||
Seq(TypeSpecifier, Opt(SpecifierQualifierList)),
|
||||
Seq(StructOrUnionSpecifier, Opt(SpecifierQualifierList)),
|
||||
Seq(TypedefName, Opt(SpecifierQualifierList)),
|
||||
Seq(TypeQualifier, Opt(SpecifierQualifierList)),
|
||||
),
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func TypeSpecifier(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("TypeSpecifier",OneOf(
|
||||
return NodeNamed("TypeSpecifier", OneOf(
|
||||
Word("void"),
|
||||
Word("char"),
|
||||
Word("short"),
|
||||
@@ -228,69 +228,68 @@ func TypeSpecifier(s string, n *Node) (string, *Node) {
|
||||
Word("_Bool"),
|
||||
Word("_Complex"),
|
||||
EnumSpecifier,
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func TypeQualifier(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("TypeQualifier",OneOf(
|
||||
return NodeNamed("TypeQualifier", OneOf(
|
||||
Word("const"),
|
||||
Word("restrict"),
|
||||
Word("volatile"),
|
||||
))(s,n)
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func StructOrUnionSpecifier(s string, n *Node) (string, *Node) {
|
||||
return OneOf(
|
||||
// Seq(StructOrUnion,Opt(Identifier),StructDeclarationList),
|
||||
Nest(StructOrUnion,Identifier),
|
||||
)(s,n)
|
||||
// Seq(StructOrUnion,Opt(Identifier),StructDeclarationList),
|
||||
Nest(StructOrUnion, Identifier),
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func StructOrUnion(s string, n *Node) (string, *Node) {
|
||||
return OneOf(
|
||||
NodeNamed("Struct",Word("struct")),
|
||||
NodeNamed("Union",Word("union")))(s,n)
|
||||
NodeNamed("Struct", Word("struct")),
|
||||
NodeNamed("Union", Word("union")))(s, n)
|
||||
}
|
||||
|
||||
func EnumSpecifier(s string, n *Node) (string, *Node) {
|
||||
return Nest(
|
||||
NodeNamed("Enum",Word("enum")),Identifier)(s,n)
|
||||
NodeNamed("Enum", Word("enum")), Identifier)(s, n)
|
||||
}
|
||||
|
||||
func Generic(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("Generic",TypeName)(s,n)
|
||||
return NodeNamed("Generic", TypeName)(s, n)
|
||||
}
|
||||
|
||||
func GenericList(s string, n *Node) (string, *Node) {
|
||||
return ChildOf(NewNode("GenericList"),Seq(
|
||||
return ChildOf(NewNode("GenericList"), Seq(
|
||||
Generic,
|
||||
ZeroOrMore(Seq(Lit(","),Generic)),
|
||||
))(s,n)
|
||||
ZeroOrMore(Seq(Lit(","), Generic)),
|
||||
))(s, n)
|
||||
}
|
||||
|
||||
func BareTypedefName(s string, n *Node) (string, *Node) {
|
||||
return NodeNamed("TypedefName",Identifier)(s,n)
|
||||
return NodeNamed("TypedefName", Identifier)(s, n)
|
||||
}
|
||||
|
||||
func TypedefName(s string, n *Node) (string, *Node) {
|
||||
return Seq(
|
||||
Opt(NodeNamed("KindQualifier",Lit("__kindof"))),
|
||||
Opt(NodeNamed("KindQualifier", Lit("__kindof"))),
|
||||
BareTypedefName,
|
||||
Opt(AngBracketed(GenericList)),
|
||||
Opt(NullableAnnotation),
|
||||
)(s,n)
|
||||
)(s, n)
|
||||
}
|
||||
|
||||
func Identifier(s string, n *Node) (string, *Node) {
|
||||
s2,n2 := NodeNamed("Identifier",
|
||||
Regexp(`[_a-zA-Z][_0-9a-zA-Z]*`))(s,n)
|
||||
s2, n2 := NodeNamed("Identifier",
|
||||
Regexp(`[_a-zA-Z][_0-9a-zA-Z]*`))(s, n)
|
||||
if n2 == nil {
|
||||
return s,nil
|
||||
return s, nil
|
||||
}
|
||||
if reservedwords.MatchString(n2.Content) {
|
||||
dbg("Identifier '%s' contains reserved word\n",n2.Content)
|
||||
return s,nil
|
||||
dbg("Identifier '%s' contains reserved word\n", n2.Content)
|
||||
return s, nil
|
||||
}
|
||||
return s2,n2
|
||||
return s2, n2
|
||||
}
|
||||
|
||||
|
||||
+22
-18
@@ -10,7 +10,7 @@ var (
|
||||
|
||||
func dbg(f string, xs ...interface{}) {
|
||||
if Debug {
|
||||
fmt.Printf(f,xs...)
|
||||
fmt.Printf(f, xs...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func (n *Node) HasFunc() bool {
|
||||
if n == nil {
|
||||
return false
|
||||
}
|
||||
for _,c := range n.Children {
|
||||
for _, c := range n.Children {
|
||||
if c.Kind == "Function" || c.HasFunc() {
|
||||
return true
|
||||
}
|
||||
@@ -27,10 +27,10 @@ func (n *Node) HasFunc() bool {
|
||||
}
|
||||
|
||||
func Parse(s string) (*Node, error) {
|
||||
s2, n := TypeName(s,NewNode("AST"))
|
||||
s2, n := TypeName(s, NewNode("AST"))
|
||||
//fmt.Printf("%p Parsed %s\n",n,s)
|
||||
if s2 != "" {
|
||||
return n,fmt.Errorf("Parse failed or incomplete. Remainder: %s",s2)
|
||||
return n, fmt.Errorf("Parse failed or incomplete. Remainder: %s", s2)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
@@ -53,25 +53,26 @@ func (n *Node) stripIndirect(k string) *Node {
|
||||
return nil
|
||||
}
|
||||
ret := NewNode(n.Kind)
|
||||
cs := append([]*Node{},n.Children...)
|
||||
cs := append([]*Node{}, n.Children...)
|
||||
|
||||
dbg("stripIndirect(): i = %d\n",i)
|
||||
dbg("stripIndirect(): i = %d\n", i)
|
||||
//Scan backwords skipping TypeQualifier and NullableAnnotation tags
|
||||
for ;i > 0 &&
|
||||
for ; i > 0 &&
|
||||
(cs[i].Kind == "TypeQualifier" ||
|
||||
cs[i].Kind == "NullableAnnotation") ; i-- { }
|
||||
cs[i].Kind == "NullableAnnotation"); i-- {
|
||||
}
|
||||
|
||||
if cs[i].Kind == k {
|
||||
dbg("stripIndirect(): last node is %s\n",k)
|
||||
dbg("stripIndirect(): last node is %s\n", k)
|
||||
ret.Children = cs[:i]
|
||||
return ret
|
||||
}
|
||||
if i > 1 && cs[i-1].Kind == "Parenthesized" {
|
||||
j := len(cs[i-1].Children) - 1
|
||||
for ;j > 0 &&
|
||||
for ; j > 0 &&
|
||||
(cs[i-1].Children[j].Kind == "TypeQualifier" ||
|
||||
cs[i-1].Children[j].Kind == "NullableAnnotation");
|
||||
j-- { }
|
||||
cs[i-1].Children[j].Kind == "NullableAnnotation"); j-- {
|
||||
}
|
||||
if cs[i-1].Children[j].Kind != k {
|
||||
return nil
|
||||
}
|
||||
@@ -121,8 +122,9 @@ func (n *Node) IsStruct() bool {
|
||||
return false
|
||||
}
|
||||
i := 0
|
||||
for ; i<len(n.Children) &&
|
||||
n.Children[i].Kind == "KindQualifier"; i++ {}
|
||||
for ; i < len(n.Children) &&
|
||||
n.Children[i].Kind == "KindQualifier"; i++ {
|
||||
}
|
||||
return n.Children[i].Kind == "Struct"
|
||||
}
|
||||
|
||||
@@ -150,8 +152,9 @@ func (n *Node) IsId() bool {
|
||||
return false
|
||||
}
|
||||
i := 0
|
||||
for ; i < len(n.Children) &&
|
||||
n.Children[i].Kind == "KindQualifier"; i++ {}
|
||||
for ; i < len(n.Children) &&
|
||||
n.Children[i].Kind == "KindQualifier"; i++ {
|
||||
}
|
||||
return !n.IsFunction() &&
|
||||
n.Children[i].Kind == "TypedefName" &&
|
||||
n.Children[i].Content == "id"
|
||||
@@ -162,8 +165,9 @@ func (n *Node) IsInstancetype() bool {
|
||||
return false
|
||||
}
|
||||
i := 0
|
||||
for ; i < len(n.Children) &&
|
||||
n.Children[i].Kind == "KindQualifier"; i++ {}
|
||||
for ; i < len(n.Children) &&
|
||||
n.Children[i].Kind == "KindQualifier"; i++ {
|
||||
}
|
||||
return n.Children[i].Kind == "TypedefName" &&
|
||||
n.Children[i].Content == "instancetype"
|
||||
}
|
||||
|
||||
+131
-130
@@ -8,64 +8,64 @@ import (
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
i := 1
|
||||
chk := func(input string,n *Node,expected string) {
|
||||
t.Run(fmt.Sprintf("Parse%d",i), func(t *testing.T) {
|
||||
chk := func(input string, n *Node, expected string) {
|
||||
t.Run(fmt.Sprintf("Parse%d", i), func(t *testing.T) {
|
||||
if n.String() != expected {
|
||||
t.Errorf("Mismatch parsing %s -- Got:\n%s\nExpected:\n%s\n",input,n.String(),expected)
|
||||
t.Errorf("Mismatch parsing %s -- Got:\n%s\nExpected:\n%s\n", input, n.String(), expected)
|
||||
}
|
||||
})
|
||||
i++
|
||||
}
|
||||
|
||||
runParseTest := func(input, expected string) {
|
||||
n,err := Parse(input)
|
||||
n, err := Parse(input)
|
||||
if err != nil {
|
||||
t.Errorf("Parse error parsing %s\n",input)
|
||||
t.Errorf("Parse error parsing %s\n", input)
|
||||
}
|
||||
chk(input,n,expected)
|
||||
chk(input, n, expected)
|
||||
}
|
||||
|
||||
runSigTest := func(input, expected string) {
|
||||
rem,n := MethodSignature(input,NewNode("AST"))
|
||||
rem, n := MethodSignature(input, NewNode("AST"))
|
||||
if rem != "" {
|
||||
t.Errorf("Parse error parsing %s. Remainder = %s\n",input,rem)
|
||||
t.Errorf("Parse error parsing %s. Remainder = %s\n", input, rem)
|
||||
}
|
||||
chk(input,n,expected)
|
||||
chk(input, n, expected)
|
||||
}
|
||||
|
||||
runParseTest(`int`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
`)
|
||||
runParseTest(`int`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
`)
|
||||
runParseTest(`char*`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'char'
|
||||
-<Pointer> '*'
|
||||
`)
|
||||
runParseTest(`struct Str`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<Struct> 'struct'
|
||||
--<Identifier> 'Str'
|
||||
`)
|
||||
runParseTest(`uint`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'uint'
|
||||
`)
|
||||
runParseTest(`uind`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'uind'
|
||||
`)
|
||||
runParseTest(`void ()`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'void'
|
||||
-<Function> ''
|
||||
`)
|
||||
runParseTest(`void (*)(struct NSRange)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'void'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -75,7 +75,7 @@ func TestParse(t *testing.T) {
|
||||
----<Identifier> 'NSRange'
|
||||
`)
|
||||
runParseTest(`void (* _Nullable)(int)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'void'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -85,20 +85,20 @@ func TestParse(t *testing.T) {
|
||||
---<TypeSpecifier> 'int'
|
||||
`)
|
||||
runParseTest(`NSRange[3]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'NSRange'
|
||||
-<Array> ''
|
||||
--<Length> '3'
|
||||
`)
|
||||
runParseTest(`struct _NSRange[3]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<Struct> 'struct'
|
||||
--<Identifier> '_NSRange'
|
||||
-<Array> ''
|
||||
--<Length> '3'
|
||||
`)
|
||||
runParseTest(`struct _NSRange (*)(int, char[])`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<Struct> 'struct'
|
||||
--<Identifier> '_NSRange'
|
||||
-<Parenthesized> ''
|
||||
@@ -111,7 +111,7 @@ func TestParse(t *testing.T) {
|
||||
---<Array> ''
|
||||
`)
|
||||
runParseTest(`struct _NSRange (*)(int, char[],NSRange)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<Struct> 'struct'
|
||||
--<Identifier> '_NSRange'
|
||||
-<Parenthesized> ''
|
||||
@@ -126,7 +126,7 @@ func TestParse(t *testing.T) {
|
||||
---<TypedefName> 'NSRange'
|
||||
`)
|
||||
runParseTest(`struct _NSRange (*)(int, char[],struct _NSRange)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<Struct> 'struct'
|
||||
--<Identifier> '_NSRange'
|
||||
-<Parenthesized> ''
|
||||
@@ -142,22 +142,22 @@ func TestParse(t *testing.T) {
|
||||
----<Identifier> '_NSRange'
|
||||
`)
|
||||
runParseTest(`mytype`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'mytype'
|
||||
`)
|
||||
runParseTest(`const int`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeQualifier> 'const'
|
||||
-<TypeSpecifier> 'int'
|
||||
`)
|
||||
runParseTest(`int[*]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Array> ''
|
||||
--<Length> '*'
|
||||
`)
|
||||
runParseTest(`int[3][5]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Array> ''
|
||||
--<Length> '3'
|
||||
@@ -165,31 +165,31 @@ func TestParse(t *testing.T) {
|
||||
--<Length> '5'
|
||||
`)
|
||||
runParseTest(`int *`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Pointer> '*'
|
||||
`)
|
||||
runParseTest(`int * _Nonnull`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Pointer> '*'
|
||||
-<NullableAnnotation> '_Nonnull'
|
||||
`)
|
||||
runParseTest(`int **`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Pointer> '*'
|
||||
-<Pointer> '*'
|
||||
`)
|
||||
runParseTest(`int *[3]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Pointer> '*'
|
||||
-<Array> ''
|
||||
--<Length> '3'
|
||||
`)
|
||||
runParseTest(`int (*)[3]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -197,7 +197,7 @@ func TestParse(t *testing.T) {
|
||||
--<Length> '3'
|
||||
`)
|
||||
runParseTest(`int (*)[*]`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -205,13 +205,13 @@ func TestParse(t *testing.T) {
|
||||
--<Length> '*'
|
||||
`)
|
||||
runParseTest(`int *()`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Pointer> '*'
|
||||
-<Function> ''
|
||||
`)
|
||||
runParseTest(`int (*)(void)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -220,7 +220,7 @@ func TestParse(t *testing.T) {
|
||||
---<TypeSpecifier> 'void'
|
||||
`)
|
||||
runParseTest(`int (*)(int, unsigned int)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -232,7 +232,7 @@ func TestParse(t *testing.T) {
|
||||
---<TypeSpecifier> 'int'
|
||||
`)
|
||||
runParseTest(`int (* _Nullable)(int, unsigned int)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -245,7 +245,7 @@ func TestParse(t *testing.T) {
|
||||
---<TypeSpecifier> 'int'
|
||||
`)
|
||||
runParseTest(`int (*const [])(unsigned int, ...)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypeSpecifier> 'int'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -259,7 +259,7 @@ func TestParse(t *testing.T) {
|
||||
---<Ellipsis> '...'
|
||||
`)
|
||||
runParseTest(`BOOL (* _Nullable)()`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'BOOL'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -267,7 +267,7 @@ func TestParse(t *testing.T) {
|
||||
-<Function> ''
|
||||
`)
|
||||
runParseTest(`BOOL (* _Nullable)(const void * _Nonnull, const void * _Nonnull)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'BOOL'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -285,7 +285,7 @@ func TestParse(t *testing.T) {
|
||||
---<NullableAnnotation> '_Nonnull'
|
||||
`)
|
||||
runParseTest(`BOOL (* _Nullable)(const void * _Nonnull, const void * _Nonnull, int (* _Nullable)(const void * _Nonnull))`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'BOOL'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -314,7 +314,7 @@ func TestParse(t *testing.T) {
|
||||
-----<NullableAnnotation> '_Nonnull'
|
||||
`)
|
||||
runParseTest(`BOOL (* _Nullable)(const void * _Nonnull, const void * _Nonnull, NSUInteger (* _Nullable)(const void * _Nonnull))`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'BOOL'
|
||||
-<Parenthesized> ''
|
||||
--<Pointer> '*'
|
||||
@@ -343,7 +343,7 @@ func TestParse(t *testing.T) {
|
||||
-----<NullableAnnotation> '_Nonnull'
|
||||
`)
|
||||
runParseTest(`NSEnumerator<ObjectType> * _Nonnull`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'NSEnumerator'
|
||||
-<GenericList> ''
|
||||
--<Generic> ''
|
||||
@@ -352,7 +352,7 @@ func TestParse(t *testing.T) {
|
||||
-<NullableAnnotation> '_Nonnull'
|
||||
`)
|
||||
runParseTest(`NSArray<NSString *> * _Nonnull`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'NSArray'
|
||||
-<GenericList> ''
|
||||
--<Generic> ''
|
||||
@@ -362,7 +362,7 @@ func TestParse(t *testing.T) {
|
||||
-<NullableAnnotation> '_Nonnull'
|
||||
`)
|
||||
runParseTest(`id _Nonnull (^ _Nonnull)(id _Nullable, NSArray<NSExpression *> * _Nonnull, NSMutableDictionary * _Nullable)`,
|
||||
`<TypeName> ''
|
||||
`<TypeName> ''
|
||||
-<TypedefName> 'id'
|
||||
-<NullableAnnotation> '_Nonnull'
|
||||
-<Parenthesized> ''
|
||||
@@ -386,13 +386,13 @@ func TestParse(t *testing.T) {
|
||||
---<NullableAnnotation> '_Nullable'
|
||||
`)
|
||||
runSigTest(`(void)pressed`,
|
||||
`<MethodSignature> ''
|
||||
`<MethodSignature> ''
|
||||
-<TypeName> ''
|
||||
--<TypeSpecifier> 'void'
|
||||
-<Identifier> 'pressed'
|
||||
`)
|
||||
runSigTest(`(void)performSelector:(SEL)aSelector target:(id)target argument:(id)arg order:(NSUInteger)order modes:(NSArray<NSRunLoopMode> *)modes`,
|
||||
`<MethodSignature> ''
|
||||
`<MethodSignature> ''
|
||||
-<TypeName> ''
|
||||
--<TypeSpecifier> 'void'
|
||||
-<Identifier> 'performSelector'
|
||||
@@ -432,30 +432,32 @@ func TestFuncs(t *testing.T) {
|
||||
var n *Node
|
||||
var err error
|
||||
f := func(input string, actual, expected interface{}) {
|
||||
t.Run(fmt.Sprintf("TestFunc%d",i),func (t *testing.T) {
|
||||
t.Run(fmt.Sprintf("TestFunc%d", i), func(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing %s\n",input)
|
||||
t.Errorf("Error parsing %s\n", input)
|
||||
}
|
||||
if actual == nil && expected == nil { return }
|
||||
if !reflect.DeepEqual(actual,expected) {
|
||||
t.Errorf("Test failed for %s\n",input)
|
||||
if actual == nil && expected == nil {
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Errorf("Test failed for %s\n", input)
|
||||
}
|
||||
})
|
||||
i++
|
||||
}
|
||||
str := "int*"
|
||||
n,err = Parse(str)
|
||||
f(str,n.HasFunc(),false)
|
||||
n, err = Parse(str)
|
||||
f(str, n.HasFunc(), false)
|
||||
str = "int (*)(void)"
|
||||
n,err = Parse(str)
|
||||
f(str,n.HasFunc(),true)
|
||||
n,err = nil,nil
|
||||
f("",n.HasFunc(),false)
|
||||
f("",n.IsId(),false)
|
||||
f("",n.IsFunction(),false)
|
||||
f("",n.IsInstancetype(),false)
|
||||
f("",n.IsArray(),false)
|
||||
f("",n.IsStruct(),false)
|
||||
n, err = Parse(str)
|
||||
f(str, n.HasFunc(), true)
|
||||
n, err = nil, nil
|
||||
f("", n.HasFunc(), false)
|
||||
f("", n.IsId(), false)
|
||||
f("", n.IsFunction(), false)
|
||||
f("", n.IsInstancetype(), false)
|
||||
f("", n.IsArray(), false)
|
||||
f("", n.IsStruct(), false)
|
||||
if n.BaseType() != nil {
|
||||
t.Errorf("BaseType() for nil node is not nil\n")
|
||||
}
|
||||
@@ -465,123 +467,122 @@ func TestFuncs(t *testing.T) {
|
||||
if n.ReturnType() != nil {
|
||||
t.Errorf("ReturnType() for nil node is not nil\n")
|
||||
}
|
||||
f("",n.String(),"")
|
||||
f("",n.Qualifiers(),"")
|
||||
f("",n.Annotations(),"")
|
||||
f("", n.String(), "")
|
||||
f("", n.Qualifiers(), "")
|
||||
f("", n.Annotations(), "")
|
||||
|
||||
str = "int"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Pointer"),false)
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Pointer"), false)
|
||||
if n.ReturnType() != nil {
|
||||
t.Errorf("Return type for non-function is not nil\n")
|
||||
}
|
||||
|
||||
str = "int*"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Pointer"),true)
|
||||
f(str,n.isIndirect("Array"),false)
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Pointer"), true)
|
||||
f(str, n.isIndirect("Array"), false)
|
||||
|
||||
str = "int* _Nullable"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Pointer"),true)
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Pointer"), true)
|
||||
|
||||
str = "int* _Nonnull"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Pointer"),true)
|
||||
f(str,n.CType(),"int* _Nonnull")
|
||||
f(str,n.CTypeSimplified(),"int*")
|
||||
f(str,n.Annotations(),"_Nonnull")
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Pointer"), true)
|
||||
f(str, n.CType(), "int* _Nonnull")
|
||||
f(str, n.CTypeSimplified(), "int*")
|
||||
f(str, n.Annotations(), "_Nonnull")
|
||||
|
||||
n,err = Parse("int[]")
|
||||
n2,err2 := Parse("int")
|
||||
n, err = Parse("int[]")
|
||||
n2, err2 := Parse("int")
|
||||
if err2 != nil {
|
||||
t.Errorf("Cannot parse int")
|
||||
}
|
||||
f(str,n.isIndirect("Pointer"),false)
|
||||
f(str,n.isIndirect("Array"),true)
|
||||
f(str,n.ArrayOf(),n2)
|
||||
f(str,n.CType(),"int[]")
|
||||
f(str,n.CTypeSimplified(),"int[]")
|
||||
f(str, n.isIndirect("Pointer"), false)
|
||||
f(str, n.isIndirect("Array"), true)
|
||||
f(str, n.ArrayOf(), n2)
|
||||
f(str, n.CType(), "int[]")
|
||||
f(str, n.CTypeSimplified(), "int[]")
|
||||
|
||||
str = "const int*[]"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Array"),true)
|
||||
f(str,n.Qualifiers(),"const")
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Array"), true)
|
||||
f(str, n.Qualifiers(), "const")
|
||||
|
||||
str = "int (*)[]"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Array"),true)
|
||||
f(str,n.isIndirect("Pointer"),true)
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Array"), true)
|
||||
f(str, n.isIndirect("Pointer"), true)
|
||||
|
||||
str = "int (* _Nullable * _Nonnull)[]"
|
||||
n,err = Parse(str)
|
||||
f(str,n.isIndirect("Pointer"),true)
|
||||
n, err = Parse(str)
|
||||
f(str, n.isIndirect("Pointer"), true)
|
||||
|
||||
str = "int (*)(void)"
|
||||
n,err = Parse(str)
|
||||
n2,err2 = Parse("int(void)")
|
||||
n, err = Parse(str)
|
||||
n2, err2 = Parse("int(void)")
|
||||
if err2 != nil {
|
||||
t.Errorf("Failed to parse int(void)")
|
||||
}
|
||||
f(str,n.PointsTo(),n2)
|
||||
f(str,n.IsPointer(),true)
|
||||
f(str,n.IsFunction(),false)
|
||||
f(str,n.CType(),"int (*)(void)")
|
||||
f(str, n.PointsTo(), n2)
|
||||
f(str, n.IsPointer(), true)
|
||||
f(str, n.IsFunction(), false)
|
||||
f(str, n.CType(), "int (*)(void)")
|
||||
|
||||
str = "int *(void)"
|
||||
n,err = Parse(str)
|
||||
f(str,n.IsFunction(),true)
|
||||
n2,_ = Parse("int*")
|
||||
f(str,n.ReturnType(),n2)
|
||||
n, err = Parse(str)
|
||||
f(str, n.IsFunction(), true)
|
||||
n2, _ = Parse("int*")
|
||||
f(str, n.ReturnType(), n2)
|
||||
|
||||
str = "__kindof NSRange"
|
||||
n,err = Parse(str)
|
||||
f(str,n.IsStruct(),false)
|
||||
f(str,n.IsInstancetype(),false)
|
||||
n, err = Parse(str)
|
||||
f(str, n.IsStruct(), false)
|
||||
f(str, n.IsInstancetype(), false)
|
||||
|
||||
str = "struct NSRange"
|
||||
n,err = Parse(str)
|
||||
f(str,n.IsStruct(),true)
|
||||
f(str,n.IsFunction(),false)
|
||||
n, err = Parse(str)
|
||||
f(str, n.IsStruct(), true)
|
||||
f(str, n.IsFunction(), false)
|
||||
|
||||
str = "id"
|
||||
n,err = Parse(str)
|
||||
f(str,n.IsId(),true)
|
||||
f(str,n.IsPointer(),true)
|
||||
n, err = Parse(str)
|
||||
f(str, n.IsId(), true)
|
||||
f(str, n.IsPointer(), true)
|
||||
|
||||
str = "int * _Nullable * _Nonnull"
|
||||
n,err = Parse(str)
|
||||
n2,_ = Parse("int")
|
||||
f(str,n.BaseType(),n2)
|
||||
n, err = Parse(str)
|
||||
n2, _ = Parse("int")
|
||||
f(str, n.BaseType(), n2)
|
||||
|
||||
str = "__kindof id"
|
||||
n,err = Parse(str)
|
||||
f(str,n.IsId(),true)
|
||||
f(str,n.IsPointer(),true)
|
||||
|
||||
n, err = Parse(str)
|
||||
f(str, n.IsId(), true)
|
||||
f(str, n.IsPointer(), true)
|
||||
|
||||
str = "NSArray<ObjectType> *"
|
||||
n,err = Parse(str)
|
||||
f(str,n.CType(),"NSArray <ObjectType>*")
|
||||
f(str,n.CTypeSimplified(),"NSArray*")
|
||||
n, err = Parse(str)
|
||||
f(str, n.CType(), "NSArray <ObjectType>*")
|
||||
f(str, n.CTypeSimplified(), "NSArray*")
|
||||
|
||||
str = "NSRange *(int, NSRange *)"
|
||||
n,err = Parse(str)
|
||||
f(str,n.renameTypedefs("NSRange","struct NSRange"),true)
|
||||
f(str,n.CType(),"struct NSRange* (int, struct NSRange*)")
|
||||
n, err = Parse(str)
|
||||
f(str, n.renameTypedefs("NSRange", "struct NSRange"), true)
|
||||
f(str, n.CType(), "struct NSRange* (int, struct NSRange*)")
|
||||
|
||||
str = "int*%&#$"
|
||||
n,err = Parse(str)
|
||||
n, err = Parse(str)
|
||||
if err == nil {
|
||||
fmt.Printf("%s\n",n.String())
|
||||
t.Errorf("Parse should have failed for %s\n",str)
|
||||
fmt.Printf("%s\n", n.String())
|
||||
t.Errorf("Parse should have failed for %s\n", str)
|
||||
}
|
||||
|
||||
var r func(i int) *Node
|
||||
r = func(i int) *Node {
|
||||
n := &Node{}
|
||||
if i > 0 {
|
||||
n.Children = []*Node{ r(i-1) }
|
||||
n.Children = []*Node{r(i - 1)}
|
||||
}
|
||||
return n
|
||||
}
|
||||
@@ -595,7 +596,7 @@ func TestFuncs(t *testing.T) {
|
||||
|
||||
func ExampleDebug() {
|
||||
Debug = true
|
||||
dbg("one %s two %d","string",15)
|
||||
dbg("one %s two %d", "string", 15)
|
||||
// Output: one string two 15
|
||||
Debug = false
|
||||
}
|
||||
|
||||
+51
-40
@@ -10,18 +10,18 @@ import (
|
||||
|
||||
type Node struct {
|
||||
Kind, Content string
|
||||
Children []*Node
|
||||
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 {
|
||||
func NewNode(k string, cs ...string) *Node {
|
||||
c := ""
|
||||
if len(cs) > 0 {
|
||||
c = cs[0]
|
||||
}
|
||||
ret := &Node{ Kind: k, Content: c, Children: []*Node{} }
|
||||
dbg("NewNode(%p) %s\n",ret,ret.Kind)
|
||||
ret := &Node{Kind: k, Content: c, Children: []*Node{}}
|
||||
dbg("NewNode(%p) %s\n", ret, ret.Kind)
|
||||
return ret
|
||||
}
|
||||
|
||||
@@ -37,48 +37,58 @@ func (n *Node) String(ls ...int) string {
|
||||
return "(*Node)String(): Recursion too deep"
|
||||
}
|
||||
}
|
||||
prefix := strings.Repeat("-",level)
|
||||
ret.WriteString(fmt.Sprintf("%s<%s> '%s'\n",prefix, n.Kind, n.Content))
|
||||
for _,c := range n.Children {
|
||||
ret.WriteString(c.String(level+1))
|
||||
prefix := strings.Repeat("-", level)
|
||||
ret.WriteString(fmt.Sprintf("%s<%s> '%s'\n", prefix, n.Kind, n.Content))
|
||||
for _, c := range n.Children {
|
||||
ret.WriteString(c.String(level + 1))
|
||||
}
|
||||
return ret.String()
|
||||
}
|
||||
|
||||
func (n *Node) AddChild(c *Node) *Node {
|
||||
erp := func(s string) {
|
||||
dbg("(%p)AddChild(%p): %s\n",n,c,s)
|
||||
dbg("(%p)AddChild(%p): %s\n", n, c, s)
|
||||
os.Exit(-1)
|
||||
}
|
||||
if n == nil { erp("Called on nil node") }
|
||||
if c == nil { erp("Child is nil") }
|
||||
if n == c { erp("Node cannot be its own child") }
|
||||
if n == nil {
|
||||
erp("Called on nil node")
|
||||
}
|
||||
if c == nil {
|
||||
erp("Child is nil")
|
||||
}
|
||||
if n == c {
|
||||
erp("Node cannot be its own child")
|
||||
}
|
||||
|
||||
// Skip literals
|
||||
if c.Kind == "Lit" { return n }
|
||||
if c.Kind == "Lit" {
|
||||
return n
|
||||
}
|
||||
|
||||
for _,d := range n.Children {
|
||||
for _, d := range n.Children {
|
||||
if c == d {
|
||||
return n
|
||||
}
|
||||
}
|
||||
dbg("(%p)AddChild(%p)\n",n,c)
|
||||
n.Children = append(n.Children,c)
|
||||
dbg("(%p)AddChild(%p)\n", n, c)
|
||||
n.Children = append(n.Children, c)
|
||||
return n
|
||||
}
|
||||
|
||||
//returns true if anything gets renamed
|
||||
func (n *Node) renameTypedefs(a,b string) (ret bool) {
|
||||
func (n *Node) renameTypedefs(a, b string) (ret bool) {
|
||||
ret = false
|
||||
if n == nil { return }
|
||||
for i,c := range n.Children {
|
||||
if n == nil {
|
||||
return
|
||||
}
|
||||
for i, c := range n.Children {
|
||||
if c.Kind == "TypedefName" && c.Content == a {
|
||||
ret = true
|
||||
n.Children[i] = NewNode("TypedefName", b)
|
||||
n.Children[i].Children = c.Children
|
||||
}
|
||||
if len(c.Children) > 0 {
|
||||
ret2 := c.renameTypedefs(a,b)
|
||||
ret2 := c.renameTypedefs(a, b)
|
||||
ret = ret || ret2
|
||||
}
|
||||
}
|
||||
@@ -88,9 +98,9 @@ func (n *Node) renameTypedefs(a,b string) (ret bool) {
|
||||
func (n *Node) CTypeSimplified() string {
|
||||
ignore := map[string]bool{
|
||||
"NullableAnnotation": true,
|
||||
"KindQualifier": true,
|
||||
"TypeQualifier": true,
|
||||
"GenericList": true,
|
||||
"KindQualifier": true,
|
||||
"TypeQualifier": true,
|
||||
"GenericList": true,
|
||||
}
|
||||
return n._CType(ignore)
|
||||
}
|
||||
@@ -106,8 +116,10 @@ func (n *Node) _CType(ignore map[string]bool) string {
|
||||
var ret strings.Builder
|
||||
childStrings := func(n *Node) []string {
|
||||
ret := []string{}
|
||||
if n == nil { return ret }
|
||||
for _,c := range n.Children {
|
||||
if n == nil {
|
||||
return ret
|
||||
}
|
||||
for _, c := range n.Children {
|
||||
if x := c._CType(ignore); x != "" {
|
||||
ret = append(ret, x)
|
||||
}
|
||||
@@ -116,41 +128,40 @@ func (n *Node) _CType(ignore map[string]bool) string {
|
||||
}
|
||||
switch n.Kind {
|
||||
case "Parenthesized":
|
||||
ret.WriteString("(" + strings.Join(childStrings(n)," ") + ")")
|
||||
ret.WriteString("(" + strings.Join(childStrings(n), " ") + ")")
|
||||
case "Function":
|
||||
ret.WriteString("(" + strings.Join(childStrings(n),", ") + ")")
|
||||
ret.WriteString("(" + strings.Join(childStrings(n), ", ") + ")")
|
||||
case "GenericList":
|
||||
ret.WriteString("<" + strings.Join(childStrings(n),", ") + ">")
|
||||
ret.WriteString("<" + strings.Join(childStrings(n), ", ") + ">")
|
||||
case "Array":
|
||||
ret.WriteString("[" + strings.Join(childStrings(n)," ") + "]")
|
||||
ret.WriteString("[" + strings.Join(childStrings(n), " ") + "]")
|
||||
default:
|
||||
ret.WriteString(n.Content)
|
||||
cc := strings.Join(childStrings(n)," ")
|
||||
cc := strings.Join(childStrings(n), " ")
|
||||
if n.Content != "" && cc != "" {
|
||||
ret.WriteString(" ")
|
||||
}
|
||||
ret.WriteString(cc)
|
||||
}
|
||||
s := ret.String()
|
||||
s = strings.ReplaceAll(s," *","*")
|
||||
s = strings.ReplaceAll(s," [","[")
|
||||
s = strings.ReplaceAll(s,") (",")(")
|
||||
s = strings.ReplaceAll(s, " *", "*")
|
||||
s = strings.ReplaceAll(s, " [", "[")
|
||||
s = strings.ReplaceAll(s, ") (", ")(")
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
func (n *Node) Qualifiers() string {
|
||||
if n == nil {
|
||||
return ""
|
||||
}
|
||||
ret := []string{}
|
||||
for _,c := range n.Children {
|
||||
for _, c := range n.Children {
|
||||
switch c.Kind {
|
||||
case "TypeQualifier":
|
||||
ret = append(ret,c.Content)
|
||||
ret = append(ret, c.Content)
|
||||
}
|
||||
}
|
||||
return strings.Join(ret," ")
|
||||
return strings.Join(ret, " ")
|
||||
}
|
||||
|
||||
func (n *Node) Annotations() string {
|
||||
@@ -158,11 +169,11 @@ func (n *Node) Annotations() string {
|
||||
return ""
|
||||
}
|
||||
ret := []string{}
|
||||
for _,c := range n.Children {
|
||||
for _, c := range n.Children {
|
||||
switch c.Kind {
|
||||
case "NullableAnnotation":
|
||||
ret = append(ret,c.Content)
|
||||
ret = append(ret, c.Content)
|
||||
}
|
||||
}
|
||||
return strings.Join(ret," ")
|
||||
return strings.Join(ret, " ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user