IDA SDK

- Main Page
- Header Files
- Classes
- Modules

- File List
- File Members

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=NULL)
| 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 (lexer_t *lx, const char *macro, const char *body, int nargs=0, bool isfunc=false)
| Define a macro.
idaman void ida_export | lex_undef (lexer_t *lx, const char *macro)
| Undefine a macro.
idaman int ida_export | lex_set_options (lexer_t *lx, int options)
| Set lexer options. More...
idaman error_t ida_export | lxget (lexer_t *lx, token_t *t)
| 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)
| Enumerate all macros. More...
idaman const char *ida_export | lxascii (const token_t *t, char *buf, size_t bufsize)
| Debug: get text representation of token.
String oriented functions
idaman error_t ida_export | lxini (lexer_t *lx, const char *line, macro_t **macros=NULL)
| Set the input line and the macro table. More...
File oriented functions
idaman error_t ida_export | lxgetsini (lexer_t *lx, const char *file)
| Initialization: file may be NULL. More...
idaman const char *ida_export | lxgetserr (lexer_t *lx, int32 *linenum, const char **lineptr, int level=0)
| Error handling. More...
idaman void ida_export | lxgetstrm (lexer_t *lx, bool del_macros)
| Termination: also see lxgetsini()

## 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 | 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' 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, 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

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.

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.

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

Preprocessor callback.

It will be called for each input line.

Returns
  an error code (0-ok)

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

Callback for #pragma directives.

Returns
  an error code (0-ok)

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

Callback for warning directives.

Returns
  an error code (0-ok)

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)

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

Callback for #undef directives.

Returns
  an error code (0-ok)

## Function Documentation

idaman lexer_t* ida_export create_lexer | ( | const char *const * | keys,
|  | size_t | size,
|  | void * | ud = `NULL`
| ) |  |

Create new lexical analyzer and set its keyword table.

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

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

Set lexer options.

Parameters

  options | Lexer options

Returns
  the old options

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
| ) |  |

Enumerate all macros.

Do so until 'cb' returns non-zero.

idaman error_t ida_export lxini | ( | lexer_t * | lx,
|  | const char * | line,
|  | macro_t ** | macros = `NULL`
| ) |  |

Set the input line and the macro table.

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

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

Initialization: file may be NULL.

Also see lxgetstrm().

idaman const char* ida_export lxgetserr | ( | 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.

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