class Rattler::Parsers::ParserScope
ParserScope
represents the scope of bindings and captures in a parse sequence.
Attributes
Public Class Methods
@return [ParserScope] an empty scope as a singleton
# File lib/rattler/parsers/parser_scope.rb, line 10 def self.empty @empty ||= self.new end
@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
@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
@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
@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
@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
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
@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
@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
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
@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