Class: Rattler::Parsers::ParserScope
- Inherits:
-
Object
- Object
- Rattler::Parsers::ParserScope
- 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
-
#bindings ⇒ Object
readonly
Returns the value of attribute bindings.
-
#captures ⇒ Object
readonly
Returns the value of attribute captures.
Class Method Summary collapse
-
.empty ⇒ ParserScope
An empty scope as a singleton.
Instance Method Summary collapse
-
#[](name) ⇒ Object
The value bound to
name
in the scope. -
#bind(new_bindings) ⇒ ParserScope
A new scope with additional bindings.
-
#capture(*new_captures) ⇒ ParserScope
A new scope with additional captures.
-
#captures_decidable? ⇒ Boolean
Whether the list of captured parse results can be known statically.
-
#each_binding {|name, value| ... } ⇒ Object
Run the block once for each binding.
-
#has_name?(name) ⇒ Boolean
Whether the scope has a binding for
name
. -
#initialize(bindings = {}, captures = [], captures_decidable = true) ⇒ ParserScope
constructor
A new instance of ParserScope.
-
#merge(other) ⇒ ParserScope
A new scope with bindings the other scope.
-
#nest ⇒ ParserScope
Create a new scope with this scope as the outer scope.
-
#with_undecidable_captures ⇒ ParserScope
A new scope with captures_decidable
false
.
Constructor Details
#initialize(bindings = {}, captures = [], captures_decidable = true) ⇒ ParserScope
Returns a new instance of 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
#bindings ⇒ Object (readonly)
Returns the value of attribute bindings.
24 25 26 |
# File 'lib/rattler/parsers/parser_scope.rb', line 24 def bindings @bindings end |
#captures ⇒ Object (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
.empty ⇒ ParserScope
Returns an empty scope as a singleton.
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.
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.
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.
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.
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
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
.
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.
49 50 51 |
# File 'lib/rattler/parsers/parser_scope.rb', line 49 def merge(other) bind(other.bindings) end |
#nest ⇒ ParserScope
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_captures ⇒ ParserScope
Returns 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 |