Restructure the type parsers. Remove struct declaration parsers.

This commit is contained in:
Greg 2019-04-18 11:22:53 -04:00
parent 6955d79702
commit b7134bedb7
3 changed files with 77 additions and 68 deletions

View File

@ -158,7 +158,7 @@ func Longest(ps ...Parser) Parser {
//node. Returns nil and the input string unless the entire sequence succeeds //node. Returns nil and the input string unless the entire sequence succeeds
func Seq(ps ...Parser) Parser { func Seq(ps ...Parser) Parser {
dbg("Seq(%p)\n",ps) dbg("Seq(%p)\n",ps)
return func(s string, n *Node) (string, *Node) { p := func(s string, n *Node) (string, *Node) {
ret := NewNode("Seq") ret := NewNode("Seq")
s2, n2 := s,n s2, n2 := s,n
for _,p := range ps { for _,p := range ps {
@ -173,15 +173,13 @@ func Seq(ps ...Parser) Parser {
} }
return s2,ret return s2,ret
} }
} return Children(p)
func SeqC(ps ...Parser) Parser {
return Children(Seq(ps...))
} }
//Like Seq but subsequent children are nested inside their earlier siblings. //Like Seq but subsequent children are nested inside their earlier siblings.
func Nest(ps ...Parser) Parser { func Nest(ps ...Parser) Parser {
dbg("Nest(%p)\n",ps) dbg("Nest(%p)\n",ps)
return func(s string, n *Node) (string, *Node) { p := func(s string, n *Node) (string, *Node) {
s2,n2 := Seq(ps...)(s,n) s2,n2 := Seq(ps...)(s,n)
if n2 == nil { if n2 == nil {
return s,nil return s,nil
@ -194,9 +192,7 @@ func Nest(ps ...Parser) Parser {
} }
return s2,ret return s2,ret
} }
} return Children(p)
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

View File

@ -3,34 +3,68 @@ 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 ChildOf(NewNode("TypeName"),SeqC( return ChildOf(NewNode("TypeName"),Seq(
SpecifierQualifierList, SpecifierQualifierList,
Opt(AbstractDeclarator), Opt(AbstractDeclarator),
))(s,n) ))(s,n)
} }
func AbstractDeclarator(s string, n *Node) (string, *Node) { func AbstractDeclarator(s string, n *Node) (string, *Node) {
return OneOf(SeqC( return OneOf(Seq(
Opt(Pointer), Opt(Pointer),
Children(OneOrMore(DirectAbstractDeclarator))), OneOrMore(DirectAbstractDeclarator)),
Pointer, Pointer,
)(s,n) )(s,n)
} }
func ParenAbstractDeclarator(s string, n *Node) (string, *Node) {
return ChildOf(NewNode("Parenthesized"),
Parenthesized(AbstractDeclarator),
)(s,n)
}
func ArrayDeclarator(s string, n *Node) (string, *Node) {
return OneOf(
ChildOf(NewNode("Array"),
Bracketed(Opt(TypeQualifierList))),
// NOTE: Parser does not allow arbitrary 'length' expressions
ChildOf(NewNode("Array"),
Bracketed(Seq(
Opt(TypeQualifierList),
NodeNamed("Length",Regexp(`[\d]+|\*`))))),
ChildOf(NewNode("Array"),
Bracketed(Seq(
Word("static"),
Opt(TypeQualifierList),
NodeNamed("Length",Regexp(`[\d]+`))))),
ChildOf(NewNode("Array"),
Bracketed(Seq(
Opt(TypeQualifierList),
Word("static"),
NodeNamed("Length",Regexp(`[\d]+`))))),
)(s,n)
}
func FunctionDeclarator(s string, n *Node) (string, *Node) {
return ChildOf(NewNode("Function"),
Parenthesized(Opt(ParameterList)),
)(s,n)
}
func DirectAbstractDeclarator(s string, n *Node) (string, *Node) { func DirectAbstractDeclarator(s string, n *Node) (string, *Node) {
return OneOf( return OneOf(
ChildOf(NewNode("Parenthesized"),Parenthesized(AbstractDeclarator)), ParenAbstractDeclarator,
NodeNamed("Array",Bracketed(Opt(TypeQualifierList))), ArrayDeclarator,
NodeNamed("Array",Bracketed(SeqC(Opt(TypeQualifierList),NodeNamed("Length",Regexp(`[\d]+|\*`))))), // NOTE: Does not allow arbitrary expressions FunctionDeclarator,
NodeNamed("Array",Bracketed(SeqC(Word("static"),Opt(TypeQualifierList),NodeNamed("Length",Regexp(`[\d]+`))))), // NOTE: Does not allow arbitrary expressions
NodeNamed("Array",Bracketed(SeqC(Opt(TypeQualifierList),Word("static"),NodeNamed("Length",Regexp(`[\d]+`))))), // NOTE: Does not allow arbitrary expressions
ChildOf(NewNode("Function"),Parenthesized(Opt(ParameterList))),
)(s,n) )(s,n)
} }
func ParameterList(s string, n *Node) (string, *Node) { func ParameterList(s string, n *Node) (string, *Node) {
return SeqC( return Seq(
Opt(Children(OneOrMore(SeqC(ParameterDeclaration,Lit(","))))), Opt(OneOrMore(Seq(ParameterDeclaration,Lit(",")))),
ParameterDeclaration, ParameterDeclaration,
)(s,n) )(s,n)
} }
@ -38,19 +72,19 @@ func ParameterList(s string, n *Node) (string, *Node) {
func ParameterDeclaration(s string, n *Node) (string, *Node) { func ParameterDeclaration(s string, n *Node) (string, *Node) {
return ChildOf(NewNode("ParameterDeclaration"),OneOf( return ChildOf(NewNode("ParameterDeclaration"),OneOf(
NodeNamed("Ellipsis",Lit("...")), NodeNamed("Ellipsis",Lit("...")),
SeqC(DeclarationSpecifiers,Declarator), Seq(DeclarationSpecifiers,Declarator),
SeqC(DeclarationSpecifiers,Opt(AbstractDeclarator)), Seq(DeclarationSpecifiers,Opt(AbstractDeclarator)),
))(s,n) ))(s,n)
} }
func DeclarationSpecifiers(s string, n *Node) (string, *Node) { func DeclarationSpecifiers(s string, n *Node) (string, *Node) {
return OneOf( return OneOf(
SeqC(StorageClassSpecifier,Opt(DeclarationSpecifiers)), Seq(StorageClassSpecifier,Opt(DeclarationSpecifiers)),
SeqC(TypeSpecifier,Opt(DeclarationSpecifiers)), Seq(TypeSpecifier,Opt(DeclarationSpecifiers)),
SeqC(StructOrUnionSpecifier,Opt(DeclarationSpecifiers)), Seq(StructOrUnionSpecifier,Opt(DeclarationSpecifiers)),
SeqC(TypeQualifier,Opt(DeclarationSpecifiers)), Seq(TypeQualifier,Opt(DeclarationSpecifiers)),
SeqC(TypedefName,Opt(DeclarationSpecifiers)), Seq(TypedefName,Opt(DeclarationSpecifiers)),
// SeqC(FunctionSpecifier,Opt(DeclarationSpecifiers)), // Seq(FunctionSpecifier,Opt(DeclarationSpecifiers)),
)(s,n) )(s,n)
} }
@ -66,7 +100,7 @@ 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 ChildOf(NewNode("Declarator"), return ChildOf(NewNode("Declarator"),
SeqC(ZeroOrMore(Pointer), DirectDeclarator))(s,n) Seq(ZeroOrMore(Pointer), DirectDeclarator))(s,n)
} }
func DirectDeclarator(s string, n *Node) (string, *Node) { func DirectDeclarator(s string, n *Node) (string, *Node) {
@ -87,7 +121,7 @@ func NullableAnnotation(s string, n *Node) (string, *Node) {
))(s,n) ))(s,n)
} }
func Pointer(s string, n *Node) (string, *Node) { func Pointer(s string, n *Node) (string, *Node) {
return SeqC( return Seq(
NodeNamed("Pointer",Lit("*")), NodeNamed("Pointer",Lit("*")),
Opt(TypeQualifierList), Opt(TypeQualifierList),
Opt(NullableAnnotation), Opt(NullableAnnotation),
@ -96,21 +130,18 @@ func Pointer(s string, n *Node) (string, *Node) {
} }
func TypeQualifierList(s string, n *Node) (string, *Node) { func TypeQualifierList(s string, n *Node) (string, *Node) {
return NodeNamed("TypeQualifierList", return OneOrMore(TypeQualifier)(s,n)
Children((OneOrMore(TypeQualifier))),
)(s,n)
} }
func SpecifierQualifierList(s string, n *Node) (string, *Node) { func SpecifierQualifierList(s string, n *Node) (string, *Node) {
return NodeNamed("SpecifierQualifierList", return NodeNamed("SpecifierQualifierList",
OneOf( OneOf(
SeqC(TypeSpecifier,Opt(SpecifierQualifierList)), Seq(TypeSpecifier,Opt(SpecifierQualifierList)),
SeqC(StructOrUnionSpecifier,Opt(SpecifierQualifierList)), Seq(StructOrUnionSpecifier,Opt(SpecifierQualifierList)),
SeqC(TypedefName,Opt(SpecifierQualifierList)), Seq(TypedefName,Opt(SpecifierQualifierList)),
SeqC(TypeQualifier,Opt(SpecifierQualifierList)), Seq(TypeQualifier,Opt(SpecifierQualifierList)),
), ),
)(s,n) )(s,n)
// OneOrMore(OneOf(TypeQualifier,TypeSpecifier)))(s,n)
} }
func TypeSpecifier(s string, n *Node) (string, *Node) { func TypeSpecifier(s string, n *Node) (string, *Node) {
@ -142,8 +173,8 @@ 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",OneOf( return NodeNamed("StructOrUnionSpecifier",OneOf(
SeqC(StructOrUnion,Opt(Identifier),StructDeclarationList), // Seq(StructOrUnion,Opt(Identifier),StructDeclarationList),
NestC(StructOrUnion,Identifier), Nest(StructOrUnion,Identifier),
))(s,n) ))(s,n)
} }
@ -153,45 +184,27 @@ func StructOrUnion(s string, n *Node) (string, *Node) {
NodeNamed("Union",Word("union")))(s,n) NodeNamed("Union",Word("union")))(s,n)
} }
func StructDeclarationList(s string, n *Node) (string, *Node) {
return NodeNamed("StructDeclarationList",OneOrMore(StructDeclaration))(s,n)
}
func StructDeclaration(s string, n *Node) (string, *Node) {
return NodeNamed("StructDeclaration",Seq(
SpecifierQualifierList,
StructDeclaratorList,
Lit(";"),
))(s,n)
}
func StructDeclaratorList(s string, n *Node) (string, *Node) {
return NodeNamed("StructDeclaratorList",Seq(
Opt(OneOrMore(Seq(StructDeclarator,Lit(",")))),
StructDeclarator,
))(s,n)
}
func StructDeclarator(s string, n *Node) (string, *Node) {
return NodeNamed("StructDeclarator",Declarator)(s,n)
}
func Generic(s string, n *Node) (string, *Node) { 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) { func GenericList(s string, n *Node) (string, *Node) {
return OneOf( return OneOf(
SeqC(Generic,Lit(","),GenericList), Seq(Generic,Lit(","),GenericList),
Generic, Generic,
)(s,n) )(s,n)
} }
func BareTypedefName(s string, n *Node) (string, *Node) {
return NodeNamed("TypedefName",Identifier)(s,n)
}
func TypedefName(s string, n *Node) (string, *Node) { func TypedefName(s string, n *Node) (string, *Node) {
return NodeNamed("TypedefName",OneOf( return OneOf(
SeqC(NodeNamed("TypedefName",Identifier),AngBracketed(GenericList)), Seq(BareTypedefName, AngBracketed(GenericList)),
Identifier, Seq(BareTypedefName, NullableAnnotation),
))(s,n) BareTypedefName,
)(s,n)
} }
func Identifier(s string, n *Node) (string, *Node) { func Identifier(s string, n *Node) (string, *Node) {

View File

@ -37,7 +37,7 @@ func (n *Node) String(ls ...int) string {
} }
} }
prefix := strings.Repeat("-",level) prefix := strings.Repeat("-",level)
ret.WriteString(fmt.Sprintf("%s<%s> %p '%s'\n",prefix, n.Kind, n, n.Content)) ret.WriteString(fmt.Sprintf("%s<%s> '%s'\n",prefix, n.Kind, n.Content))
for _,c := range n.Children { for _,c := range n.Children {
ret.WriteString(c.String(level+1)) ret.WriteString(c.String(level+1))
} }