kevinmehall / rust-peg

Parsing Expression Grammar (PEG) parser generator for Rust
https://crates.io/crates/peg
MIT License
1.48k stars 106 forks source link

Re-usable parsers #25

Open aomader opened 10 years ago

aomader commented 10 years ago

Assume I define a few parsers within a module and export the parsing functions, similar like the following:

pub use self::parser::empty as empty;

peg! parser(r#"
#[pub]
empty -> ()
    = "" { () }
"#)

I'm wondering if it is possible to reuse the exporter parser in another module in another PEG macro call. In general, I'm interested in reusing parsers defined in different source files.

Any ideas?

untitaker commented 10 years ago

You might be able to do that by importing the internal parse_empty from the self:parser module. I am not sure though.

kevinmehall commented 10 years ago

Yeah, @untitaker's correct; it would have to call the private parser::parse_empty rather than the public wrapper parser::empty, which would require some kind of flag to make those internal functions public, and syntax in the other grammar to use them into the generated module's scope.

Another potential issue is that I plan to add a facility for a grammar to have a state object threaded through all the parse rules, for things like string interners, filenames for source maps, etc. It would have to ensure both grammars have the same type of state object so they're compatible.

aomader commented 10 years ago

I thought it wouldn't be that easy, too bad. Are you planning on adding such functionality in the near future?

One more thing, it would be awesome to allow to import some stuff into the parser and making it available for the whole. Else I have to write long statements like super::super::... or I have to re-export them using pub use .... Any advice on this one?

mcandre commented 1 year ago

I would love for the peg DSL to make it easier to automatically export the generated grammar for use in a library. Don't assume that people only write executables.

As a workaround, I am writing a pub fn in my lib.rs that invokes the pub fn within the parser! block. Annoying to have to repeat myself, but that's what I'm doing meanwhile.

kevinmehall commented 1 year ago

@mcandre that sounds like a different issue than what this feature request is about (sharing rules between multiple PEG grammars). Can you open a new issue with more details and ideally a code example of what's not working?

Mingun commented 6 months ago

Probably the way to do that is to introduce another macro, say, parser_fragment!, which will define functions which can be used in a regular parser! macro, but without boilerplate that is needed for stand-alone parser.