Module: XRay::ParserVisitable

Includes:
Observable
Included in:
CSS::VisitableParser, HTML::VisitableParser, JS::VisitableParser
Defined in:
lib/parser_visitable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/parser_visitable.rb', line 23

def self.included(klass)
  klass.public_instance_methods(true).each do |method|
    wrap(klass, method)
  end

  def klass.method_added(method)
    unless @flag
      @flag = true
      ParserVisitable.wrap(self, method)
      @flag = false
    end
  end
end

.wrap(klass, method) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/parser_visitable.rb', line 37

def self.wrap(klass, method)
  method = method.to_s
  unless method.index 'parse_'
    return
  end

  klass.instance_eval do
    old_method = "#{method}_without_visit"
    name = method.sub /^parse_/, ''
    alias_method old_method, method

    define_method(method) do |*args, &block|
      before name, *args
      node = self.send(old_method, *args, &block)
      node && visit(name, node)
      node
    end
  end
end

Instance Method Details

#add_visitor(visitor) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/parser_visitable.rb', line 68

def add_visitor(visitor)
  @visitors ||= {}
  
  visitor.class.public_instance_methods(true).each do |method|
    method = method.to_s
    if method.index('visit_') == 0 || 
        method.index('before_parse_') == 0
      cache = @visitors[method] ||= []
      cache << visitor
    end 
  end
end

#add_visitors(visitors) ⇒ Object



81
82
83
# File 'lib/parser_visitable.rb', line 81

def add_visitors(visitors)
  visitors.each { |visitor| add_visitor visitor }
end

#parse_no_throwObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/parser_visitable.rb', line 57

def parse_no_throw
  root = []
  begin
    root = self.parse
  rescue ParseError => e
    @results ||= []
    @results << LogEntry.new(e.to_s, :fatal, e.position.row, e.position.column)
  end
  root
end

#resultsObject



85
86
87
# File 'lib/parser_visitable.rb', line 85

def results
  @results || []
end