SyntaxTree
Syntax Tree is a suite of tools built on top of the internal CRuby parser. It provides the ability to generate a syntax tree from source, as well as the tools necessary to inspect and manipulate that syntax tree. It can be used to build formatters, linters, language servers, and more.
It is built with only standard library dependencies. It additionally ships with a plugin system so that you can build your own syntax trees from other languages and incorporate these tools.
Installation
Syntax Tree is both a command-line interface and a library. If you’re only looking to use the command-line interface, then we recommend installing the gem globally, as in:
gem install syntax_tree
To run the CLI with the gem installed globally, you would run:
stree version
If you’re planning on using Syntax Tree as a library within your own project, we recommend installing it as part of your gem bundle. First, add this line to your application’s Gemfile:
gem "syntax_tree"
And then execute:
bundle install
To run the CLI with the gem installed in your gem bundle, you would run:
bundle exec stree version
CLI
Syntax Tree ships with the stree CLI, which can be used to inspect and manipulate Ruby code. Below are listed all of the commands built into the CLI that you can use.
For many commands, file paths are accepted after the configuration options. For all of these commands, you can alternatively pass in content through STDIN or through the -e option to specify an inline script.
ast
This command will print out a textual representation of the syntax tree associated with each of the files it finds. To execute, run:
stree ast path/to/file.rb
For a file that contains 1 + 1, you will receive:
(program (statements (binary (int "1") + (int "1"))))
check
This command is meant to be used in the context of a continuous integration or git hook. It checks each file given to make sure that it matches the expected format. It can be used to ensure unformatted content never makes it into a codebase.
stree check path/to/file.rb
For a file that matches the expected format, you will receive:
All files matched expected format.
If there are files with unformatted code, you will receive:
[warn] path/to/file.rb The listed files did not match the expected format.
To change the print width that you are checking against, specify the --print-width option, as in:
stree check --print-width=100 path/to/file.rb
ctags
This command will output to stdout a set of tags suitable for usage with ctags.
stree ctags path/to/file.rb
For a file containing the following Ruby code:
class Foo end class Bar < Foo end
you will receive:
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ Bar test.rb /^class Bar < Foo$/;" c inherits:Foo Foo test.rb /^class Foo$/;" c
expr
This command will output a Ruby case-match expression that would match correctly against the first expression of the input.
stree expr path/to/file.rb
For a file that contains 1 + 1, you will receive:
SyntaxTree::