Class: Dentaku::TokenMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/dentaku/token_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(categories = nil, values = nil, children = []) ⇒ TokenMatcher

Returns a new instance of TokenMatcher.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/dentaku/token_matcher.rb', line 7

def initialize(categories = nil, values = nil, children = [])
  # store categories and values as hash to optimize key lookup, h/t @jan-mangs
  @categories = [categories].compact.flatten.each_with_object({}) { |c, h| h[c] = 1 }
  @values     = [values].compact.flatten.each_with_object({}) { |v, h| h[v] = 1 }
  @children   = children.compact
  @invert     = false

  @min = 1
  @max = 1
  @range = (@min..@max)
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



5
6
7
# File 'lib/dentaku/token_matcher.rb', line 5

def categories
  @categories
end

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/dentaku/token_matcher.rb', line 5

def children
  @children
end

#valuesObject (readonly)

Returns the value of attribute values.



5
6
7
# File 'lib/dentaku/token_matcher.rb', line 5

def values
  @values
end

Instance Method Details

#==(token) ⇒ Object



28
29
30
# File 'lib/dentaku/token_matcher.rb', line 28

def ==(token)
  leaf_matcher? ? matches_token?(token) : any_child_matches_token?(token)
end

#caretObject



47
48
49
50
# File 'lib/dentaku/token_matcher.rb', line 47

def caret
  @caret = true
  self
end

#caret?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/dentaku/token_matcher.rb', line 52

def caret?
  @caret
end

#invertObject



23
24
25
26
# File 'lib/dentaku/token_matcher.rb', line 23

def invert
  @invert = ! @invert
  self
end

#leaf_matcher?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/dentaku/token_matcher.rb', line 69

def leaf_matcher?
  children.empty?
end

#leaf_matchersObject



73
74
75
# File 'lib/dentaku/token_matcher.rb', line 73

def leaf_matchers
  leaf_matcher? ? [self] : children
end

#match(token_stream, offset = 0) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dentaku/token_matcher.rb', line 32

def match(token_stream, offset = 0)
  matched_tokens = []
  matched = false

  while self == token_stream[matched_tokens.length + offset] && matched_tokens.length < @max
    matched_tokens << token_stream[matched_tokens.length + offset]
  end

  if @range.cover?(matched_tokens.length)
    matched = true
  end

  [matched, matched_tokens]
end

#plusObject



63
64
65
66
67
# File 'lib/dentaku/token_matcher.rb', line 63

def plus
  @max = Float::INFINITY
  @range = (@min..@max)
  self
end

#starObject



56
57
58
59
60
61
# File 'lib/dentaku/token_matcher.rb', line 56

def star
  @min = 0
  @max = Float::INFINITY
  @range = (@min..@max)
  self
end

#|(other_matcher) ⇒ Object



19
20
21
# File 'lib/dentaku/token_matcher.rb', line 19

def |(other_matcher)
  self.class.new(:nomatch, :nomatch, leaf_matchers + other_matcher.leaf_matchers)
end