8. Patterns¶
Syntax
Pattern
::=PatternWithoutAlternation
(|PatternWithoutAlternation
)*PatternWithoutAlternation
::=SingularPattern
|SpreadPattern
SingularPattern
::=BindPattern
|IgnorePattern
|LiteralPattern
|TuplePattern
|AccessPattern
|ArrayPattern
|ParenthesisedPattern
|ConstructorPattern
|RangePattern
|GuardedPattern
|ModulePattern
|TokenMacroInvocation
|PatternMacroInvocation
8.1. Bind Patterns¶
syntax
BindPattern
::=Visibility
? mut?Name
Examples
pub mut foo := 1;
mut foo
foo
8.2. Ignore Patterns¶
syntax
IgnorePattern
::=
_
Examples
match t {
Some(_) => {}
None => {}
}
8.3. Literal Patterns¶
syntax
LiteralPattern
::=BooleanLiteral
|CharacterLiteral
|StringLiteral
|NumericLiteral
Examples
false := false
'a' := 'a'
"foo" := "foo"
1 := 1
8.4. Tuple Pattern¶
syntax
TuplePattern
::= (PatternArgumentList
? )PatternArgumentList
::=MacroInvocationHeader
PatternArgument
(,PatternArgument
)* ,?PatternArgument
::=Name
(=SingularPattern
)? |SpreadPattern
Examples
match (a, b) {
(1, 2) => {
print("basic")
}
(2..<10, y) => {
print("y is {y}")
}
_ => {
print("unclear")
}
}
8.5. Access Patterns¶
syntax
AccessPattern
::=SingularPattern
::Name
Examples
Direction := enum(
North,
South,
East,
West
)
compute_index := (direction: Direction, other: Direction) => {
match (direction, other) {
(Direction::South, Direction::North) => 0,
(Direction::North, Direction::South) => 1,
(Direction::East, Direction::West) => 2,
(Direction::West, Direction::East) => 3,
_ => panic("invalid direction")
}
}
8.6. Array Patterns¶
syntax
ArrayPattern
::= [PatternList
? ]PatternList
::=Pattern
(,Pattern
)* ,?
Examples
[1, 2, 3]
[1, 2, 3, ...rest]
[1, 2, ...middle, 9, 10]
8.7. Parenthesised Patterns¶
syntax
ParenthesisedPattern
::= (Pattern
)
Examples
(1 | 2 | 3 | 4)
8.8. Constructor Patterns¶
syntax
ConstructorPattern
::=SingularPattern
(PatternArgumentList
? )
Examples
Dog(breed = dog_breed, name = dog_name) := Dog(
name = "Bob",
breed = "Husky"
)
viktor := Dog(name = "Viktor", breed = "Husky")
match viktor {
Dog(breed = "Husky", ...) => {
print("viktor is a husky")
}
_ => {
print("viktor is not a husky")
}
}
8.9. Range Patterns¶
syntax
RangePattern
::=InclusiveRangePattern
|ExclusiveRangePattern
InclusiveRangePattern
::=RangePatternBound
? ..RangePatternBound
?ExclusiveRangePattern
::=RangePatternBound
? ..<RangePatternBound
?RangePatternBound
::=CharacterLiteral
|NumericLiteral
Examples
main := (k: i32) => {
2..6 := k
'c'..'z' := cast<_, char>(k)
match k {
-3..56 => print("it's negative three!"),
57..<59 => print("between 57 and 59"),
_ => print("not negative three"),
};
}
8.10. Guarded Patterns¶
Warning
This is a work in progress.
syntax
GuardedPattern
::=SingularPattern
ifExpression
Examples
t := 4
k := 1
// Parsed as `Or(1, 4) if k`
match t {
(1 | 4) if k == 1 => {},
_ => {}
}
// Parsed as `Or(1, 4 if k )`
match t {
1 | 4 if k == 1 => {},
_ => {}
}
8.11. Module Patterns¶
Warning
This is a work in progress.
syntax
ModulePattern
::= {ModulePatternList
? }ModulePatternList
::=ModulePatternArgument
(,ModulePatternArgument
)* ,?ModulePatternArgument
::=Name
(asPattern
)?
Examples
{
Colour as MyColour,
Shape as MyShape,
} := import("drawing")
8.12. Spread Patterns¶
syntax
SpreadPattern
::= ...BindPattern
?
Examples
(first, ...) := (1, 2, 3, 4, 5)
Dog(name, ...rest) := Dog(name = "Bob", breed = "Husky")
[a, b, c, ...] := [1, 2, 3, 4, 5]
8.13. Macro Invocations as Patterns¶
syntax
PatternMacroInvocation
::=MacroInvocationHeader
SingularPattern
Examples