Class: LangScan::Ruby::Parser

Inherits:
Ripper show all
Defined in:
lib/langscan/ruby.rb

Constant Summary collapse

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

Constants inherited from Ripper

Ripper::EVENTS, Ripper::PARSER_EVENTS, Ripper::PARSER_EVENT_TABLE, Ripper::SCANNER_EVENTS, Ripper::SCANNER_EVENT_TABLE, Ripper::Version

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Ripper

#column, #end_seen?, lex, #lineno, parse, sexp, sexp_raw, slice, token_match, tokenize, yydebug, yydebug=

Constructor Details

#initialize(src) ⇒ Parser

Returns a new instance of Parser.



42
43
44
45
46
# File 'lib/langscan/ruby.rb', line 42

def initialize(src)
  super
  @fragments = {}
  @found = {}
end

Instance Attribute Details

#fragmentsObject (readonly)

Returns the value of attribute fragments.



47
48
49
# File 'lib/langscan/ruby.rb', line 47

def fragments
  @fragments
end

Instance Method Details

#on_call(recv, _, meth) ⇒ Object



128
129
130
131
# File 'lib/langscan/ruby.rb', line 128

def on_call(recv, _, meth)
  name, key = meth
  @found[key] = :funcall
end

#on_class(*args) ⇒ Object



104
105
106
107
108
# File 'lib/langscan/ruby.rb', line 104

def on_class(*args)
  name_key, * = args
  name, key = name_key
  @found[key] = :classdef
end

#on_def(*args) ⇒ Object



116
117
118
119
120
# File 'lib/langscan/ruby.rb', line 116

def on_def(*args)
  name_key, * = args
  name, key = name_key
  @found[key] = :fundef
end

#on_defs(*args) ⇒ Object



122
123
124
125
126
# File 'lib/langscan/ruby.rb', line 122

def on_defs(*args)
  obj, _, name_key, * = args
  name, key = name_key
  @found[key] = :fundef
end

#on_module(*args) ⇒ Object



110
111
112
113
114
# File 'lib/langscan/ruby.rb', line 110

def on_module(*args)
  name_key, * = args
  name, key = name_key
  @found[key] = :moduledef
end

#on_symbol_literal(*args) ⇒ Object



133
134
135
136
137
# File 'lib/langscan/ruby.rb', line 133

def on_symbol_literal(*args)
  name_key, * = args
  name, key = name_key
  @found[key] = :symbol
end

#parseObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/langscan/ruby.rb', line 139

def parse
  super
  byteno = 0
  @fragments.keys.sort.each {|key|
    type, text = @fragments[key]
    len = text.length
    l1, c1 = key
    if /\n/ =~ text
      l2 = l1+text.count("\n")
    else
      l2 = l1
    end
    byteno2 = byteno+len
    fragment = Fragment.new(type, text, l1, byteno)
    if type = @found[key]
      fragment.type = type
    end
    yield fragment
    byteno = byteno2
  }
end