Class: Ripper

Inherits:
Object show all
Defined in:
lib/langscan/ruby/compat/ripper/core.rb,
lib/langscan/ruby/compat/ripper/sexp.rb,
lib/langscan/ruby/compat/ripper/lexer.rb,
lib/langscan/ruby/compat/ripper/filter.rb,
ext/langscan/ruby/compat/ripper/ripper.c

Defined Under Namespace

Classes: Filter, Lexer, SexpBuilder, SexpBuilderPP, TokenPattern

Constant Summary collapse

PARSER_EVENT_TABLE =

This table contains name of parser events and its arity.

{
  :BEGIN => 1,
  :END => 1,
  :alias => 2,
  :alias_error => 1,
  :aref => 2,
  :aref_field => 2,
  :arg_ambiguous => 0,
  :arg_paren => 1,
  :arglist_add => 2,
  :arglist_add_block => 2,
  :arglist_add_star => 2,
  :arglist_new => 0,
  :arglist_prepend => 2,
  :array => 1,
  :assign => 2,
  :assign_error => 1,
  :assoc_new => 2,
  :assoclist_from_args => 1,
  :bare_assoc_hash => 1,
  :begin => 1,
  :binary => 3,
  :blockvar_add_block => 2,
  :blockvar_add_star => 2,
  :blockvar_new => 1,
  :bodystmt => 4,
  :brace_block => 2,
  :break => 1,
  :call => 3,
  :case => 2,
  :class => 3,
  :class_name_error => 1,
  :command => 2,
  :command_call => 4,
  :const_ref => 1,
  :constpath_field => 2,
  :constpath_ref => 2,
  :def => 3,
  :defined => 1,
  :defs => 5,
  :do_block => 1,
  :do_block => 2,
  :dot2 => 2,
  :dot3 => 2,
  :dyna_symbol => 1,
  :else => 1,
  :elsif => 3,
  :ensure => 1,
  :fcall => 1,
  :field => 3,
  :for => 3,
  :hash => 1,
  :if => 3,
  :if_mod => 2,
  :ifop => 3,
  :iter_block => 2,
  :lambda => 2,
  :massign => 2,
  :method_add_arg => 2,
  :mlhs_add => 2,
  :mlhs_add_star => 2,
  :mlhs_new => 0,
  :mlhs_paren => 1,
  :module => 2,
  :mrhs_add => 2,
  :mrhs_add_star => 2,
  :mrhs_new => 0,
  :mrhs_new_from_arglist => 1,
  :next => 1,
  :opassign => 3,
  :param_error => 1,
  :params => 4,
  :paren => 1,
  :parse_error => 1,
  :pragma => 2,
  :program => 1,
  :qwords_add => 2,
  :qwords_new => 0,
  :redo => 0,
  :regexp_literal => 2,
  :rescue => 4,
  :rescue_mod => 2,
  :restparam => 1,
  :retry => 0,
  :return => 1,
  :return0 => 0,
  :sclass => 2,
  :space => 1,
  :stmts_add => 2,
  :stmts_new => 0,
  :string_add => 2,
  :string_concat => 2,
  :string_content => 0,
  :string_dvar => 1,
  :string_embexpr => 1,
  :string_literal => 1,
  :super => 1,
  :symbol => 1,
  :symbol_literal => 1,
  :topconst_field => 1,
  :topconst_ref => 1,
  :unary => 2,
  :undef => 1,
  :unless => 3,
  :unless_mod => 2,
  :until => 2,
  :until_mod => 2,
  :var_alias => 2,
  :var_field => 1,
  :var_ref => 1,
  :void_stmt => 0,
  :when => 3,
  :while => 2,
  :while_mod => 2,
  :word_add => 2,
  :word_new => 0,
  :words_add => 2,
  :words_new => 0,
  :xstring_add => 2,
  :xstring_literal => 1,
  :xstring_new => 0,
  :yield => 1,
  :yield0 => 0,
  :zsuper => 0
}
PARSER_EVENTS =

This array contains name of parser events.

PARSER_EVENT_TABLE.keys
SCANNER_EVENT_TABLE =

This table contains name of scanner events and its arity (arity is always 1 for all scanner events).

{
  :CHAR => 1,
  :__end__ => 1,
  :backref => 1,
  :backtick => 1,
  :comma => 1,
  :comment => 1,
  :const => 1,
  :cvar => 1,
  :embdoc => 1,
  :embdoc_beg => 1,
  :embdoc_end => 1,
  :embexpr_beg => 1,
  :embexpr_end => 1,
  :embvar => 1,
  :float => 1,
  :gvar => 1,
  :heredoc_beg => 1,
  :heredoc_end => 1,
  :ident => 1,
  :ignored_nl => 1,
  :int => 1,
  :ivar => 1,
  :kw => 1,
  :label => 1,
  :lambda => 1,
  :lambda_arg => 1,
  :lambeg => 1,
  :lbrace => 1,
  :lbracket => 1,
  :lparen => 1,
  :nl => 1,
  :op => 1,
  :period => 1,
  :qwords_beg => 1,
  :rbrace => 1,
  :rbracket => 1,
  :regexp_beg => 1,
  :regexp_end => 1,
  :rparen => 1,
  :semicolon => 1,
  :sp => 1,
  :symbeg => 1,
  :tstring_beg => 1,
  :tstring_content => 1,
  :tstring_end => 1,
  :words_beg => 1,
  :words_sep => 1
}
SCANNER_EVENTS =

This array contains name of scanner events.

SCANNER_EVENT_TABLE.keys
EVENTS =

This table contains name of all ripper events.

PARSER_EVENTS + SCANNER_EVENTS
Version =
rb_str_new2(RIPPER_VERSION)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(src, filename = "(ripper)", lineno = 1) ⇒ Object

Create a new Ripper object. src must be a String, a IO, or an Object which has #gets method.

This method does not starts parsing. See also Ripper#parse and Ripper.parse.



14205
14206
14207
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 14205

static VALUE
ripper_initialize(argc, argv, self)
int argc;

Class Method Details

.lex(src, filename = '-', lineno = 1) ⇒ Object

Tokenizes Ruby program and returns an Array of Array, which is formatted like [[lineno, column], type, token].

require 'ripper'
require 'pp'

p Ripper.scan("def m(a) nil end")
  #=> [[[1,  0], :on_kw,     "def"],
       [[1,  3], :on_sp,     " "  ],
       [[1,  4], :on_ident,  "m"  ],
       [[1,  5], :on_lparen, "("  ],
       [[1,  6], :on_ident,  "a"  ],
       [[1,  7], :on_rparen, ")"  ],
       [[1,  8], :on_sp,     " "  ],
       [[1,  9], :on_kw,     "nil"],
       [[1, 12], :on_sp,     " "  ],
       [[1, 13], :on_kw,     "end"]]


38
39
40
# File 'lib/langscan/ruby/compat/ripper/lexer.rb', line 38

def Ripper.lex(src, filename = '-', lineno = 1)
  Lexer.new(src, filename, lineno).lex
end

.parse(src, filename = '(ripper)', lineno = 1) ⇒ Object

Parses Ruby program read from src. src must be a String or a IO or a object which has #gets method.



19
20
21
# File 'lib/langscan/ruby/compat/ripper/core.rb', line 19

def Ripper.parse(src, filename = '(ripper)', lineno = 1)
  new(src, filename, lineno).parse
end

.sexp(src, filename = '-', lineno = 1) ⇒ Object

EXPERIMENTAL

Parses src and create S-exp tree. This method is for mainly developper use.

require 'ripper'
require 'pp

pp Ripper.sexp("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]]]]


35
36
37
# File 'lib/langscan/ruby/compat/ripper/sexp.rb', line 35

def Ripper.sexp(src, filename = '-', lineno = 1)
  SexpBuilderPP.new(src, filename, lineno).parse
end

.sexp_raw(src, filename = '-', lineno = 1) ⇒ Object



39
40
41
# File 'lib/langscan/ruby/compat/ripper/sexp.rb', line 39

def Ripper.sexp_raw(src, filename = '-', lineno = 1)
  SexpBuilder.new(src, filename, lineno).parse
end

.slice(src, pattern, n = 0) ⇒ Object

EXPERIMENTAL

Parses src and return a string which was matched to pattern. pattern should be described as Regexp.

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"


80
81
82
83
84
85
# File 'lib/langscan/ruby/compat/ripper/lexer.rb', line 80

def Ripper.slice(src, pattern, n = 0)
  if m = token_match(src, pattern)
  then m.string(n)
  else nil
  end
end

.token_match(src, pattern) ⇒ Object

:nodoc:



87
88
89
# File 'lib/langscan/ruby/compat/ripper/lexer.rb', line 87

def Ripper.token_match(src, pattern)   #:nodoc:
  TokenPattern.compile(pattern).match(src)
end

.tokenize(src, filename = '-', lineno = 1) ⇒ Object

Tokenizes Ruby program and returns an Array of String.



16
17
18
# File 'lib/langscan/ruby/compat/ripper/lexer.rb', line 16

def Ripper.tokenize(src, filename = '-', lineno = 1)
  Lexer.new(src, filename, lineno).tokenize
end

.yydebugBoolean

Get yydebug.

Returns:

  • (Boolean)


14244
14245
14246
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 14244

static VALUE
ripper_s_get_yydebug(self)
VALUE self;

.yydebug=(flag) ⇒ Object

Set yydebug.



14257
14258
14259
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 14257

static VALUE
ripper_s_set_yydebug(self, flag)
VALUE self, flag;

Instance Method Details

#columnInteger

Return column number of current parsing line. This number starts from 0.

Returns:

  • (Integer)


14331
14332
14333
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 14331

static VALUE
ripper_column(self)
VALUE self;

#end_seen?Boolean

Return if parsed source ended by _END_. This number starts from 1.

Returns:

  • (Boolean)


13863
13864
13865
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 13863

VALUE
rb_parser_end_seen_p(vparser)
VALUE vparser;

#linenoInteger

Return line number of current parsing line. This number starts from 1.

Returns:

  • (Integer)


14354
14355
14356
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 14354

static VALUE
ripper_lineno(self)
VALUE self;

#parseObject

Start parsing and returns the value of the root action.



14302
14303
14304
# File 'ext/langscan/ruby/compat/ripper/ripper.c', line 14302

static VALUE
ripper_parse(self)
VALUE self;