yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
884 stars 112 forks source link

Get AST from parser built from combinators? #113

Closed stephenwhittle closed 4 years ago

stephenwhittle commented 4 years ago

I'm evaluating the best way forward with implementing a parser for a family of file formats that has a large number of ordered optional variables that vary according to specific file subtype, so I intend to make heavy use of parameterized rules to specify which variables are acceptable and their ordering at a file level.

As an alternative to abusing your (now fixed, thank you!) macro implementation to specify the file-level schema, I have the following simple DSL that essentially allows me to emulate macros using the parser combinator system:

Definition FlightPathMarkerGauge <=
ObjectHeader("+Flight Path Marker")
& OptionalVariable("Color")
& OptionalVariable("Filename")
& OptionalVariable("Center Offsets");

Definition GaugesFile <= ListOf(OneOf(FlightPathMarkerGauge, OtherGaugeTypes...));

Omitting the shared_ptrs for brevity, FlightPathMarkerGauge expands to peg::Sequence(peg::LiteralString("+Flight Path Marker"), peg::Character(':'), Value, peg::opt(peg::Sequence(peg::LiteralString("$Color"), peg::Character(':'), Value)), ......)

I know I'm going to need to alter ObjectHeader and OptionalVariable etc to use peg::ref to have 1:1 equivalency to macros, but I'd like to verify that the structure is being expressed correctly and matches my input data before I perform that refactoring.

As a result, I'd like to run this against some test input and view the AST of the result, but the enable_ast method is only available on peg::parser, not peg::Definition. Is there a way to do this, or a way to convert a Definition to a grammar string that can be fed to peg::parser::parse?

yhirose commented 4 years ago

@stephenwhittle, thanks for the feedback. I am probably not smart enough to understand what you are trying to do. :)

But at least, I implemented the AST support for combinators. You can now declare definitions with AST action handler with AST_DEFINITIONS macro.

https://github.com/yhirose/cpp-peglib/blob/94c73b1e313d7fed4bcf9793c06c22337c982025/test/test1.cc#L678-L716

Hope it helps!

stephenwhittle commented 4 years ago

This is very helpful, thank you!