ripper-docs

Ripper

Ripper is a Ruby standard library that acts as an event-based Ruby script parser. It can be used to gather information about Ruby scripts without running them. It can also generate syntax trees from source code.

Ripper is generated from Ruby’s own parsing code, and is distributed with Ruby. That means the Ripper syntax is completely up-to-date with its corresponding Ruby.

When Ruby parses source code, it first scans the source code into a series of tokens. When a scanner token is recognized, Ripper dispatches a scanner event. The scanner events are then grouped into parser events by applying the Ruby grammar. When a set of scanner events is grouped (i.e., the production rule in the grammar is reduced), Ripper dispatches a parser event.

Each event will create a node in the syntax tree. For instance, an assignment node (assign) for value = 7 would come from scanner nodes for value (ident), = (op) and 7 (int). A parser event can group parser events, scanner events, or both.

Ruby’s parser generator (in parse.y) contains comments for Ripper to use. When Ripper is compiled, it modifies Ruby’s compiler code in parse.y based on these comments to make its own compiler code, very similar to Ruby’s.

Ripper allows you to define handlers for scanner and parser events, which can let you analyze Ruby programs, or recognize control structures (e.g. an assignment inside an if statement, which is often an error).

This document contains notes on various subjects pertaining to Ripper, as well as references for all of the various events and methods that Ripper uses internally. Here is a list of the pages that you can find in this document: