Reference

Parsing

class axaxaxas.ParseRule(head, symbols, *, penalty=0)

Represents a single production in a context free grammar.

class axaxaxas.ParseTree(rule, children=None)

Tree structure representing a sucessfully parsed rule

class axaxaxas.ParseForest(top_partial_rule)

Represents a collection of related ParseTree objects.

__iter__()

Iterators over the list of contained ParseTree objects. Calling all is somewhat faster

all()

Returns a list of the contained ParseTree objects

apply(builder)

Constructs a result step at a time using the given Builder.

count()

Returns a count of the contained ParseTree objects

single()

Returns the only ParseTree in the collection, or throws if there are multiple.

class axaxaxas.ParseRuleSet

Stores a set of ParseRule, with fast retrieval by rule head

add(rule)

Adds a new ParseRule to the set

get(head, lookahead_token=None)

Returns a list of ParseRule objects with matching head

is_anonymous(head)

Returns true if a given head symbol should be omitted from error reporting

axaxaxas.parse(rule_set, head, tokens, *, fail_if_empty=True)

Parses a stream of tokens according to the grammer in rule_set by attempting to match the non-terminal specified by head.

axaxaxas.unparse(parse_tree)

Converts a ParseTree back to a list of tokens

Errors

class axaxaxas.ParseError(message, start_index, end_index)

Base parse error

class axaxaxas.AmbiguousParseError(message, start_index, end_index, values)

Indicates that there were multiple possible parses in a context that requires only one

class axaxaxas.NoParseError(message, start_index, end_index, encountered, expected_terminals, expected)

Indicates there were no possible parses

class axaxaxas.InfiniteParseError(message, start_index, end_index)

Indicates there were infinite possible parses

Building

class axaxaxas.BuilderContext

Contains information about the location and rule currently being parsed. The exact meaning is specific to each method of Builder.

rule

The relevant ParseRule. Can be None for merge_vertical calls at the top level.

symbol_index

context.rule.symbols[context.symbol_index] indicates the relevant symbol of the rule. Note symbol_index may be len(context.rule.symbols) in some circumstances.

start_index

The first token in the relevant range of tokens.

end_index

After the last token in the relevant range of tokens.

class axaxaxas.Builder

Abstract base class for constructing parse trees and other objects from a ParseForest. See Builders for more details.

begin_multiple(context, prev_value)

Called when a plus or star symbols is enountered

end_multiple(context, prev_value)

Called when there are no more matches for a plus or star symbol

end_rule(context, prev_value)

Called when a new rule is completed

extend(context, prev_value, extension_value)

Called when a symbol is matched. Potentially multiple times for a star or plus symbol

merge(context, values)

Called when there are multiple possible parses, unless merge_vertical and merge_horizontal is overriden.

merge_horizontal(context, values)

Called when there are multiple possible parses of a ParseRule.

merge_vertical(context, values)

Called when multiple possible ParseRule objects could match a non terminal

skip_optional(context, prev_value)

Called when an optional symbol is skipped over

start_rule(context)

Called when a new rule is started

terminal(context, token)

Called when a terminal is matched

axaxaxas.make_list_builder(builder)

Takes a Builder which lacks an implementation for merge_horizontal and merge_vertical, and returns a new Builder that will accumulate all possible built parse trees into a list

axaxaxas.make_iter_builder(builder)

Takes a Builder which lacks an implemenation for merge_horizontal and merge_vertical, and returns a new Builder that will accumulate all possible built parse trees into an iterator.

Symbols

class axaxaxas.Symbol(*, star=False, optional=False, plus=False, name=None, greedy=False, lazy=False)

Base class for non-terminals and terminals, this is used when defining ParseRule objects

class axaxaxas.NonTerminal(head, prefer_early=False, prefer_late=False, **kwargs)

Represents a non-terminal symbol in the grammar, matching tokens according to any ParseRules with the specified head

class axaxaxas.Terminal(token, **kwargs)

Represents a terminal symbol in the grammar, matching a single token of the input

match(token)

Returns true if token is matched by this Terminal