moonlark/libstarlark

181 views
Skip to first unread message

Gregg Reynolds

unread,
Jul 9, 2021, 12:51:32 PM7/9/21
to bazel-discuss
Hi folks,

Recently I've been working on tooling to support automated
maintenance of Bazel BUILD files for OCaml/Coq projects that use
OBazl. I've got a watch service that monitors a source tree to
detect source changes, and a dependency analysis tool that uses
codept to determine the dependencies of changed files. The
remaining piece of the puzzle is a tool to update BUILD.bazel
files using the dependency lists; today, the first part of that piece
is in place: moonlark.

libstarlark is a Starlark (i.e. Bazel language) parser written in
C11. moonlark is a Lua binding on libstarlark. Development was
motivated by the need to support automated maintenance of BUILD
files for OCaml projects using OBazl, but the design is
language-agnostic. Two applications are provided. moonlark:edit
is a C application that takes a filename argument, uses
libstarklark to parse the file, converts the AST to a Lua table,
and invokes a user-provided Lua function, passing the AST table.
User code is then responsible for manipulating the AST. An
included library of Lua code will contain routines to support AST
editing, but in the current version only serialization is
supported. A second target, moonlark:repl, launches a Lua
interpreter with moonlark preloaded.

Lua is a simple but powerful language; the code to serialize an AST is only about 65 short lines of Lua code.  Developers who want to build tools that need to parse Starlark files should be able to use moonlark to mangle the AST ad libitum and emit whatever they want with relative ease.

Feedback is welcome. You can file an issue on Github, or ask on the OBazl discord server.

Enjoy,

Gregg

Laurent Le Brun

unread,
Jul 9, 2021, 2:12:50 PM7/9/21
to Gregg Reynolds, bazel-discuss
> no suitable tool for programmatically editing BUILD files was available

https://github.com/bazelbuild/buildtools/ is the standard way of manipulating Starlark ASTs. It's usable as a library, so you can do any transformation you need. It has a lot of helper functions since it's also used for linting, etc. I would encourage everyone to use it and build on top of it.

-- 
Laurent


--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAO40Mi%3DKfJQFtmWi_A%2BS9kDNX19pH2XCaFZ8TZKBmwD1%2BrrUww%40mail.gmail.com.

Gregg Reynolds

unread,
Jul 9, 2021, 2:36:23 PM7/9/21
to Laurent Le Brun, bazel-discuss
On Fri, Jul 9, 2021 at 1:12 PM Laurent Le Brun <laur...@google.com> wrote:
> no suitable tool for programmatically editing BUILD files was available

https://github.com/bazelbuild/buildtools/ is the standard way of manipulating Starlark ASTs. It's usable as a library, so you can do any transformation you need. It has a lot of helper functions since it's also used for linting, etc. I would encourage everyone to use it and build on top of it.

Sorry, I should have been more clear. I meant no tool suitable for my purposes.  Buildtools is great stuff; libstarlark should not be viewed as a criticism of it.

Gregg 

Laurent Le Brun

unread,
Jul 12, 2021, 10:52:50 AM7/12/21
to Gregg Reynolds, bazel-discuss
No worries, but I wonder why it was not suitable for you. The comment "A long-term goal is to support editing capabilities matching or exceeding those of Buildozer" suggests that a lot of duplicated work is planned.

In my opinion, Buildtools could be a building block for any tool doing static analysis or refactoring of Starlark. As more and more people use it, the set of helper functions can be extended.

-- 
Laurent
Reply all
Reply to author
Forward
0 new messages