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.
86 87 88 |
# File 'lib/slim_lint/sexp_visitor.rb', line 86 def captures @captures end |
#patterns ⇒ Object (readonly)
Registered patterns that this visitor will look for when traversing the SlimLint::Sexp.
83 84 85 |
# File 'lib/slim_lint/sexp_visitor.rb', line 83 def patterns @patterns end |
Instance Method Details
#anything ⇒ SlimLint::Matcher::Anything
Represents a pattern that matches anything.
128 129 130 |
# File 'lib/slim_lint/sexp_visitor.rb', line 128 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.
138 139 140 141 142 143 |
# File 'lib/slim_lint/sexp_visitor.rb', line 138 def capture(capture_name, matcher) @captures ||= SlimLint::CaptureMap.new @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.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/slim_lint/sexp_visitor.rb', line 100 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.
121 122 123 |
# File 'lib/slim_lint/sexp_visitor.rb', line 121 def on_start(&block) define_method(:on_start, block) end |