Class: Stupidedi::Reader::DelegatedInput

Inherits:
AbstractInput show all
Defined in:
lib/stupidedi/reader/input/delegated_input.rb

Querying the Position collapse

Querying the Position collapse

Advancing the Cursor collapse

Instance Method Summary collapse

Methods inherited from AbstractInput

#==, #at, #defined_at?, #empty?, #index, #take

Methods included from Inspect

#inspect

Constructor Details

#initialize(delegate, offset = 0, line = 1, column = 1) ⇒ DelegatedInput

Returns a new instance of DelegatedInput.



7
8
9
10
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 7

def initialize(delegate, offset = 0, line = 1, column = 1)
  @delegate, @offset, @line, @column =
    delegate, offset, line, column
end

Instance Attribute Details

#column (readonly)

Returns the value of attribute column.



22
23
24
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 22

def column
  @column
end

#line (readonly)

Returns the value of attribute line.



19
20
21
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 19

def line
  @line
end

#offset (readonly)

Returns the value of attribute offset.



16
17
18
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 16

def offset
  @offset
end

Instance Method Details

#drop(n) ⇒ AbstractInput

Advance the cursor forward n elements

Parameters:

  • n (Integer)

    the number of elements to advance (n >= 0)

Returns:

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 45

def drop(n)
  raise ArgumentError, "n must be positive" unless n >= 0

  suffix = @delegate.drop(n)
  prefix = @delegate.take(n)

  length = prefix.length
  count  = prefix.count("\n")

  column = unless count.zero?
             length - prefix.rindex("\n")
           else
             @column + length
           end

  copy(:delegate => suffix,
       :offset   => @offset + length,
       :line     => @line + count,
       :column   => column)
end

#positionString

The file name, URI, etc that identifies the input stream

Returns:

  • (String)


25
26
27
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 25

def position
  Position.new(@offset, @line, @column, nil)
end

#pretty_print(q)

This method returns an undefined value.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/stupidedi/reader/input/delegated_input.rb', line 83

def pretty_print(q)
  q.text("DelegateInput")
  q.group(2, "(", ")") do
    preview = @delegate.take(4)
    preview = if preview.empty?
                "EOF"
              elsif preview.length <= 3
                preview.inspect
              else
                (preview.take(3) + "...").inspect
              end

    q.text preview
    q.text " at line #{@line}, column #{@column}, offset #{@offset}"
  end
end