pidgin
build non-recursive grammars
Pidgin is a Rust library for generating non-recursive grammars which allow one to convert strings to parse trees.
Pidgin's grammars are implemented underlyingly as regular expressions with named matching groups. Rust's regular expression engine is very fast but it has a limitation which makes parsing hierarchically structured patterns with repeating components difficult: you cannot use a particular match name more than once in a pattern. This makes a simple grammar such as
foo -> bar baz | baz bar
bar -> '1'
baz -> '2'
problematic. The foo
rule uses both bar
and baz
twice.
Pidgin allows you to work around this restriction by managing group renaming.
Another disadvantage of regular expressions is that the more expressive they are the more difficult they are to read. Pidgin allows you to construct expressive regular expressions without obscuring your intention.
Example
extern crate pidgin;
Limitations
Recursion
Because you must define sub-grammars before you use them, and because Rust's regular expression engine and formalism cannot represent it in any case, you cannot produce a recursive grammar in Pidgin. Something like this is impossible:
XP -> XP conj XP
Named Captures in Regex Elements
Pidgin allows you to provide regular expression elements to rules. Such an element
is a Rust expression which may be converted to a String
via to_string
bracketed
in the grammar!
macro body by r(
and )
, so, for example r(r"\d+")
. Regex
elements allow you to insert regex expressions into the grammar that the grammar formalism
doesn't provide itself, like anchors and character classes.
Rust's regular expression engine does not allow the reuse of names in named capturing
groups, however. (?<foo>bar) baz (?<foo>qux)
won't compile into a usable regex because
the name "foo" is reused. The following grammar produces just this situation:
grammar!;
The solution is to avoid named capturing groups in grammar regex elements. The grammar itself will handle the named capturing, so you should never need to do this.
Benchmarks
The principle motivation behind Pidgin is simply to produce something like an
abstract syntax tree one can use to better understand matched text, but the
regular expressions its [vec]
elements generate generally match as well as or better than
naively constructed regular expressions represented as alternations of
expressions.
// good
let rx = new.unwrap;
// better!
let rx = grammar!.rx.unwrap;
There is a simple benchmark suite included in the benches/
directory demonstrating this. The suite compares matching
speed for "naive" regular expressions of the form foo|bar|baz
to those
generated by Pidgin, which involve no backtracking and, in this case, would
be something like foo|ba[rz]
. The benchmark suite compares non-matching to
matching and bounded to unbounded patterns, where a bounded pattern must match
from the first character to the last and an unbounded pattern must match
somewhere in the string searched. The mean match/non-match times for these 8
cases were
pidgin | match | bounded | time |
---|---|---|---|
✓ | ✓ | 4.8857 ms | |
✓ | 26.189 ms | ||
✓ | 15.930 ms | ||
156.43 ms | |||
✓ | ✓ | ✓ | 2.2982 ms |
✓ | ✓ | 3.9700 ms | |
✓ | ✓ | 443.04 µs | |
✓ | 1.0646 ms |
The better time is bolded for each pair. The Pidgin regex outperforms the naive regex in every case.
The full report can be generated by running
Full API
The complete Pidgin API is available at https://docs.rs/pidgin/.
Acknowledgments
In writing this I received considerable advice from TurkeyMcMac.