-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to capture the delimiters in a delimited repeat. #259
Comments
You could do something like: rule list<I, S>(item: rule<I>, sep: rule<S>) -> (Option<I>, Vec<(S, I)>)
= first:item() items:(s:sep() i:item() { (s, i) })* { (Some(first), items) }
/ { (None, vec![]) }
rule use_it() = list(<expr()>, <comma()>) which is kind of like what I would be interested to hear your experience and pain points in using this library for a lossless parser. Are you producing a typed or untyped syntax tree? |
It looks like the use of
I've only just started working on converting the existing hand build parser to peg, so I don't know what pain points I'll run into. This is the first major one. A couple of minor points:
[1] I'm adapting an existing split lexer + parser, that clusters trivia like whitespace/comments with the adjacent tokens before parsing, so I'm parsing these token clusters (which also include position information, but only care about the root token for determining the parser.
still feels a little bit awkward.
I'm not sure what you mean by this? |
I just discovered that I can't implement |
I also have a use case where I'd like to collect the delimiters. So adding syntactic sugar may be useful. Also, it should probably return the separator that follows an item rather than the separator that precedes it (at least for my use case). I'm currently successfully using the pub rule cmdline() -> Result<CommandLine, &'input str>
= delimited_cmdline: list(<pipeline()>, <pipeline_separator()>) {
let (pipe0, rest) = delimited_cmdline;
let mut pipelines = vec![pipe0?];
for (i, (sep, pipe)) in rest.into_iter().enumerate() {
if matches!(sep, "&") {
let mut last = &mut pipelines[i];
last.bg_job = true;
}
pipelines.push(pipe.unwrap());
}
Ok(CommandLine {
pipelines
})
}
rule pipeline_separator() -> &'input str
= $(";") / $("&") |
Yeah, one argument against making this some kind of built-in syntax is the number of different return types you might want, depending on how the separators associate with the items and whether empty lists and leading/trailing separators should be allowed:
(where |
The better alternative may then in fact be to improve documentation for the technique that uses |
I'm looking at migrating full-moon to use rust-peg for parsing. However, since it captures the entire text (such as whitespace and comments), I need to be able to capture the result of delimiters, as well as the main item, if I were to use
**
, or++
.The text was updated successfully, but these errors were encountered: