Skip to content

Documentation for hir::Repetition is misleading #731

Closed
@nwalfield

Description

@nwalfield

Consider the following code:

use regex_syntax::hir;

fn main() {
    // Is this: xab+y or x(ab)+y?
    let xabplusy = hir::Hir::concat(vec![
        hir::Hir::literal(hir::Literal::Unicode('x')),
        hir::Hir::repetition(hir::Repetition {
            kind: hir::RepetitionKind::OneOrMore,
            greedy: true,
            hir: Box::new(
                hir::Hir::concat(vec![
                    hir::Hir::literal(hir::Literal::Unicode('a')),
                    hir::Hir::literal(hir::Literal::Unicode('b')),
                ])
            )
        }),
        hir::Hir::literal(hir::Literal::Unicode('x'))
    ]);

    let regex = xabplusy.to_string();
    eprintln!("{}", regex);
}

Running the above code yields: xab+y

The documentation for Repetition says:

hir: Box<Hir>

The expression being repeated.

This leads me to believe that the whole Hir will be repeated (in this case ab). But, at least in the case of a Hir::concat, it appears that only the last Hir in the vector (b) is repeated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions