[][src]Crate regex_syntax

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.

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:

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

Modules

ast

Defines an abstract syntax for regular expressions.

hir

Defines a high-level intermediate representation for regular expressions.

utf8

Converts ranges of Unicode scalar values to equivalent ranges of UTF-8 bytes.

Structs

Parser

A convenience parser for regular expressions.

ParserBuilder

A builder for a regular expression parser.

UnicodeWordError

An error that occurs when the Unicode-aware \w class is unavailable.

Enums

Error

This error type encompasses any error that can be returned by this crate.

Functions

escape

Escapes all regular expression meta characters in text.

escape_into

Escapes all meta characters in text and writes the result into buf.

is_meta_character

Returns true if the give character has significance in a regex.

is_word_byte

Returns true if and only if the given character is an ASCII word character.

is_word_character

Returns true if and only if the given character is a Unicode word character.

try_is_word_character

Returns true if and only if the given character is a Unicode word character.

Type Definitions

Result

A type alias for dealing with errors returned by this crate.