Class: Taipo::Parser::SyntaxState Private

Inherits:
Object
  • Object
show all
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

Since:

  • 1.0.0

Instance Method Summary collapse

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

Examples:

status_array = [ :foo, :bar ]
counter_array = [ [ :angle ], { angle: '>' } ]
state = SyntaxState.new(status_array, counter_array)

Parameters:

  • state_names (Array<Symbol>)

    an array of symbols designating the particular states to be used

  • counter_names_and_closers (Array<Array<Symbol>,Hash<Symbol, String>>) (defaults to: nil)

    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



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

Parameters:

  • key (Symbol)

    the counter to check

Returns:

  • (Boolean)

    the result

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key to set

Since:

  • 1.0.0



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.

Note:

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

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

Parameters:

  • except (Array<Symbol>) (defaults to: [])

    keys not to update to :allowed (will instead be set to :prohibited)

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key to check

Returns:

  • (Boolean)

    the result

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key for the counter

Returns:

  • (Integer)

    the count

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key for the counter

Raises:

  • (RangeError)

Since:

  • 1.0.0



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)

Parameters:

  • key (Symbol)

    the key to disable

Since:

  • 1.0.0



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)

Parameters:

  • key (Symbol)

    the key to disable

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key for the counter

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key for the counter

Returns:

  • (Boolean)

    the result

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key for the counter

Returns:

  • (Boolean)

    the result

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key to set

Since:

  • 1.0.0



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.

Note:

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

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

Parameters:

  • except (Array<Symbol>) (defaults to: [])

    keys not to update to :prohibited (will instead be set to :allowed)

Since:

  • 1.0.0



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

Parameters:

  • key (Symbol)

    the key to check

Returns:

  • (Boolean)

Since:

  • 1.0.0



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.

Note:

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

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

Parameters:

  • status (Symbol)

    the value for all statuses

  • except (Hash<Array<Symbol>, Symbol>) (defaults to: {})

    the exceptions

Options Hash (except:):

  • :exceptions (Array<Symbol>)

    keys not to update to status

  • :status (Symbol)

    the value for exceptions

Since:

  • 1.0.0



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

#unbalancedArray<Symbol>

Get the names of the unbalanced brackets

Returns:

  • (Array<Symbol>)

    an array of the names of the unbalanced brackets

Since:

  • 1.0.0



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