Class: Parser::Lexer::StackState

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/lexer/stack_state.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ StackState

Returns a new instance of StackState.



4
5
6
7
# File 'lib/parser/lexer/stack_state.rb', line 4

def initialize(name)
  @name  = name.freeze
  clear
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/parser/lexer/stack_state.rb', line 31

def active?
  @stack[0] == 1
end

#clearObject



9
10
11
# File 'lib/parser/lexer/stack_state.rb', line 9

def clear
  @stack = 0
end

#lexpopObject



27
28
29
# File 'lib/parser/lexer/stack_state.rb', line 27

def lexpop
  push(pop || pop)
end

#popObject



20
21
22
23
24
25
# File 'lib/parser/lexer/stack_state.rb', line 20

def pop
  bit_value = @stack & 1
  @stack  >>= 1

  bit_value == 1
end

#push(bit) ⇒ Object



13
14
15
16
17
18
# File 'lib/parser/lexer/stack_state.rb', line 13

def push(bit)
  bit_value = bit ? 1 : 0
  @stack = (@stack << 1) | bit_value

  bit
end

#to_sObject Also known as: inspect



35
36
37
# File 'lib/parser/lexer/stack_state.rb', line 35

def to_s
  "[#{@stack.to_s(2)} <= #{@name}]"
end