macro_rules! parse {
($regex:expr) => { ... };
(options: $options:expr, $regex:expr) => { ... };
}Expand description
Produces spanned syntax nodes for the provided regex. The regex must be a constant expression (but not necessarily a string literal).
This is a preferred way to define syntax nodes in compile time (as opposed to using RegexOptions::parse())
because the latter can lead to unused Syntax capacity added to the data section
of the built executable. This padding is inaccessible, but the Rust compiler isn’t smart enough to realize this.
This macro computes the exact necessary capacity to store syntax nodes.
§Examples
use compile_regex::{ast, parse};
const SYNTAX: &[ast::Spanned] = parse!(r"^\s*\d{3,5}?");
assert_eq!(SYNTAX.len(), 5);
assert_matches!(SYNTAX[0].node, ast::Node::LineAssertion); // ^
assert_matches!(SYNTAX[4].node, ast::Node::CountedRepetition(_)); // {3,5}?§Use with RegexOptions
The macro optionally accepts parsing options.
use compile_regex::{ast, parse, RegexOptions};
const SYNTAX: &[ast::Spanned] = parse!(
options: RegexOptions::DEFAULT.ignore_whitespace(true),
r"(?<digits> # This is a comment :) so the closing brace should be ignored
[0- 9]+ # without ignoring whitespace, this range would be invalid
)"
);
assert!(SYNTAX
.iter()
.any(|spanned| matches!(spanned.node, ast::Node::SetRange)));