3 ms·
Yeah I am slowly working on it. I think even custom alphabets wouldn't work for me. I'm trying to build something so this works: let is_even = |x: &i32
by IshKebab 3y ago
Yeah I am slowly working on it. I think even custom alphabets wouldn't work for me. I'm trying to build something so this works:
let is_even = |x: &i32| *x % 2 == 0;
let is_odd = |x: &i32| *x % 2 == 0;
let pattern = sequence(&[
is_even,
is_odd,
repeat(or(is_even, is_odd), 2, 4),
]);
let mut matcher = Matcher::compile(pattern);
let elements = [4, 1, 23, 5, 6];
for element in elements {
matcher.step(element);
}
assert!(matcher.matches());
I'll get there one day.
Amazing work on the Regex crate by the way!
- ynik 3y agoIt's not that hard to build a backtracking matching engine, at least if performance is not important. I did that in the past for the C# syntax tree used in a decompiler. Basically the syntax tree has a bunch of additional (non-syntax) pattern nodes, that support flexible regex-style matching of a pattern-tree against a concrete syntax tree. Example pattern: https://github.com/icsharpcode/ILSpy/blob/1100d64e4bbd878164e3cbf17d8b741ad11bdb44/ICSharpCode.Decompiler/CSharp/Transforms/CombineQueryExpressions.cs#L92 https://github.com/icsharpcode/ILSpy/blob/1100d64e4bbd878164... Implementation: https://github.com/icsharpcode/ILSpy/tree/1100d64e4bbd878164e3cbf17d8b741ad11bdb44/ICSharpCode.Decompiler/CSharp/Syntax/PatternMatching https://github.com/icsharpcode/ILSpy/tree/1100d64e4bbd878164...