Skip to content
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

Allow scoping in rule arguments #261

Closed
Ondraceq opened this issue Jun 25, 2021 · 2 comments
Closed

Allow scoping in rule arguments #261

Ondraceq opened this issue Jun 25, 2021 · 2 comments

Comments

@Ondraceq
Copy link

Hi,
I'm trying to use parametrized rules with my own enum type (&[T]).
Problem is that when using parametrized rules, I cannot use scoping. I have managed to do a workaround, but it's really not ideal.
Here is the code with the workaround - and with what I think would be better way to be able to write it.

#[derive(Debug, Clone, PartialEq)]
pub enum MyEnum {
    Value,
}

const ENUM_VALUE: MyEnum = MyEnum::Value;

peg::parser! {
    grammar my_parser() for [MyEnum] {
        rule single(val:MyEnum) -> MyEnum = [v if v == val] { v }
        // pub rule value() -> MyEnum = single(MyEnum::Value) // error
        pub rule value() -> MyEnum = single(ENUM_VALUE)
    }
}

I'm using the "0.7.0" version from crates.io

@Ondraceq Ondraceq changed the title Allow indentation in rule arguments Allow scoping in rule arguments Jun 25, 2021
@kevinmehall
Copy link
Owner

kevinmehall commented Jun 26, 2021

Thanks for pointing that out. As a better workaround, it should work if you wrap it in an extra set of parentheses: single((MyEnum::Value)).

The rule_arg() rule in the meta-grammar needs to be expanded to better cover Rust expressions. Right now it only accepts literals, variable names, and anything in parentheses.

@Ondraceq
Copy link
Author

Thank you - I didn't know that the parenthesis will work.
This makes this issue low priority - maybe just mention it somewhere in the docs/examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants