Class: HamlI18nLint::RubyParser

Inherits:
Ripper::SexpBuilderPP
  • Object
show all
Defined in:
lib/haml_i18n_lint/ruby_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ RubyParser

Returns a new instance of RubyParser.



12
13
14
15
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 12

def initialize(src, *)
  @src = src
  super
end

Class Method Details

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



6
7
8
9
10
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 6

def self.sexp(src, filename = '-', lineno = 1)
  builder = new(src, filename, lineno)
  sexp = builder.parse
  sexp unless builder.error?
end

Instance Method Details

#on_heredoc_beg(tok) ⇒ Object



23
24
25
26
27
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 23

def on_heredoc_beg(tok)
  @buf ||= []
  @buf << [[lineno, column]]
  super
end

#on_heredoc_end(tok) ⇒ Object



34
35
36
37
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 34

def on_heredoc_end(tok)
  @buf.last << [lineno, column + tok.size-1]
  super
end

#on_string_literal(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 39

def on_string_literal(*args)
  pos = @buf.pop
  lineno_pos = pos.map(&:first)
  column_pos = pos.map(&:last)
  lines = @src.lines[lineno_pos.first-1...lineno_pos.last]
  if lineno_pos.first == lineno_pos.last
    lines[0] = lines[0].byteslice(column_pos.first..column_pos.last).force_encoding(@src.encoding)
  else
    lines[0] = lines[0].dup.tap { |l| l.force_encoding(Encoding::BINARY)[0...column_pos.first] = ''; l.force_encoding(@src.encoding) }
    lines[-1] = lines[-1].dup.tap { |l| l.force_encoding(Encoding::BINARY)[column_pos.last+1..-1] = ''; l.force_encoding(@src.encoding) }
  end
  args.unshift(:string_literal, lines.join, pos)
  args
end

#on_tstring_beg(tok) ⇒ Object



17
18
19
20
21
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 17

def on_tstring_beg(tok)
  @buf ||= []
  @buf << [[lineno, column]]
  super
end

#on_tstring_end(tok) ⇒ Object



29
30
31
32
# File 'lib/haml_i18n_lint/ruby_parser.rb', line 29

def on_tstring_end(tok)
  @buf.last << [lineno, column]
  super
end