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

Instance Method Summary collapse

Instance Attribute Details

#capturesHash (readonly)

Returns map of capture names to captured values.

Returns:

  • (Hash)

    map of capture names to captured values



90
91
92
# File 'lib/slim_lint/sexp_visitor.rb', line 90

def captures
  @captures
end

#patternsObject (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

#anythingSlimLint::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.

Parameters:

Returns:



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.

Parameters:

  • sexp_pattern (Sexp)

Yields:

  • block to execute when the specified pattern is matched

Yield Parameters:

Yield Returns:

  • (SlimLint::Sexp, Symbol, void)

    If a Sexp is returned, indicates that traversal should jump directly to that Sexp. If ‘:stop` is returned, halts further traversal down this branch (i.e. stops recursing, but traversal at higher levels will continue). Otherwise traversal will continue as normal.



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.

Yields:

  • block to execute

Yield Parameters:

Yield Returns:

  • (Symbol)

    if ‘:stop`, indicates that no further processing should occur



125
126
127
# File 'lib/slim_lint/sexp_visitor.rb', line 125

def on_start(&block)
  define_method(:on_start, block)
end