regex_syntax - Rust (original) (raw)

logo

Expand description

This crate provides a robust regular expression parser.

This crate defines two primary types:

These two types come with conversion routines:

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-levelparse 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()),
]));

Concrete syntax supported

The concrete syntax is documented as part of the public API of theregex 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:

  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 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.

This crate provides limited support for literal extraction from Hirvalues. 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: