2019-04-18 09:38:46 -04:00
|
|
|
package types
|
|
|
|
|
2019-05-11 23:03:56 -04:00
|
|
|
// A parser to recognize type names in C/Objective-C
|
2019-04-18 09:38:46 -04:00
|
|
|
|
2019-05-03 13:14:30 -04:00
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2019-04-18 14:40:02 -04:00
|
|
|
var TypeName func(s string, n *Node) (string, *Node)
|
|
|
|
|
|
|
|
func init() {
|
2019-05-03 13:14:30 -04:00
|
|
|
instancename := regexp.MustCompile("instancename")
|
|
|
|
instancetype := regexp.MustCompile("instancetype")
|
|
|
|
cacheable := func(s string) bool {
|
|
|
|
return !instancetype.MatchString(s) && !instancename.MatchString(s)
|
|
|
|
}
|
|
|
|
|
2019-05-11 23:03:56 -04:00
|
|
|
//memoize the TypeName function for performance
|
2019-04-18 14:40:02 -04:00
|
|
|
cache := map[string]*Node{}
|
|
|
|
TypeName = func(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
if n2, ok := cache[s]; ok {
|
|
|
|
return "", n2
|
2019-04-18 14:40:02 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
s2, n2 := _TypeName(s, n)
|
2019-05-03 13:14:30 -04:00
|
|
|
if s2 == "" && cacheable(s) {
|
2019-04-18 14:40:02 -04:00
|
|
|
cache[s] = n2
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return s2, n2
|
2019-04-18 14:40:02 -04:00
|
|
|
}
|
2019-05-11 23:03:56 -04:00
|
|
|
//for debug purposes, the following line can be uncommented, which will
|
|
|
|
//memoization memoization
|
2019-05-03 13:14:30 -04:00
|
|
|
//TypeName = _TypeName
|
2019-04-18 14:40:02 -04:00
|
|
|
}
|
|
|
|
|
2019-05-24 12:36:01 -04:00
|
|
|
func MethodSignature(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return ChildOf(NewNode("MethodSignature"), Seq(
|
2019-05-24 12:36:01 -04:00
|
|
|
Parenthesized(TypeName),
|
2019-06-04 13:01:51 -04:00
|
|
|
Identifier,
|
2019-05-24 12:36:01 -04:00
|
|
|
Opt(MethodParameterList),
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:01:51 -04:00
|
|
|
func MethodParameterList(s string, n *Node) (string, *Node) {
|
2019-06-04 00:14:04 -04:00
|
|
|
return Seq(
|
2019-06-04 13:01:51 -04:00
|
|
|
FirstMethodParameter,
|
|
|
|
ZeroOrMore(MethodParameter),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-06-04 00:14:04 -04:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:01:51 -04:00
|
|
|
func FirstMethodParameter(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return ChildOf(NewNode("MethodParameter"), Seq(
|
2019-06-04 13:01:51 -04:00
|
|
|
Lit(":"),
|
|
|
|
Parenthesized(TypeName),
|
|
|
|
Identifier,
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func MethodParameter(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return ChildOf(NewNode("MethodParameter"), Seq(
|
2019-06-04 00:14:04 -04:00
|
|
|
Identifier,
|
2019-05-24 12:36:01 -04:00
|
|
|
Lit(":"),
|
|
|
|
Parenthesized(TypeName),
|
|
|
|
Identifier,
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:40:02 -04:00
|
|
|
func _TypeName(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return ChildOf(NewNode("TypeName"), Seq(
|
2019-04-18 09:38:46 -04:00
|
|
|
SpecifierQualifierList,
|
|
|
|
Opt(AbstractDeclarator),
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func AbstractDeclarator(s string, n *Node) (string, *Node) {
|
2019-04-18 11:22:53 -04:00
|
|
|
return OneOf(Seq(
|
2019-06-11 12:38:22 -04:00
|
|
|
Opt(Pointer),
|
|
|
|
OneOrMore(DirectAbstractDeclarator)),
|
2019-04-18 09:38:46 -04:00
|
|
|
Pointer,
|
2019-04-26 14:08:43 -04:00
|
|
|
Block,
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 11:22:53 -04:00
|
|
|
func ParenAbstractDeclarator(s string, n *Node) (string, *Node) {
|
|
|
|
return ChildOf(NewNode("Parenthesized"),
|
2019-06-11 12:38:22 -04:00
|
|
|
Parenthesized(AbstractDeclarator),
|
|
|
|
)(s, n)
|
2019-04-18 11:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Length", Regexp(`[\d]+|\*`))))),
|
2019-04-18 11:22:53 -04:00
|
|
|
|
|
|
|
ChildOf(NewNode("Array"),
|
|
|
|
Bracketed(Seq(
|
|
|
|
Word("static"),
|
|
|
|
Opt(TypeQualifierList),
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Length", Regexp(`[\d]+`))))),
|
2019-04-18 11:22:53 -04:00
|
|
|
|
|
|
|
ChildOf(NewNode("Array"),
|
|
|
|
Bracketed(Seq(
|
|
|
|
Opt(TypeQualifierList),
|
|
|
|
Word("static"),
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Length", Regexp(`[\d]+`))))),
|
|
|
|
)(s, n)
|
2019-04-18 11:22:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func FunctionDeclarator(s string, n *Node) (string, *Node) {
|
2020-06-24 13:03:53 -04:00
|
|
|
return ChildOf(NewNode("Function"), Seq(
|
2019-06-11 12:38:22 -04:00
|
|
|
Parenthesized(Opt(ParameterList)),
|
2020-06-24 13:03:53 -04:00
|
|
|
ZeroOrMore(Attribute),
|
|
|
|
))(s, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Attribute(s string, n *Node) (string, *Node) {
|
|
|
|
return Seq(
|
|
|
|
Word("__attribute__"),
|
|
|
|
ChildOf(NewNode("parens"), Parenthesized(Parenthesized(
|
|
|
|
Attr,
|
|
|
|
))),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 11:22:53 -04:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func Attr(s string, n *Node) (string, *Node) {
|
|
|
|
return NodeNamed("Attribute", OneOf(
|
|
|
|
Word("noreturn"),
|
|
|
|
))(s, n)
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:38:46 -04:00
|
|
|
func DirectAbstractDeclarator(s string, n *Node) (string, *Node) {
|
|
|
|
return OneOf(
|
2019-04-18 11:22:53 -04:00
|
|
|
ParenAbstractDeclarator,
|
|
|
|
ArrayDeclarator,
|
|
|
|
FunctionDeclarator,
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParameterList(s string, n *Node) (string, *Node) {
|
2019-04-18 11:22:53 -04:00
|
|
|
return Seq(
|
2019-06-11 12:38:22 -04:00
|
|
|
Opt(OneOrMore(Seq(ParameterDeclaration, Lit(",")))),
|
2019-04-18 10:21:03 -04:00
|
|
|
ParameterDeclaration,
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParameterDeclaration(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return ChildOf(NewNode("ParameterDeclaration"), OneOf(
|
|
|
|
NodeNamed("Ellipsis", Lit("...")),
|
|
|
|
Seq(DeclarationSpecifiers, Declarator),
|
|
|
|
Seq(DeclarationSpecifiers, Opt(AbstractDeclarator)),
|
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeclarationSpecifiers(s string, n *Node) (string, *Node) {
|
|
|
|
return OneOf(
|
2019-06-11 12:38:22 -04:00
|
|
|
Seq(StorageClassSpecifier, Opt(DeclarationSpecifiers)),
|
|
|
|
Seq(TypeSpecifier, Opt(DeclarationSpecifiers)),
|
|
|
|
Seq(StructOrUnionSpecifier, Opt(DeclarationSpecifiers)),
|
|
|
|
Seq(TypeQualifier, Opt(DeclarationSpecifiers)),
|
|
|
|
Seq(TypedefName, Opt(DeclarationSpecifiers)),
|
2019-04-18 11:22:53 -04:00
|
|
|
// Seq(FunctionSpecifier,Opt(DeclarationSpecifiers)),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func StorageClassSpecifier(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NodeNamed("StorageClassSpecifier", OneOf(
|
2019-04-18 09:38:46 -04:00
|
|
|
Word("typedef"),
|
|
|
|
Word("extern"),
|
|
|
|
Word("static"),
|
|
|
|
Word("auto"),
|
|
|
|
Word("register"),
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Declarator(s string, n *Node) (string, *Node) {
|
2019-04-18 10:21:03 -04:00
|
|
|
return ChildOf(NewNode("Declarator"),
|
2019-06-11 12:38:22 -04:00
|
|
|
Seq(ZeroOrMore(Pointer), DirectDeclarator))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func DirectDeclarator(s string, n *Node) (string, *Node) {
|
2019-04-18 14:04:01 -04:00
|
|
|
return OneOf(
|
2019-06-11 12:38:22 -04:00
|
|
|
Identifier,
|
|
|
|
Parenthesized(Declarator),
|
|
|
|
// INCOMPLETE
|
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NullableAnnotation(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NodeNamed("NullableAnnotation", OneOf(
|
2019-04-18 09:38:46 -04:00
|
|
|
Word("_Nullable"),
|
|
|
|
Word("_Nonnull"),
|
|
|
|
Word("_Null_unspecified"),
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
2019-04-26 14:08:43 -04:00
|
|
|
|
2019-04-18 09:38:46 -04:00
|
|
|
func Pointer(s string, n *Node) (string, *Node) {
|
2019-04-18 11:22:53 -04:00
|
|
|
return Seq(
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Pointer", Lit("*")),
|
2019-04-18 09:38:46 -04:00
|
|
|
Opt(TypeQualifierList),
|
|
|
|
Opt(NullableAnnotation),
|
|
|
|
Opt(Pointer),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 14:08:43 -04:00
|
|
|
//FIXME: not sure how correct this is...
|
|
|
|
func Block(s string, n *Node) (string, *Node) {
|
|
|
|
return Seq(
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Block", Lit("^")),
|
2019-04-26 14:08:43 -04:00
|
|
|
Opt(NullableAnnotation),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-26 14:08:43 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:38:46 -04:00
|
|
|
func TypeQualifierList(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return OneOrMore(TypeQualifier)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func SpecifierQualifierList(s string, n *Node) (string, *Node) {
|
|
|
|
return NodeNamed("SpecifierQualifierList",
|
|
|
|
OneOf(
|
2019-06-11 12:38:22 -04:00
|
|
|
Seq(TypeSpecifier, Opt(SpecifierQualifierList)),
|
|
|
|
Seq(StructOrUnionSpecifier, Opt(SpecifierQualifierList)),
|
|
|
|
Seq(TypedefName, Opt(SpecifierQualifierList)),
|
|
|
|
Seq(TypeQualifier, Opt(SpecifierQualifierList)),
|
2019-04-18 09:38:46 -04:00
|
|
|
),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TypeSpecifier(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NodeNamed("TypeSpecifier", OneOf(
|
2019-04-18 09:38:46 -04:00
|
|
|
Word("void"),
|
|
|
|
Word("char"),
|
|
|
|
Word("short"),
|
|
|
|
Word("int"),
|
|
|
|
Word("long"),
|
|
|
|
Word("float"),
|
|
|
|
Word("double"),
|
|
|
|
Word("signed"),
|
|
|
|
Word("unsigned"),
|
|
|
|
Word("_Bool"),
|
|
|
|
Word("_Complex"),
|
2019-04-29 13:31:13 -04:00
|
|
|
EnumSpecifier,
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TypeQualifier(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NodeNamed("TypeQualifier", OneOf(
|
2019-04-18 09:38:46 -04:00
|
|
|
Word("const"),
|
|
|
|
Word("restrict"),
|
|
|
|
Word("volatile"),
|
2019-06-13 16:47:36 -04:00
|
|
|
Word("__restrict"),
|
2019-06-11 12:38:22 -04:00
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func StructOrUnionSpecifier(s string, n *Node) (string, *Node) {
|
2019-04-29 13:31:13 -04:00
|
|
|
return OneOf(
|
2019-06-11 12:38:22 -04:00
|
|
|
// Seq(StructOrUnion,Opt(Identifier),StructDeclarationList),
|
|
|
|
Nest(StructOrUnion, Identifier),
|
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func StructOrUnion(s string, n *Node) (string, *Node) {
|
|
|
|
return OneOf(
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Struct", Word("struct")),
|
|
|
|
NodeNamed("Union", Word("union")))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
2019-04-29 13:31:13 -04:00
|
|
|
func EnumSpecifier(s string, n *Node) (string, *Node) {
|
|
|
|
return Nest(
|
2019-06-11 12:38:22 -04:00
|
|
|
NodeNamed("Enum", Word("enum")), Identifier)(s, n)
|
2019-04-29 13:31:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:38:46 -04:00
|
|
|
func Generic(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NodeNamed("Generic", TypeName)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenericList(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return ChildOf(NewNode("GenericList"), Seq(
|
2019-04-18 09:38:46 -04:00
|
|
|
Generic,
|
2019-06-11 12:38:22 -04:00
|
|
|
ZeroOrMore(Seq(Lit(","), Generic)),
|
|
|
|
))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 11:22:53 -04:00
|
|
|
func BareTypedefName(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NodeNamed("TypedefName", Identifier)(s, n)
|
2019-04-18 11:22:53 -04:00
|
|
|
}
|
|
|
|
|
2019-04-18 09:38:46 -04:00
|
|
|
func TypedefName(s string, n *Node) (string, *Node) {
|
2019-05-03 13:14:30 -04:00
|
|
|
return Seq(
|
2019-06-11 12:38:22 -04:00
|
|
|
Opt(NodeNamed("KindQualifier", Lit("__kindof"))),
|
2019-04-18 11:22:53 -04:00
|
|
|
BareTypedefName,
|
2019-05-03 13:14:30 -04:00
|
|
|
Opt(AngBracketed(GenericList)),
|
|
|
|
Opt(NullableAnnotation),
|
2019-06-11 12:38:22 -04:00
|
|
|
)(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Identifier(s string, n *Node) (string, *Node) {
|
2019-06-11 12:38:22 -04:00
|
|
|
s2, n2 := NodeNamed("Identifier",
|
|
|
|
Regexp(`[_a-zA-Z][_0-9a-zA-Z]*`))(s, n)
|
2019-04-18 09:38:46 -04:00
|
|
|
if n2 == nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
return s, nil
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
|
|
|
if reservedwords.MatchString(n2.Content) {
|
2019-06-11 12:38:22 -04:00
|
|
|
dbg("Identifier '%s' contains reserved word\n", n2.Content)
|
|
|
|
return s, nil
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return s2, n2
|
2019-04-18 09:38:46 -04:00
|
|
|
}
|