| 1 |
# frozen_string_literal: true |
| 2 |
require 'ripper/core' |
| 3 |
require 'ripper/lexer' |
| 4 |
require 'ripper/filter' |
| 5 |
require 'ripper/sexp' |
| 6 |
|
| 7 |
# Ripper is a Ruby script parser. |
| 8 |
# |
| 9 |
# You can get information from the parser with event-based style. |
| 10 |
# Information such as abstract syntax trees or simple lexical analysis of the |
| 11 |
# Ruby program. |
| 12 |
# |
| 13 |
# == Usage |
| 14 |
# |
| 15 |
# Ripper provides an easy interface for parsing your program into a symbolic |
| 16 |
# expression tree (or S-expression). |
| 17 |
# |
| 18 |
# Understanding the output of the parser may come as a challenge, it's |
| 19 |
# recommended you use PP to format the output for legibility. |
| 20 |
# |
| 21 |
# require 'ripper' |
| 22 |
# require 'pp' |
| 23 |
# |
| 24 |
# pp Ripper.sexp('def hello(world) "Hello, #{world}!"; end') |
| 25 |
# #=> [:program, |
| 26 |
# [[:def, |
| 27 |
# [:@ident, "hello", [1, 4]], |
| 28 |
# [:paren, |
| 29 |
# [:params, [[:@ident, "world", [1, 10]]], nil, nil, nil, nil, nil, nil]], |
| 30 |
# [:bodystmt, |
| 31 |
# [[:string_literal, |
| 32 |
# [:string_content, |
| 33 |
# [:@tstring_content, "Hello, ", [1, 18]], |
| 34 |
# [:string_embexpr, [[:var_ref, [:@ident, "world", [1, 27]]]]], |
| 35 |
# [:@tstring_content, "!", [1, 33]]]]], |
| 36 |
# nil, |
| 37 |
# nil, |
| 38 |
# nil]]]] |
| 39 |
# |
| 40 |
# You can see in the example above, the expression starts with +:program+. |
| 41 |
# |
| 42 |
# From here, a method definition at +:def+, followed by the method's identifier |
| 43 |
# <code>:@ident</code>. After the method's identifier comes the parentheses |
| 44 |
# +:paren+ and the method parameters under +:params+. |
| 45 |
# |
| 46 |
# Next is the method body, starting at +:bodystmt+ (+stmt+ meaning statement), |
| 47 |
# which contains the full definition of the method. |
| 48 |
# |
| 49 |
# In our case, we're simply returning a String, so next we have the |
| 50 |
# +:string_literal+ expression. |
| 51 |
# |
| 52 |
# Within our +:string_literal+ you'll notice two <code>@tstring_content</code>, |
| 53 |
# this is the literal part for <code>Hello, </code> and <code>!</code>. Between |
| 54 |
# the two <code>@tstring_content</code> statements is a +:string_embexpr+, |
| 55 |
# where _embexpr_ is an embedded expression. Our expression consists of a local |
| 56 |
# variable, or +var_ref+, with the identifier (<code>@ident</code>) of +world+. |
| 57 |
# |
| 58 |
# == Resources |
| 59 |
# |
| 60 |
# * {Ruby Inside}[http://www.rubyinside.com/using-ripper-to-see-how-ruby-is-parsing-your-code-5270.html] |
| 61 |
# |
| 62 |
# == Requirements |
| 63 |
# |
| 64 |
# * ruby 1.9 (support CVS HEAD only) |
| 65 |
# * bison 1.28 or later (Other yaccs do not work) |
| 66 |
# |
| 67 |
# == License |
| 68 |
# |
| 69 |
# Ruby License. |
| 70 |
# |
| 71 |
# - Minero Aoki |
| 72 |
# - [email protected] |
| 73 |
# - http://i.loveruby.net |
| 74 |
class Ripper; end |