Crate regex_syntax
source · [−]Expand description
This crate provides a robust regular expression parser.
This crate defines two primary types:
Ast
is the abstract syntax of a regular expression. An abstract syntax corresponds to a structured representation of the concrete syntax of a regular expression, where the concrete syntax is the pattern string itself (e.g.,foo(bar)+
). Given some abstract syntax, it can be converted back to the original concrete syntax (modulo some details, like whitespace). To a first approximation, the abstract syntax is complex and difficult to analyze.Hir
is the high-level intermediate representation (“HIR” or “high-level IR” for short) of regular expression. It corresponds to an intermediate state of a regular expression that sits between the abstract syntax and the low level compiled opcodes that are eventually responsible for executing a regular expression search. Given some high-level IR, it is not possible to produce the original concrete syntax (although it is possible to produce an equivalent concrete syntax, but it will likely scarcely resemble the original pattern). To a first approximation, the high-level IR is simple and easy to analyze.
These two types come with conversion routines:
- An
ast::parse::Parser
converts concrete syntax (a&str
) to anAst
. - A
hir::translate::Translator
converts anAst
to aHir
.
As a convenience, the above two conversion routines are combined into one via
the top-level Parser
type. This Parser
will first
convert your pattern to an Ast
and then convert the Ast
to an Hir
.
Example
This example shows how to parse a pattern string into its HIR:
use regex_syntax::Parser;
use regex_syntax::hir::{self, Hir};
let hir = Parser::new().parse("a|b").unwrap();
assert_eq!(hir, Hir::alternation(vec![
Hir::literal(hir::Literal::Unicode('a')),
Hir::literal(hir::Literal::Unicode('b')),
]));
Concrete syntax supported
The concrete syntax is documented as part of the public API of the
regex
crate.
Input safety
A key feature of this library is that it is safe to use with end user facing input. This plays a significant role in the internal implementation. In particular:
- Parsers provide a
nest_limit
option that permits callers to control how deeply nested a regular expression is allowed to be. This makes it possible to do case analysis over anAst
or anHir
using recursion without worrying about stack overflow. - Since relying on a particular stack size is brittle, this crate goes to
great lengths to ensure that all interactions with both the
Ast
and theHir
do not use recursion. Namely, they use constant stack space and heap space proportional to the size of the original pattern string (in bytes). This includes the type’s corresponding destructors. (One exception to this is literal extraction, but this will eventually get fixed.)
Error reporting
The Display
implementations on all Error
types exposed in this library
provide nice human readable errors that are suitable for showing to end users
in a monospace font.
Literal extraction
This crate provides limited support for
literal extraction from Hir
values.
Be warned that literal extraction currently uses recursion, and therefore,
stack size proportional to the size of the Hir
.
The purpose of literal extraction is to speed up searches. That is, if you
know a regular expression must match a prefix or suffix literal, then it is
often quicker to search for instances of that literal, and then confirm or deny
the match using the full regular expression engine. These optimizations are
done automatically in the regex
crate.
Crate features
An important feature provided by this crate is its Unicode support. This
includes things like case folding, boolean properties, general categories,
scripts and Unicode-aware support for the Perl classes \w
, \s
and \d
.
However, a downside of this support is that it requires bundling several
Unicode data tables that are substantial in size.
A fair number of use cases do not require full Unicode support. For this reason, this crate exposes a number of features to control which Unicode data is available.
If a regular expression attempts to use a Unicode feature that is not available
because the corresponding crate feature was disabled, then translating that
regular expression to an Hir
will return an error. (It is still possible
construct an Ast
for such a regular expression, since Unicode data is not
used until translation to an Hir
.) Stated differently, enabling or disabling
any of the features below can only add or subtract from the total set of valid
regular expressions. Enabling or disabling a feature will never modify the
match semantics of a regular expression.
The following features are available:
- unicode - Enables all Unicode features. This feature is enabled by default, and will always cover all Unicode features, even if more are added in the future.
- unicode-age -
Provide the data for the
Unicode
Age
property. This makes it possible to use classes like\p{Age:6.0}
to refer to all codepoints first introduced in Unicode 6.0 - unicode-bool -
Provide the data for numerous Unicode boolean properties. The full list
is not included here, but contains properties like
Alphabetic
,Emoji
,Lowercase
,Math
,Uppercase
andWhite_Space
. - unicode-case - Provide the data for case insensitive matching using Unicode’s “simple loose matches” specification.
- unicode-gencat -
Provide the data for
Uncode general categories.
This includes, but is not limited to,
Decimal_Number
,Letter
,Math_Symbol
,Number
andPunctuation
. - unicode-perl -
Provide the data for supporting the Unicode-aware Perl character classes,
corresponding to
\w
,\s
and\d
. This is also necessary for using Unicode-aware word boundary assertions. Note that if this feature is disabled, the\s
and\d
character classes are still available if theunicode-bool
andunicode-gencat
features are enabled, respectively. - unicode-script -
Provide the data for
Unicode scripts and script extensions.
This includes, but is not limited to,
Arabic
,Cyrillic
,Hebrew
,Latin
andThai
. - unicode-segment -
Provide the data necessary to provide the properties used to implement the
Unicode text segmentation algorithms.
This enables using classes like
\p{gcb=Extend}
,\p{wb=Katakana}
and\p{sb=ATerm}
.
Modules
Defines an abstract syntax for regular expressions.
Defines a high-level intermediate representation for regular expressions.
Converts ranges of Unicode scalar values to equivalent ranges of UTF-8 bytes.
Structs
A convenience parser for regular expressions.
A builder for a regular expression parser.
An error that occurs when the Unicode-aware \w
class is unavailable.
Enums
This error type encompasses any error that can be returned by this crate.
Functions
Escapes all regular expression meta characters in text
.
Escapes all meta characters in text
and writes the result into buf
.
Returns true if the given character has significance in a regex.
Returns true if and only if the given character is an ASCII word character.
Returns true if and only if the given character is a Unicode word character.
Returns true if and only if the given character is a Unicode word character.
Type Definitions
A type alias for dealing with errors returned by this crate.