ohmjs / ohm

A library and language for building parsers, interpreters, compilers, etc.
MIT License
4.93k stars 217 forks source link

Idea: Syntax for specifying binding rules #49

Open pdubroy opened 8 years ago

pdubroy commented 8 years ago

It might be nice to have a syntax for specifying lexical binding rules for languages. Currently, the best way to do name resolution is inside an attribute/operation, but this makes it more difficult to reuse the results. A declarative syntax could be useful for (say) an IDE that wants support renaming variables, without having to rely on language- and implementation-specific attributes/operations.

One idea I had is a syntax to embed the binding rules in the grammar itself: https://gist.github.com/pdubroy/984ad6e3ecf181b1c15c#file-gistfile1-txt. However, this would complicate the grammars somewhat, and would likely only be able to handle simple cases.

Another option would be some sort of supplemental syntax -- maybe separate rules in the grammar that define how names are resolved. E.g.:

ML {
  Expr = ...
  LetExpr = ...

  @LetExpr = /* binding syntax goes here */
}

Some relevant papers:

pdubroy commented 8 years ago

Also: Toward a General Theory of Names, Binding and Scope

pdubroy commented 8 years ago

If we implement this, it would require a lot of thought and careful design, and we should start by implementing it as a library feature first.

alexwarth commented 8 years ago

Eelco Visser and co. seem to have a nice way to specify this kind of thing. We should study their approach carefully. Here are some links: