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 any attributes on rules #383

Open
Mingun opened this issue Sep 21, 2024 · 1 comment
Open

Allow any attributes on rules #383

Mingun opened this issue Sep 21, 2024 · 1 comment

Comments

@Mingun
Copy link

Mingun commented Sep 21, 2024

I want to feature gate some rules in grammar, so tried to place #[cfg(feature = "...")] on it, but grammar! does not accept that. It would be nice if I could do that:

peg::parser! {
  /// Contains generated parser for Kaitai Struct expression language.
  grammar parser() for str {
    /// Entry point for parsing enum values (keys of `number: enum_variant_name`).
    // TODO: It is better to use integer() rule to parse that
    // See https://github.com/kaitai-io/kaitai_struct/issues/1132
    #[cfg(feature = "compatible")] //<<<<<<<<<<<<<<<<<<<<<< impossible
    pub rule parse_enum_value() -> BigInt
      = "0x" n:$(hex()+) {? to_int(n, 16) }
      / n:$(['0'..='9' | '_']+) {? to_int(n, 10) }
      ;
    #[cfg(not(feature = "compatible"))] //<<<<<<<<<<<<<<<<<<<<<< impossible
    pub rule parse_enum_value() -> BigInt
      = "-" n:integer() { -n }
      / integer()
      ;
  }
}
@kevinmehall
Copy link
Owner

cfg could definitely be propagated to the pub and internal functions that implement the rule plus things like the struct field used for #[cache]. For arbitrary attributes, it's not clear where they should be applied in the generated code, and are probably too much of a forwards-compatibility hazard, though.

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