Skip to content

Commit 7f78208

Browse files
committed
syntax: polish and doc updates
This updates docs in a number of places, including adding examples. We also make it so zero-width matches never impact the 'utf8' property. In practice, this means '(?-u:\B)' is now considered to match valid UTF-8, which is consistent with the fact that 'a*' is considered to match valid UTF-8 too. We also do a refresh of the 'Look' and 'LookSet' APIs.
1 parent d6a6976 commit 7f78208

File tree

10 files changed

+746
-316
lines changed

10 files changed

+746
-316
lines changed

regex-syntax/README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ concrete syntax that produced the `Hir`.
3030
This example shows how to parse a pattern string into its HIR:
3131

3232
```rust
33-
use regex_syntax::Parser;
34-
use regex_syntax::hir::{self, Hir};
33+
use regex_syntax::{hir::Hir, parse};
3534

36-
let hir = Parser::new().parse("a|b").unwrap();
35+
let hir = parse("a|b").unwrap();
3736
assert_eq!(hir, Hir::alternation(vec![
38-
Hir::literal(hir::Literal::Unicode('a')),
39-
Hir::literal(hir::Literal::Unicode('b')),
37+
Hir::literal("a".as_bytes()),
38+
Hir::literal("b".as_bytes()),
4039
]));
4140
```
4241

regex-syntax/src/error.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use core::{cmp, fmt, result};
2-
31
use alloc::{
42
format,
53
string::{String, ToString},
@@ -9,9 +7,6 @@ use alloc::{
97

108
use crate::{ast, hir};
119

12-
/// A type alias for dealing with errors returned by this crate.
13-
pub type Result<T> = result::Result<T, Error>;
14-
1510
/// This error type encompasses any error that can be returned by this crate.
1611
///
1712
/// This error type is marked as `non_exhaustive`. This means that adding a
@@ -42,8 +37,8 @@ impl From<hir::Error> for Error {
4237
#[cfg(feature = "std")]
4338
impl std::error::Error for Error {}
4439

45-
impl fmt::Display for Error {
46-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40+
impl core::fmt::Display for Error {
41+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4742
match *self {
4843
Error::Parse(ref x) => x.fmt(f),
4944
Error::Translate(ref x) => x.fmt(f),
@@ -91,8 +86,8 @@ impl<'e> From<&'e hir::Error> for Formatter<'e, hir::ErrorKind> {
9186
}
9287
}
9388

94-
impl<'e, E: fmt::Display> fmt::Display for Formatter<'e, E> {
95-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89+
impl<'e, E: core::fmt::Display> core::fmt::Display for Formatter<'e, E> {
90+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
9691
let spans = Spans::from_formatter(self);
9792
if self.pattern.contains('\n') {
9893
let divider = repeat_char('~', 79);
@@ -158,7 +153,7 @@ struct Spans<'p> {
158153

159154
impl<'p> Spans<'p> {
160155
/// Build a sequence of spans from a formatter.
161-
fn from_formatter<'e, E: fmt::Display>(
156+
fn from_formatter<'e, E: core::fmt::Display>(
162157
fmter: &'p Formatter<'e, E>,
163158
) -> Spans<'p> {
164159
let mut line_count = fmter.pattern.lines().count();
@@ -238,7 +233,7 @@ impl<'p> Spans<'p> {
238233
pos += 1;
239234
}
240235
let note_len = span.end.column.saturating_sub(span.start.column);
241-
for _ in 0..cmp::max(1, note_len) {
236+
for _ in 0..core::cmp::max(1, note_len) {
242237
notes.push('^');
243238
pos += 1;
244239
}

regex-syntax/src/hir/literal.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ use crate::hir::{self, Hir};
101101
/// This shows how to extract prefixes:
102102
///
103103
/// ```
104-
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, Parser};
105-
///
106-
/// let hir = Parser::new().parse(r"(a|b|c)(x|y|z)[A-Z]+foo")?;
104+
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, parse};
107105
///
106+
/// let hir = parse(r"(a|b|c)(x|y|z)[A-Z]+foo")?;
108107
/// let got = Extractor::new().extract(&hir);
109108
/// // All literals returned are "inexact" because none of them reach the
110109
/// // match state.
@@ -129,11 +128,10 @@ use crate::hir::{self, Hir};
129128
/// ```
130129
/// use regex_syntax::{
131130
/// hir::literal::{Extractor, ExtractKind, Literal, Seq},
132-
/// Parser,
131+
/// parse,
133132
/// };
134133
///
135-
/// let hir = Parser::new().parse(r"foo|[A-Z]+bar")?;
136-
///
134+
/// let hir = parse(r"foo|[A-Z]+bar")?;
137135
/// let got = Extractor::new().kind(ExtractKind::Suffix).extract(&hir);
138136
/// // Since 'foo' gets to a match state, it is considered exact. But 'bar'
139137
/// // does not because of the '[A-Z]+', and thus is marked inexact.
@@ -237,9 +235,9 @@ impl Extractor {
237235
/// for character classes being turned into literal sequences.
238236
///
239237
/// ```
240-
/// use regex_syntax::{hir::literal::{Extractor, Seq}, Parser};
238+
/// use regex_syntax::{hir::literal::{Extractor, Seq}, parse};
241239
///
242-
/// let hir = Parser::new().parse(r"[0-9]")?;
240+
/// let hir = parse(r"[0-9]")?;
243241
///
244242
/// let got = Extractor::new().extract(&hir);
245243
/// let expected = Seq::new([
@@ -274,9 +272,9 @@ impl Extractor {
274272
/// This shows how to decrease the limit and compares it with the default.
275273
///
276274
/// ```
277-
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, Parser};
275+
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, parse};
278276
///
279-
/// let hir = Parser::new().parse(r"(abc){8}")?;
277+
/// let hir = parse(r"(abc){8}")?;
280278
///
281279
/// let got = Extractor::new().extract(&hir);
282280
/// let expected = Seq::new(["abcabcabcabcabcabcabcabc"]);
@@ -311,9 +309,9 @@ impl Extractor {
311309
/// This shows how to decrease the limit and compares it with the default.
312310
///
313311
/// ```
314-
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, Parser};
312+
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, parse};
315313
///
316-
/// let hir = Parser::new().parse(r"(abc){2}{2}{2}")?;
314+
/// let hir = parse(r"(abc){2}{2}{2}")?;
317315
///
318316
/// let got = Extractor::new().extract(&hir);
319317
/// let expected = Seq::new(["abcabcabcabcabcabcabcabc"]);
@@ -353,9 +351,9 @@ impl Extractor {
353351
/// sequence returned.
354352
///
355353
/// ```
356-
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, Parser};
354+
/// use regex_syntax::{hir::literal::{Extractor, Literal, Seq}, parse};
357355
///
358-
/// let hir = Parser::new().parse(r"[ab]{2}{2}")?;
356+
/// let hir = parse(r"[ab]{2}{2}")?;
359357
///
360358
/// let got = Extractor::new().extract(&hir);
361359
/// let expected = Seq::new([

0 commit comments

Comments
 (0)