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


86
87
88
# File 'lib/slim_lint/sexp_visitor.rb', line 86

def captures
  @captures
end

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

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

Parameters:

Returns:


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.

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.


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.

Yields:

  • block to execute

Yield Parameters:

Yield Returns:

  • (Symbol)

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


121
122
123
# File 'lib/slim_lint/sexp_visitor.rb', line 121

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