textmate_grammar.parser

Module Contents

Classes

GrammarParser

The abstract grammar parser object

TokenParser

The parser for grammars for which only the token is provided.

MatchParser

The parser for grammars for which a match pattern is provided.

ParserHasPatterns

PatternsParser

The parser for grammars for which several patterns are provided.

BeginEndParser

The parser for grammars for which a begin/end pattern is provided.

BeginWhileParser

The parser for grammars for which a begin/end pattern is provided.

API

class textmate_grammar.parser.GrammarParser(grammar: dict, language_parser: textmate_grammar.parsers.base.LanguageParser | None = None, key: str = '', is_capture: bool = False, **kwargs)[source]

Bases: abc.ABC

The abstract grammar parser object

Initialization

Initialize a Parser object.

Parameters:
  • grammar – The grammar dictionary.

  • language – The language parser object. Defaults to None.

  • key – The key for the parser. Defaults to “”.

  • is_capture – Indicates if the parser is a capture. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

None

static initialize(grammar: dict, **kwargs)[source]

Initializes the parser based on the grammar.

Parameters:
  • grammar – The grammar to initialize the parser with.

  • kwargs – Additional keyword arguments.

Returns:

The initialized parser.

property comment: str
property disabled: bool
parse(handler: textmate_grammar.handler.ContentHandler, starting: textmate_grammar.handler.POS = (0, 0), boundary: textmate_grammar.handler.POS | None = None, **kwargs) tuple[bool, list[textmate_grammar.elements.Capture | textmate_grammar.elements.ContentElement], tuple[int, int] | None][source]

The method to parse a handler using the current grammar.

Parameters:
  • handler – The ContentHandler object that will handle the parsed content.

  • starting – The starting position for parsing. Defaults to (0, 0).

  • boundary – The boundary position for parsing. Defaults to None.

  • **kwargs

    Additional keyword arguments that can be passed to the parser.

Returns:

A tuple containing: - parsed: A boolean indicating whether the parsing was successful. - elements: A list of Capture or ContentElement objects representing the parsed content. - span: A tuple containing the starting and ending positions of the parsed content, or None if parsing failed.

match_and_capture(handler: textmate_grammar.handler.ContentHandler, pattern: textmate_grammar.handler.Pattern, starting: textmate_grammar.handler.POS, boundary: textmate_grammar.handler.POS, parsers: dict[int, textmate_grammar.parser.GrammarParser] | None = None, parent_capture: textmate_grammar.elements.Capture | None = None, **kwargs) tuple[tuple[textmate_grammar.handler.POS, textmate_grammar.handler.POS] | None, str, list[textmate_grammar.elements.Capture | textmate_grammar.elements.ContentElement]][source]

Matches a pattern and its capture groups.

Matches the pattern on the handler between the starting and boundary positions. If a pattern is matched, its capture groups are initialized as Capture objects. These are only parsed after the full handler has been parsed. This occurs in GrammarParser.parse when calling parse_captures.

Parameters:
  • handler – The content handler to match the pattern on.

  • pattern – The pattern to match.

  • starting – The starting position for the match.

  • boundary – The boundary position for the match.

  • parsers – A dictionary of parsers.

  • parent_capture – The parent capture object.

  • kwargs – Additional keyword arguments.

Returns:

A tuple containing the span of the match, the matched string, and a list of capture objects or content elements.

class textmate_grammar.parser.TokenParser(grammar: dict, **kwargs)[source]

Bases: textmate_grammar.parser.GrammarParser

The parser for grammars for which only the token is provided.

Initialization

Initialize a Parser object.

Parameters:
  • grammar – The grammar dictionary.

  • language – The language parser object. Defaults to None.

  • key – The key for the parser. Defaults to “”.

  • is_capture – Indicates if the parser is a capture. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

None

class textmate_grammar.parser.MatchParser(grammar: dict, **kwargs)[source]

Bases: textmate_grammar.parser.GrammarParser

The parser for grammars for which a match pattern is provided.

Initialization

Initialize a Parser object.

Parameters:
  • grammar – The grammar dictionary.

  • language – The language parser object. Defaults to None.

  • key – The key for the parser. Defaults to “”.

  • is_capture – Indicates if the parser is a capture. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

None

class textmate_grammar.parser.ParserHasPatterns(grammar: dict, **kwargs)[source]

Bases: textmate_grammar.parser.GrammarParser, abc.ABC

class textmate_grammar.parser.PatternsParser(grammar: dict, **kwargs)[source]

Bases: textmate_grammar.parser.ParserHasPatterns

The parser for grammars for which several patterns are provided.

Initialization

Initialize a Parser object.

Parameters:
  • grammar – The grammar dictionary.

  • language – The language parser object. Defaults to None.

  • key – The key for the parser. Defaults to “”.

  • is_capture – Indicates if the parser is a capture. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

None

class textmate_grammar.parser.BeginEndParser(grammar: dict, **kwargs)[source]

Bases: textmate_grammar.parser.ParserHasPatterns

The parser for grammars for which a begin/end pattern is provided.

Initialization

Initialize a Parser object.

Parameters:
  • grammar – The grammar dictionary.

  • language – The language parser object. Defaults to None.

  • key – The key for the parser. Defaults to “”.

  • is_capture – Indicates if the parser is a capture. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

None

class textmate_grammar.parser.BeginWhileParser(grammar: dict, **kwargs)[source]

Bases: textmate_grammar.parser.PatternsParser

The parser for grammars for which a begin/end pattern is provided.

Initialization

Initialize a Parser object.

Parameters:
  • grammar – The grammar dictionary.

  • language – The language parser object. Defaults to None.

  • key – The key for the parser. Defaults to “”.

  • is_capture – Indicates if the parser is a capture. Defaults to False.

  • kwargs – Additional keyword arguments.

Returns:

None