Sha256: 9b1a9d140dbeb2247e1a308a1564825f56d555a739a4676ef8f72f2b21b9c434

Contents?: true

Size: 1.38 KB

Versions: 6883

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

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 @context.in_kwarg

      puts decorate(range,
                    Color.green("#{type} #{val.inspect}"),
                    "#{state.to_s.ljust(12)} #{@cond} #{@cmdarg} #{more}")

      [ type, [val, range] ]
    end

    def state=(new_state)
      puts "  #{Color.yellow(">>> STATE SET <<<", bold: true)} " +
           "#{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] = Color.underline(line[from...to])

      tail_len   = to - from - 1
      tail       = '~' * (tail_len >= 0 ? tail_len : 0)
      decoration =  "#{" " * from}#{Color.red("^#{tail}", bold: true)} #{token} ".
                        ljust(68) + info

      [ line, decoration ]
    end

  end

end

Version data entries

6,883 entries across 6,877 versions & 35 rubygems

Version Path
ory-client-0.0.1.alpha111 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha110 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha109 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
avalara_sdk-2.4.26 vendor/bundle/ruby/2.6.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha108 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-oathkeeper-client-0.38.23.beta1 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-oathkeeper-client-0.38.22.beta1 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha107 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-hydra-client-1.11.7 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha106 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-hydra-client-1.11.6 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-keto-client-0.8.0.alpha1 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha105 vendor/bundle/ruby/2.5.0/gems/parser-3.1.1.0/lib/parser/lexer/explanation.rb
parser-3.1.1.0 lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha104 vendor/bundle/ruby/2.5.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha103 vendor/bundle/ruby/2.5.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb
ory-hydra-client-1.11.5 vendor/bundle/ruby/2.5.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha101 vendor/bundle/ruby/2.5.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb
ory-client-0.0.1.alpha100 vendor/bundle/ruby/2.5.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb
ory-hydra-client-1.11.4 vendor/bundle/ruby/2.5.0/gems/parser-3.1.0.0/lib/parser/lexer/explanation.rb