Class: Pegarus::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/pegarus/evaluator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_executor(pattern, subject) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/pegarus/evaluator.rb', line 3

def self.new_executor(pattern, subject)
  pattern.instance_variable_set :@evaluator, new

  class << pattern
    class_eval <<-eval
      def match(subject)
        @evaluator.match self, subject
      end
    eval
  end

  pattern.match subject
end

Instance Method Details

#always(pattern) ⇒ Object



35
36
37
# File 'lib/pegarus/evaluator.rb', line 35

def always(pattern)
  failure
end

#any(pattern) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/pegarus/evaluator.rb', line 39

def any(pattern)
  if @subject.size > @index + pattern.count
    @index += pattern.count
  else
    failure
  end
end

#any_range(pattern) ⇒ Object



47
48
49
# File 'lib/pegarus/evaluator.rb', line 47

def any_range(pattern)
  failure
end

#character(pattern) ⇒ Object



51
52
53
# File 'lib/pegarus/evaluator.rb', line 51

def character(pattern)
  failure
end

#character_range(pattern) ⇒ Object



55
56
57
# File 'lib/pegarus/evaluator.rb', line 55

def character_range(pattern)
  failure
end

#choice(pattern) ⇒ Object



59
60
61
# File 'lib/pegarus/evaluator.rb', line 59

def choice(pattern)
  failure
end

#concatenation(pattern) ⇒ Object



63
64
65
# File 'lib/pegarus/evaluator.rb', line 63

def concatenation(pattern)
  failure
end

#difference(pattern) ⇒ Object



67
68
69
# File 'lib/pegarus/evaluator.rb', line 67

def difference(pattern)
  failure
end

#failureObject



27
28
29
# File 'lib/pegarus/evaluator.rb', line 27

def failure
  @failure = true
end

#failure?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/pegarus/evaluator.rb', line 31

def failure?
  @failure
end

#if(pattern) ⇒ Object



71
72
73
# File 'lib/pegarus/evaluator.rb', line 71

def if(pattern)
  failure
end

#match(pattern, subject) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/pegarus/evaluator.rb', line 17

def match(pattern, subject)
  @subject  = subject
  @failure  = false
  @index    = 0

  pattern.visit self

  failure? ? nil : @index
end

#never(pattern) ⇒ Object



75
76
77
# File 'lib/pegarus/evaluator.rb', line 75

def never(pattern)
  failure
end

#product(pattern) ⇒ Object



79
80
81
# File 'lib/pegarus/evaluator.rb', line 79

def product(pattern)
  failure
end

#set(pattern) ⇒ Object



83
84
85
# File 'lib/pegarus/evaluator.rb', line 83

def set(pattern)
  failure
end

#unless(pattern) ⇒ Object



87
88
89
# File 'lib/pegarus/evaluator.rb', line 87

def unless(pattern)
  failure
end