Module: SlimLint::SexpVisitor::DSL
- Included in:
- Linter, RubyExtractor
- Defined in:
- lib/slim_lint/sexp_visitor.rb
Overview
Exposes a convenient Domain-specific Language (DSL) that makes declaring Sexp match patterns very easy.
Include them with ‘extend SlimLint::SexpVisitor::DSL`
Instance Attribute Summary collapse
-
#captures ⇒ Hash
readonly
Map of capture names to captured values.
-
#patterns ⇒ Object
readonly
Registered patterns that this visitor will look for when traversing the SlimLint::Sexp.
Instance Method Summary collapse
-
#anything ⇒ SlimLint::Matcher::Anything
Represents a pattern that matches anything.
-
#capture(capture_name, matcher) ⇒ SlimLint::Matcher::Capture
Represents a pattern that matches the specified matcher, storing the matched value in the captures list under the given name.
-
#on(sexp_pattern) {|sexp| ... } ⇒ Object
DSL helper that defines a sexp pattern and block that will be executed if the given pattern is found.
-
#on_start {|sexp| ... } ⇒ Object
Define a block of code to run before checking for any pattern matches.
Instance Attribute Details
#captures ⇒ Hash (readonly)
Returns map of capture names to captured values.
90 91 92 |
# File 'lib/slim_lint/sexp_visitor.rb', line 90 def captures @captures end |
#patterns ⇒ Object (readonly)
Registered patterns that this visitor will look for when traversing the SlimLint::Sexp.
87 88 89 |
# File 'lib/slim_lint/sexp_visitor.rb', line 87 def patterns @patterns end |
Instance Method Details
#anything ⇒ SlimLint::Matcher::Anything
Represents a pattern that matches anything.
132 133 134 |
# File 'lib/slim_lint/sexp_visitor.rb', line 132 def anything SlimLint::Matcher::Anything.new end |
#capture(capture_name, matcher) ⇒ SlimLint::Matcher::Capture
Represents a pattern that matches the specified matcher, storing the matched value in the captures list under the given name.
142 143 144 145 146 147 |
# File 'lib/slim_lint/sexp_visitor.rb', line 142 def capture(capture_name, matcher) @captures ||= SlimLint::CaptureMap.new matcher = SexpPattern.new(matcher, nil) unless matcher.respond_to?(:match?) @captures[capture_name] = SlimLint::Matcher::Capture.from_matcher(matcher) end |
#on(sexp_pattern) {|sexp| ... } ⇒ Object
DSL helper that defines a sexp pattern and block that will be executed if the given pattern is found.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/slim_lint/sexp_visitor.rb', line 104 def on(sexp_pattern, &block) # TODO: Index Sexps on creation so we can quickly jump to potential # matches instead of checking array. @patterns ||= [] @pattern_number ||= 1 # Use a monotonically increasing number to identify the method so that in # debugging we can simply look at the nth defintion in the class. unique_method_name = :"on_pattern_#{@pattern_number}" define_method(unique_method_name, block) @pattern_number += 1 @patterns << SexpPattern.new(sexp_pattern, unique_method_name) end |
#on_start {|sexp| ... } ⇒ Object
Define a block of code to run before checking for any pattern matches.
125 126 127 |
# File 'lib/slim_lint/sexp_visitor.rb', line 125 def on_start(&block) define_method(:on_start, block) end |