The treebuilderutil module

Helper functions and classes for the treebuilder module.

class BuildResult(tree, start, end, offset, lexicons)

Bases: tuple

encapsulates the return values of TreeBuilder.build_new_tree()

property end

Alias for field number 2

property lexicons

Alias for field number 4

property offset

Alias for field number 3

property start

Alias for field number 1

property tree

Alias for field number 0

class ReplaceResult(start, end, lexicons)

Bases: tuple

encapsulates the return values of TreeBuilder.replace_tree()

property end

Alias for field number 1

property lexicons

Alias for field number 2

property start

Alias for field number 0

class Changes[source]

Bases: object

Store changes that have to be made to a tree.

This object is used by get_changes(). Calling add() merges new changes with the existing changes.

text
root_lexicon
start
removed
added
add(text, root_lexicon=False, start=0, removed=None, added=None)[source]

Merge new change with existing changes.

If added and removed are not given, all text after start is considered to be replaced.

has_changes()[source]

Return True when there are actually changes.

new_position(pos)[source]

Return how the current changes would affect an older start.

get_prepared_lexer(tree, text, start)[source]

Get a prepared lexer reading from text, positioned at (or before) start.

Returns the three-tuple (lexer, events, tokens). The events stream is returned seperately because the last Event can be pushed back, so it is yielded again. The tokens are the last tokens group that remained the same.

Returns None when no position to start can be found, just start from the beginning in this case.

events_with_tokens(start_token, last_token)[source]

Yield (Event, tokens) tuples for start_token until and including last_token.

Events are yielded together with token groups (or single tokens in a 1-length tuple).

This can be used to compare an existing token structure with events originating from a lexer.

get_lexer(token)[source]

Get a Lexer initialized at the token’s ancestry.

new_tree(token)[source]

Return an empty context (and its root) with the same ancestry as the token’s.

find_token_before(node, pos)[source]

A version of find_token_before() that can handle empty contexts.

The new tree built inside build_new() can have an empty context at the beginning and/or the end. Returns None if there is no token left from pos.