rust-bakery / nom

Rust parser combinator framework
MIT License
9.18k stars 792 forks source link

How to refer to a previously parsed string? #1687

Open ckp95 opened 10 months ago

ckp95 commented 10 months ago

Conceptual question, I wasn't able to figure this out from reading the docs. Let's say I have some XML-like language where you can have tags with arbitrary alphanumeric text in them, and they have to be symmetric, like this:

some text <foo>some more text</foo> even more text!

The possible tags aren't hardcoded ahead of time, they can be anything so long as the tags balance. So this is invalid:

some text <foo>some more text</bar> even more text!

The parser for the second tag would have to look for the exact string matched by the parser for the first tag (kinda like regex capture groups), but I don't know how to express that in this library.

Xiretza commented 10 months ago

Your parser would match an arbitrary start tag (which gives it the tag name), followed by the contents (some more text) followed by an end tag with the same name as the start tag. Since it's all one parser, it knows the start tag.