jtmoulia / neotomex

A PEG parser/transformer with a pleasant Elixir DSL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

String-less API -- expressions as symbols

jtmoulia opened this issue · comments

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.

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

  • nonterminal a -> a
  • terminal: Regex, String, Character
  • sequence a b -> a - b
  • priority a / b -> a | b
  • zero or one a -> a|:q
  • zero or more a* -> a|:*
  • one or more a+ -> a|:+
  • not !a -> !a
  • and &a -> &a
  • prune {a} -> {a}

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.

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.