Parser
Parser is a production-ready Ruby parser written in pure Ruby. It performs on par or better than Ripper, Melbourne, JRubyParser or ruby_parser.
Installation
$ gem install parser
Usage
Parse a chunk of code:
require 'parser/ruby20'
p Parser::Ruby20.parse("2 + 2")
# (send
# (int 2) :+
# (int 2))
Parse a chunk of code and display all diagnostics:
parser = Parser::Ruby20.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)))
If you reuse the same parser object for multiple #parse
runs, you need to #reset
it.
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 and 2.1 (preliminary) syntax with backwards-compatible AST formats.
- 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