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
Grammar ambiguity between {action} and {count}. #74
Comments
An alternate syntax could, but doesn't have to, address #47 |
I Kevin, i have another related issue: for an IMAP protocol parser there is that notion of a literal.
Is there a way to do that? |
This is not supported, since you can't do variable repetition. But I'm sure there is a way to do this using a conditional capture (which is apparently still undocumented). I'll try to write a grammar that does this when I find time. |
Okay, here you go: #![feature(plugin, str_char, collections)]
#![plugin(peg_syntax_ext)]
peg! grammar(r#"
number -> usize
= [0-9]+ { match_str.parse().unwrap() }
pos -> usize
= &. { start_pos }
#[pub]
literal -> &'input str
= "{" n:number "}\r\n" start:pos ( . {?
println!("{}", start_pos - start);
if start_pos - start < n { Ok(()) } else { Err("") }
} )* {? if match_str.len() - start == n { Ok(match_str) } else { Err("literal") } }
"#);
fn main() {
println!("{:?}", grammar::literal("{8}\r\n12345678"));
} |
Thanks, that's great!
This successfully parses my test strings and also removes the {}\r\n part: Thanks! 👍 |
As I see, this project inspired by pegjs. I proposed unambiguous and intuitive and clear syntax for that project for the description of number of repetitions. Besides, in syntax are supported also a separator between elements and an opportunity to take the number of repetitions from earlier parser data (thus, allowing to parse grammars like PS. Also I noted that in grammar the type of returned values is appropriated to rules though actually it would be more logical to appropriate it to actions, they give the typified result. If you are interested in making grammar of more logical, then it is possible to look how it was made by me for the pegjs project (at present in a repository the preliminary version and sometime I will lick it into shape. But I think, the idea shall be clear. For code generation we inference types of all rules from types of actions. For actions types need to be set obviously). |
I'm changing the syntax to I like @buster and @Mingun's suggestion of variable repetition counts from a previously captured variable, and while it is not in the "context free" language class, I don't think it poses any problems for PEG. Opened #143. |
Actions that consist of returning a single integer are ambiguous with the repetition count syntax introduced by #20.
Those should be actions, but are parsed as repeat counts.
One option that minimally solves the problem is to simply drop
x {5}
in favor ofx {5, 5}
. A bare comma is not valid Rust, but the distinction between actions and the remaining{ , }
forms may be hard for humans to parse.Does anyone have ideas for a new syntax for the bounded-repeat functionality?
The text was updated successfully, but these errors were encountered: