Class: Rux::Lex::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rux/lex/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, is_terminal, transitions) ⇒ State



19
20
21
22
23
24
# File 'lib/rux/lex/state.rb', line 19

def initialize(name, is_terminal, transitions)
  @name = name
  @is_terminal = is_terminal
  @transitions = transitions
  @cache = {}
end

Instance Attribute Details

#is_terminalObject (readonly) Also known as: terminal?

Returns the value of attribute is_terminal.



15
16
17
# File 'lib/rux/lex/state.rb', line 15

def is_terminal
  @is_terminal
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/rux/lex/state.rb', line 15

def name
  @name
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



15
16
17
# File 'lib/rux/lex/state.rb', line 15

def transitions
  @transitions
end

Class Method Details

.parse(state_str, transition_strs, inputs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/rux/lex/state.rb', line 4

def self.parse(state_str, transition_strs, inputs)
  is_terminal = state_str.end_with?('*')
  state_name = "tRUX_#{state_str.chomp('*').upcase}".to_sym

  transitions = transition_strs.each_with_object([]).with_index do |(ts, ret), idx|
    ret << Transition.parse(ts, inputs[idx]) if ts
  end

  new(state_name, is_terminal, transitions)
end

Instance Method Details

#[](chr) ⇒ Object



26
27
28
29
30
# File 'lib/rux/lex/state.rb', line 26

def [](chr)
  @cache[chr] ||= transitions.find do |trans|
    trans.matches?(chr)
  end
end