Syntax Tree Project Overview


Syntax Tree Project Overview

Michael
Alright, so today we're talking about a Ruby project called Syntax Tree. I saw a talk by Kevin Newton from Shopify, and it's a lot more than just a formatter.

Hope
Yeah, that's the impression I got. He started it as a standard library formatter for Ruby, but now it's a whole ecosystem for working with Ruby's syntax tree. Let's start with the basics: what is a syntax tree anyway?

Michael
Right, so first we need to understand what a parser does. A parser takes plain text and transforms it into a structured representation. He gave this great example using English: 'Matz is nice, so we are nice.' First, lexical analysis breaks it into tokens – nouns, verbs, adjectives, conjunctions – then semantic analysis groups them into phrases according to a grammar.

Hope
And that structure is a tree. For English, you get subject-phrases, verb-phrases, and eventually a sentence node. The same idea applies to Ruby: the parser turns source code into a tree of nodes.

Michael
Exactly. In Ruby, each node is a class. Kevin showed a simple example where a VerbPhrase node has two children: a verb and an adjective. In Syntax Tree, there are 151 such node types – actually, he said it's now around 162, but the point is they represent every construct in Ruby.

Hope
And these nodes have methods like accept and child_nodes, which are part of the visitor pattern. The visitor pattern lets you traverse the tree without writing huge case statements. A visitor has visit methods for each node type, and the node calls back into the visitor – double dispatch.

Michael
He showed a clever shortcut: since most nodes just visit their children, you can define a generic visit method that calls visit_child_nodes, and then alias every visit method to that. That way, you can write a visitor that only overrides the nodes you care about.

Hope
That's super practical. So Syntax Tree provides that object layer on top of the raw parse result. It uses Ripper, Ruby's standard parser generator, but Ripper is event-based and lacks location information. Kevin had to build extra machinery to track positions and handle things like comments.

Michael
Yeah, comments are tricky because they can appear almost anywhere. His solution is to collect all comments during parsing, then after the tree is built, do a binary search over child_nodes to attach each comment to the most appropriate node – either the node that immediately follows it or the one on the same line.

Hope
He also mentioned that Ripper doesn't handle everything. For example, lambda local variables that shadow outer scope – he had to write a subparser for that. But the beauty of Syntax Tree is that you don't have to worry about any of those details. You just get a clean tree to work with.

Michael
So once you have the tree, what can you do? Formatting, for one. The formatter uses the prettyprint algorithm. You build up a document tree of groups, text, and breakables. When a line gets too long, you break the outermost group first. Kevin even published a gem called prettier_print that improves on Ruby's standard prettyprint library.

Hope
There's also a CLI with some neat features. ast prints the tree, format does the formatting, but the one I loved is match. It gives you a Ruby pattern-matching expression that exactly matches the source you provided. So if you run match on some code, you get an in clause that you can use directly in a case statement.

Michael
And search takes a pattern – using the same syntax from match – and finds all matching nodes in your file. So you can do things like 'find every integer with value 2' just by reusing the output from match. That's incredibly powerful for static analysis.

Hope
Then there's the language server. Syntax Tree implements the Language Server Protocol so you can get formatting on save in your editor. And Shopify is building the Ruby LSP on top of it, which adds things like document symbols, folding ranges, and semantic highlighting.

Michael
Finally, the coolest part: translation. Since Syntax Tree uses its own node types, you can write visitors that convert them to other representations. There's already a visitor that converts to the ruby_parser AST, another for the parser gem used by RuboCop, and even one for YARV bytecode. Kevin added that just hours before the talk.

Hope
That YARV visitor is wild – it compiles syntax tree nodes directly into Ruby VM instruction sequences. In theory, you could parse source, mutate the tree, convert to YARV, and execute it. That opens the door for things like polyfilling newer syntax on older runtimes.

Michael
And there's also a mutation visitor for code transformations. You give it a query using pattern matching, and it produces a new tree with the changes applied. That's essentially a linter and auto-corrector built in. Kevin mentioned a branch that implements seven RuboCop rules using this very approach.

Hope
So to wrap up: Syntax Tree provides a consistent, immutable syntax tree for Ruby, built on top of Ripper, with a visitor pattern that makes it easy to build tools. It has a formatter, a CLI, a language server, and translators to other formats. And the best part is, when the underlying parser changes – and Kevin is rewriting the Ruby parser at Shopify – your code using Syntax Tree won't need to change.

Michael
Exactly. It's a single interface for all your Ruby parsing needs. If you're writing a linter, formatter, code mod, or any static analysis tool, Syntax Tree hides all the gritty details and lets you focus on your logic.

Ask anything
We've read this whole study set, so questions about it get the best answers.