ete 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 an [`Ast`](ast::Ast). * A [`hir::translate::Translator`] converts an [`Ast`](ast::Ast) to a [`Hir`](hir::Hir). 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`. It's also exposed as top-level [`parse`] free function. # Example This example shows how to parse a pattern string into its HIR: ``` use regex_syntax::{hir::Hir, parse}; let hir = parse("a|b")?; assert_eq!(hir, Hir::alternation(vec![ Hir::literal("a".as_bytes()), Hir::literal("b".as_bytes()), ])); # Ok::<(), Box>(()) ``` # Concrete syntax supported The concrete syntax is documented as part of the public API of the [`regex` crate](https://docs.rs/regex/%2A/regex/#syntax). # 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: 1. 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 an `Ast` or an `Hir` using recursion without worrying about stack overflow. 2. 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 the `Hir` 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](hir::literal). Be warned that literal extraction 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: * **std** - Enables support for the standard library. This feature is enabled by default. When disabled, only `core` and `alloc` are used. Otherwise, enabling `std` generally just enables `std::error::Error` trait impls for the various error types. * **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](https://www.unicode.org/reports/tr44/tr44-24.html#Character_Age). 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` and `White_Space`. * **unicode-case** - Provide the data for case insensitive matching using [Unicode's "simple loose matches" specification](https://www.unicode.org/reports/tr18/#Simple_Loose_Matches). * **unicode-gencat** - Provide the data for [Unicode general categories](https://www.unicode.org/reports/tr44/tr44-24.html#General_Category_Values). This includes, but is not limited to, `Decimal_Number`, `Letter`, `Math_Symbol`, `Number` and `Punctuation`. * **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 the `unicode-bool` and `unicode-gencat` features are enabled, respectively. * **unicode-script** - Provide the data for [Unicode scripts and script extensions](https://www.unicode.org/reports/tr24/). This includes, but is not limited to, `Arabic`, `Cyrillic`, `Hebrew`, `Latin` and `Thai`. * **unicode-segment** - Provide the data necessary to provide the properties used to implement the [Unicode text segmentation algorithms](https://www.unicode.org/reports/tr29/). This enables using classes like `\p{gcb=Extend}`, `\p{wb=Katakana}` and `\p{sb=ATerm}`. * **arbitrary** - Enabling this feature introduces a public dependency on the [`arbitrary`](https://crates.io/crates/arbitrary) crate. Namely, it implements the `Arbitrary` trait from that crate for the [`Ast`](crate::ast::Ast) type. This feature is disabled by default.