ransack.parser module

parser.py - Provides a Parser class for parsing input data using Lark.

This module defines a custom grammar for parsing various expressions such as comparisons, arithmetic operations, logical operators, IP address parsing, datetime formats, and strings. The Parser class utilizes the Lark parser to build a parse tree for input expressions that conform to the grammar rules.

Classes:
  • Parser: Encapsulates the Lark parser with the defined grammar to parse

    input data into an abstract syntax tree (AST).

class ransack.parser.Parser(context: dict[str, Any] | None = None)

Parser for query expressions based on a custom grammar using Lark.

This class accepts an optional context dictionary at initialization. The context provides user-defined variables that can be referenced in expressions and take precedence over variables from the input data source.

To avoid ambiguity, variables prefixed with a dot (e.g., .foo) are always resolved from the data, while unprefixed variables (e.g., foo) are resolved from the context if present.