Sha256: 8a60dd14a171119bdc53928c512160397ab3b61fd9d694f01cb7b88b030995f9

Contents?: true

Size: 1.31 KB

Versions: 51

Compression:

Stored size: 1.31 KB

Contents

module Parser

  module Lexer::Explanation

    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

    # Like #advance, but also pretty-print the token and its position
    # in the stream to `stdout`.
    def advance
      type, (val, range) = advance_before_explanation

      more = "(in-kwarg)" if @in_kwarg

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

      [ type, [val, range] ]
    end

    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

    private

    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

  end

end

Version data entries

51 entries across 51 versions & 4 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/parser-2.4.0.2/lib/parser/lexer/explanation.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/parser-2.4.0.2/lib/parser/lexer/explanation.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/explanation.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/explanation.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/explanation.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.2/lib/parser/lexer/explanation.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.0/lib/parser/lexer/explanation.rb
parser-2.4.0.2 lib/parser/lexer/explanation.rb
parser-2.4.0.1 lib/parser/lexer/explanation.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/parser-2.4.0.0/lib/parser/lexer/explanation.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/parser-2.4.0.0/lib/parser/lexer/explanation.rb
parser-2.4.0.0 lib/parser/lexer/explanation.rb
parser-2.3.3.1 lib/parser/lexer/explanation.rb
parser-2.3.3.0 lib/parser/lexer/explanation.rb
parser-2.3.2.0 lib/parser/lexer/explanation.rb
parser-2.3.1.4 lib/parser/lexer/explanation.rb
parser-2.3.1.3 lib/parser/lexer/explanation.rb
erruby_parser-2.3.1.2 lib/parser/lexer/explanation.rb
parser-2.3.1.2 lib/parser/lexer/explanation.rb
parser-2.3.1.1 lib/parser/lexer/explanation.rb