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