vsbenas / parser-gen

A parser generator in Lua using PEG syntax.
MIT License
49 stars 9 forks source link

Feature request: a mode for explicit node creation in ASTs #5

Closed hishamhm closed 7 years ago

hishamhm commented 7 years ago

Currently, parser-gen generates an AST where it produces a node for each rule, unless told so via fragment. This produces a lot of intermediate nodes in ASTs (e.g. statement, simpleexp, term, factor in the Tiny example of README.md).

It would be nice to, instead of marking which rules don't generate nodes, to be able to mark which rules should generate nodes (perhaps with a node annotation or some other syntax?).

vsbenas commented 7 years ago

The idea behind generating the AST automatically was to minimize the grammar, but provide a complete AST, so that it can be reduced afterwards as necessary. The node functionality can be achieved by disabling AST generation and adding captures to the tables, however, I do see your point. I'm not sure whether this approach is better in the general case and I'd like to hear some more opinions on this; I'm not against implementing this approach in a separate branch.

vsbenas commented 7 years ago

I added this feature to the latest commit, please test it out. Before compiling the grammar use

pg.usenodes(true)