Class: SexpPath::Matcher::Base

Inherits:
Sexp
  • Object
show all
Defined in:
lib/sexp_path/matcher/base.rb

Overview

This is the base class for all Sexp matchers.

A matcher should implement the following methods:

  • satisfy?

  • inspect

satisfy? determines whether the matcher matches a given input, and inspect will print the matcher nicely in a user’s console.

The base matcher is created with the SexpQueryBuilder as follows

Q?{ s() }

Direct Known Subclasses

All, Any, Atom, Block, Child, Include, Not, Pattern, Sibling, Type, Wild

Instance Method Summary collapse

Methods inherited from Sexp

#satisfy?

Methods included from Traverse

#capture_as, #replace_sexp, #search, #search_each

Instance Method Details

#&(o) ⇒ Object

Combines the Matcher with another Matcher, the resulting one will be satisfied only if both Matchers would be satisfied.

Example:

t(:a) & include(:b)


28
29
30
# File 'lib/sexp_path/matcher/base.rb', line 28

def & o
  SexpPath::Matcher::All.new(self, o)
end

#-@Object

Returns a Matcher that matches whenever this Matcher would not have matched

Example:

-s(:a)


36
37
38
# File 'lib/sexp_path/matcher/base.rb', line 36

def -@
  SexpPath::Matcher::Not.new(self)
end

#>>(o) ⇒ Object

Returns a Matcher that matches if this has a sibling o

Example:

s(:a) >> s(:b)


44
45
46
# File 'lib/sexp_path/matcher/base.rb', line 44

def >> o
  SexpPath::Matcher::Sibling.new(self, o)
end

#inspectObject

Formats the matcher as:

q(:a, :b)


50
51
52
53
# File 'lib/sexp_path/matcher/base.rb', line 50

def inspect
  children = map{|e| e.inspect}.join(', ')
  "q(#{children})"
end

#|(o) ⇒ Object

Combines the Matcher with another Matcher, the resulting one will be satisfied if either Matcher would be satisfied.

Example:

s(:a) | s(:b)


19
20
21
# File 'lib/sexp_path/matcher/base.rb', line 19

def | o
  SexpPath::Matcher::Any.new(self, o)
end