textmate_grammar.handler

Module Contents

Classes

ContentHandler

The handler object targetted for parsing.

Data

POS

API

textmate_grammar.handler.POS = None
class textmate_grammar.handler.ContentHandler(content: str, pre_processor: Callable[[str], str] = _dummy_pre_processor)[source]

The handler object targetted for parsing.

To parse a string or file, it needs to be loaded into the ContentHandler object. The handler will take care of all read actions on the input stream, where the contents are index by a tuple (line_number, line_position). Additionally, the handler contains the search method to match a search span against a input oniguruma regex pattern.

Initialization

Initialize a new instance of the Handler class.

Parameters:
  • content (str) – The source code to be processed.

  • pre_processor (BasePreProcessor) – A pre-processor to use on the input string of the parser

Variables:
  • content – The source code to be processed.

  • lines – A list of lines in the source code, with a newline character at the end of each line.

  • line_lengths – A list of lengths of each line in the source code.

  • anchor – The current position in the source code.

notLookForwardEOL = 'compile(...)'
classmethod from_path(file_path: pathlib.Path, **kwargs) textmate_grammar.handler.ContentHandler[source]

Loads a file from a path

next(pos: textmate_grammar.handler.POS, step: int = 1) textmate_grammar.handler.POS[source]

Returns the next position on the current handler.

Parameters:
  • pos – The current position as a tuple (line, column).

  • step – The number of steps to move forward. Defaults to 1.

Returns:

The next position as a tuple (line, column).

prev(pos: textmate_grammar.handler.POS, step: int = 1) textmate_grammar.handler.POS[source]

Returns the previous position on the current handler.

Parameters:
  • pos – The current position as a tuple (line, column).

  • step – The number of steps to go back. Defaults to 1.

Returns:

The previous position as a tuple (line, column).

range(start: textmate_grammar.handler.POS, close: textmate_grammar.handler.POS) list[textmate_grammar.handler.POS][source]

Returns a list of positions between the start and close positions.

Parameters:
  • start – The starting position.

  • close – The closing position.

Returns:

A list of positions between the start and close positions.

chars(start: textmate_grammar.handler.POS, close: textmate_grammar.handler.POS) dict[textmate_grammar.handler.POS, str][source]

Returns a dictionary mapping each position within the given range to the corresponding source character.

Parameters:
  • start – The starting position of the range.

  • close – The closing position of the range.

Returns:

A dictionary mapping each position within the range to the corresponding source character.

read_pos(start_pos: textmate_grammar.handler.POS, close_pos: textmate_grammar.handler.POS, skip_newline: bool = True) str[source]

Reads the content between the start and end positions.

Parameters:
  • start_pos – The starting position of the content.

  • close_pos – The closing position of the content.

  • skip_newline – Whether to skip the newline character at the end of the content.

Returns:

The content between the start and end positions.

Raises:

ImpossibleSpan – If the start position is greater than the close position.

read_line(pos: textmate_grammar.handler.POS) str[source]

Reads a line from the specified position and returns it.

Parameters:

pos – The position of the line to read. The first element is the line number (0-based), and the second element is the starting position within the line.

Returns:

The line starting from the specified position.

read(start_pos: textmate_grammar.handler.POS, length: int = 1, skip_newline: bool = True) str[source]

Reads the content from start for a length.

Parameters:
  • start_pos – The starting position to read from.

  • length – The number of characters to read. Defaults to 1.

  • skip_newline – Whether to skip the newline character at the end of the read content. Defaults to True.

Returns:

The content read from the specified position.

Raises:

ImpossibleSpan – If the length is negative.

search(pattern: onigurumacffi._Pattern, starting: textmate_grammar.handler.POS, boundary: textmate_grammar.handler.POS | None = None, greedy: bool = False, **kwargs) tuple[onigurumacffi._Match | None, tuple[textmate_grammar.handler.POS, textmate_grammar.handler.POS] | None][source]

Matches the stream against a capture group.

Parameters:
  • pattern – The regular expression pattern to match against the stream.

  • starting – The starting position in the stream.

  • boundary – The boundary position in the stream. Defaults to None.

  • greedy – Determines if the matching should be greedy or not. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

A tuple containing the matching result and the span of the match.

Note

  • The stream is matched against the input pattern. If there are any capture groups, each is then subsequently parsed by the inputted parsers. The number of parsers therefore must match the number of capture groups of the expression, or there must be a single parser and no capture groups.

  • The greedy parameter determines if the matching should be greedy or not. If set to True, the matching will try to consume as much of the stream as possible. If set to False, the matching will stop at the first match found.

  • The boundary parameter can be used to specify a boundary position in the stream. If provided, the matching will not go beyond this boundary position.

  • The leading_chars parameter can be used to specify the type of leading characters allowed, with:
    • 0: none allowed

    • 1: whitespace characters allowed

    • 2: any character allowed.