File: binding.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 (49 lines) | stat: -rw-r--r-- 1,258 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

# Used internally for representing a bind placeholder in a Parslet::Transform
# pattern. This is the superclass for all bindings. 
#
# It defines the most permissive kind of bind, the one that matches any subtree
# whatever it looks like. 
#
class Parslet::Pattern::SubtreeBind < Struct.new(:symbol)
  def variable_name
    symbol
  end
  
  def inspect
    "#{bind_type_name}(#{symbol.inspect})"
  end
  
  def can_bind?(subtree)
    true
  end

private 
  def bind_type_name 
    if md=self.class.name.match(/(\w+)Bind/)
      md.captures.first.downcase
    else
      # This path should never be used, but since this is for inspection only, 
      # let's not raise.
      'unknown_bind'
    end
  end
end

# Binds a symbol to a simple subtree, one that is not either a sequence of
# elements or a collection of attributes. 
#
class Parslet::Pattern::SimpleBind < Parslet::Pattern::SubtreeBind
  def can_bind?(subtree)
    not [Hash, Array].include?(subtree.class)
  end
end

# Binds a symbol to a sequence of simple leafs ([element1, element2, ...])
#
class Parslet::Pattern::SequenceBind < Parslet::Pattern::SubtreeBind
  def can_bind?(subtree)
    subtree.kind_of?(Array) &&
      (not subtree.any? { |el| [Hash, Array].include?(el.class) })
  end
end