Skip to content

vtf txb display highlightertree

srccircumflex edited this page Apr 24, 2023 · 4 revisions

↑ vtf-txb-display

highlightertree

The module contains the derivatives of the syntaxtree for the highlighters and the objects for designing the Highlight-Syntax-Tree.

Module contents

Objects

... for the design

class highlightertree.HighlighterBranch(_SyntaxBranch)

The object represents a syntax branch of the highlighting.

The definition of the node point and the stop point of a branch are each given by a RegularExpression; in addition, a graphical representation can optionally be assigned to the matched characters of the points, as SGRWrap-parameters. The graphical rendering of the characters matching node_pattern and stop_pattern, is implemented by the Highlighter* component in the Display* via SGRWrap. Therefore, the keyword arguments *_sgr_params, *_sgr_inner and *_sgr_cellular are passed to SGRWrap.

The *_priority of a syntax leaf over others found in a string is realized with __lt__.

The parameterization can be defined differently by an executable object, this executable object gets the origin HighlighterLeaf and the other HighlighterLeaf and must return a boolean value whether the origin HighlighterLeaf has a higher priority than the other. If True is passed to *_priority, the earliest leaf has the highest priority, and if there is a tie the match with the largest span has priority. If the *_priority parameter is False, the earliest leaf with the smallest span has priority.

The stop_pattern of a branch can also be specified as an executable object, for example, to define the end of a branch depending on the node that occurred. The function is then executed when the branch is activated with the node-HighlighterLeaf and must return the stopping pattern.

>>> HighlighterBranch(node_pattern="['\"]", stop_pattern=lambda leaf: leaf.match.group())

A branch is activated when the node_pattern occurs and when the node leaf has the highest priority over other found leaves in the string. By default, the activation method returns the same object if the stop_pattern is parameterized as a pattern. If an executable object is passed as described above, an image of the current attributes of the branch is created by default during activation to preserve the stopping pattern (snap-method). A deviating activation function can also be passed via the keyword argument activate, this function then receives this branch object on activation and must return a branch object.

The parameterizations of multirow and multiline are evaluated within the parsing process and indicate whether the branch is valid across rows or lines. The string passed during the single execution of a parsing method in the _HighlightTree* (_SyntaxTree) is interpreted as a row; a line break is defined via the argument has_end of the parsing methods.

The leaves of the branch are also specified as RegularExpression -- SGRWrap-parameter pairs using the add_leaf method, the prority is defined as for the node and stop leaves. Further ramifications are again added as a HighlighterBranch via the add_branch method.

The freely definable labels of a branch or leaf, are used for a later identification of syntax definitions, for example by the methods remove_leafs_by_label and remove_branches_by_label in the HighlighterBranch.

__init__(node_pattern, stop_pattern, *, node_sgr_params=None, node_sgr_inner=True, node_sgr_cellular=True, node_priority=True, stop_sgr_params=None, stop_sgr_inner=True, stop_sgr_cellular=True, stop_priority=True, activate=None, multirow=True, multiline=False, label=None)

add_branch(branch) -> None

Add a ramification to the HighlighterBranch.

add_leaf(pattern, sgr_params, *, sgr_inner=True, sgr_cellular=True, priority=True, label=None) -> None

Add a highlight rule to the HighlighterBranch.

class highlightertree.HighlighterGlobals(** _SyntaxGlobals**)

The container for branch-independent highlight definitions.

Definitions are created as RegularExpression -- SGRWrap-parameter pairs using the add method, additionally each object can be used as a label to remove definitions afterwards.

The graphical rendering of the characters matching pattern, is implemented by the Highlighter* component in the Display* via SGRWrap. Therefore, the keyword arguments sgr_inner and sgr_cellular are passed to SGRWrap.

The priority of a syntax leaf over others found in a string is realized with __lt__.

The parameterization can be defined differently by an executable object, this executable object gets the origin HighlighterLeaf and the other HighlighterLeaf and must return a boolean value whether the origin HighlighterLeaf has a higher priority than the other. If True is passed to priority, the earliest leaf has the highest priority, and if there is a tie the match with the largest span has priority. If the priority parameter is False, the earliest leaf with the smallest span has priority.

from re import compile
from vtframework.iodata.sgr import Ground
__highlighter__: HighlightAdvanced

# enable trailing spaces
__highlighter__.globals.add(compile("\\s*$"), Ground.name("cyan"), label="trailing spaces")

# disable trailing spaces
__highlighter__.globals.remove_by_label("trailing spaces")

leafs: tuple[tuple[Pattern | str,Callable[ [HighlighterGlobals, Pattern | str, Match, int], HighlighterLeaf], Any], ...]

add(pattern, sgr_params, *, sgr_inner=True, sgr_cellular=True, priority=True, label=None) -> None

Add a globally applicable highlight rule to the HighlighterGlobals.

class highlightertree.HighlighterLeaf(_SyntaxLeaf)

The syntax leaf object is generated as the result of a parse by _HighlightTree* (_SyntaxTree) and is used for further automatic processing of the highlighting.

The object contains the parent HighlighterBranch or HighlighterGlobals, the re.Match, the relative start ( relstart ) of a substring and, if the leaf represents the beginning of a HighlighterBranch, the beginning branch under node.

In addition, to the deviation is assigned the relevant _Row object and an action, for the graphical conversion of the matched text.

Since the string is sliced during the parsing process, the start/end/span methods of the re.Match object (also realized as properties in the HighlighterLeaf) may not return the actual values with reference to the passed string; therefore, the values can be obtained considering the relative starting point via the properties with total_* prefix.

Syntax tree derivatives

class highlightertree._HighlightTreeAdvanced(_SyntaxTree)

branch_growing(string, has_end, _branches_) -> None

This method is executed during the parsing process.

Apply to a string only the HighlighterBranch configurations (skip parsing the leaves) and expand or shorten the list of _branches_. Via has_end it is specified whether the string has a terminating end and is processed in connection with the multiline parameterization of the HighlighterBranch. The list of _branches_ represents the current sequence of active HighlighterBranch's; if it is empty, the root is the current HighlighterBranch.

@property
globals() -> HighlighterGlobals
The HighlighterGlobals.

map_globals(string, row, _out_) -> None

This method is executed during the parsing process.

Apply the leaves defined in the HighlighterGlobals to a string, append the parsed leaves to the _out_ list as HighlighterLeaf objects.

map_leafs(string, row, has_end, _branches_, _leaf_out_) -> None

This method is executed during the parsing process.

Apply the entire configurations of the HighlighterBranch's and their HighlighterLeaf's to a string. Append the parsed leaves to the list _leaf_out_ and expand or shorten the list of active _branches_. Via has_end it is specified whether the string has a terminating end and is processed in connection with the multiline parameterization of the HighlighterBranch. The list of _branches_ represents the current sequence of active HighlighterBranch's; if it is empty, the root is the current HighlighterBranch.

purge_globals() -> HighlighterGlobals

Reinitialize the current HighlighterGlobals.

purge_root() -> HighlighterBranch

Reinitialize the current HighlighterBranch.
@property
root() -> HighlighterBranch
The root-HighlighterBranch.

set_globals(__new_globals) -> None

Set the HighlighterGlobals.

set_root(__new_root) -> None

Set the HighlighterBranch.

class highlightertree._HighlightTreeRegex(_SyntaxTree)

branch_growing(*args, **kwargs) -> None

Raises AttributeError.
@property
globals() -> HighlighterGlobals
The HighlighterGlobals.

map_globals(string, row, _out_) -> None

This method is executed during the parsing process.

Apply the leaves defined in the globals to a string, append the parsed leaves to the _out_ list as HighlighterLeaf objects.

map_leafs(*args, **kwargs) -> None

Raises AttributeError.

map_tree(*args, **kwargs) -> None

Raises AttributeError.

purge_globals() -> HighlighterGlobals

Reinitialize the current HighlighterGlobals.

purge_root() -> _SyntaxBranch

Raises AttributeError.
@property
root() -> _SyntaxBranch
Raises AttributeError.

set_globals(__new_globals) -> None

Set the HighlighterGlobals.

set_root(__new_root) -> None

Raises AttributeError.

Date: 20 Dec 2022
Version: 0.1
Author: Adrian Hoefflin [srccircumflex]
Doc-Generator: "pyiStructure-RSTGenerator" <prototype>
Clone this wiki locally