elm-syntax icon indicating copy to clipboard operation
elm-syntax copied to clipboard

Potential use for Elm code generation?

Open michaeljones opened this issue 7 years ago • 1 comments

In the last few weeks I have become interested the idea of generating Elm code from other languages. This is motivated by a couple of internal projects:

  • Sharing constants between Typescript & Elm. Having the official definitions in Typescript & being able to generate an Elm module that mirrored it would be useful.
  • Handling language translations written with ICU strings. Being able to generate an Elm module from the ICU strings that achieves the intended logic of the ICU syntax.

Due to whitespace being significant it seems like it is a hard task to easily export valid Elm code from a quickly written program. It seems like it might be easier to rely on generating some kind of JSON AST representation and then have a commonly used tool to do convert the JSON to valid Elm code.

I see you've commented on a related issue on elm-format and there is another issue about it too.

Do you believe that the JSON structure you have developed and your Elm.Writer module are in a good position to help with that? I'm keen to do some experiments. Do you think it would be useful to have 'elm-to-json-ast' and 'json-ast-to-elm' command line tools? And maybe some kind of Typescript API for generating the JSON AST? Potentially as separate projects if you'd like to keep this project clean and with a clear scope?

Thanks for the project. It is very cool to see.

michaeljones avatar Sep 16 '18 10:09 michaeljones

I'm also interested in this use case. I have a project called MetaElm where I code-generate Elm from inside Elm. I have a minimal AST structure, and it can emit code as seen here:

https://github.com/showell/MetaElm/blob/master/src/MeElmCode.elm

But it's kinda crude and doesn't always handle things like parentheses nicely. I'd rather write to another AST, but I haven't gotten my head around elm-syntax yet, and I wonder if it's too low level.

showell avatar Nov 06 '19 20:11 showell

I'd say this problem has been solved with https://package.elm-lang.org/packages/mdgriffith/elm-codegen/latest/.

jfmengels avatar Jul 18 '24 21:07 jfmengels