IDA C++ SDK 9.2
Loading...
Searching...
No Matches
lex.hpp File Reference

Tools for parsing C-like input. More...

Go to the source code of this file.

Classes

struct  token_t
 Parser token. More...
struct  lex_value_t

Typedefs

typedef ushort lxtype
 see Parser token types
typedef error_t lx_resolver_t(lexer_t *lx, void *ud, token_t *curtok, sval_t *res)
 Preprocessor callback for unknown tokens.
typedef int idaapi lx_preprocessor_cb(void *ud, const char *fname, int nl, const char *line)
 Preprocessor callback.
typedef int idaapi lx_pragma_cb(void *ud, const char *line)
 Callback for #pragma directives.
typedef int idaapi lx_warning_cb(void *ud, const char *line)
 Callback for #warning directives.
typedef int idaapi lx_macro_cb(void *ud, const char *name, const char *body, int nargs, bool isfunc, bool is_new_macro)
 Callback for #define directives.
typedef int idaapi lx_undef_cb(void *ud, const char *name)
 Callback for #undef directives.

Functions

 DECLARE_TYPE_AS_MOVABLE (token_t)
 MSC_DIAG_OFF (4826) struct cast_t
 Preprocessor cast.
 MSC_DIAG_ON (4826) typedef error_t lx_parse_cast_t(lexer_t *lx
 Preprocessor callbacks for casts.
idaman lexer_t *ida_export create_lexer (const char *const *keys, size_t size, void *ud=nullptr, uint32 macro_flags=0)
 Create new lexical analyzer and set its keyword table.
idaman void ida_export destroy_lexer (lexer_t *lx)
 Destroy a lexical analyzer.
idaman error_t ida_export lex_define_macro (lexer_t *lx, const char *macro, const char *body, int nargs=0, bool isfunc=false)
 Define a macro.
idaman void ida_export lex_undefine_macro (lexer_t *lx, const char *macro)
 Undefine a macro.
idaman int ida_export lex_set_options (lexer_t *lx, int options)
 Set lexer options.
idaman error_t ida_export lex_get_token (lexer_t *lx, token_t *t, int32 *p_lnnum=nullptr)
 Get next token.
idaman int ida_export lex_enum_macros (const lexer_t *lx, int idaapi cb(const char *name, const char *body, int nargs, bool isfunc, void *ud), void *ud=nullptr)
 Enumerate all macros.
idaman const char *ida_export lex_print_token (qstring *buf, const token_t *t)
 Debug: get text representation of token.
idaman bool ida_export is_c_keyword (const char *name)
 is NAME a valid C keyword (exclude 'this')
String oriented functions
idaman error_t ida_export lex_init_string (lexer_t *lx, const char *line, void *macros=nullptr)
 Set the input line and the macro table.
File oriented functions
idaman error_t ida_export lex_init_file (lexer_t *lx, const char *file)
 Initialization: file may be nullptr.
idaman const char *ida_export lex_get_file_line (lexer_t *lx, int32 *linenum, const char **lineptr, int level=0)
 Error handling.
idaman void ida_export lex_term_file (lexer_t *lx, bool del_macros)
 Termination: also see lex_init_file()

Variables

const lxtype lx_end = 1
 no more tokens
const lxtype lx_ident = 2
 ident
const lxtype lx_number = 3
 long constant
const lxtype lx_string = 4
 string constant (token_t.chr != 0 => unicode string)
const lxtype lx_char = 5
 char constant
const lxtype lx_typename = 6
 user-defined type
const lxtype lx_float = 7
 IEEE floating point constant.
const lxtype lx_int64 = 8
 int64 constant
const lxtype lx_callcnv = 9
 custom calling convention
const lxtype lx_key = 128
 keywords start.
cast_t * cast
cast_t token_tct

Token stack

typedef qstack< token_ttokenstack_t
 see get_token(), unget_token()
bool get_token (token_t *t, lexer_t *lx, tokenstack_t &buf)
 Retrieve token from a stack or lexer.
void unget_token (const token_t &t, tokenstack_t &buf)
 Push a token back onto the token stack.

Detailed Description

Tools for parsing C-like input.

Functions in this file use objects of opaque type lexer_t. To create a lexer_t instance, use create_lexer().

Typedef Documentation

◆ lxtype

◆ lx_resolver_t

typedef error_t lx_resolver_t(lexer_t *lx, void *ud, token_t *curtok, sval_t *res)

Preprocessor callback for unknown tokens.

Will be called when preprocessor calculates the value of #if expression.

◆ lx_preprocessor_cb

typedef int idaapi lx_preprocessor_cb(void *ud, const char *fname, int nl, const char *line)

Preprocessor callback.

It will be called for each input line.

Returns
an error code (0-ok)

◆ lx_pragma_cb

typedef int idaapi lx_pragma_cb(void *ud, const char *line)

Callback for #pragma directives.

Returns
an error code (0-ok)

◆ lx_warning_cb

typedef int idaapi lx_warning_cb(void *ud, const char *line)

Callback for #warning directives.

Returns
an error code (0-ok)

◆ lx_macro_cb

typedef int idaapi lx_macro_cb(void *ud, const char *name, const char *body, int nargs, bool isfunc, bool is_new_macro)

Callback for #define directives.

Returns
an error code (0-ok)

◆ lx_undef_cb

typedef int idaapi lx_undef_cb(void *ud, const char *name)

Callback for #undef directives.

Returns
an error code (0-ok)

◆ tokenstack_t

Function Documentation

◆ DECLARE_TYPE_AS_MOVABLE()

DECLARE_TYPE_AS_MOVABLE ( token_t )

◆ MSC_DIAG_OFF()

MSC_DIAG_OFF ( 4826 )

Preprocessor cast.

◆ MSC_DIAG_ON()

MSC_DIAG_ON ( 4826 )

Preprocessor callbacks for casts.

Will be called when preprocessor calculates the value of #if expression.

◆ create_lexer()

idaman lexer_t *ida_export create_lexer ( const char *const * keys,
size_t size,
void * ud = nullptr,
uint32 macro_flags = 0 )

Create new lexical analyzer and set its keyword table.

If keys==nullptr, then set the default C keyword table

◆ destroy_lexer()

idaman void ida_export destroy_lexer ( lexer_t * lx)

Destroy a lexical analyzer.

◆ lex_define_macro()

idaman error_t ida_export lex_define_macro ( lexer_t * lx,
const char * macro,
const char * body,
int nargs = 0,
bool isfunc = false )

Define a macro.

◆ lex_undefine_macro()

idaman void ida_export lex_undefine_macro ( lexer_t * lx,
const char * macro )

Undefine a macro.

Parameters
lxthe lexer object
macromacro to undefine

◆ lex_set_options()

idaman int ida_export lex_set_options ( lexer_t * lx,
int options )

Set lexer options.

Parameters
lxthe lexer object
optionsLexer options
Returns
the old options

◆ lex_get_token()

idaman error_t ida_export lex_get_token ( lexer_t * lx,
token_t * t,
int32 * p_lnnum = nullptr )

Get next token.

Parameters
lxthe lexer object
tthe output buffer for the token
p_lnnumthe line number where the token starts

◆ lex_enum_macros()

idaman int ida_export lex_enum_macros ( const lexer_t * lx,
int idaapi cbconst char *name, const char *body, int nargs, bool isfunc, void *ud,
void * ud = nullptr )

Enumerate all macros.

Do so until 'cb' returns non-zero.

◆ lex_print_token()

idaman const char *ida_export lex_print_token ( qstring * buf,
const token_t * t )

Debug: get text representation of token.

◆ lex_init_string()

idaman error_t ida_export lex_init_string ( lexer_t * lx,
const char * line,
void * macros = nullptr )

Set the input line and the macro table.

if macros==nullptr, the macro table will not be changed.

◆ lex_init_file()

idaman error_t ida_export lex_init_file ( lexer_t * lx,
const char * file )

Initialization: file may be nullptr.

Also see lex_term_file().

◆ lex_get_file_line()

idaman const char *ida_export lex_get_file_line ( lexer_t * lx,
int32 * linenum,
const char ** lineptr,
int level = 0 )

Error handling.

if level > 0, then return information about the enclosing file which included the current one.

◆ lex_term_file()

idaman void ida_export lex_term_file ( lexer_t * lx,
bool del_macros )

Termination: also see lex_init_file()

◆ get_token()

bool get_token ( token_t * t,
lexer_t * lx,
tokenstack_t & buf )
inline

Retrieve token from a stack or lexer.

If buf is not empty then get the token on top of the stack. If buf is empty then gen the next token from the lexer.

Returns
success

◆ unget_token()

void unget_token ( const token_t & t,
tokenstack_t & buf )
inline

Push a token back onto the token stack.

◆ is_c_keyword()

idaman bool ida_export is_c_keyword ( const char * name)

is NAME a valid C keyword (exclude 'this')

Variable Documentation

◆ cast

cast_t* cast

◆ ct

cast_t token_t* ct