class Taipo::Parser::SyntaxState

A state machine

@since 1.0.0 @api private

Public Class Methods

new(state_names, counter_names_and_closers = nil) click to toggle source

Initialize a new state machine

@param state_names [Array<Symbol>] an array of symbols designating the

particular states to be used

@param counter_names_and_closers [Array<Array<Symbol>,Hash<Symbol,

String>>] an array of two collections: an array of symbols
designating the names the state machine will use for counting
brackets and a hash of closing characters used for each bracket (the
key for each closing character should have the same name as the name
used for the counter)

@since 1.0.0 @api private

@example

status_array = [ :foo, :bar ]
counter_array = [ [ :angle ], { angle: '>' } ]
state = SyntaxState.new(status_array, counter_array)
# 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

Public Instance Methods

active?(key) click to toggle source

Check if the counter for the given key has been incremented

@param key [Symbol] the counter to check

@return [Boolean] the result

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 47
def active?(key)
  @counter[key] > 0
end
allow(key) click to toggle source

Set the status for the given key to be :allowed

@param key [Symbol] the key to set

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 57
def allow(key)
  @status[key] = :allowed
end
allow_all(except: []) click to toggle source

Set all statuses to be :allowed except those specified in the except array

@note Statuses which have been set to :disabled will not be updated.

@param except [Array<Symbol>] keys not to update to :allowed (will

instead be set to +:prohibited+)

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 71
def allow_all(except: [])
  set_all :allowed, except: { exceptions: except, status: :prohibited }
end
allowed?(key) click to toggle source

Check if the given key is allowed

@param key [Symbol] the key to check

@return [Boolean] the result

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 83
def allowed?(key)
  @status[key] == :allowed
end
count(key) click to toggle source

Get the count for the given key

@param key [Symbol] the key for the counter

@return [Integer] the count

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 95
def count(key)
  @counter[key]
end
decrement(key) click to toggle source

Decrement the count for the given key by 1

@param key [Symbol] the key for the counter

@since 1.0.0 @api private

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

Disable the status of the given key (by setting it to :disabled)

@param key [Symbol] the key to disable

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 117
def disable(key)
  @status[key] = :disabled
end
enable(key) click to toggle source

Enable the status of the given key (by setting it to :prohibited)

@param key [Symbol] the key to disable

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 127
def enable(key)
  @status[key] = :prohibited
end
increment(key) click to toggle source

Increment the counter for the given key by 1

@param key [Symbol] the key for the counter

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 137
def increment(key)
  @counter[key] += 1
end
inside?(key) click to toggle source

Check if we are 'inside' a set of brackets (eg. a pair of parentheses) for a given key

@param key [Symbol] the key for the counter

@return [Boolean] the result

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 150
def inside?(key)
  @counter[key] > 0
end
outside?(key) click to toggle source

Check if we are 'outside' a set of brackets (eg. a pair of parentheses) for a given key

@param key [Symbol] the key for the counter

@return [Boolean] the result

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 163
def outside?(key)
  @counter[key] == 0
end
prohibit(key) click to toggle source

Set the status for the given key to be :prohibited

@param key [Symbol] the key to set

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 173
def prohibit(key)
  @status[key] = :prohibited
end
prohibit_all(except: []) click to toggle source

Set all statuses to be :prohibited except those specified in the except array

@note Statuses which have been set to :disabled will not be updated.

@param except [Array<Symbol>] keys not to update to :prohibited (will

instead be set to +:allowed+)

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 187
def prohibit_all(except: [])
  set_all :prohibited, except: { exceptions: except, status: :allowed }
end
prohibited?(key) click to toggle source

Check if the given key is allowed

@param key [Symbol] the key to check

@since 1.0.0 @api private

# File lib/taipo/parser/syntax_state.rb, line 197
def prohibited?(key)
  @status[key] == :prohibited
end
set_all(status, except: {}) click to toggle source

Set all statuses to be status except those specified in the except array

@note Statuses which have been set to :disabled will not be updated.

@param status [Symbol] the value for all statuses @param except [Hash<Array<Symbol>, Symbol>] the exceptions @option except [Array<Symbol>] :exceptions keys not to update to

+status+

@option except [Symbol] :status the value for exceptions

@since 1.0.0 @api private

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

Get the names of the unbalanced brackets

@return [Array<Symbol>] an array of the names of the unbalanced

brackets

@since 1.0.0 @api

# 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