Class: Sexp::Sibling

Inherits:
Matcher show all
Defined in:
lib/sexp.rb

Overview

See Matcher for sibling relations: <,<<,>>,>

Instance Attribute Summary collapse

Attributes inherited from Sexp

#comments, #file, #line

Instance Method Summary collapse

Methods inherited from Matcher

#&, #-@, #/, #=~, #>>, #greedy?, match_subs=, match_subs?, parse, #|

Methods inherited from Sexp

#/, #=~, _, ___, all, any, #array_type?, atom, child, #compact, #deep_each, #depth, #each_of_type, #each_sexp, #find_and_replace_all, #find_node, #find_nodes, from_array, #gsub, include, #line_max, m, #map, #mass, #method_missing, #new, not?, #replace_sexp, #respond_to?, s, #search_each, #sexp_body, #sexp_body=, #sexp_type, #sexp_type=, #shift, #structure, #sub, t, #to_a

Constructor Details

#initialize(subject, sibling, distance = nil) ⇒ Sibling

Creates a Matcher which will match any pair of Sexps that are siblings. Defaults to matching the immediate following sibling.



1320
1321
1322
1323
1324
# File 'lib/sexp.rb', line 1320

def initialize subject, sibling, distance = nil
  @subject = subject
  @sibling = sibling
  @distance = distance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Sexp

Instance Attribute Details

#distanceObject (readonly)

An optional distance requirement for the matcher.



1314
1315
1316
# File 'lib/sexp.rb', line 1314

def distance
  @distance
end

#siblingObject (readonly)

The RHS of the matcher.



1309
1310
1311
# File 'lib/sexp.rb', line 1309

def sibling
  @sibling
end

#subjectObject (readonly)

The LHS of the matcher.



1304
1305
1306
# File 'lib/sexp.rb', line 1304

def subject
  @subject
end

Instance Method Details

#==(o) ⇒ Object

:nodoc:



1345
1346
1347
1348
1349
1350
# File 'lib/sexp.rb', line 1345

def == o # :nodoc:
  super &&
    self.subject  == o.subject &&
    self.sibling  == o.sibling &&
    self.distance == o.distance
end

#inspectObject

:nodoc:



1352
1353
1354
# File 'lib/sexp.rb', line 1352

def inspect # :nodoc:
  "%p >> %p" % [subject, sibling]
end

#pretty_print(q) ⇒ Object

:nodoc:



1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
# File 'lib/sexp.rb', line 1356

def pretty_print q # :nodoc:
  if distance then
    q.group 1, "sibling(", ")" do
      q.seplist [subject, sibling, distance] do |v|
        q.pp v
      end
    end
  else
    q.group 1 do
      q.pp subject
      q.text " >> "
      q.pp sibling
    end
  end
end

#satisfy?(o) ⇒ Boolean

Satisfied if o contains subject followed by sibling

Returns:

  • (Boolean)


1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
# File 'lib/sexp.rb', line 1329

def satisfy? o
  # Future optimizations:
  # * Shortcut matching sibling
  subject_matches = index_matches(subject, o)
  return nil if subject_matches.empty?

  sibling_matches = index_matches(sibling, o)
  return nil if sibling_matches.empty?

  subject_matches.any? { |i1, _data_1|
    sibling_matches.any? { |i2, _data_2|
      distance ? (i2-i1 == distance) : i2 > i1
    }
  }
end