jtmoulia / neotomex

A PEG parser/transformer with a pleasant Elixir DSL.
BSD 3-Clause "New" or "Revised" License
68 stars 10 forks source link

String-less API -- expressions as symbols #8

Open jtmoulia opened 10 years ago

jtmoulia commented 10 years ago

At the moment, the Elixir interface specified in ExGrammar is nice for specifying transforms, but not expressions: in define(id, expr, body), id is an atom naming the expression, and expr is a String which follows the formal PEG syntax provided in Ford's original paper. ExGrammar uses its PEG metagrammar to to parse the expression for that particular definition.

Using Elixir's macro system, it is possible to make a less "stringified" interface. Something like:

define a, a / b  do
define b, ({[A-Za-z]} a:+) . do
  ... transform
end

I'll follow this up with a real proposal for a macro parsable PEG expression syntax, and then see what an actual implementation looks like.

Parboiled provides a demo of the general idea.

jtmoulia commented 10 years ago

Here's a potential mapping of PEG expressions to valid Elixir expressions.

I still need to explore how Elixir applies it's order of operations when quoting expressions: a mismatch between the different expressions' OOPs will be a pain in the ass to unravel, as opposed to Elixir's quoting mechanism handling it automatically.

Other potential issues: in Elixir, parantheses seem to be used to group terms, but upon quoting are only reflected in the AST's nesting.

jtmoulia commented 10 years ago

Leaving this open, but putting it on the backburner. I think having to shoehorn the PEG expressions into Erlang terms results in a loss of expressivity, so I'm going to just keep writing grammars instead.