httk.core.optimade.filter

The OPTIMADE filter language as a first-class httk citizen.

This package makes the OPTIMADE filter language a standard httk query feature: parse_optimade_filter() parses a filter string into a nested tuple abstract syntax tree (the “ojf” format, FilterAst), e.g.:

('AND', ('HAS_ALL', ('=', '='), ('Identifier', 'elements'),
         (('String', 'Ga'), ('String', 'Ti'))),
        ('OR', ('=', ('Identifier', 'nelements'), ('Number', '3')),
               ('=', ('Identifier', 'nelements'), ('Number', '2'))))

It is the sibling of the vendored OPTIMADE property definitions in httk.registry.schemas.core — two halves of the same standard: the property definitions describe what can be queried, and this package parses the query language itself. The grammar is loaded from the packaged optimade_filter_grammar.ebnf (see the adjacent README.md for provenance) and parsed by the vendored LR(1) parser generator in _miniparser.

Errors raised on bad user input are ParserSyntaxError; grammar problems raise ParserGrammarError; parser bugs raise ParserInternalError. All are subclasses of ParserError.

Submodules

Attributes

Exceptions

ParserError

Common base class for all non-exit exceptions.

ParserGrammarError

Common base class for all non-exit exceptions.

ParserInternalError

Common base class for all non-exit exceptions.

ParserSyntaxError

Common base class for all non-exit exceptions.

Functions

optimade_parse_tree_to_ojf(ast)

Convert a raw OPTIMADE parse tree into the public AST format.

parse_optimade_filter(filter_string[, verbosity])

Parse an OPTIMADE filter into the public abstract syntax tree format.

parse_optimade_filter_raw(filter_string[, verbosity])

Parse an OPTIMADE filter and return its raw grammar parse tree.

Package Contents

exception httk.core.optimade.filter.ParserError[source]

Bases: Exception

Common base class for all non-exit exceptions.

exception httk.core.optimade.filter.ParserGrammarError[source]

Bases: ParserError

Common base class for all non-exit exceptions.

exception httk.core.optimade.filter.ParserInternalError[source]

Bases: ParserError

Common base class for all non-exit exceptions.

exception httk.core.optimade.filter.ParserSyntaxError(*args)[source]

Bases: ParserError

Common base class for all non-exit exceptions.

info
line
pos
linestr
type httk.core.optimade.filter.FilterAst = tuple[Any, ...][source]
httk.core.optimade.filter.optimade_parse_tree_to_ojf(ast)[source]

Convert a raw OPTIMADE parse tree into the public AST format.

Parameters:

ast (tuple[Any, ...]) – Raw parse tree rooted at Filter.

Returns:

Nested tuple abstract syntax tree in ojf format.

Raises:
  • ParserSyntaxError – If the tree uses an unsupported or invalid construct, or is too deeply nested to convert within the recursion limit.

  • ParserInternalError – If the tree has an invalid internal shape.

Return type:

FilterAst

httk.core.optimade.filter.parse_optimade_filter(filter_string, verbosity=0)[source]

Parse an OPTIMADE filter into the public abstract syntax tree format.

Parameters:
  • filter_string (str) – OPTIMADE filter expression to parse.

  • verbosity (int | Any) – Diagnostic verbosity setting passed to the parser.

Returns:

Nested tuple abstract syntax tree in ojf format.

Raises:
  • ParserSyntaxError – If the filter cannot be parsed, or is too long or too deeply nested to parse within the interpreter’s recursion limit.

  • ParserError – If the filter cannot be parsed.

Return type:

FilterAst

httk.core.optimade.filter.parse_optimade_filter_raw(filter_string, verbosity=0)[source]

Parse an OPTIMADE filter and return its raw grammar parse tree.

Parameters:
  • filter_string (str) – OPTIMADE filter expression to parse.

  • verbosity (int | Any) – Diagnostic verbosity setting passed to the parser.

Returns:

Raw nested tuple parse tree before ojf conversion.

Raises:
  • ParserSyntaxError – If the filter cannot be parsed, or is too long or too deeply nested to parse within the interpreter’s recursion limit.

  • ParserError – If the filter cannot be parsed.

Return type:

tuple[Any, …]