Cucumber Expressions parser and matcher for MoonBit
moon add moonrockz/cucumber-expressionslet expr = @cucumber-expressions.Expression::parse!("I have {int} cucumber(s) in my {word}")
let m = expr.match_("I have 42 cucumbers in my basket").unwrap()
let count = m.params[0].value // "42"
let place = m.params[1].value // "basket"| Parameter | Description | Example match |
|---|---|---|
| {int} | Integers, optionally negative | 42, -1 |
| {float} | Decimal and scientific notation | 3.14, -1.5e10 |
| {string} | Single- or double-quoted strings | "hello", 'hi' |
| {word} | A single word (no whitespace) | banana |
| {} | Anonymous — matches anything | whatever you want |
let expr = @cucumber-expressions.Expression::parse!("{word} costs {float} dollars")
let m = expr.match_("coffee costs 4.50 dollars").unwrap()
// m.params[0] => { value: "coffee", type_: Word }
// m.params[1] => { value: "4.50", type_: Float }let expr = @cucumber-expressions.Expression::parse!("I have {int} cucumber(s)")
expr.match_("I have 1 cucumber") // matches
expr.match_("I have 5 cucumbers") // matcheslet expr = @cucumber-expressions.Expression::parse!("I have a cat/dog")
expr.match_("I have a cat") // matches
expr.match_("I have a dog") // matcheslet registry = @cucumber-expressions.ParamTypeRegistry::default()
registry.register(
"color",
@cucumber-expressions.ParamType::Custom("color"),
["red", "green", "blue"],
)
let expr = @cucumber-expressions.Expression::parse_with_registry!(
"the {color} ball",
registry,
)
let m = expr.match_("the red ball").unwrap()
// m.params[0] => { value: "red", type_: Custom("color") }let expr = @cucumber-expressions.Expression::parse!("{word} is {int}")
match expr.match_("MoonBit is 1") {
Some(m) => {
let name = m.params[0] // { value: "MoonBit", type_: Word }
let num = m.params[1] // { value: "1", type_: Int }
}
None => println("no match")
}| Variant | Cause |
|---|---|
| UnmatchedBrace | Missing closing } |
| UnmatchedParen | Missing closing ) |
| CannotEscape | Invalid escape sequence |
| UnexpectedEscapeEnd | Backslash at end of expression |
| ValidationError | Structural errors (empty alternation, nested optionals, etc.) |
| UnknownParameterType | Unregistered {name} in expression |
try {
let _ = @cucumber-expressions.Expression::parse!("{unknown}")
} catch {
@cucumber-expressions.ExpressionError::UnknownParameterType(name=name, ..) =>
println("Unknown parameter: " + name)
}pub suberror ExpressionError {
UnmatchedBrace(Int, String)
UnmatchedParen(Int, String)
CannotEscape(Int, Char, String)
UnexpectedEscapeEnd(Int, String)
ValidationError(Int, String)
UnknownParameterType(String, String)
}impl Show for ExpressionErrorpub(all) struct Expression {
// private fields
}fn Expression::parse_with_registry(expression : String, registry : ParamTypeRegistry) -> Expression raise ExpressionErrorpub(all) enum ParamType {
Int
Float
String_
Word
Anonymous
Custom(String)
}fn ParamTypeRegistry::register(self : ParamTypeRegistry, name : String, type_ : ParamType, patterns : Array[String]) -> Unitpub(all) enum Token {
Text(String)
BeginParameter
EndParameter
BeginOptional
EndOptional
Alternation
WhiteSpace(String)
}fn compile_expression(expression : String, registry? : ParamTypeRegistry) -> String raise ExpressionErrorfn version() -> StringInstall
Download zipCucumber Expressions parser and matcher for MoonBit