Class: SyntaxTree::Parser::MultiByteString

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

Overview

Represents a line in the source. If this class is being used, it means that there are characters in the string that are multi-byte, so we will build up an array of indices, such that array will be equal to the index of the character within the string.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, line) ⇒ MultiByteString

Returns a new instance of MultiByteString.



41
42
43
44
45
46
47
48
49
50
# File 'lib/syntax_tree/parser.rb', line 41

def initialize(start, line)
  @start = start
  @indices = []

  line
    .each_char
    .with_index(start) do |char, index|
      char.bytesize.times { @indices << index }
    end
end

Instance Attribute Details

#indicesObject (readonly)

Returns the value of attribute indices.



39
40
41
# File 'lib/syntax_tree/parser.rb', line 39

def indices
  @indices
end

#startObject (readonly)

Returns the value of attribute start.



39
40
41
# File 'lib/syntax_tree/parser.rb', line 39

def start
  @start
end

Instance Method Details

#[](byteindex) ⇒ Object

Technically it’s possible for the column index to be a negative value if there’s a BOM at the beginning of the file, which is the reason we need to compare it to 0 here.



55
56
57
# File 'lib/syntax_tree/parser.rb', line 55

def [](byteindex)
  indices[[byteindex, 0].max]
end