Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Prototype of imperative (Starlark-based) syntax traversal#72

Closed
adonovan wants to merge 26 commits intomainfrom
adonovan/starlark-visitor
Closed

Prototype of imperative (Starlark-based) syntax traversal#72
adonovan wants to merge 26 commits intomainfrom
adonovan/starlark-visitor

Conversation

@adonovan
Copy link

@adonovan adonovan commented Mar 22, 2022

This is an extremely rough proof of concept that I threw together last night of an alternative notation to TSG for expressing the syntax tree traversal. The StackGraph node and edge creation primitives are not yet implemented.

See https://github.com/github/stack-graphs/blob/adonovan/starlark-visitor/demo/demo.go for the driver program. It's in Go and uses the Go bindings for tree-sitter to parse the input file, and it uses Starlark (a simple embeddable Python dialect) to express the tree traversal.

See https://github.com/github/stack-graphs/blob/adonovan/starlark-visitor/demo/demo.star for the Starlark program. Observe that it allows, in few lines of familiar imperative code, fine control over the traversal ordering, and that it closely resembles the concepts and terminology of the Tree Sitter grammar.

This gist shows the output of the tool when run on its own Go source: https://gist.github.com/adonovan/4b477ceaa2d5016a826213697d639953

dcreager and others added 25 commits March 21, 2022 16:37
The first part of our stack graph loader creates a stack graph node for
each graph DSL node.  You use attributes to specify the kind of stack
graph node (defaulting to “internal”), along with any other necessary
properties of the node.
If you set the `source_node` attribute for a graph DSL node, whose value
must be a syntax node reference, then we will calculate the location of
that syntax node in the source file and fill in the stack graph node's
“source info” accordingly.
- Add `StackGraphLanguage` constructor that accepts a parse
  `tree_sitter_graph::ast::File` instead of a source.
- Add parameter to pass `Functions` to the `StackGraphLanguage`.
- Rename `load_` to `build_` where TSG is executed.
- Add parameter to pass global `Variables` to `build_` method.
- Implement loading tree-sitter language and tree-sitter-graph file
- Parse test files and run the given TSG file against them

Co-authored-by: Douglas Creager <dcreager@dcreager.net>
Error context was lost for some errors, resulting in unhelpful error
messages. This uses the same mechanism for wrapping errors everywhere,
and ensures context is preserved up to the top level.
@adonovan adonovan force-pushed the adonovan/starlark-visitor branch 2 times, most recently from 76bd8eb to 7467eba Compare March 24, 2022 16:06
@adonovan adonovan force-pushed the adonovan/starlark-visitor branch from 7467eba to 2550d4d Compare March 24, 2022 16:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants