Bug fix to stripAbstract(), some minor parser restructuring.
This commit is contained in:
parent
e7270e95cf
commit
6955d79702
|
@ -1,7 +1,6 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,12 +16,6 @@ func init() {
|
||||||
reservedwords = regexp.MustCompile("^(void|char|short|int|long|float|double|signed|unsigned|_Bool|_Complex|const|restrict|volatile|struct|union|enum)$")
|
reservedwords = regexp.MustCompile("^(void|char|short|int|long|float|double|signed|unsigned|_Bool|_Complex|const|restrict|volatile|struct|union|enum)$")
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbg(f string, xs ...interface{}) {
|
|
||||||
if Debug {
|
|
||||||
fmt.Printf(f,xs...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Parser func(string, *Node) (string, *Node)
|
type Parser func(string, *Node) (string, *Node)
|
||||||
|
|
||||||
// Adders
|
// Adders
|
||||||
|
@ -202,6 +195,9 @@ func Nest(ps ...Parser) Parser {
|
||||||
return s2,ret
|
return s2,ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func NestC(ps ...Parser) Parser {
|
||||||
|
return Children(Nest(ps...))
|
||||||
|
}
|
||||||
|
|
||||||
//ZeroOrMore returns a sequence of zero or more nodes
|
//ZeroOrMore returns a sequence of zero or more nodes
|
||||||
func ZeroOrMore(p Parser) Parser {
|
func ZeroOrMore(p Parser) Parser {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package types
|
||||||
// Parsers for recognizing type names in C/Objective-C
|
// Parsers for recognizing type names in C/Objective-C
|
||||||
|
|
||||||
func TypeName(s string, n *Node) (string, *Node) {
|
func TypeName(s string, n *Node) (string, *Node) {
|
||||||
return NodeNamed("TypeName",Seq(
|
return ChildOf(NewNode("TypeName"),SeqC(
|
||||||
SpecifierQualifierList,
|
SpecifierQualifierList,
|
||||||
Opt(AbstractDeclarator),
|
Opt(AbstractDeclarator),
|
||||||
))(s,n)
|
))(s,n)
|
||||||
|
@ -30,8 +30,8 @@ func DirectAbstractDeclarator(s string, n *Node) (string, *Node) {
|
||||||
|
|
||||||
func ParameterList(s string, n *Node) (string, *Node) {
|
func ParameterList(s string, n *Node) (string, *Node) {
|
||||||
return SeqC(
|
return SeqC(
|
||||||
Opt(Children(OneOrMore(SeqC(ParameterDeclaration,Lit(","))))),
|
Opt(Children(OneOrMore(SeqC(ParameterDeclaration,Lit(","))))),
|
||||||
ParameterDeclaration,
|
ParameterDeclaration,
|
||||||
)(s,n)
|
)(s,n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,8 +47,9 @@ func DeclarationSpecifiers(s string, n *Node) (string, *Node) {
|
||||||
return OneOf(
|
return OneOf(
|
||||||
SeqC(StorageClassSpecifier,Opt(DeclarationSpecifiers)),
|
SeqC(StorageClassSpecifier,Opt(DeclarationSpecifiers)),
|
||||||
SeqC(TypeSpecifier,Opt(DeclarationSpecifiers)),
|
SeqC(TypeSpecifier,Opt(DeclarationSpecifiers)),
|
||||||
|
SeqC(StructOrUnionSpecifier,Opt(DeclarationSpecifiers)),
|
||||||
SeqC(TypeQualifier,Opt(DeclarationSpecifiers)),
|
SeqC(TypeQualifier,Opt(DeclarationSpecifiers)),
|
||||||
SeqC(Identifier,Opt(DeclarationSpecifiers)),
|
SeqC(TypedefName,Opt(DeclarationSpecifiers)),
|
||||||
// SeqC(FunctionSpecifier,Opt(DeclarationSpecifiers)),
|
// SeqC(FunctionSpecifier,Opt(DeclarationSpecifiers)),
|
||||||
)(s,n)
|
)(s,n)
|
||||||
}
|
}
|
||||||
|
@ -64,8 +65,8 @@ func StorageClassSpecifier(s string, n *Node) (string, *Node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Declarator(s string, n *Node) (string, *Node) {
|
func Declarator(s string, n *Node) (string, *Node) {
|
||||||
return NodeNamed("Declarator",
|
return ChildOf(NewNode("Declarator"),
|
||||||
Seq(ZeroOrMore(Pointer), DirectDeclarator))(s,n)
|
SeqC(ZeroOrMore(Pointer), DirectDeclarator))(s,n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DirectDeclarator(s string, n *Node) (string, *Node) {
|
func DirectDeclarator(s string, n *Node) (string, *Node) {
|
||||||
|
@ -140,10 +141,10 @@ func TypeQualifier(s string, n *Node) (string, *Node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func StructOrUnionSpecifier(s string, n *Node) (string, *Node) {
|
func StructOrUnionSpecifier(s string, n *Node) (string, *Node) {
|
||||||
return NodeNamed("StructOrUnionSpecifier",Children(OneOf(
|
return NodeNamed("StructOrUnionSpecifier",OneOf(
|
||||||
Seq(StructOrUnion,Opt(Identifier),StructDeclarationList),
|
SeqC(StructOrUnion,Opt(Identifier),StructDeclarationList),
|
||||||
Nest(StructOrUnion,Identifier),
|
NestC(StructOrUnion,Identifier),
|
||||||
)))(s,n)
|
))(s,n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StructOrUnion(s string, n *Node) (string, *Node) {
|
func StructOrUnion(s string, n *Node) (string, *Node) {
|
||||||
|
|
|
@ -8,6 +8,12 @@ var (
|
||||||
Debug bool = false
|
Debug bool = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func dbg(f string, xs ...interface{}) {
|
||||||
|
if Debug {
|
||||||
|
fmt.Printf(f,xs...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Parse(s string) *Node {
|
func Parse(s string) *Node {
|
||||||
_, n2 := TypeName(s,NewNode("AST"))
|
_, n2 := TypeName(s,NewNode("AST"))
|
||||||
return n2
|
return n2
|
||||||
|
@ -23,6 +29,9 @@ func (n *Node) isAbstract(k string) bool {
|
||||||
|
|
||||||
//Strip one level of pointer or array indirection from a node
|
//Strip one level of pointer or array indirection from a node
|
||||||
func (n *Node) stripAbstract(k string) *Node {
|
func (n *Node) stripAbstract(k string) *Node {
|
||||||
|
if n == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
i := len(n.Children) - 1
|
i := len(n.Children) - 1
|
||||||
if i < 1 {
|
if i < 1 {
|
||||||
return nil
|
return nil
|
||||||
|
@ -30,19 +39,23 @@ func (n *Node) stripAbstract(k string) *Node {
|
||||||
ret := NewNode(n.Kind)
|
ret := NewNode(n.Kind)
|
||||||
cs := n.Children[:]
|
cs := n.Children[:]
|
||||||
|
|
||||||
fmt.Printf("stripAbstract(): i = %d\n",i)
|
dbg("stripAbstract(): i = %d\n",i)
|
||||||
//Scan backwords skipping NullableAnnotation tags
|
//Scan backwords skipping TypeQualifier and NullableAnnotation tags
|
||||||
for ;i > 0 && cs[i].Kind == "NullableAnnotation"; i-- { }
|
for ;i > 0 &&
|
||||||
|
(cs[i].Kind == "TypeQualifier" ||
|
||||||
|
cs[i].Kind == "NullableAnnotation") ; i-- { }
|
||||||
|
|
||||||
if cs[i].Kind == k {
|
if cs[i].Kind == k {
|
||||||
fmt.Printf("stripAbstract(): last node is %s\n",k)
|
dbg("stripAbstract(): last node is %s\n",k)
|
||||||
ret.Children = cs[:i]
|
ret.Children = cs[:i]
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
if i > 1 && cs[i-1].Kind == "Parenthesized" {
|
if i > 1 && cs[i-1].Kind == "Parenthesized" {
|
||||||
j := len(cs[i-1].Children) - 1
|
j := len(cs[i-1].Children) - 1
|
||||||
//Scan backwards skipping TypeQualifier tags
|
for ;j > 0 &&
|
||||||
for ;j > 0 && cs[i-1].Children[j].Kind == "TypeQualifier"; j-- { }
|
(cs[i-1].Children[j].Kind == "TypeQualifier" ||
|
||||||
|
cs[i-1].Children[j].Kind == "NullableAnnotation");
|
||||||
|
j-- { }
|
||||||
if cs[i-1].Children[j].Kind != k {
|
if cs[i-1].Children[j].Kind != k {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -62,7 +75,7 @@ func (n *Node) stripAbstract(k string) *Node {
|
||||||
//PointsTo, when called on a pointer node returns a node describing the type
|
//PointsTo, when called on a pointer node returns a node describing the type
|
||||||
//pointed to. Otherwise returns nil when called on non-pointer types.
|
//pointed to. Otherwise returns nil when called on non-pointer types.
|
||||||
func (n *Node) PointsTo() *Node {
|
func (n *Node) PointsTo() *Node {
|
||||||
fmt.Printf("PointsTo()\n")
|
dbg("PointsTo()\n")
|
||||||
return n.stripAbstract("Pointer")
|
return n.stripAbstract("Pointer")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +83,7 @@ func (n *Node) PointsTo() *Node {
|
||||||
//of the elements of the array. Otherwise returns nil when called on
|
//of the elements of the array. Otherwise returns nil when called on
|
||||||
//non-array types.
|
//non-array types.
|
||||||
func (n *Node) ArrayOf() *Node {
|
func (n *Node) ArrayOf() *Node {
|
||||||
fmt.Printf("ArrayOf()\n")
|
dbg("ArrayOf()\n")
|
||||||
return n.stripAbstract("Array")
|
return n.stripAbstract("Array")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user