| IDA SDK

Classes | Functions | Macros | Typedefs | Variables
lex.hpp File Reference

## 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().

## Classes
struct | token_t
| Parser token. More...
struct | cast_t
| Preprocessor cast. More...
struct | lex_value_t

## Functions
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. More...
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. More...
idaman int ida_export | lex_set_options (lexer_t *lx, int options)
| Set lexer options. More...
idaman error_t ida_export | lex_get_token (lexer_t *lx, token_t *t, int32 *p_lnnum=nullptr)
| Get next token. More...
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. More...
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. More...
File oriented functions
idaman error_t ida_export | lex_init_file (lexer_t *lx, const char *file)
| Initialization: file may be nullptr. More...
idaman const char *ida_export | lex_get_file_line (lexer_t *lx, int32 *linenum, const char **lineptr, int level=0)
| Error handling. More...
idaman void ida_export | lex_term_file (lexer_t *lx, bool del_macros)
| Termination: also see lex_init_file()

## Token stack
typedef qstack< token_t > | tokenstack_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. More...
void | unget_token (const token_t &t, tokenstack_t &buf)
| Push a token back onto the token stack.

## Macros
#define | LEX_MACRO_SYSTEM 1
| define system macros such as `__LINUX__`
#define | LXOPT_PARSE_FLOATS 0x0001
| enable floating point constants
#define | LXOPT_REQ_SEPARATOR 0x0002
| require a separator between a number and an ident or a character/string constant or dot
#define | LXOPT_NOCASE_FILES 0x0004
| case-insensitive file search
#define | LXOPT_C99_CONSTANTS 0x0008
| the size and sign of constants depend on the value itself and the 'U', 'L', and 'LL' modifier suffixes. More...

## 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. More...
typedef error_t | lx_parse_cast_t(lexer_t *lx, cast_t *cast, token_t *ct)
| Preprocessor callbacks for casts. More...
typedef int idaapi | lx_preprocessor_cb(void *ud, const char *fname, int nl, const char *line)
| Preprocessor callback. More...
typedef int idaapi | lx_pragma_cb(void *ud, const char *line)
| Callback for #pragma directives. More...
typedef int idaapi | lx_warning_cb(void *ud, const char *line)
| Callback for #warning directives. More...
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. More...
typedef int idaapi | lx_undef_cb(void *ud, const char *name)
| Callback for #undef directives. More...

## 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_key = 128
| keywords start. More...

## Typedef Documentation

## ◆ 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_parse_cast_t

typedef error_t lx_parse_cast_t(lexer_t *lx, cast_t *cast, token_t *ct)

Preprocessor callbacks for casts.

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)

## Function Documentation

## ◆ 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

## ◆ lex_undefine_macro()

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

Undefine a macro.

Parameters

  lx | the lexer object
  macro | macro to undefine

## ◆ lex_set_options()

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

Set lexer options.

Parameters

  lx | the lexer object
  options | Lexer 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

  lx | the lexer object
  t | the output buffer for the token
  p_lnnum | the 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_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.

## ◆ 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