caketop / python-starlark-go

🐍 Python bindings for starlark-go 🐍
https://python-starlark-go.readthedocs.io/
Apache License 2.0
24 stars 8 forks source link

Access syntax tree? #174

Open tadamcz opened 1 year ago

tadamcz commented 1 year ago

Great library, thanks so much for your work! I really appreciate e.g. how you've translated exceptions in a way that lets you easily access the line and column number of a Starlark error 👌.

Does Starlark provide a way to access the syntax tree of an expression? Is this something we could add to python-starlark-go? (If Starlark exposes the tree I'm happy to take a shot at it, but my go skills are very poor).

So, for example, in the Starlark code:

a = 1
b = 2
c = a * 2
result = b + c 

we could access the fact that result depends on b and c via the operator +, and in turn c depends on a (and a literal) via *.

jordemort commented 1 year ago

I think this might be possible to some degree, but we aren't using any part of the API that would give us something like this at the moment. I will keep this in mind as I am looking at the starlark-go API docs to implement some other things.

tadamcz commented 1 year ago

Ok, thanks! To clarify, do you know if there is an API for this in starlark-go? (Obviously they have the machinery for this internally, my question is if there's a reasonably "public-facing" API e.g. with docs).

jordemort commented 1 year ago

@tadamcz Yeah I think the 'syntax` package is what you'd be looking for: https://pkg.go.dev/go.starlark.net@v0.0.0-20230302034142-4b1e35fe2254/syntax