10.5. typped.pratt_constructs

This module holds the Construct class and the ConstructTable class definitions. They are basically just data containers, which are accessed by the PrattParser instances. Each PrattParser instance has a ConstructTable instance. The ConstructTable instances hold Construct instances.

Constructs themselves are just handler functions for a pratt parser which are also associated with a triggering token label, a preconditions function, and other data. See the documentation on preconditioned dispatching pratt parsers.

The actual dispatching of handlers is done by the dispatch_handler routine of the ConstructTable. It runs the preconditions functions in order, and returns a handler function for the winning construct. This handler function has its arguments bound, since they are known at the time the handler is chosen. This bound handler function first runs the registered handler; it then does type checking and other options before returning the subtree.

class typped.pratt_constructs.Construct(parser_instance, construct_label, trigger_head_or_tail=None, trigger_token_label=None, handler_fun=None, precond_fun=None, precond_priority=0, prec=0, assoc=None, key_eval_and_data_on_token_value=False, lookup_depends_on_pstate=False)[source]

Bases: object

A syntax construct in the language. Usually corresponds to a subtree of the final parse tree. Essentially a data frame for a handler function containing extra context information such as the preconditions function, etc.

A construct is possibly triggered when a hander needs to be dispatched for its kind of token. The preconditions functions for all such constructs are run in priority-sorted order. A construct is triggered if its preconditions function is the first to evaluate to true. It then provides the handler function to be called.

A list of typesigs must be stored because overloaded functions use the same handler function (it is unknown which signature will apply until after the arguments/subtree is parsed to resolve the overload). Some additional information (eval functions and ast labels) is stored in dicts keyed by typesigs.

This is a low-level container class which blindly creates, saves, and retrieves data. Higher-level constraints on constructs (such as preventing redefinition with a new head_or_tail or trigger_token_label) are handled in the ConstructTable class or the def_construct method of PrattParser.

parser_instance
construct_label
trigger_head_or_tail
trigger_token_label
handler_fun
precond_fun
precond_priority
assoc
prec
original_sigs
ast_data_dict
eval_fun_dict
key_eval_and_data_on_token_value
is_empty
static run(construct, tok, lex, processed_left=None, extra_data=None)[source]

Run the handler associated with the construct. Check the returned parse subtree with process_and_check_node. Return the subtree if it passes type checking.

This is a static method because the arguments will be bound using functools.partial before it is dispatched to be called.

save_eval_fun(eval_fun, type_sig=None, token_value_key=None)[source]

Save data in the eval_fun_dict and ast_data_dict, keyed by the TypeSig instance typesig and also by the arg_types of that typesig.

If type checking is disabled for the parser then type_sig is set to None.

If key_eval_and_data_on_token_value is true for the construct then the value token_value_key is also used in the lookup key.

save_ast_data(ast_data, type_sig=None, token_value_key=None)[source]

Save data in the eval_fun_dict and ast_data_dict, keyed by the TypeSig instance typesig and also by the arg_types of that typesig.

If type checking is disabled for the parser then type_sig is set to None.

If key_eval_and_data_on_token_value is true for the construct then the value token_value_key is also used in the lookup key.

get_eval_fun(orig_sig, token_value_key=None)[source]

Return an evaluation function saved by save_eval_fun. The orig_sig argument is the original signature it was saved under, not the expanded signature.

get_ast_data(orig_sig, token_value_key=None)[source]

Return the ast data saved by save_ast_data. The orig_sig argument is the original signature it was saved under, not the expanded signature.

overload(val_type=None, arg_types=None, eval_fun=None, ast_data=None, token_value_key=None, num_args=None)[source]

Overload the construct to allow a different type signature with a different evaluation function and AST data element. This is the user-level interface.

To overload only on the number of argments when all types are allowed, set num_args to the number. Note that in that case val_type and arg_types are ignored.

unregister_overload(type_sig, token_value_key=None)[source]

Unregister an overload. If the last overload is deleted then the original_sigs attribute will be left empty. The construct will still be in the any ConstructTable that it was part of, though. Called from the unregister_construct of ConstructTable.

If token_value_key is set then only that token value is removed, unless it is the last one in which case the whole signature is removed.

class typped.pratt_constructs.ConstructTable(parser_instance)[source]

Bases: object

A dict holding Construct objects, with related methods. Each PrattParser instance has a ConstructTable instance to hold its constructs.

register_construct(head_or_tail, trigger_token_subclass, handler_fun, precond_fun, precond_priority, construct_label, type_sig, eval_fun, ast_data, token_value_key)[source]

Register a construct (either head or tail) with the subclass for this kind of token, setting the given properties. This method is only ever called from the def_construct method of a PrattParser instance.

The head_or_tail argument must be HEAD or TAIL.

The type_sig argument must be a valid TypeSig instance.

If token_value_key is set then it will be used as part of the key on evaluation functions and AST data. The key_eval_and_data_on_token_value attribute of the Construct instance is set based on this being set, and must always be the same for a given construct.

unregister_construct(construct, type_sig=None, token_value_key=None)[source]

Unregister the previously-registered construct.

If construct_label is not set then all head or tail handlers matching head_or_tail and trigger_token_label are unregistered.

The type_sig argument must be a valid TypeSig instance or else None. If type_sig is None then all overloads are unregistered; otherwise only the particular signature is unregistered.

No error is raised if a matching construct function is not found.

lookup_winning_construct(head_or_tail, trigger_token_instance, lex=None, extra_data=None)[source]

Look up and return the “winning” construct for the given head or tail position, based on the current state.

This method evaluates each preconditions function in the sorted dict for this kind of token and the specified kind of construct (head or tail), returning the construct associated with the first one which evaluates to True. Raises NoHandlerFunctionDefined if no handler function can be found.

This function also sets the attribute construct of this token instance to the label of the winning precondition function.

dispatch_handler(head_or_tail, trigger_token_instance, lex, extra_data, left=None)[source]

Look up and return a wrapper for the “winning” handler function for the token, with its arguments bound. Sets extra_data attribute on trigger_token_instance.