From 907ccf98663d2553a60923ef0c026738f1544fb1 Mon Sep 17 00:00:00 2001 From: MegaIng Date: Wed, 29 Apr 2020 11:19:07 +0200 Subject: [PATCH] Added documentation for `__default__` --- docs/visitors.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/visitors.md b/docs/visitors.md index c60c1dc..0238e05 100644 --- a/docs/visitors.md +++ b/docs/visitors.md @@ -12,7 +12,7 @@ Visitors visit each node of the tree, and run the appropriate method on it accor They work bottom-up, starting with the leaves and ending at the root of the tree. -**Example** +**Example:** ```python class IncreaseAllNumbers(Visitor): def number(self, tree): @@ -67,7 +67,7 @@ All these classes implement the transformer interface: By default, transformers only visit rules. `visit_tokens=True` will tell Transformer to visit tokens as well. This is a slightly slower alternative to `lexer_callbacks`, but it's easier to maintain and works for all algorithms (even when there isn't a lexer). -Example: +**Example:** ```python class T(Transformer): @@ -95,7 +95,7 @@ When used on a transformer/visitor class definition, it applies to all the callb - `meta` - Provides two arguments: `children` and `meta` (instead of just the first) - `tree` - Provides the entire tree as the argument, instead of the children. -Examples: +**Examples:** ```python @v_args(inline=True) @@ -105,11 +105,19 @@ class SolveArith(Transformer): class ReverseNotation(Transformer_InPlace): - @v_args(tree=True): + @v_args(tree=True) def tree_node(self, tree): tree.children = tree.children[::-1] ``` +### `__default__` and `__default_token__` +These are the functions that are called on if a function with a corresponding name has not been found. + +- The `__default__` method has the signature `(data, children, meta)`, with `data` being the data attribute of the node. It defaults to reconstruct the Tree + +- The `__default_token__` just takes the `Token` as an argument. It defaults to just return the argument. + + ### Discard When raising the `Discard` exception in a transformer callback, that node is discarded and won't appear in the parent.