Class: Rattler::Parsers::ParserScope

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/parsers/parser_scope.rb

Overview

ParserScope represents the scope of bindings and captures in a parse sequence.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bindings = {}, captures = [], captures_decidable = true) ⇒ ParserScope



18
19
20
21
22
# File 'lib/rattler/parsers/parser_scope.rb', line 18

def initialize(bindings={}, captures=[], captures_decidable=true)
  @bindings = bindings
  @captures = captures
  @captures_decidable = captures_decidable
end

Instance Attribute Details

#bindingsObject (readonly)

Returns the value of attribute bindings.



24
25
26
# File 'lib/rattler/parsers/parser_scope.rb', line 24

def bindings
  @bindings
end

#capturesObject (readonly)

Returns the value of attribute captures.



24
25
26
# File 'lib/rattler/parsers/parser_scope.rb', line 24

def captures
  @captures
end

Class Method Details

.emptyParserScope



10
11
12
# File 'lib/rattler/parsers/parser_scope.rb', line 10

def self.empty
  @empty ||= self.new
end

Instance Method Details

#[](name) ⇒ Object



83
84
85
# File 'lib/rattler/parsers/parser_scope.rb', line 83

def [](name)
  @bindings[name]
end

#bind(new_bindings) ⇒ ParserScope



29
30
31
# File 'lib/rattler/parsers/parser_scope.rb', line 29

def bind(new_bindings)
  self.class.new(@bindings.merge(new_bindings), @captures, @captures_decidable)
end

#capture(*new_captures) ⇒ ParserScope



35
36
37
# File 'lib/rattler/parsers/parser_scope.rb', line 35

def capture(*new_captures)
  self.class.new(@bindings, @captures + new_captures, @captures_decidable)
end

#captures_decidable?Boolean



64
65
66
# File 'lib/rattler/parsers/parser_scope.rb', line 64

def captures_decidable?
  @captures_decidable
end

#each_binding {|name, value| ... } ⇒ Object

Run the block once for each binding

Yields:

  • (name, value)

    the binding name and value



77
78
79
# File 'lib/rattler/parsers/parser_scope.rb', line 77

def each_binding
  @bindings.each {|name, value| yield name, value }
end

#has_name?(name) ⇒ Boolean



70
71
72
# File 'lib/rattler/parsers/parser_scope.rb', line 70

def has_name?(name)
  @bindings.has_key?(name)
end

#merge(other) ⇒ ParserScope



49
50
51
# File 'lib/rattler/parsers/parser_scope.rb', line 49

def merge(other)
  bind(other.bindings)
end

#nestParserScope

Create a new scope with this scope as the outer scope. The new scope inherits this scope’s bindings, but not its captures.



43
44
45
# File 'lib/rattler/parsers/parser_scope.rb', line 43

def nest
  self.class.new(@bindings, [], @captures_decidable)
end

#with_undecidable_capturesParserScope



54
55
56
57
58
59
60
# File 'lib/rattler/parsers/parser_scope.rb', line 54

def with_undecidable_captures
  if captures_decidable?
    self.class.new(@bindings, @captures, false)
  else
    self
  end
end