Class: Sexp::Pattern

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

Overview

Matches any atom who’s string representation matches the patterns passed in.

examples:

s(:a) / s{ m('a') }                                      #=> [s(:a)]
s(:a) / s{ m(/\w/,/\d/) }                                #=> [s(:a)]
s(:tests, s(s(:test_a), s(:test_b))) / s{ m(/test_\w/) } #=> [s(:test_a),

TODO: maybe don’t require non-sexps? This does respond to =~ now.

Direct Known Subclasses

Klass

Constant Summary

Constants inherited from Sexp

UNASSIGNED

Instance Attribute Summary collapse

Attributes inherited from Sexp

#comments, #file, #line, #line_max

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, k, m, #map, #mass, #method_missing, #new, not?, q, #replace_sexp, #respond_to?, s, #search_each, #sexp_body, #sexp_body=, #sexp_type, #sexp_type=, #shift, #structure, #sub, t, #to_a, #value

Constructor Details

#initialize(pattern) ⇒ Pattern

Create a Patten matcher which will match any atom that either matches the input pattern.



834
835
836
# File 'lib/sexp_matcher.rb', line 834

def initialize pattern
  @pattern = pattern
end

Dynamic Method Handling

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

Instance Attribute Details

#patternObject (readonly)

The regexp to match for the pattern.



824
825
826
# File 'lib/sexp_matcher.rb', line 824

def pattern
  @pattern
end

Instance Method Details

#==(o) ⇒ Object

:nodoc:



826
827
828
# File 'lib/sexp_matcher.rb', line 826

def == o # :nodoc:
  super && self.pattern == o.pattern
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


855
856
857
# File 'lib/sexp_matcher.rb', line 855

def eql? o
  super and self.pattern.eql? o.pattern
end

#hashObject



859
860
861
# File 'lib/sexp_matcher.rb', line 859

def hash
  [super, pattern].hash
end

#inspectObject

:nodoc:



845
846
847
# File 'lib/sexp_matcher.rb', line 845

def inspect # :nodoc:
  "m(%p)" % pattern
end

#pretty_print(q) ⇒ Object

:nodoc:



849
850
851
852
853
# File 'lib/sexp_matcher.rb', line 849

def pretty_print q # :nodoc:
  q.group 1, "m(", ")" do
    q.pp pattern
  end
end

#satisfy?(o) ⇒ Boolean

Satisfied if o is an atom, and o matches pattern

Returns:

  • (Boolean)


841
842
843
# File 'lib/sexp_matcher.rb', line 841

def satisfy? o
  !o.kind_of?(Sexp) && o.to_s =~ pattern # TODO: question to_s
end