Lexer

class understand.Lexer

Bases: object

A lexical stream generated for a file entity.

The original file must exist and be unchanged from the last database reparse. The first lexeme (token) in the stream can be accessed using first. A lexer can be iterated over, where each item returned is a lexeme.

import understand

db = understand.open("test.und")

file = db.lookup("test.cpp")[0]
for lexeme in file.lexer():
    print(lexeme.text(), end="")
    if lexeme.ent():
        print("@", end="")

Lexer and lexeme line and column numbers are 1-based.

Methods Summary

first

Return the first lexeme for the lexer.

lexeme

Return the lexeme at the specified line and column.

lexemes

Return a list of lexemes.

lines

Return the number of lines in the lexer.

Methods Documentation

first() understand.Lexeme

Return the first lexeme for the lexer.

Return type:

understand.Lexeme

Returns:

the first understand.Lexeme

lexeme(line, column) understand.Lexeme

Return the lexeme at the specified line and column.

Parameters:
  • line (integer) – the line number

  • column (integer) – the column number

Return type:

understand.Lexeme

Returns:

the lexeme at the location

lexemes(start_line=None, end_line=None) list[understand.Lexeme]

Return a list of lexemes.

If the optional parameters start_line or end_line are specified, only the lexemes within these lines are returned.

Parameters:
  • start_line (integer or None) – Optional start line

  • end_line (integer or None) – Optional start line

Return type:

list[understand.Lexeme]

Returns:

a list of lexemes

lines() int

Return the number of lines in the lexer.

Return type:

int

Returns:

the number of lines