[Awesome Ruby Gem - Abstract Syntax Tree (AST)] Use parser gem to parse Abstract Syntax Tree (AST) in Ruby
parser
Parser is a production-ready Ruby parser written in pure Ruby. It recognizes as much or more code than Ripper, Melbourne, JRubyParser or ruby_parser, and is vastly more convenient to use.
You can also use unparser mbj/unparser: Turn Ruby AST into semantically equivalent Ruby source - https://github.com/mbj/unparser to produce equivalent source code from Parser’s ASTs.
Features
-
Precise source location reporting.
-
Documented AST format which is convenient to work with.
-
A simple interface and a powerful, tweakable one.
-
Parses 1.8, 1.9, 2.0, 2.1, 2.2 and 2.3 syntax with backwards-compatible AST formats.
-
Parses MacRuby and RubyMotion syntax extensions.
-
Rewriting support.
-
Parsing error recovery.
-
Improved clang-like diagnostic messages with location information.
-
Written in pure Ruby, runs on MRI >=2.0.0, JRuby and Rubinius (and historically, all versions of Ruby since 1.8)
-
Only one runtime dependency: the
ast
gem. -
Insane Ruby lexer rewritten from scratch in Ragel.
-
100% test coverage for Bison grammars (except error recovery).
-
Readable, commented source code.
Installation
You can install it as a gem:
1 | gem install parser |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usage
Load Parser (see the backwards compatibility section below for explanation of emit_*
calls):
1 | require 'parser/current' |
Parse a chunk of code:
1 | p Parser::CurrentRuby.parse("2 + 2") |
Access the AST’s source map:
1 | p Parser::CurrentRuby.parse("2 + 2").loc |
Traverse the AST:
see the documentation for gem ast.
Parse a chunk of code and display all diagnostics:
1 | parser = Parser::CurrentRuby.new |
If you reuse the same parser object for multiple #parse
runs, you need to #reset
it.
You can also use the ruby-parse
utility (it’s bundled with the gem) to play with Parser:
1 | ruby-parse -L -e "2+2" |
Documentation
Documentation for Parser is available online File: README — Documentation by YARD 0.8.7.4 - https://whitequark.github.io/parser/
.
Node names
Several Parser nodes seem to be confusing enough to warrant a dedicated README section.
(block)
The (block) node passes a Ruby block, that is, a closure, to a method call represented by its first child, a (send), (super) or (zsuper) node. To demonstrate:
1 | ruby-parse -e 'foo { |x| x + 2 }' |
(begin) and (kwbegin)
TL;DR: Unless you perform rewriting, treat (begin) and (kwbegin) as the same node type.
Both (begin) and (kwbegin) nodes represent compound statements, that is, several expressions which are executed sequentally and the value of the last one is the value of entire compound statement. They may take several forms in the source code:
-
foo; bar: without delimiters
-
(foo; bar): parenthesized
-
begin foo; bar; end: grouped with begin keyword
-
def x; foo; bar; end: grouped inside a method definition
and so on.
1 | ruby-parse -e '(foo; bar)' |
Note that, despite its name, kwbegin node only has tangential relation to the begin keyword. Normally, Parser AST is semantic, that is, if two constructs look differently but behave identically, they get parsed to the same node. However, there exists a peculiar construct called post-loop in Ruby:
1 | begin |
This specific syntactic construct, that is, keyword begin…end block followed by a postfix while, behaves very unlike other similar constructs, e.g. (body) while condition. While the body itself is wrapped into a while-post node, Parser also supports rewriting, and in that context it is important to not accidentally convert one kind of loop into another.
$ ruby-parse -e 'begin foo end while cond'
(while-post
(send nil :cond)
(kwbegin
(send nil :foo)))
$ ruby-parse -e 'foo while cond'
(while
(send nil :cond)
(send nil :foo))
$ ruby-parse -e '(foo) while cond'
(while
(send nil :cond)
(begin
(send nil :foo)))
(Parser also needs the (kwbegin) node type internally, and it is highly problematic to map it back to (begin).)
References
[1] whitequark/parser: A Ruby parser. - https://github.com/whitequark/parser
[2] parser | RubyGems.org | your community gem host - https://rubygems.org/gems/parser
[3] File: README — Documentation by YARD 0.8.7.4 - https://whitequark.github.io/parser/
[4] Module: AST::Sexp — Documentation by YARD 0.9.12 - https://whitequark.github.io/ast/AST/Sexp.html
[5] Static Analysis in Ruby - RubyGuides - https://www.rubyguides.com/2015/08/static-analysis-in-ruby/
[8] class Ripper - Documentation for Ruby 3.0.0 - https://docs.ruby-lang.org/en/3.0.0/Ripper.html