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:
objectA 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 usingfirst. A lexer can be iterated over, where each item returned is alexeme.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
Return the first lexeme for the lexer.
Return the lexeme at the specified line and column.
Return a list of lexemes.
Return the number of lines in the lexer.
Classes Summary
Methods Documentation
- first() understand.Lexeme#
Return the first lexeme for the lexer.
- Return type:
- 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:
- 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:
objectA token recieved from a
lexer.It has an associated range represented with
line_begin,column_begin,line_end, andcolumn_end. A lexeme also has thetextfor the range and atokendescribing the kind (comment, whitespace, etc). Theinactivemethod can be used to test if the lexeme is in inactive code.If a corresponding
entityand/orreferenceexists for the lexeme, it can be retrieved withentandrefrespectively. Lexemes can be navigated withnextandprevious.Lexeme line and column numbers are 1-based, and both the beginning and the end are inclusive.
Methods Summary
Return the column of the first character of the lexeme.
Return the column of the last character of the lexeme.
Return the entity associated with the lexeme or None if none.
Return True if the lexeme is part of inactive code.
Return the line of the first character of the lexeme.
Return the line of the last character of the lexeme.
Return the next lexeme, or None if no lexemes remain.
Return the previous lexeme, or None if beginning of file.
Return the reference associated with the lexeme, or None if none.
Return the text for the lexeme, which may be empty ("").
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:
- 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:
- 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:
- Returns:
the previous lexeme if it exists
- ref() understand.Ref#
Return the reference associated with the lexeme, or None if none.
- Return type:
- 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