Skip to main content

Node pattern

rascal-0.34.0

Synopsis

Node in abstract pattern.

Syntax

Pat ₁ ( Pat₂, ..., Patₙ)

Name (Pat₂, ..., Patₘ)

Pat ₁ ( Pat₁₂, ..., Pat₁ₙ, KeywordLabel₁ = Pat₂₂, ..., KeywordLabelₙ = Pat₂ₙ)

Name ( Pat₁₂, ..., Pat₁ₙ, KeywordLabel₁ = Pat₂₂, ..., KeywordLabelₙ = Pat₂ₙ)

Description

A node pattern matches a Node value or a Constructor value, provided that Name matches with the constructor symbol of that value and Pat₂, Pat₂, ..., Patₙ match the children of that value in order. Any variables bound by nested patterns are available from left to right.

If Name identifies a Constructor of an Algebraic Data Type then not only the name must match but also the arity and the declared types of the children of the constructor and its keyword parameters.

The label of a node can also be a Pattern itself (Pat₁), in which case it must be of type str

Nodes may have keyword fields which can be matched via the literal KeywordLabel and a respective pattern for each field.

Note that during matching the keyword fields which are not listed in the pattern are ignored. This means that their presence or absence in the subject does not influence the match. When a keyword field is mentioned in the match pattern, then the match will fail or succeeed if the respective pattern fails or succeeds.

Examples


Match on node values (recall that the function symbol of a node has to be quoted, see [Values/Node]):

rascal>import IO;
ok
rascal>if("f"(A,13,B) := "f"("abc", 13, false))
>>>>>>> println("A = <A>, B = <B>");
A = abc, B = false
ok

Define a data type and use it to match:

rascal>data Color = red(int N) | black(int N);
ok
rascal>if(red(K) := red(13))
>>>>>>> println("K = <K>");
K = 13
ok