Class: BetterHtml::Tokenizer::TokenArray

Inherits:
Object
  • Object
show all
Defined in:
lib/better_html/tokenizer/token_array.rb

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ TokenArray

Returns a new instance of TokenArray.



6
7
8
9
10
# File 'lib/better_html/tokenizer/token_array.rb', line 6

def initialize(list)
  @list = list
  @current = 0
  @last = @list.size
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/better_html/tokenizer/token_array.rb', line 37

def any?
  !empty?
end

#currentObject



41
42
43
# File 'lib/better_html/tokenizer/token_array.rb', line 41

def current
  @list[@current] unless empty?
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/better_html/tokenizer/token_array.rb', line 33

def empty?
  size <= 0
end

#lastObject



45
46
47
# File 'lib/better_html/tokenizer/token_array.rb', line 45

def last
  @list[@last - 1] unless empty?
end

#popObject



20
21
22
23
24
25
26
# File 'lib/better_html/tokenizer/token_array.rb', line 20

def pop
  raise "no tokens left to pop" if empty?

  item = @list[@last - 1]
  @last -= 1
  item
end

#shiftObject



12
13
14
15
16
17
18
# File 'lib/better_html/tokenizer/token_array.rb', line 12

def shift
  raise "no tokens left to shift" if empty?

  item = @list[@current]
  @current += 1
  item
end

#sizeObject



49
50
51
# File 'lib/better_html/tokenizer/token_array.rb', line 49

def size
  @last - @current
end

#trim(type) ⇒ Object



28
29
30
31
# File 'lib/better_html/tokenizer/token_array.rb', line 28

def trim(type)
  shift while current&.type == type
  pop while last&.type == type
end