Class: Taipo::Parser::SyntaxState Private
- Inherits:
-
Object
- Object
- Taipo::Parser::SyntaxState
- Defined in:
- lib/taipo/parser/syntax_state.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A state machine
Instance Method Summary collapse
-
#active?(key) ⇒ Boolean
private
Check if the counter for the given
keyhas been incremented. -
#allow(key) ⇒ Object
private
Set the status for the given
keyto be:allowed. -
#allow_all(except: []) ⇒ Object
private
Set all statuses to be
:allowedexcept those specified in theexceptarray. -
#allowed?(key) ⇒ Boolean
private
Check if the given
keyis allowed. -
#count(key) ⇒ Integer
private
Get the count for the given
key. -
#decrement(key) ⇒ Object
private
Decrement the count for the given
keyby 1. -
#disable(key) ⇒ Object
private
Disable the status of the given
key(by setting it to:disabled). -
#enable(key) ⇒ Object
private
Enable the status of the given
key(by setting it to:prohibited). -
#increment(key) ⇒ Object
private
Increment the counter for the given
keyby 1. -
#initialize(state_names, counter_names_and_closers = nil) ⇒ SyntaxState
constructor
private
Initialize a new state machine.
-
#inside?(key) ⇒ Boolean
private
Check if we are ‘inside’ a set of brackets (eg. a pair of parentheses) for a given
key. -
#outside?(key) ⇒ Boolean
private
Check if we are ‘outside’ a set of brackets (eg. a pair of parentheses) for a given
key. -
#prohibit(key) ⇒ Object
private
Set the status for the given
keyto be:prohibited. -
#prohibit_all(except: []) ⇒ Object
private
Set all statuses to be
:prohibitedexcept those specified in theexceptarray. -
#prohibited?(key) ⇒ Boolean
private
Check if the given
keyis allowed. -
#set_all(status, except: {}) ⇒ Object
private
Set all statuses to be
statusexcept those specified in theexceptarray. -
#unbalanced ⇒ Array<Symbol>
Get the names of the unbalanced brackets.
Constructor Details
#initialize(state_names, counter_names_and_closers = nil) ⇒ SyntaxState
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new state machine
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/taipo/parser/syntax_state.rb', line 28 def initialize(state_names, counter_names_and_closers = nil) @status = Hash[state_names.map { |s| [s, :prohibited] }] if counter_names_and_closers.nil? @counter = Array.new @closers = Array.new else @counter = Hash[counter_names_and_closers[0].map { |c| [c, 0] }] @closers = counter_names_and_closers[1] end end |
Instance Method Details
#active?(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the counter for the given key has been incremented
47 48 49 |
# File 'lib/taipo/parser/syntax_state.rb', line 47 def active?(key) @counter[key] > 0 end |
#allow(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set the status for the given key to be :allowed
57 58 59 |
# File 'lib/taipo/parser/syntax_state.rb', line 57 def allow(key) @status[key] = :allowed end |
#allow_all(except: []) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Statuses which have been set to :disabled will not be updated.
Set all statuses to be :allowed except those specified in the except array
71 72 73 |
# File 'lib/taipo/parser/syntax_state.rb', line 71 def allow_all(except: []) set_all :allowed, except: { exceptions: except, status: :prohibited } end |
#allowed?(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given key is allowed
83 84 85 |
# File 'lib/taipo/parser/syntax_state.rb', line 83 def allowed?(key) @status[key] == :allowed end |
#count(key) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the count for the given key
95 96 97 |
# File 'lib/taipo/parser/syntax_state.rb', line 95 def count(key) @counter[key] end |
#decrement(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Decrement the count for the given key by 1
105 106 107 108 109 |
# File 'lib/taipo/parser/syntax_state.rb', line 105 def decrement(key) msg = 'Trying to reduce count below zero.' raise RangeError, msg if @counter[key] == 0 @counter[key] -= 1 end |
#disable(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Disable the status of the given key (by setting it to :disabled)
117 118 119 |
# File 'lib/taipo/parser/syntax_state.rb', line 117 def disable(key) @status[key] = :disabled end |
#enable(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Enable the status of the given key (by setting it to :prohibited)
127 128 129 |
# File 'lib/taipo/parser/syntax_state.rb', line 127 def enable(key) @status[key] = :prohibited end |
#increment(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Increment the counter for the given key by 1
137 138 139 |
# File 'lib/taipo/parser/syntax_state.rb', line 137 def increment(key) @counter[key] += 1 end |
#inside?(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if we are ‘inside’ a set of brackets (eg. a pair of parentheses) for a given key
150 151 152 |
# File 'lib/taipo/parser/syntax_state.rb', line 150 def inside?(key) @counter[key] > 0 end |
#outside?(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if we are ‘outside’ a set of brackets (eg. a pair of parentheses) for a given key
163 164 165 |
# File 'lib/taipo/parser/syntax_state.rb', line 163 def outside?(key) @counter[key] == 0 end |
#prohibit(key) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set the status for the given key to be :prohibited
173 174 175 |
# File 'lib/taipo/parser/syntax_state.rb', line 173 def prohibit(key) @status[key] = :prohibited end |
#prohibit_all(except: []) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Statuses which have been set to :disabled will not be updated.
Set all statuses to be :prohibited except those specified in the except array
187 188 189 |
# File 'lib/taipo/parser/syntax_state.rb', line 187 def prohibit_all(except: []) set_all :prohibited, except: { exceptions: except, status: :allowed } end |
#prohibited?(key) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the given key is allowed
197 198 199 |
# File 'lib/taipo/parser/syntax_state.rb', line 197 def prohibited?(key) @status[key] == :prohibited end |
#set_all(status, except: {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Statuses which have been set to :disabled will not be updated.
Set all statuses to be status except those specified in the except array
214 215 216 217 218 219 |
# File 'lib/taipo/parser/syntax_state.rb', line 214 def set_all(status, except: {}) @status.transform_values! { |v| v = status unless v == :disabled } except[:exceptions].each do |k| @status[k] = except[:status] unless @status[k] == :disabled end end |
#unbalanced ⇒ Array<Symbol>
Get the names of the unbalanced brackets
228 229 230 231 232 |
# File 'lib/taipo/parser/syntax_state.rb', line 228 def unbalanced() @counter.reduce(Array.new) do |memo, c| (c[1] == 0) ? memo : memo.push(@closers[c[0]]) end end |