Class TypeStringParser
java.lang.Object
com.pnfsoftware.jeb.core.units.code.asm.type.TypeStringParser
A parser for C declarations, types, prototypes and routine signatures.
Examples:
ITypeManager typeman = ...; IStructureType tHouse = typeman.createStructure("House"); typeman.addStructureField(tHouse, "field0", typeman.getType("int")); TypeStringParser parser = new TypeStringParser(typeman); parser.parseType("int a;"); // name is optional; semicolon is also optional parser.parseType("int"); // name optional; semicolon optional parser.parseType("House*"); parser.parseType("void (*f)()"); // for function pointers, a name (which will be discarded) is mandatory; semicolon is optional parser.parseType("char* __cdecl f(int, double);"); // the format for anonymous prototypes is the following (no name; again, final semi-colon optional) // anonymous prototypes follow a JEB-custom format: their calling convention precedes the return type, and is stored in angled brackets // prototype attributes are not supported parser.parsePrototype("void()"); parser.parsePrototype("int();"); parser.parsePrototype("int(void)"); parser.parsePrototype("<__cdecl> int(int)"); parser.parsePrototype("<__cdecl> int(int, int)"); parser.parsePrototype("<__cdecl> int(int, char*, ...)"); // signatures: standard C signatures with support for MSVC- and GCC-style attributes parser.parseSignature("void __cdecl f()"); parser.parseSignature("void f(void)"); parser.parseSignature("void f(int a)"); parser.parseSignature("void f(unsigned, unsigned int *)"); parser.parseSignature("void f(int a, void __cdecl (*pf[4])(int a))"); parser.parseSignature("void f(int a, ...)"); parser.parseSignature("void f(int a[3])"); parser.parseSignature("void __attribute__((noreturn)) exit(int code)"); //GCC no-return attribute parser.parseSignature("void __cdecl __declspec(noreturn) exit(int)"); //MSVC no-return attribute
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
static String
static String
massageSingleDeclaration
(String declstring, boolean discardCppTemplates) Parse a C declaration.parseDeclaration
(String s, boolean discardCppTemplates) Parse a C declaration.Deprecated.Parse a C function signature.parseSignature
(String s, boolean discardCppTemplates) Parse a C function signature.Parse a non-complex C type.Parse any C type or types.static String
static void
This static method can be used to verify that the provided argument looks like a valid C declaration.static void
verifyDeclaration
(String declstr, boolean discardCppTemplates) Basic verification to ensure that the provided string looks like a declaration string.static void
verifySignature
(String sigstr, boolean discardCppTemplates) Basic verification to ensure that the provided string looks like a signature string.static void
verifyType
(String typestr, boolean discardCppTemplates) Basic verification to ensure that the provided string looks like a type string.
-
Field Details
-
TYPE
public static final int TYPE- See Also:
-
PROTO
public static final int PROTO- See Also:
-
SIG
public static final int SIG- See Also:
-
DECL
public static final int DECL- See Also:
-
-
Constructor Details
-
TypeStringParser
Create a type parser.- Parameters:
typeman
- the type manager to be used as a source of existing types and recipient of newly-parsed types
-
-
Method Details
-
parseType
Parse a non-complex C type. It is recommended to useparseDeclaration(String)
instead when possible.- Parameters:
s
- a type string, e.gunsigned int
orchar
.- Returns:
- never null
- Throws:
TypeStringParseException
-
parseDeclaration
Parse a C declaration.A declaration specifies a type and a name. If you need to parse a pure type, see
parseType(String)
.- Parameters:
s
- declaration, e.gunsigned int a
orchar* array[3]
orvoid __cdecl (*pf)(int)
(a function pointer named pf)- Returns:
- a declaration object (type, name)
- Throws:
TypeStringParseException
-
parseDeclaration
public TypeStringParser.Decl parseDeclaration(String s, boolean discardCppTemplates) throws TypeStringParseException Parse a C declaration.A declaration specifies a type and a name. If you need to parse a pure type, see
parseType(String)
.- Parameters:
s
- declaration, e.gunsigned int a
orchar* array[3]
orvoid __cdecl (*pf)(int)
(a function pointer named pf)discardCppTemplates
-- Returns:
- a declaration (type, name)
- Throws:
TypeStringParseException
-
parsePrototype
Deprecated.It is recommended to useparseSignature(String)
instead.Parse an anonymous prototype written using the following style:<calling-convention> returnType(paramType1 _) <calling-convention> returnType(paramType1, paramType2) <calling-convention> returnType(paramType1, paramType2, ...)
- Parameters:
s
- a JEB-style prototype string- Returns:
- a prototype object
- Throws:
TypeStringParseException
-
parseSignature
Parse a C function signature.- Parameters:
s
- a signature, e.g.int __cdecl foo(int a, unsigned char * b)
- Returns:
- a prototype item also holding the routine name and parameter names
- Throws:
TypeStringParseException
-
parseSignature
public IPrototypeItem parseSignature(String s, boolean discardCppTemplates) throws TypeStringParseException Parse a C function signature.- Parameters:
s
- a signature, e.g.int __cdecl foo(int a, unsigned char * b)
discardCppTemplates
- if true, templated types "<...>" will be erased, thereby making the type potentially parsable as a C type- Returns:
- a prototype item also holding the routine name and parameter names
- Throws:
TypeStringParseException
-
parseTypesRaw
Parse any C type or types.- Parameters:
s
- a string buffer defining types- Returns:
- the list of parsed types
- Throws:
TypeStringParseException
-
verify
public static void verify(int what, String cstr, boolean discardCppTemplates) throws TypeStringParseException This static method can be used to verify that the provided argument looks like a valid C declaration.- Parameters:
what
-TYPE
orSIG
cstr
- a C declaration or typediscardCppTemplates
- if true, templated types "<...>" will be erased, thereby making the type potentially parsable as a C type- Throws:
TypeStringParseException
- a parsing error occurred
-
verifyType
public static void verifyType(String typestr, boolean discardCppTemplates) throws TypeStringParseException Basic verification to ensure that the provided string looks like a type string.- Parameters:
typestr
-discardCppTemplates
-- Throws:
TypeStringParseException
-
verifyDeclaration
public static void verifyDeclaration(String declstr, boolean discardCppTemplates) throws TypeStringParseException Basic verification to ensure that the provided string looks like a declaration string.- Parameters:
declstr
-discardCppTemplates
-- Throws:
TypeStringParseException
-
verifySignature
public static void verifySignature(String sigstr, boolean discardCppTemplates) throws TypeStringParseException Basic verification to ensure that the provided string looks like a signature string.- Parameters:
sigstr
-discardCppTemplates
-- Throws:
TypeStringParseException
-
massageForType
-
massageForDeclaration
-
massageSingleDeclaration
public static String massageSingleDeclaration(String declstring, boolean discardCppTemplates) throws TypeStringParseException - Throws:
TypeStringParseException
-
removeCppTemplates
- Parameters:
s
- a C or C++ declaration or type, possibly with templates- Returns:
- the same C or C++ declaration or type, without templates information
-
parseSignature(String)
instead.