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:
objectA 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_tailortrigger_token_label) are handled in theConstructTableclass or thedef_constructmethod ofPrattParser.-
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.partialbefore 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_dictandast_data_dict, keyed by theTypeSiginstancetypesigand also by thearg_typesof that typesig.If type checking is disabled for the parser then
type_sigis set toNone.If
key_eval_and_data_on_token_valueis true for the construct then the valuetoken_value_keyis 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_dictandast_data_dict, keyed by theTypeSiginstancetypesigand also by thearg_typesof that typesig.If type checking is disabled for the parser then
type_sigis set toNone.If
key_eval_and_data_on_token_valueis true for the construct then the valuetoken_value_keyis 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. Theorig_sigargument 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. Theorig_sigargument 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_argsto the number. Note that in that caseval_typeandarg_typesare ignored.
-
unregister_overload(type_sig, token_value_key=None)[source]¶ Unregister an overload. If the last overload is deleted then the
original_sigsattribute will be left empty. The construct will still be in the anyConstructTablethat it was part of, though. Called from theunregister_constructofConstructTable.If
token_value_keyis 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:
objectA dict holding
Constructobjects, with related methods. EachPrattParserinstance has aConstructTableinstance 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_constructmethod of aPrattParserinstance.The
head_or_tailargument must beHEADorTAIL.The
type_sigargument must be a validTypeSiginstance.If
token_value_keyis set then it will be used as part of the key on evaluation functions and AST data. Thekey_eval_and_data_on_token_valueattribute of theConstructinstance 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_labelis not set then all head or tail handlers matchinghead_or_tailandtrigger_token_labelare unregistered.The
type_sigargument must be a validTypeSiginstance or elseNone. Iftype_sigisNonethen 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. RaisesNoHandlerFunctionDefinedif no handler function can be found.This function also sets the attribute
constructof this token instance to the label of the winning precondition function.
-