File: engine.rb

package info (click to toggle)
ruby-parslet 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 473
  • sloc: ruby: 5,220; makefile: 2
file content (112 lines) | stat: -rw-r--r-- 2,539 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

require 'parslet/atoms/visitor'

module Parslet::Accelerator
  # @api private
  class Apply
    def initialize(engine, expr)
      @engine = engine
      @expr = expr
    end

    def visit_parser(root)
      false
    end
    def visit_entity(name, block)
      false
    end
    def visit_named(name, atom)
      match(:as) do |key|
        @engine.try_bind(key, name)
      end
    end
    def visit_repetition(tag, min, max, atom)
      match(:rep) do |e_min, e_max, expr|
        e_min == min && e_max == max && @engine.match(atom, expr)
      end
    end
    def visit_alternative(alternatives)
      match(:alt) do |*expressions|
        return false if alternatives.size != expressions.size

        alternatives.zip(expressions).all? do |atom, expr|
          @engine.match(atom, expr)
        end
      end
    end
    def visit_sequence(sequence)
      match(:seq) do |*expressions|
        return false if sequence.size != expressions.size

        sequence.zip(expressions).all? do |atom, expr|
          @engine.match(atom, expr)
        end
      end
    end
    def visit_lookahead(positive, atom)
      match(:absent) do |expr|
        return positive == false && @engine.match(atom, expr)
      end
      match(:present) do |expr|
        return positive == true && @engine.match(atom, expr)
      end
    end
    def visit_re(regexp)
      match(:re) do |*bind_conditions|
        bind_conditions.all? { |bind_cond| 
          @engine.try_bind(bind_cond, regexp) }
      end
    end
    def visit_str(str)
      match(:str) do |*bind_conditions|
        bind_conditions.all? { |bind_cond| 
          @engine.try_bind(bind_cond, str) }
      end
    end

    def match(type_tag)
      expr_tag = @expr.type
      if expr_tag == type_tag
        yield *@expr.args
      end
    end
  end

  # @api private
  class Engine
    attr_reader :bindings

    def initialize 
      @bindings = {}
    end

    def match(atom, expr)
      atom.accept(
        Apply.new(self, expr))
    end

    def try_bind(variable, value)
      if bound? variable
        return value == lookup(variable)
      else
        case variable
          when Symbol
            bind(variable, value)
        else
          # This does not look like a variable - let's try matching it against
          # the value: 
          variable === value
        end    
      end
    end
    def bound? var
      @bindings.has_key? var
    end
    def lookup var
      @bindings[var]
    end
    def bind var, val
      @bindings[var] = val
    end
  end
end