class Rattler::Parsers::ParserScope

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

Attributes

bindings[R]
captures[R]

Public Class Methods

empty() click to toggle source

@return [ParserScope] an empty scope as a singleton

# File lib/rattler/parsers/parser_scope.rb, line 10
def self.empty
  @empty ||= self.new
end
new(bindings={}, captures=[], captures_decidable=true) click to toggle source

@param [Hash] bindings labeled bindings @param [Array] captures captured parse results @param [Boolean] captures_decidable whether the list of captured parse

results can be known statically
# 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

Public Instance Methods

[](name) click to toggle source

@param [Symbol] name a binding name @return the value bound to name in the scope

# File lib/rattler/parsers/parser_scope.rb, line 83
def [](name)
  @bindings[name]
end
bind(new_bindings) click to toggle source

@param [Hash] new_bindings bindings to add or replace the current

bindings

@return [ParserScope] a new scope with additional bindings

# 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) click to toggle source

@param new_captures captures to add to the current captures @return [ParserScope] a new scope with additional captures

# 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?() click to toggle source

@return whether the list of captured parse results can be known

statically
# File lib/rattler/parsers/parser_scope.rb, line 64
def captures_decidable?
  @captures_decidable
end
each_binding() { |name, value| ... } click to toggle source

Run the block once for each binding @yield [name, value] the binding name and value @return self#bindings

# File lib/rattler/parsers/parser_scope.rb, line 77
def each_binding
  @bindings.each {|name, value| yield name, value }
end
has_name?(name) click to toggle source

@param [Symbol] name a binding name @return whether the scope has a binding for name

# File lib/rattler/parsers/parser_scope.rb, line 70
def has_name?(name)
  @bindings.has_key?(name)
end
merge(other) click to toggle source

@param [ParserScope] other another scope @return [ParserScope] a new scope with bindings the other scope

# File lib/rattler/parsers/parser_scope.rb, line 49
def merge(other)
  bind(other.bindings)
end
nest() click to toggle source

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

@return [ParserScope] a new scope with this scope as the outer scope

# File lib/rattler/parsers/parser_scope.rb, line 43
def nest
  self.class.new(@bindings, [], @captures_decidable)
end
with_undecidable_captures() click to toggle source

@return [ParserScope] a new scope with captures_decidable false

# 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