Module: Fdlint::Parser::ParserVisitable
- Included in:
- CSS::CssParser, HTML::HtmlParser, JS::JsParser
- Defined in:
- lib/fdlint/parser/parser_visitable.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#add_visitor(scope, visitor) ⇒ Object
Public: Add visitor hook.
-
#add_visitors(visitors) ⇒ Object
Public: Add visitor hooks.
- #parse_no_throw ⇒ Object
- #results ⇒ Object
- #visitors ⇒ Object
Class Method Details
.included(klass) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 21 def self.included(klass) klass.public_instance_methods(true).grep(/^parse_/) do |method| wrap(klass, method) end def klass.method_added(method) unless @flag @flag = true case method.to_s when /^parse_/ ParserVisitable.wrap(self, method) end @flag = false end end end |
.wrap(klass, method) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 38 def self.wrap(klass, method) method = method.to_s 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 node = self.send(old_method, *args, &block) node && visit(name, node) node end end end |
Instance Method Details
#add_visitor(scope, visitor) ⇒ Object
Public: Add visitor hook
target - The target to watch. For example, when
target is 'selector', then the visitor
will be invoked after <code>parse_select
</code> invoked by parser.
visitor - The handler proc. It will be yielded
with these arguments:
* node: current node scope, same as
`target` here
* source: the entire file source
Returns the visitors for the scope
102 103 104 105 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 102 def add_visitor(scope, visitor) cache = visitors[scope.to_s] ||= [] cache << visitor end |
#add_visitors(visitors) ⇒ Object
Public: Add visitor hooks
visitors - The visitors array. The members in the
Array must be presented in this format:
`[ scope, proc ]`
where scope is the name of target node
scope; proc is the handler.
Returns Parser’s visitors
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 76 def add_visitors(visitors) if visitors.is_a? Array visitors.each do |target, visitor| add_visitor target, visitor end else visitors.public_methods(false).grep(/^visit_/).map do |method| add_visitor method.to_s.sub( /^visit_/, '' ), visitors.method( method ) end end self.visitors end |
#parse_no_throw ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 57 def parse_no_throw root = [] begin root = self.parse rescue ParseError => e results << LogEntry.new(e.to_s, :fatal, e.position.row, e.position.column) end root end |
#results ⇒ Object
111 112 113 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 111 def results @results ||= [] end |
#visitors ⇒ Object
107 108 109 |
# File 'lib/fdlint/parser/parser_visitable.rb', line 107 def visitors @visitors ||= {} end |