Module: Parser::Lexer::Explanation

Defined in:
lib/parser/lexer/explanation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/parser/lexer/explanation.rb', line 5

def self.included(klass)
  klass.class_exec do
    alias_method :state_before_explanation=,  :state=
    alias_method :advance_before_explanation, :advance

    remove_method :state=, :advance
  end
end

Instance Method Details

#advanceObject

Like #advance, but also pretty-print the token and its position in the stream to stdout.



16
17
18
19
20
21
22
23
24
# File 'lib/parser/lexer/explanation.rb', line 16

def advance
  type, (val, range) = advance_before_explanation

  puts decorate(range,
                "\e[0;32m#{type} #{val.inspect}\e[0m",
                "#{state.to_s.ljust(12)} #{@cond} #{@cmdarg}\e[0m")

  [ type, [val, range] ]
end

#decorate(range, token, info) ⇒ Object (private)



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/parser/lexer/explanation.rb', line 35

def decorate(range, token, info)
  from, to = range.begin.column, range.end.column

  line = range.source_line
  line[from...to] = "\e[4m#{line[from...to]}\e[0m"

  tail_len   = to - from - 1
  tail       = "~" * (tail_len >= 0 ? tail_len : 0)
  decoration =  "#{" " * from}\e[1;31m^#{tail}\e[0m #{token} ".
                    ljust(70) + info

  [ line, decoration ]
end

#state=(new_state) ⇒ Object



26
27
28
29
30
31
# File 'lib/parser/lexer/explanation.rb', line 26

def state=(new_state)
  puts "  \e[1;33m>>> STATE SET <<<\e[0m " +
       "#{new_state.to_s.ljust(12)} #{@cond} #{@cmdarg}".rjust(66)

  self.state_before_explanation = new_state
end