EBNF Syntax:
() = GROUP
[] = OPTIONAL
{}* = ZERO OR MORE
{}+ = ONE OR MORE
External rules: EXPR, STMT, BLOCK, IDENT, NUMBER, BOOLEAN_EXPR
MATCH = "match" EXPR "{" MATCH_ARM { "," MATCH_ARM }* [ "," ] "}"
MATCH_ARM = "case" MATCH_ARM_LEFT "=>" MATCH_ARM_RIGHT
MATCH_ARM_LEFT = EXPR ({ "|" EXPR }+ | MATCH_ARM_LEFT_WHEN)
| NUMBER ( ".." | "..." ) NUMBER
| "var" IDENT MATCH_ARM_LEFT_WHEN
| "_"
MATCH_ARM_LEFT_WHEN = "when" BOOLEAN_EXPR
MATCH_ARM_RIGHT = EXPR|STMT|BLOCK
The match construct is an EXPRESSION.
Therefore, it MUST return a value (or null by default).
The match expression MUST contain at least one MATCH_ARM.
The match arms MUST be exhaustive.
Cases which are not covered MUST produce a compile-time exception.
All match arms MUST be followed by a COMMA (U+002C),
EXCEPT for the last match arm, where the COMMA is OPTIONAL.
// A valid match expression.
// Exhaustive through the use of an ANY ('_') match arm.
match some_number {
case 0 => print ("Zero!"),
case 2 | 3 | 5 | 7 | 11 => print ("A prime!"),
case var x when x in [2, 3, 5, 7, 11] => print ("#{x} is a prime!"),
case 13 ... 19 when animal => print ("A really old animal!"),
case 13 ... 19 => print ("A teen!"),
case var x when x > 1000 => print ("A big number!"),
case _ => print ("Whoops"),
}
// A valid match expression on boolean values.
// Exhaustive by covering all possible values.
match some_boolean {
true => print ("Yes!"),
false => print ("No!"),
}
// Another valid match expression on boolean values.
// Exhaustive through the use of an ANY ('_') match arm.
match some_boolean {
true => print ("Yes!"),
_ => print ("No!"),
}
// An invalid match expression (non-exhaustive)
match some_number {
case 0 => print ("Zero!"),
case 1 => print ("One!),
} // This produces a compile-time error!