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()
// m.params[0].value => IntVal(42), m.params[0].raw => "42"
// m.params[1].value => WordVal("basket"), m.params[1].raw => "basket"| Parameter | Description | Example match | Value type |
|---|---|---|---|
| {int} | Integers, optionally negative | 42, -1 | IntVal(Int) |
| {float} | Decimal and scientific notation | 3.14, -1.5e10 | FloatVal(Double) |
| {double} | Same as float | 3.14, 1.5e10 | DoubleVal(Double) |
| {long} | 64-bit integers | 9223372036854775807 | LongVal(Int64) |
| {byte} | Byte-range integers | 127, 255 | ByteVal(Byte) |
| {short} | Short integers | 8080 | ShortVal(Int) |
| {bigdecimal} | Arbitrary-precision decimals | 99.99 | BigDecimalVal(Decimal) |
| {biginteger} | Arbitrary-precision integers | 12345678901234567890 | BigIntegerVal(BigInt) |
| {string} | Single- or double-quoted strings | "hello", 'hi' | StringVal(String) |
| {word} | A single word (no whitespace) | banana | WordVal(String) |
| {} | Anonymous — matches anything | whatever you want | AnonymousVal(String) |
let expr = @cucumber-expressions.Expression::parse!("{word} costs {float} dollars")
let m = expr.match_("coffee costs 4.50 dollars").unwrap()
// m.params[0].value => WordVal("coffee"), m.params[0].raw => "coffee"
// m.params[1].value => FloatVal(4.5), m.params[1].raw => "4.50"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"),
[@cucumber-expressions.RegexPattern("red|green|blue")],
transformer=@cucumber-expressions.Transformer::new(fn(groups) {
@cucumber-expressions.ParamValue::CustomVal(@any.of(groups[0]))
}),
)
let expr = @cucumber-expressions.Expression::parse_with_registry!(
"the {color} ball",
registry,
)
let m = expr.match_("the red ball").unwrap()
// m.params[0].value => CustomVal(<Any>), m.params[0].raw => "red"let expr = @cucumber-expressions.Expression::parse!("{word} is {int}")
match expr.match_("MoonBit is 1") {
Some(m) => {
let name = m.params[0] // { value: WordVal("MoonBit"), type_: Word, raw: "MoonBit" }
let num = m.params[1] // { value: IntVal(1), type_: Int, raw: "1" }
}
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(position~ : Int, message~ : String)
UnmatchedParen(position~ : Int, message~ : String)
CannotEscape(position~ : Int, character~ : Char, message~ : String)
UnexpectedEscapeEnd(position~ : Int, message~ : String)
ValidationError(position~ : Int, message~ : String)
UnknownParameterType(name~ : String, message~ : String)
} derive(Debug)impl Show for ExpressionError#deprecated("`ExpressionError::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ExpressionError::to_repr(ExpressionError) -> Reprpub(all) struct Expression {
// private fields
}fn Expression::parse_with_registry(expression : String, registry : ParamTypeRegistry) -> Expression raise ExpressionErrorpub(all) struct ParamTypeEntry {
name : String
type_ : ParamType
patterns : Array[RegexPattern]
transformer : Transformer
} derive(Debug)impl Eq for ParamTypeEntry#deprecated("`ParamTypeEntry::equal` is deprecated, use `Eq::equal` instead.")
fn ParamTypeEntry::equal(self : ParamTypeEntry, other : ParamTypeEntry) -> Bool#deprecated("`ParamTypeEntry::not_equal` is deprecated, use `Eq::not_equal` instead.")
fn ParamTypeEntry::not_equal(x : ParamTypeEntry, y : ParamTypeEntry) -> Bool#deprecated("`ParamTypeEntry::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ParamTypeEntry::to_repr(ParamTypeEntry) -> Reprpub(all) struct ParamTypeRegistry {
// private fields
}fn ParamTypeRegistry::register(self : ParamTypeRegistry, name : String, type_ : ParamType, patterns : Array[RegexPattern], transformer? : Transformer) -> Unitimpl Eq for ParamValueimpl Show for ParamValueimpl Debug for ParamValue#deprecated("`ParamValue::equal` is deprecated, use `Eq::equal` instead.")
fn ParamValue::equal(self : ParamValue, other : ParamValue) -> Bool#deprecated("`ParamValue::not_equal` is deprecated, use `Eq::not_equal` instead.")
fn ParamValue::not_equal(x : ParamValue, y : ParamValue) -> Bool#deprecated("`ParamValue::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ParamValue::to_repr(self : ParamValue) -> Repr#deprecated("`RegexPattern::equal` is deprecated, use `Eq::equal` instead.")
fn RegexPattern::equal(RegexPattern, RegexPattern) -> Bool#deprecated("`RegexPattern::not_equal` is deprecated, use `Eq::not_equal` instead.")
fn RegexPattern::not_equal(x : RegexPattern, y : RegexPattern) -> Bool#deprecated("`RegexPattern::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn RegexPattern::to_repr(RegexPattern) -> Reprpub(all) struct Transformer {
// private fields
}impl Debug for Transformer#deprecated("`Transformer::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn Transformer::to_repr(_self : Transformer) -> Reprfn compile_expression(expression : String, registry? : ParamTypeRegistry) -> String raise ExpressionErrorfn version() -> StringInstall
Download zipCucumber Expressions parser and matcher for MoonBit