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

Returns a new instance of ParserScope.

Parameters:

  • bindings (Hash) (defaults to: {})

    labeled bindings

  • captures (Array) (defaults to: [])

    captured parse results

  • captures_decidable (Boolean) (defaults to: true)

    whether the list of captured parse results can be known statically



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

Returns an empty scope as a singleton.

Returns:



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

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

Instance Method Details

#[](name) ⇒ Object

Returns the value bound to name in the scope.

Parameters:

  • name (Symbol)

    a binding name

Returns:

  • the value bound to name in the scope



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

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

#bind(new_bindings) ⇒ ParserScope

Returns a new scope with additional bindings.

Parameters:

  • new_bindings (Hash)

    bindings to add or replace the current bindings

Returns:

  • (ParserScope)

    a new scope with additional bindings



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

Returns a new scope with additional captures.

Parameters:

  • new_captures

    captures to add to the current captures

Returns:

  • (ParserScope)

    a new scope with additional captures



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

Returns whether the list of captured parse results can be known statically.

Returns:

  • (Boolean)

    whether the list of captured parse results can be known statically



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

Returns:

  • self#bindings



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

Returns whether the scope has a binding for name.

Parameters:

  • name (Symbol)

    a binding name

Returns:

  • (Boolean)

    whether the scope has a binding for name



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

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

#merge(other) ⇒ ParserScope

Returns a new scope with bindings the other scope.

Parameters:

Returns:

  • (ParserScope)

    a new scope with bindings the other scope



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.

Returns:

  • (ParserScope)

    a new scope with this scope as the outer scope



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

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

#with_undecidable_capturesParserScope

Returns a new scope with captures_decidable false.

Returns:

  • (ParserScope)

    a new scope with captures_decidable false



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