jtmoulia / neotomex

A PEG parser/transformer with a pleasant Elixir DSL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Throwaway matches

jtmoulia opened this issue · comments

Transforms often have to work through the exhaust of many part sequence matches.

For example, here is a define from the JSON.exs example:

define :pair, "space? string space? ':' space? json_value space?" do
  [_, string, _, _, _, val, _] -> {String.to_atom(string), val}
end

It needs the placeholders to properly match the list, but they distract from the intent of the transform. If these matches could be discarded, the sequence match could be reduced to only the elements which are needed by the transform.

The thought: use <...> to bracket matches which shouldn't be passed to the transform function. The example above becomes:

define :pair, "<space?> string <space?> <':'> <space?> json_value <space?>" do
  [string, val] -> {String.to_atom(string), val}
end

Open questions

  1. Can multiple match elements be surrounded? The compiler could create an implicit sequence for the contained elements.
  2. What happens if a definition has no matches, e.g. A -> <'a'>. My feeling is that it should simply return nil.