Class: Worte::Printer::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/worte/printer/simple.rb

Direct Known Subclasses

Colorized

Instance Method Summary collapse

Instance Method Details

#error_marker(length) ⇒ Object



30
31
32
# File 'lib/worte/printer/simple.rb', line 30

def error_marker(length)
  '^' * length
end


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/worte/printer/simple.rb', line 6

def print(text, tokens, where=STDOUT)
  text.split("\n").each_with_index do |line, l|
    line_tokens = tokens.select { |t| t.position[0] == l }
    if line_tokens.collect(&:correct).all?
      next # Do not print lines without errors
    end
    where.print "#{l + 1}: "
    where.puts line
    col = 0
    where.print ' ' * ((l + 1).to_s.length + 2)
    line_tokens.each do |token|
      if !token.correct
        if col != token.position[1]
          where.print (' ' * (token.position[1] - col))
          col = token.position[1]
        end
        where.print error_marker(token.word.length)
        col += token.word.length
      end
    end
    where.puts
  end
end