- class tyrian.typarser.grammar_parser.GrammarParser(raw_grammar: dict=None, token_defs: dict=None, grammar_mapping: dict=None, settings: dict=None)¶
Does the grunt work of parsing the Grammar into a usable object; see grammar_nodes for more
Parameters:
- raw_grammar – single string containing raw grammar definitions, see load_grammar
- token_defs – dictionary of token definitions, see load_token_definitions
- load_grammar_mapping(nodes)¶
Load in a mapping between grammars and Nodes
Supply a dictionary with a mapping between subgrammar names and Node objects
Parameters: grammar_mapping – dictionary mapping subgrammars to appropriate Nodes
- load_grammar(content:str)¶
Load grammars from a string. All grammars need not be necessarily be loaded at once, but all must be loaded before parse_grammars() is called.
Parameters: content – single string containing raw grammar definitions a grammar can be defined like so: name: <content>; whereby within the following constructs are permissible; OR, which can be nested, is denoted by a pipe character: <token> | <token> many of a particular token: <token>+ a subgrammar or token is simply specified by name; NAME
- load_token_definitions(defs: dict)¶
Loads token definitions.
expected to be formatted as follows;
Parameters: defs – dictionary containing token definitions { 'literal': { '<content>': '<name>', ... }, 'regex': { '<regex_expr>': '<name>', ... } }
- parse_grammars()¶
Parses loaded grammars into “check trees”.
These “check trees” consist of a root ContainerNode, where a list of tokens can be passed into the root GrammarNode‘s check() function and validated according to the loaded grammars.
- parse_grammar(grammar: str, grammar_key: str, settings: dict)¶
See parse_grammars()
Parameters:
- grammar – single string containing a single raw grammar definition, see parse_grammars
- grammar_key – key for grammar, aka name of grammar
- settings – dictionary of settings for Nodes