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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Matcher

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

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

Instance Attribute Details

#pattern ⇒ Object (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

#hash ⇒ Object



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

def hash
  [super, pattern].hash
end

#inspect ⇒ Object

: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