finesse.script.parser module

Kat parser to convert tokens to productions.

This is a recursive descent parser, providing unlimited lookahead capabilities to allow arbitrary context-free grammars to be parsed. The implementation of memoization makes it a packrat parser which runs in linear time at the expense of potentially unlimited memory use. In practice memory use is limited by the simplicity of typical kat scripts (e.g. expressions don’t tend to have many subexpressions).

This is inspired by Python’s PEG parser, used as of 3.9. Some introductory information can be found in this series of blog posts.

Sean Leavey <sean.leavey@ligo.org>

class finesse.script.parser.KatParser[source]

Bases: object

Kat script parser.

This uses so-called packrat parsing to reduce, via productions, tokens yielded from a token stream generated from an input file or string to KatScriptItem objects containing the associated tokens.

array(*args)[source]
array_value(*args)[source]
array_values(*args)[source]
element(*args)[source]
element_key_value_list(*args)[source]
element_params(*args)[source]
element_value_list(*args)[source]
empty(*args)[source]
expect_production(production, *args, **kwargs)[source]
expect_token(*args)[source]
expr(*args)[source]
expr1(*args)[source]
expr2(*args)[source]
expr3(*args)[source]
expr4(*args)[source]
function(*args)[source]
function_key_value_list(*args)[source]
function_params(*args)[source]
function_value_list(*args)[source]
get_token()[source]
invalid_array(*args)[source]
invalid_expr4(*args)[source]
invalid_script_line(*args)[source]
key_value(*args)[source]
loop(production, nonempty, *args, **kwargs)[source]
mark()[source]
maybe_trailing_comma(multiline)[source]
maybe_whitespace(multiline, whitespace)[source]

Expect zero or more whitespace if enabled, else return [].

negative_lookahead(token_type)[source]
parse(string)[source]

Parse the contents of string.

Parameters

stringstr

The string to parse kat script from.

Returns

KatScript

The parsed kat script.

parse_file(fobj)[source]

Parse the contents of the specified file.

Parameters

fobjio.FileIO

The file object to parse kat script from. This should be opened in text mode.

Returns

KatScript

The parsed kat script.

peek_token()[source]
positional_value(*args)[source]
positive_lookahead(token_type)[source]
reset(pos)[source]
property script[source]
script_line(*args)[source]
script_line_empty(*args)[source]
start()[source]
statement(*args)[source]
value(*args)[source]