class Ripper
Ripper is a Ruby script parser.
You can get information from the parser with event-based style. Information such as abstract syntax trees or simple lexical analysis of the Ruby program.
Usage
Ripper provides an easy interface for parsing your program into a symbolic expression tree (or S-expression).
Understanding the output of the parser may come as a challenge, it’s recommended you use PP to format the output for legibility.
require 'ripper'
require 'pp'
pp Ripper.sexp('def hello(world) "Hello, #{world}!"; end')
#=> [:program,
[[:def,
[:@ident, "hello", [1, 4]],
[:paren,
[:params, [[:@ident, "world", [1, 10]]], nil, nil, nil, nil, nil, nil]],
[:bodystmt,
[[:string_literal,
[:string_content,
[:@tstring_content, "Hello, ", [1, 18]],
[:string_embexpr, [[:var_ref, [:@ident, "world", [1, 27]]]]],
[:@tstring_content, "!", [1, 33]]]]],
nil,
nil,
nil]]]]
You can see in the example above, the expression starts with :program.
From here, a method definition at :def, followed by the method’s identifier :@ident. After the method’s identifier comes the parentheses :paren and the method parameters under :params.
Next is the method body, starting at :bodystmt (stmt meaning statement), which contains the full definition of the method.
In our case, we’re simply returning a String, so next we have the :string_literal expression.
Within our :string_literal you’ll notice two @tstring_content, this is the literal part for Hello, and !. Between the two @tstring_content statements is a :string_embexpr, where embexpr is an embedded expression. Our expression consists of a local variable, or var_ref, with the identifier (@ident) of world.
Resources
-
[Ruby Inside](www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing- your-code-5270.html)
Requirements
-
ruby 1.9 (support CVS HEAD only)
-
bison 1.28 or later (Other yaccs do not work)
License
Ruby License.
-
Minero Aoki
-
aamine@loveruby.net
Constants
- EVENTS
-
This array contains name of all ripper events.
- EXPR_ARG
- EXPR_ARG_ANY
- EXPR_BEG
- EXPR_BEG_ANY
- EXPR_CLASS
- EXPR_CMDARG
- EXPR_DOT
- EXPR_END
- EXPR_ENDARG
- EXPR_ENDFN
- EXPR_END_ANY
- EXPR_FITEM
- EXPR_FNAME
- EXPR_LABEL
- EXPR_LABELED
- EXPR_MID
- EXPR_NONE
- EXPR_VALUE
- PARSER_EVENTS
-
This array contains name of parser events.
- PARSER_EVENT_TABLE
- SCANNER_EVENTS
-
This array contains name of scanner events.
- SCANNER_EVENT_TABLE
- Version
-
version of
Ripper
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 971
def self.dedent_string: (String input, int width) -> Integer
USE OF RIPPER LIBRARY ONLY.
Strips up to width leading whitespaces from input, and returns the stripped column width.
(String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) → Array[[ [ Integer, Integer ], Symbol, String, Lexer::State ]]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 998
def self.lex: (String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) -> Array[[ [ Integer, Integer ], Symbol, String, Lexer::State ]]
Tokenizes the Ruby program and returns an array of an array, which is formatted like [[lineno, column], type, token, state]. The filename argument is mostly ignored. By default, this method does not handle syntax errors in src, use the raise_errors keyword to raise a SyntaxError for an error in src.
require 'ripper'
require 'pp'
pp Ripper.lex("def m(a) nil end")
#=> [[[1, 0], :on_kw, "def", FNAME ],
[[1, 3], :on_sp, " ", FNAME ],
[[1, 4], :on_ident, "m", ENDFN ],
[[1, 5], :on_lparen, "(", BEG|LABEL],
[[1, 6], :on_ident, "a", ARG ],
[[1, 7], :on_rparen, ")", ENDFN ],
[[1, 8], :on_sp, " ", BEG ],
[[1, 9], :on_kw, "nil", END ],
[[1, 12], :on_sp, " ", END ],
[[1, 13], :on_kw, "end", END ]]
(int) → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1006
def self.lex_state_name: (int) -> String
Returns a string representation of lex_state.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1114
def initialize: (File | _Gets | String src, ?String filename, ?Integer lineno) -> void
Create a new Ripper object. src must be a String, an IO, or an Object which has #gets method.
This method does not starts parsing. See also Ripper#parse and Ripper.parse.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1015
def self.parse: (File | _Gets | String src, ?String filename, ?Integer lineno) -> nil
(File | _Gets | String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) → Array[untyped]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1038
def self.sexp: (File | _Gets | String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) -> Array[untyped]
- EXPERIMENTAL
-
Parses
srcand create S-exp tree. Returns more readable tree rather thanRipper.sexp_raw. This method is mainly for developer use. Thefilenameargument is mostly ignored. By default, this method does not handle syntax errors insrc, returningnilin such cases. Use theraise_errorskeyword to raise aSyntaxErrorfor an error insrc.
require 'ripper'
require 'pp'
pp Ripper.sexp("def m(a) nil end")
#=> [:program,
[[:def,
[:@ident, "m", [1, 4]],
[:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil, nil, nil, nil]],
[:bodystmt, [[:var_ref, [:@kw, "nil", [1, 9]]]], nil, nil, nil]]]]
(File | _Gets | String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) → Array[untyped]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1066
def self.sexp_raw: (File | _Gets | String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) -> Array[untyped]
- EXPERIMENTAL
-
Parses
srcand create S-exp tree. This method is mainly for developer use. Thefilenameargument is mostly ignored. By default, this method does not handle syntax errors insrc, returningnilin such cases. Use theraise_errorskeyword to raise aSyntaxErrorfor an error insrc.
require 'ripper'
require 'pp'
pp Ripper.sexp_raw("def m(a) nil end")
#=> [:program,
[:stmts_add,
[:stmts_new],
[:def,
[:@ident, "m", [1, 4]],
[:paren, [:params, [[:@ident, "a", [1, 6]]], nil, nil, nil]],
[:bodystmt,
[:stmts_add, [:stmts_new], [:var_ref, [:@kw, "nil", [1, 9]]]],
nil,
nil,
nil]]]]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1084
def self.slice: (String src, String pattern, ?Integer n) -> String?
- EXPERIMENTAL
-
Parses
srcand return a string which was matched topattern.patternshould be described asRegexp.
require 'ripper' p Ripper.slice('def m(a) nil end', 'ident') #=> "m" p Ripper.slice('def m(a) nil end', '[ident lparen rparen]+') #=> "m(a)" p Ripper.slice("<<EOS\nstring\nEOS", 'heredoc_beg nl $(tstring_content*) heredoc_end', 1) #=> "string\n"
(String src, String pattern) → Ripper::TokenPattern::MatchData?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1086
def self.token_match: (String src, String pattern) -> Ripper::TokenPattern::MatchData?
(File | _Gets | String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) → Array[String]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1101
def self.tokenize: (File | _Gets | String src, ?String filename, ?Integer lineno, ?raise_errors: boolish) -> Array[String]
Tokenizes the Ruby program and returns an array of strings. The filename and lineno arguments are mostly ignored, since the return value is just the tokenized input. By default, this method does not handle syntax errors in src, use the raise_errors keyword to raise a SyntaxError for an error in src.
p Ripper.tokenize("def m(a) nil end") # => ["def", " ", "m", "(", "a", ")", " ", "nil", " ", "end"]
Public Instance Methods
() → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1602
def _dispatch_0: () -> void
(untyped a) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1604
def _dispatch_1: (untyped a) -> void
(untyped a, untyped b) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1606
def _dispatch_2: (untyped a, untyped b) -> void
(untyped a, untyped b, untyped c) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1608
def _dispatch_3: (untyped a, untyped b, untyped c) -> void
(untyped a, untyped b, untyped c, untyped d) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1610
def _dispatch_4: (untyped a, untyped b, untyped c, untyped d) -> void
(untyped a, untyped b, untyped c, untyped d, untyped e) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1612
def _dispatch_5: (untyped a, untyped b, untyped c, untyped d, untyped e) -> void
(untyped a, untyped b, untyped c, untyped d, untyped e, untyped f) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1614
def _dispatch_6: (untyped a, untyped b, untyped c, untyped d, untyped e, untyped f) -> void
(untyped a, untyped b, untyped c, untyped d, untyped e, untyped f, untyped g) → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1616
def _dispatch_7: (untyped a, untyped b, untyped c, untyped d, untyped e, untyped f, untyped g) -> void
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1124
def column: () -> Integer?
Return column number of current parsing line. This number starts from 0.
(untyped msg) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1624
def compile_error: (untyped msg) -> untyped
This method is called when the parser found syntax error.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1132
def debug_output: () -> untyped
Get debug output.
(untyped) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1140
def debug_output=: (untyped) -> untyped
Set debug output.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1635
def dedent_string: (String input, Integer width) -> Integer
USE OF RIPPER LIBRARY ONLY.
Strips up to width leading whitespaces from input, and returns the stripped column width.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1148
def encoding: () -> Encoding
Return encoding of the source.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1156
def end_seen?: () -> bool
Return true if parsed source ended by +END+.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1164
def error?: () -> bool
Return true if parsed source has errors.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1172
def filename: () -> String
Return current parsing filename.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1180
def lineno: () -> Integer?
Return line number of current parsing line. This number starts from 1.
(untyped a, untyped b, untyped c, untyped d, untyped e, untyped f, untyped g) → void
() → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1566
def parse: () -> nil
Start parsing and returns the value of the root action.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1574
def state: () -> Integer?
Return scanner state of current token.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1582
def token: () -> String?
Return the current token string.
(untyped fmt, *untyped args) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1644
def warn: (untyped fmt, *untyped args) -> untyped
This method is called when weak warning is produced by the parser. fmt and args is printf style.
(untyped fmt, *untyped args) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1653
def warning: (untyped fmt, *untyped args) -> untyped
This method is called when strong warning is produced by the parser. fmt and args is printf style.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.2.0/stdlib/ripper/0/ripper.rbs, line 1590
def yydebug: () -> bool
Get yydebug.