Class: DParse::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/d-parse/position.rb

Constant Summary collapse

FAR_BEHIND =
new(index: -1, line: -1, column: -1)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index: 0, line: 0, column: 0) ⇒ Position

Returns a new instance of Position.



7
8
9
10
11
# File 'lib/d-parse/position.rb', line 7

def initialize(index: 0, line: 0, column: 0)
  @index = index
  @line = line
  @column = column
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



5
6
7
# File 'lib/d-parse/position.rb', line 5

def column
  @column
end

#indexObject (readonly)

Returns the value of attribute index.



3
4
5
# File 'lib/d-parse/position.rb', line 3

def index
  @index
end

#lineObject (readonly)

Returns the value of attribute line.



4
5
6
# File 'lib/d-parse/position.rb', line 4

def line
  @line
end

Instance Method Details

#advance(char) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/d-parse/position.rb', line 15

def advance(char)
  Position.new(
    index: @index + 1,
    line: char == "\n" ? @line + 1 : @line,
    column: char == "\n" ? 0 : @column + 1,
  )
end

#inspectObject



27
28
29
# File 'lib/d-parse/position.rb', line 27

def inspect
  "Pos(#{@index}; #{@line}:#{@column})"
end

#to_sObject



23
24
25
# File 'lib/d-parse/position.rb', line 23

def to_s
  "line #{@line + 1}, column #{@column + 1}"
end