Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (58 sloc) 2.69 KB

NAME

PAST::Pattern - A library for pattern matching on PAST::Nodes.

SYNOPSIS

load_bytecode('PAST/Pattern.pbc');
$P1 = get_hll_global ['PAST';'Pattern'], 'Val'
$P0 = $P1.'new'('value'=>1)
$P2 = get_a_past_node_from_somewhere()
$P3 = $P0.match($P2)
$I0 = istrue $P3
print "Result as bool is "
say $I0

DESCRIPTION

PAST::Pattern and its subclasses allow determining whether a PAST::Node matches a certain pattern, in addition to allowing modification of any matches sub-trees similarly to regex substitution.

PAST::Pattern is Capture

Subroutines

patternize(value)

Converts an object that is not a pattern(does not implement the ACCEPTS method) into one that is. If value does the invokable role, it is used to create a PAST::Pattern::Closure pattern. Otherwise, it is used to create a PAST::Pattern::Constant pattern.

Public Methods

transform(past, transform)

Uses the invokable object or PAST::Transformer in transform to transform any sub-trees of past that match the pattern.

ACCEPTS(node, [option1 = value1, option2 => value2, ...])>

Tests whether the node or any sub-tree thereof matches the pattern. Returns a PAST::Pattern::Match result object. Valid options are g or global and p or pos. If g or global are true, the match is global. After the first match is found, the search will continue until every sub-tree has been checked for matching. If no matches are found, a false PAST::Pattern::Match object will be returned. If only one match is found, ACCEPTS returns the same value with or without g or global. If multiple matches are found, the result is a true PAST::Pattern::Match object whose array part contains the individual match results. If p or pos are provided, they specify a PAST::Node that must exactly match the pattern. Supplying one of both g or global and p or pos produces an error.

Subclass Methods

The following method should be implemented in any subclass, as .ACCEPTS relies on it.

ACCEPTSEXACTLY(node)

Tests whether the specific node matches the pattern without recursing into children. The result should be a PAST::Pattern::Match object. Every PAST::Pattern subclass MUST implement this method.

Internal Methods

attr(name, value, has_value)

A helper method for defining attribute accessors.

ACCEPTSGLOBALLY(node)

Used by ACCEPTS to implement global matching.

SEE ALSO

  • docs/pct/past_building_blocks.pod

  • docs/pdds/pdd26_ast.pod