The Understand Python API gives scripts access to everything Understand knows about your code — entities, references, metrics, and dependencies — and powers custom checks, graphs, and reports.

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. A lexeme’s beginning and end are both inclusive.

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.

Classes Summary

Lexeme

A token recieved from a 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, or None if there is no lexeme there

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

Lexeme#

class understand.Lexeme#

Bases: object

A token recieved from a lexer.

It has an associated range represented with line_begin, column_begin, line_end, and column_end. A lexeme also has the text for the range and a token describing the kind (comment, whitespace, etc). The inactive method can be used to test if the lexeme is in inactive code.

If a corresponding entity and/or reference exists for the lexeme, it can be retrieved with ent and ref respectively. Lexemes can be navigated with next and previous.

Lexeme line and column numbers are 1-based, and both the beginning and the end are inclusive.

Methods Summary

column_begin

Return the column of the first character of the lexeme.

column_end

Return the column of the last character of the lexeme.

ent

Return the entity associated with the lexeme or None if none.

inactive

Return True if the lexeme is part of inactive code.

line_begin

Return the line of the first character of the lexeme.

line_end

Return the line of the last character of the lexeme.

next

Return the next lexeme, or None if no lexemes remain.

previous

Return the previous lexeme, or None if beginning of file.

ref

Return the reference associated with the lexeme, or None if none.

text

Return the text for the lexeme, which may be empty ("").

token

Return the token kind of the lexeme.

Methods Documentation

column_begin() → int#

Return the column of the first character of the lexeme.

Columns are 1-based and the beginning is inclusive.

Return type:

int

Returns:

the beginning column

column_end() → int#

Return the column of the last character of the lexeme.

Columns are 1-based and the end is inclusive. For example, a two-character name at column 9 ends at column 10.

Return type:

int

Returns:

the ending column

ent() → understand.Ent#

Return the entity associated with the lexeme or None if none.

Return type:

understand.Ent

Returns:

the associated entity or None if none

inactive() → bool#

Return True if the lexeme is part of inactive code.

Return type:

bool

Returns:

True if the lexeme is part of inactive code

line_begin() → int#

Return the line of the first character of the lexeme.

Lines are 1-based and the beginning is inclusive.

Return type:

int

Returns:

the beginning line

line_end() → int#

Return the line of the last character of the lexeme.

Lines are 1-based and the end is inclusive.

Return type:

int

Returns:

the ending line

next(ignore_whitespace=False, ignore_comments=False) → understand.Lexeme#

Return the next lexeme, or None if no lexemes remain.

Parameters:
  • ignore_whitespace (bool or None) – if True, skip whitespace lexemes. False by default.

  • ignore_comments (bool or None) – if True, skip comment lexemes. False by default.

Return type:

understand.Lexeme

Returns:

the next lexeme if it exists

previous(ignore_whitespace=False, ignore_comments=False) → understand.Lexeme#

Return the previous lexeme, or None if beginning of file.

Parameters:
  • ignore_whitespace (bool or None) – if True, skip whitespace lexemes. False by default.

  • ignore_comments (bool or None) – if True, skip comment lexemes. False by default.

Return type:

understand.Lexeme

Returns:

the previous lexeme if it exists

ref() → understand.Ref#

Return the reference associated with the lexeme, or None if none.

Return type:

understand.Ref

Returns:

the associated reference or None if none

text() → str#

Return the text for the lexeme, which may be empty (“”).

Return type:

str

Returns:

the associated text which may be empty

token() → str#

Return the token kind of the lexeme.

Return type:

str

Returns:

a token value

Values include:

  • Comment

  • Continuation

  • EndOfStatement

  • Identifier

  • Keyword

  • Label

  • Literal

  • Newline

  • Operator

  • Preprocessor

  • Punctuation

  • String

  • Whitespace

  • Indent

  • Dedent