knit_script.knit_script_interpreter.Knit_Script_Parser module
Parser code for accessing Parglare language support.
This module provides the Knit_Script_Parser class, which implements the concrete parser for knit script files using the parglare parsing library. It handles grammar loading, parser configuration, and provides debugging capabilities for grammar and parsing operations.
- class Knit_Script_Parser(debug_grammar=False, debug_parser=False, debug_parser_layout=False)[source]
Bases:
_Parser_Base
Parser for reading knit script files using parglare library.
The Knit_Script_Parser class provides the concrete implementation of knit script parsing using the parglare parsing framework. It loads the knit script grammar from resources, configures the parser with appropriate actions, and provides debugging capabilities for grammar development and troubleshooting parsing issues.
- This parser supports comprehensive debugging options including grammar state visualization, shift-reduce operation tracking, and layout information display.
The parser integrates with the knit script action system to convert parsed syntax trees into executable knit script elements.
- __init__(debug_grammar=False, debug_parser=False, debug_parser_layout=False)[source]
Initialize the knit script parser with debugging options.
Loads the knit script grammar from package resources and creates a configured parglare parser instance with the specified debugging settings and action handlers.
- Parameters:
debug_grammar (bool, optional) – If True, provides full parglare output for grammar states during parsing. Useful for debugging grammar definitions and conflicts. Defaults to False.
debug_parser (bool, optional) – If True, provides full parglare output for parsed file shift-reduce status. Useful for debugging parsing operations and understanding parser behavior.
False. (Defaults to)
debug_parser_layout (bool, optional) – If True, provides layout information from parser including whitespace and indentation handling. Useful for debugging layout-sensitive parsing issues.
False.
- parse(pattern, pattern_is_file=False)
Execute the parsing code for the parglare parser.
Parses the provided knit script code or file and returns the resulting abstract syntax tree. Handles both string-based parsing and file-based parsing based on the pattern_is_file parameter.
- Parameters:
pattern (str) – Either a file path or the knit script string to be parsed, depending on the pattern_is_file parameter.
pattern_is_file (bool, optional) – If True, treats pattern as a file path and reads the content from that file.
False (If)
False. (treats pattern as the actual knit script code to parse. Defaults to)
- Returns:
List of statements parsed from the input, representing the abstract syntax tree of the knit script program.
- Return type:
list[Any]
- Raises:
Parsing_Exception – If parsing fails due to syntax errors or other parsing issues. This wraps the original parglare ParseError with additional KnitScript-specific context and formatting.