Language::AttributeGrammar::Engine - Attribute grammar combinators


  • new

    Create a new engine. No initialization is needed.

  • $engine->add_case($case_name)

    Make sure a visitor is installed for class $case_name. Usually this is not necessary. You need this only when you want a visitor installed for a class that has no attributes defined.

  • $engine->add_visitor($case, $visitor)

    Add an action to perform when the an object of class $case is visited.

        $engine->add_visitor(Foo => sub { ... });
  • $engine->make_visitor($method_name)

    Install a visitor named $method_name in all the defined cases. This actually modifies the packages, so it's probably a good idea to choose a non-conflicting method name like 'MODULENAME_visit0001'.

  • $engine->annotate($method_name, $tree, $top_attrs)

    Run the visitors on $tree, after having installed a visitor using make_visitor in the method name $method_name. Set attributes $top_attrs (a hash) on the top node of the tree. Returns a structure where you can query any attribute of any visited node using:

        my $attrs = $engine($method_name, $tree, {});
        my $attr_value = $attrs->get($node)->get('attr')->get;

    Using the annotated tree directly uses a bunch of memory, since it has to hold every attribute pair. If you are only interested in one attribute of the top node, use:

  • $engine->evaluate($method_name, $tree, $attr, $top_attrs)

    Does the same as annotate, but returns the value of $attr on the root node of the tree all in one pass. Doing this in one pass allows the engine to clean up intermediate values when they are not needed anymore. This is the preferred form of usage.