Sha256: 0de1fb41b74d696c4226ff6e68d37de4eb0b1e631bc546c481e7908b24f9eab2

Contents?: true

Size: 1.99 KB

Versions: 63

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true
module YARD
  module Parser::Ruby::Legacy
    class TokenList < Array
      include RubyToken

      def initialize(content = nil)
        self << content if content
      end

      def to_s(full_statement = false, show_block = true)
        inject([]) do |acc, token|
          break acc if !full_statement && TkStatementEnd === token
          acc << (!show_block && TkBlockContents === token ? "" : token.text)
        end.join
      end

      # @param [TokenList, Token, String] tokens
      #   A list of tokens. If the token is a string, it
      #   is parsed with {RubyLex}.
      def push(*tokens)
        tokens.each do |tok|
          if tok.is_a?(TokenList) || tok.is_a?(Array)
            concat tok
          elsif tok.is_a?(Token)
            super tok
          elsif tok.is_a?(String)
            parse_content(tok)
          else
            raise ArgumentError, "Expecting token, list of tokens or string of code to be tokenized. Got #{tok.class}"
          end
        end
        self
      end
      alias << push

      def squeeze(type = TkSPACE)
        last = nil
        TokenList.new(map {|t| x = t.is_a?(type) && last.is_a?(type) ? nil : t; last = t; x })
      end

      private

      def parse_content(content)
        lex = RubyLex.new(content)
        loop do
          tk = lex.token
          break if tk.nil?
          self << convert_token(lex, tk)
        end
      end

      def convert_token(lex, tk)
        if TkIDENTIFIER === tk && lex.peek == ':'
          next_tk = lex.token
          sym = TkLABEL.new(tk.line_no, tk.char_no, nil)
          sym.lex_state = lex.lex_state
          sym.set_text(tk.text + next_tk.text)
        elsif TkSYMBEG === tk
          next_tk = lex.token
          if next_tk
            sym = TkSYMBOL.new(tk.line_no, tk.char_no, nil)
            sym.lex_state = lex.lex_state
            sym.set_text(tk.text + next_tk.text)
          else
            tk
          end
        else
          tk
        end
      end
    end
  end
end

Version data entries

63 entries across 62 versions & 9 rubygems

Version Path
kinetic_sdk-5.0.29 gems/yard-0.9.36/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.28 gems/yard-0.9.36/lib/yard/parser/ruby/legacy/token_list.rb
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
yard-0.9.37 lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.27 gems/yard-0.9.36/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.26 gems/yard-0.9.36/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.25 gems/yard-0.9.25/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.24 gems/yard-0.9.25/lib/yard/parser/ruby/legacy/token_list.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
yard-0.9.36 lib/yard/parser/ruby/legacy/token_list.rb
yard-0.9.35 lib/yard/parser/ruby/legacy/token_list.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.23 gems/yard-0.9.25/lib/yard/parser/ruby/legacy/token_list.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/yard-0.9.34/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.22 gems/yard-0.9.25/lib/yard/parser/ruby/legacy/token_list.rb
kinetic_sdk-5.0.21 gems/yard-0.9.25/lib/yard/parser/ruby/legacy/token_list.rb
yard-0.9.34 lib/yard/parser/ruby/legacy/token_list.rb