Class: Sexp::Sibling

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

Overview

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

Constant Summary

Constants inherited from Sexp

UNASSIGNED

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.



1323
1324
1325
1326
1327
# File 'lib/sexp.rb', line 1323

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.



1317
1318
1319
# File 'lib/sexp.rb', line 1317

def distance
  @distance
end

#siblingObject (readonly)

The RHS of the matcher.



1312
1313
1314
# File 'lib/sexp.rb', line 1312

def sibling
  @sibling
end

#subjectObject (readonly)

The LHS of the matcher.



1307
1308
1309
# File 'lib/sexp.rb', line 1307

def subject
  @subject
end

Instance Method Details

#==(o) ⇒ Object

:nodoc:



1348
1349
1350
1351
1352
1353
# File 'lib/sexp.rb', line 1348

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

#inspectObject

:nodoc:



1355
1356
1357
# File 'lib/sexp.rb', line 1355

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

#pretty_print(q) ⇒ Object

:nodoc:



1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
# File 'lib/sexp.rb', line 1359

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)


1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/sexp.rb', line 1332

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