Class: SlimLint::Sexp
- Inherits:
-
Array
- Object
- Array
- SlimLint::Sexp
- 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
-
#finish ⇒ Object
Stores the line number of the code in the original document that corresponds to this Sexp.
-
#start ⇒ Object
Stores the line number of the code in the original document that corresponds to this Sexp.
Instance Method Summary collapse
- #column ⇒ Object
-
#initialize(*array_sexp, start:, finish:) ⇒ Sexp
constructor
Creates an Sexp from the given Array-based Sexp.
-
#inspect ⇒ String
Returns pretty-printed representation of this S-expression.
- #line ⇒ Object
- #location ⇒ Object
-
#match?(sexp_pattern) ⇒ Boolean
Returns whether this Sexp matches the given Sexp pattern.
- #to_array ⇒ Object
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.
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
#finish ⇒ Object
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 |
#start ⇒ Object
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
#column ⇒ Object
17 18 19 |
# File 'lib/slim_lint/sexp.rb', line 17 def column start[1] if start end |
#inspect ⇒ String
Returns pretty-printed representation of this S-expression.
96 97 98 |
# File 'lib/slim_lint/sexp.rb', line 96 def inspect display end |
#line ⇒ Object
13 14 15 |
# File 'lib/slim_lint/sexp.rb', line 13 def line start[0] if start end |
#location ⇒ Object
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.
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_array ⇒ Object
89 90 91 |
# File 'lib/slim_lint/sexp.rb', line 89 def to_array map(&:to_array) end |