Class: SlimLint::Sexp

Inherits:
Array
  • Object
show all
Defined in:
lib/slim_lint/sexp.rb

Overview

Symbolic expression which represents tree-structured data.

The main use of this particular implementation is to provide a single location for defining convenience helpers when operating on Sexps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*array_sexp, start:, finish:) ⇒ Sexp

Creates an SlimLint::Sexp from the given Array-based Sexp.

This provides a convenient way to convert between literal arrays of Symbols and SlimLint::Sexps containing Atoms and nested SlimLint::Sexps. These objects all expose a similar API that conveniently allows the two objects to be treated similarly due to duck typing.

Parameters:

  • array_sexp (Array)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slim_lint/sexp.rb', line 29

def initialize(*array_sexp, start:, finish:)
  @start = start
  @finish = finish
  array_sexp.each do |atom_or_sexp|
    case atom_or_sexp
    when Sexp, Atom
      push atom_or_sexp
    when Array
      push Sexp.new(*atom_or_sexp, start: start, finish: finish)
    else
      push SlimLint::Atom.new(atom_or_sexp, pos: start)
    end
  end
end

Instance Attribute Details

#finishObject

Stores the line number of the code in the original document that corresponds to this Sexp.



11
12
13
# File 'lib/slim_lint/sexp.rb', line 11

def finish
  @finish
end

#startObject

Stores the line number of the code in the original document that corresponds to this Sexp.



11
12
13
# File 'lib/slim_lint/sexp.rb', line 11

def start
  @start
end

Instance Method Details

#columnObject



17
18
19
# File 'lib/slim_lint/sexp.rb', line 17

def column
  start[1] if start
end

#inspectString

Returns pretty-printed representation of this S-expression.

Returns:

  • (String)


96
97
98
# File 'lib/slim_lint/sexp.rb', line 96

def inspect
  display
end

#lineObject



13
14
15
# File 'lib/slim_lint/sexp.rb', line 13

def line
  start[0] if start
end

#locationObject



44
45
46
47
48
49
50
51
# File 'lib/slim_lint/sexp.rb', line 44

def location
  SourceLocation.new(
    start_line: start[0],
    start_column: start[1],
    last_line: (finish || start)[0],
    last_column: (finish || start)[1]
  )
end

#match?(sexp_pattern) ⇒ Boolean

Returns whether this SlimLint::Sexp matches the given Sexp pattern.

A Sexp pattern is simply an incomplete Sexp prefix.

Note that nested Sexps will also be matched, so be careful about the cost of matching against a complicated pattern.

Examples:

The following Sexp:

  [:html, :doctype, "html5"]

...will match the given patterns:

  [:html]
  [:html, :doctype]
  [:html, :doctype, "html5"]

Parameters:

  • sexp_pattern (Object, Array)

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/slim_lint/sexp.rb', line 73

def match?(sexp_pattern)
  # Delegate matching logic if we're comparing against a matcher
  if sexp_pattern.is_a?(SlimLint::Matcher::Base)
    return sexp_pattern.match?(self)
  end

  # If there aren't enough items to compare then this obviously won't match
  return false unless sexp_pattern.is_a?(Array) && length >= sexp_pattern.length

  sexp_pattern.each_with_index do |sub_pattern, index|
    return false unless self[index].match?(sub_pattern)
  end

  true
end

#to_arrayObject



89
90
91
# File 'lib/slim_lint/sexp.rb', line 89

def to_array
  map(&:to_array)
end