Cucumber Expressions parser and matcher for MoonBit
Dependencies
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(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
Double_
Long
Byte
Short
BigDecimal
BigInteger
Custom(String)
}pub(all) struct ParamTypeEntry {
name : String
type_ : ParamType
patterns : Array[RegexPattern]
transformer : Transformer
}pub(all) struct ParamTypeRegistry {
// private fields
}fn ParamTypeRegistry::register(self : ParamTypeRegistry, name : String, type_ : ParamType, patterns : Array[RegexPattern], transformer? : Transformer) -> Unitimpl Eq for RegexPatternimpl Show for RegexPatternpub(all) enum Token {
Text(String)
BeginParameter
EndParameter
BeginOptional
EndOptional
Alternation
WhiteSpace(String)
}pub(all) struct Transformer {
// private fields
}fn compile_expression(expression : String, registry? : ParamTypeRegistry) -> String raise ExpressionErrorfn version() -> StringInstall
Download zipCucumber Expressions parser and matcher for MoonBit
Dependencies