design principle
Patterns are (mostly) the inverse operation of their construction syntax
The syntax of pattern matching in Moshi.jl is highly based on MLStyle.jl, which is the predecessor of Moshi.jl.
design principle
Patterns are (mostly) the inverse operation of their construction syntax
All the pattern matching statements in Moshi.jl are wrapped in @match
macro.
While patterns in Moshi are highly extensible, we also provide a set of builtin patterns. In this section, we will go through these builtin patterns.
The wildcard pattern matches any value. It is denoted by _
.
The variable pattern matches any value and binds it to a variable. It is denoted by a variable name.
The type annotation pattern matches a value of a specific type. It is denoted by ::Type
.
The quote pattern is a pattern that matches a quoted value. It is the simplest pattern in Moshi. Literal values are automatically quoted in Moshi.
For none literal values, you can use the quote syntax in Julia like all other Julia program:
The quote pattern is the most fundamental pattern in Moshi. We will see it more often in the following sections.
The call pattern is the deconstruction pattern for constructors. It is defined as the same syntax as a constructor call in Julia. For example
The call pattern also works on ADTs. It follows the same syntax as the constructor call of a variant:
The tuple pattern is a pattern that matches a tuple. It is defined as the same syntax as a tuple in Julia. The elements of the tuple are patterns.
The vector pattern is a pattern that matches a vector. It is defined as the same syntax as a vector in Julia. The elements of the vector are patterns.
The vector pattern can be typed just like how you construct a vector in Julia:
this will match the vector only when the vector is of type Vector{Int}
.
The splatting pattern is a pattern that matches a collection and binds the rest of the collection to a variable.
The actual binding value depends on the context of splatting. For example, in a tuple pattern, the splatting pattern
binds the rest of the tuple as a Tuple
value:
In a vector pattern, the splatting pattern binds the rest of the vector as a view type SubArray
of
the input value:
The or pattern is a pattern that matches either of the two patterns. It is defined as the ||
operator in Julia.
Similar to the or pattern, the and pattern is a pattern that matches both of the two patterns. It is defined as the &&
operator in Julia.
The guard pattern is a pattern that matches a value when the guard expression is true. It is defined as the if
keyword in Julia.
You can match Julia expressions in the same way as how you would create them. Consider the following example:
When matching expression blocks, the LineNumberNode
is treated as a wildcard _
as syntax sugar.
For example:
However, in some cases, one may want to match the LineNumberNode
. In such cases, we can try to match
a pattern containing a sub-pattern of the type annotation pattern <pattern>::LineNumberNode
or the call pattern
LineNumberNode(line, file)
:
thse patterns are also composible, for example you can compose with a guard:
this pattern above now matches only if the line number is greater than 3.
The pattern matching in Moshi
is backed by a pattern language describing the structure of the data. In this section
we will discuss the formal definitions of the pattern language. The pattern language is defined as the following BNF: