starlark-rust icon indicating copy to clipboard operation
starlark-rust copied to clipboard

write/serialize starlark

Open UebelAndre opened this issue 4 years ago • 7 comments

Is it possible to write or serialize starlark that's been parsed in using this module to a file?

UebelAndre avatar Aug 26 '21 18:08 UebelAndre

Currently that is not possible. We do have a Display implementation for all the things inside the AstModule, so it would be pretty easy to add it if you need. Note that the Display produces an equivalent source code, with additional brackets etc to guarantee no ambiguity - it's not as the user wrote, nor is it pretty printed.

However, what's the use of writing the Starlark module back to disk? Given there is no way to modify the module I would have thought there wasn't much benefit? We do imagine that one day Starlark will provide an explicit AST in Rust, and perhaps formatters etc, but haven't done all the work required for that yet.

ndmitchell avatar Aug 27 '21 09:08 ndmitchell

what's the use of writing the Starlark module back to disk?

I'm looking for something that can generate Starlark code with some additional configuration options to also ingest it and combine it in an output file. I've been able to use black successfully in the past to make things human readable modules but rendering the module in the first place is by far the most complicated step.

Given there is no way to modify the module I would have thought there wasn't much benefit?

I don't understand what you mean here. Can you expand on this?

We do imagine that one day Starlark will provide an explicit AST in Rust

Forgive my ignorance, AST?

UebelAndre avatar Aug 27 '21 17:08 UebelAndre

The data type AstModule (where AST = Abstract Syntax Tree) is an opaque type in Starlark, so its details are not exposed to the user. Internally it's defined as a series of statements, e.g. if/for/expression etc. But because those details aren't exposed, there's no way to produce an AstModule other than by parsing an input string. And because the only way is to parse an input string, it becomes much less useful to write/serialise the result.

My general approach to writing some Starlark with configuration has been to use a standard textual interpolation approach - e.g. https://mustache.github.io/, which can generate Starlark as well as anything else.

ndmitchell avatar Aug 27 '21 17:08 ndmitchell

My general approach to writing some Starlark with configuration has been to use a standard textual interpolation approach - e.g. https://mustache.github.io/, which can generate Starlark as well as anything else.

Yeah, this is where all the complexity comes from. It'd be nice if there was some common functionality for writing starlark (or some subsets of it). My primary use case is not writing arbitrary Starlark but instantiations of macros/functions. I don't have a need for writing anything defining conditionals or control-flow.

UebelAndre avatar Aug 27 '21 17:08 UebelAndre

Makes sense - I think that one day this Starlark library will expose the internals of AstModule along with suitable pretty printers, but I suspect it's not going to be too soon, alas.

You might be able to get away with using a Python parser/printer library, given that Starlark is a subset of Python.

ndmitchell avatar Aug 27 '21 18:08 ndmitchell

Makes sense - I think that one day this Starlark library will expose the internals of AstModule along with suitable pretty printers, but I suspect it's not going to be too soon, alas.

Sounds good, thanks

You might be able to get away with using a Python parser/printer library, given that Starlark is a subset of Python.

Are you familiar with one written in Rust?

UebelAndre avatar Aug 27 '21 18:08 UebelAndre

Are you familiar with one written in Rust?

Not really, although a quick Google shows a few possible ones - although not really sure of their capabilities

ndmitchell avatar Aug 27 '21 20:08 ndmitchell