Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write/serialize starlark #31

Open
UebelAndre opened this issue Aug 26, 2021 · 7 comments
Open

write/serialize starlark #31

UebelAndre opened this issue Aug 26, 2021 · 7 comments

Comments

@UebelAndre
Copy link

UebelAndre commented Aug 26, 2021

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

@UebelAndre UebelAndre changed the title write/de-serialize starlark write/deserialize starlark Aug 26, 2021
@UebelAndre UebelAndre changed the title write/deserialize starlark write/serialize starlark Aug 27, 2021
@ndmitchell
Copy link
Contributor

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.

@UebelAndre
Copy link
Author

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?

@ndmitchell
Copy link
Contributor

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.

@UebelAndre
Copy link
Author

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.

@ndmitchell
Copy link
Contributor

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.

@UebelAndre
Copy link
Author

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?

@ndmitchell
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants