Parser
Parser is a Ruby parser written in pure Ruby.
Installation
$ gem install parser
Usage
Parse a chunk of code:
require 'parser/ruby19'
p Parser::Ruby19.parse("2 + 2")
# (send
# (int 2) :+
# (int 2))
Parse a chunk of code and display all diagnostics:
parser = Parser::Ruby19.new
parser.diagnostics.consumer = lambda do |diag|
puts diag.render
end
buffer = Parser::Source::Buffer.new('(string)')
buffer.source = "foo *bar"
p parser.parse(buffer)
# (string):1:5: warning: `*' interpreted as argument prefix
# foo *bar
# ^
# (send nil :foo
# (splat
# (send nil :bar)))
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 and 2.0 syntax with backwards-compatible AST formats (WIP, only 1.8 & 1.9 yet).
- Parsing error recovery.
- Improved clang-like diagnostic messages with location information.
- Written in pure Ruby, runs on MRI 1.8.7 or >=1.9.2, JRuby and Rubinius in 1.8 and 1.9 mode.
- Single runtime dependency: the ast gem.
- RubyParser compatibility (WIP, no, not really yet).
- Insane Ruby lexer rewritten from scratch in Ragel.
- 100% test coverage for Bison grammars (except error recovery).
- Readable, commented source code.
Contributors
- Peter Zotov (whitequark)
- Magnus Holm (judofyr)
Acknowledgements
The lexer testsuite and ruby_parser compatibility testsuite are derived from ruby_parser.
The Bison parser rules are derived from Ruby MRI parse.y.
Contributing
- Make sure you have Ragel 6.8 installed
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request