Skip to content

Commit ce0cc1e

Browse files
committed
Documentation
1 parent bb0f030 commit ce0cc1e

File tree

3 files changed

+311
-66
lines changed

3 files changed

+311
-66
lines changed

README.md

Lines changed: 265 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,303 @@
77
[![Build Status](https://github.com/ruby-syntax-tree/syntax_tree/actions/workflows/main.yml/badge.svg)](https://github.com/ruby-syntax-tree/syntax_tree/actions/workflows/main.yml)
88
[![Gem Version](https://img.shields.io/gem/v/syntax_tree.svg)](https://rubygems.org/gems/syntax_tree)
99

10-
A fast Ruby parser and formatter with only standard library dependencies.
10+
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.
11+
12+
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.
13+
14+
- [Installation](#installation)
15+
- [CLI](#cli)
16+
- [ast](#ast)
17+
- [check](#check)
18+
- [format](#format)
19+
- [write](#write)
20+
- [Library](#library)
21+
- [SyntaxTree.read(filepath)](#syntaxtreereadfilepath)
22+
- [SyntaxTree.parse(source)](#syntaxtreeparsesource)
23+
- [SyntaxTree.format(source)](#syntaxtreeformatsource)
24+
- [Nodes](#nodes)
25+
- [child_nodes](#child_nodes)
26+
- [Pattern matching](#pattern-matching)
27+
- [pretty_print(q)](#pretty_printq)
28+
- [to_json(*opts)](#to_jsonopts)
29+
- [format(q)](#formatq)
30+
- [Visitor](#visitor)
31+
- [visit_method](#visit_method)
32+
- [Language server](#language-server)
33+
- [textDocument/formatting](#textdocumentformatting)
34+
- [textDocument/inlayHints](#textdocumentinlayhints)
35+
- [syntaxTree/visualizing](#syntaxtreevisualizing)
36+
- [Contributing](#contributing)
37+
- [License](#license)
1138

1239
## Installation
1340

14-
Add this line to your application's Gemfile:
41+
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:
42+
43+
```sh
44+
gem install syntax_tree
45+
```
46+
47+
To run the CLI with the gem installed globally, you would run:
48+
49+
```sh
50+
stree version
51+
```
52+
53+
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:
1554

1655
```ruby
1756
gem "syntax_tree"
1857
```
1958

2059
And then execute:
2160

22-
$ bundle install
61+
```sh
62+
bundle install
63+
```
2364

24-
Or install it yourself as:
65+
To run the CLI with the gem installed in your gem bundle, you would run:
2566

26-
$ gem install syntax_tree
67+
```sh
68+
bundle exec stree version
69+
```
2770

28-
## Usage
71+
## CLI
2972

30-
From code:
73+
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. Note that for all commands that operate on files, you can also pass in content through STDIN.
3174

32-
```ruby
33-
require "syntax_tree"
75+
### ast
3476

35-
pp SyntaxTree.parse(source) # print out the AST
36-
puts SyntaxTree.format(source) # format the AST
77+
This command will print out a textual representation of the syntax tree associated with each of the files it finds. To execute, run:
78+
79+
```sh
80+
stree ast path/to/file.rb
3781
```
3882

39-
From the CLI:
83+
For a file that contains `1 + 1`, you will receive:
84+
85+
```
86+
(program (statements (binary (int "1") + (int "1"))))
87+
```
88+
89+
### check
90+
91+
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.
4092

4193
```sh
42-
$ stree ast program.rb
43-
(program
44-
(statements
45-
...
94+
stree check path/to/file.rb
95+
```
96+
97+
For a file that matches the expected format, you will receive:
98+
99+
```
100+
All files matched expected format.
101+
```
102+
103+
If there are files with unformatted code, you will receive:
104+
46105
```
106+
[warn] path/to/file.rb
107+
The listed files did not match the expected format.
108+
```
109+
110+
### format
47111

48-
or
112+
This command will output the formatted version of each of the listed files. Importantly, it will not write that content back to the source files. It is meant to display the formatted version only.
49113

50114
```sh
51-
$ stree format program.rb
52-
class MyClass
53-
...
115+
stree format path/to/file.rb
116+
```
117+
118+
For a file that contains `1 + 1`, you will receive:
119+
120+
```ruby
121+
1 + 1
54122
```
55123

56-
or
124+
### write
125+
126+
This command will format the listed files and write that formatted version back to the source files. Note that this overwrites the original content, to be sure to be using a version control system.
57127

58128
```sh
59-
$ stree write program.rb
60-
program.rb 1ms
129+
stree write path/to/file.rb
61130
```
62131

63-
## Development
132+
This will list every file that is being formatted. It will output light gray if the file already matches the expected format. It will output in regular color if it does not.
133+
134+
```
135+
path/to/file.rb 0ms
136+
```
137+
138+
## Library
139+
140+
Syntax Tree can be used as a library to access the syntax tree underlying Ruby source code.
141+
142+
### SyntaxTree.read(filepath)
143+
144+
This function takes a filepath and returns a string associated with the content of that file. It is similar in functionality to `File.read`, except htat it takes into account Ruby-level file encoding (through magic comments at the top of the file).
145+
146+
### SyntaxTree.parse(source)
147+
148+
This function takes an input string containing Ruby code and returns the syntax tree associated with it. The top-level node is always a `SyntaxTree::Program`, which contains a list of top-level expression nodes.
149+
150+
### SyntaxTree.format(source)
151+
152+
This function takes an input string containing Ruby code, parses it into its underlying syntax tree, and formats it back out to a string.
153+
154+
## Nodes
155+
156+
There are many different node types in the syntax tree. They are meant to be treated as immutable structs containing links to child nodes with minimal logic contained within their implementation. However, for the most part they all respond to a certain set of APIs, listed below.
157+
158+
### child_nodes
159+
160+
One of the easiest ways to descend the tree is to use the `child_nodes` function. It is implemented on every node type (leaf nodes return an empty array). If the goal is to simply walk through the tree, this is the easiest way to go.
161+
162+
```ruby
163+
program = SyntaxTree.parse("1 + 1")
164+
program.child_nodes.first.child_nodes.first
165+
# => (binary (int "1") :+ (int "1"))
166+
```
167+
168+
### Pattern matching
169+
170+
Pattern matching is another way to descend the tree which is more specific than using `child_nodes`. Using Ruby's built-in pattern matching, you can extract the same information but be as specific about your constraints as you like. For example, with minimal constraints:
171+
172+
```ruby
173+
program = SyntaxTree.parse("1 + 1")
174+
program => { statements: { body: [binary] } }
175+
binary
176+
# => (binary (int "1") :+ (int "1"))
177+
```
178+
179+
Or, with more constraints on the types to ensure we're getting exactly what we expect:
180+
181+
```ruby
182+
program = SyntaxTree.parse("1 + 1")
183+
program => SyntaxTree::Program[statements: SyntaxTree::Statements[body: [SyntaxTree::Binary => binary]]]
184+
binary
185+
# => (binary (int "1") :+ (int "1"))
186+
```
187+
188+
### pretty_print(q)
189+
190+
Every node responds to the `pretty_print` Ruby interface, which makes it usable by the `pp` library. You _can_ use this API manually, but it's mostly there for compatibility and not meant to be directly invoked. For example:
191+
192+
```ruby
193+
pp SyntaxTree.parse("1 + 1")
194+
# (program (statements (binary (int "1") + (int "1"))))
195+
```
196+
197+
### to_json(*opts)
198+
199+
Every node responds to the `to_json` Ruby interface, which makes it usable by the `json` library. Much like `pretty_print`, you could use this API manually, but it's mostly used by `JSON` to dump the nodes to a serialized format. For example:
200+
201+
```ruby
202+
program = SyntaxTree.parse("1 + 1")
203+
program => { statements: { body: [{ left: }] } }
204+
puts JSON.dump(left)
205+
# {"type":"int","value":"1","loc":[1,0,1,1],"cmts":[]}
206+
```
207+
208+
### format(q)
209+
210+
Every node responds to `format`, which formats the content nicely. The API mirrors that used by the `pretty_print` gem in that it accepts a formatter object and calls methods on it to generate its own internal representation of the text that will be outputted. Because of this, it's easier to not use this API directly and instead to call `SyntaxTree.format`. You _can_ however use this directly if you create the formatter yourself, as in:
211+
212+
```ruby
213+
source = "1+1"
214+
program = SyntaxTree.parse(source)
215+
program => { statements: { body: [binary] } }
216+
217+
formatter = SyntaxTree::Formatter.new(source, [])
218+
binary.format(formatter)
219+
220+
formatter.flush
221+
formatter.output.join
222+
# => "1 + 1"
223+
```
224+
225+
## Visitor
226+
227+
If you want to operate over a set of nodes in the tree but don't want to walk the tree manually, the `Visitor` class makes it easy. `SyntaxTree::Visitor` is an implementation of the double dispatch visitor pattern. It works by the user defining visit methods that process nodes in the tree, which then call back to other visit methods to continue the descent. This is easier shown in code.
228+
229+
Let's say, for instance, that you wanted to find every place in source where you have an arithmetic problem between two integers (this is pretty contrived, but it's just for illustration). You could define a visitor that only explicitly visits the `SyntaxTree::Binary` node, as in:
230+
231+
```ruby
232+
class ArithmeticVisitor < SyntaxTree::Visitor
233+
def visit_binary(node)
234+
if node in { left: SyntaxTree::Int, operator: :+ | :- | :* | :/, right: SyntaxTree::Int }
235+
puts "The result is: #{node.left.value.to_i.public_send(node.operator, node.right.value.to_i)}"
236+
end
237+
end
238+
end
239+
240+
visitor = ArithmeticVisitor.new
241+
visitor.visit(SyntaxTree.parse("1 + 1"))
242+
# The result is: 2
243+
```
244+
245+
With visitors, you only define handlers for the nodes that you need. You can find the names of the methods that you will need to define within the base visitor, as they're all aliased to the default behavior (visiting the child nodes). Note that when you define a handler for a node, you have to tell Syntax Tree how to walk further. In the example above, we don't need to go any further because we already know the child nodes are `SyntaxTree::Int`, so they can't possibly contain more `SyntaxTree::Binary` nodes. In other circumstances you may not know though, so you can either:
246+
247+
* call `super` (which will do the default and visit all child nodes)
248+
* call `visit_child_nodes` manually
249+
* call `visit(child)` with each child that you want to visit
250+
* call nothing if you're sure you don't want to descend further
251+
252+
### visit_method
253+
254+
When you're creating a visitor, it's very easy to accidentally mistype a visit method. Unfortunately, there's no way to tell Ruby to explicitly override a parent method, so it would then be easy to define a method that never gets called. To mitigate this risk, there's `Visitor.visit_method(name)`. This method accepts a symbol that is checked against the list of known visit methods. If it's not in the list, then an error will be raised. It's meant to be used like:
255+
256+
```ruby
257+
class ArithmeticVisitor < SyntaxTree::Visitor
258+
visit_method def visit_binary(node)
259+
# ...
260+
end
261+
end
262+
```
263+
264+
This will only be checked once when the file is first required. If there is a typo in your method name (or the method no longer exists for whatever reason), you will receive an error like so:
265+
266+
```
267+
~/syntax_tree/lib/syntax_tree/visitor.rb:46:in `visit_method': Invalid visit method: visit_binar (SyntaxTree::Visitor::VisitMethodError)
268+
Did you mean? visit_binary
269+
visit_in
270+
visit_ivar
271+
from (irb):2:in `<class:ArithmeticVisitor>'
272+
from (irb):1:in `<main>'
273+
from bin/console:8:in `<main>'
274+
```
275+
276+
## Language server
277+
278+
Syntax Tree additionally ships with a language server conforming to the [language server protocol](https://microsoft.github.io/language-server-protocol/). It can be invoked through the CLI by running:
279+
280+
```sh
281+
stree lsp
282+
```
283+
284+
By default, the language server is relatively minimal, mostly meant to provide a registered formatter for the Ruby language. However there are a couple of additional niceties baked in. There are related projects that configure and use this language server within IDEs. For example, to use this code with VSCode, see [ruby-syntax-tree/vscode-syntax-tree](https://github.com/ruby-syntax-tree/vscode-syntax-tree).
285+
286+
### textDocument/formatting
287+
288+
As mentioned above, the language server responds to formatting requests with the formatted document. It typically responds on the order of tens of milliseconds, so it should be fast enough for any IDE.
289+
290+
### textDocument/inlayHints
291+
292+
The language server also responds to the relatively new inlay hints request. This request allows the language server to define additional information that should exist in the source code as helpful hints to the developer. In our case we use it to display things like implicit parentheses. For example, if you had the following code:
293+
294+
```ruby
295+
1 + 2 * 3
296+
```
297+
298+
Implicity, the `2 * 3` is going to be executed first because the `*` operator has higher precedence than the `+` operator. However, to ease mental overhead, our language server includes small parentheses to make this explicit, as in:
299+
300+
```ruby
301+
1 +2 * 3
302+
```
64303

65-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
304+
### syntaxTree/visualizing
66305

67-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
306+
The language server additionally includes this custom request to return a textual representation of the syntax tree underlying the source code of a file. Language server clients can use this to (for example) open an additional tab with this information displayed.
68307

69308
## Contributing
70309

bin/setup

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)