Class: YARD::Tags::TypesExplainer::Parser

Inherits:
Object
  • Object
show all
Includes:
CodeObjects
Defined in:
lib/yard/tags/types_explainer.rb

Constant Summary collapse

TOKENS =
{
  :collection_start => /</,
  :collection_end => />/,
  :fixed_collection_start => /\(/,
  :fixed_collection_end => /\)/,
  :type_name => /#{ISEP}#{METHODNAMEMATCH}|#{NAMESPACEMATCH}|\w+/,
  :type_next => /[,;]/,
  :whitespace => /\s+/,
  :hash_collection_start => /\{/,
  :hash_collection_next => /=>/,
  :hash_collection_end => /\}/,
  :parse_end => nil
}

Constants included from CodeObjects

CodeObjects::BUILTIN_ALL, CodeObjects::BUILTIN_CLASSES, CodeObjects::BUILTIN_EXCEPTIONS, CodeObjects::BUILTIN_EXCEPTIONS_HASH, CodeObjects::BUILTIN_MODULES, CodeObjects::CONSTANTMATCH, CodeObjects::CONSTANTSTART, CodeObjects::CSEP, CodeObjects::CSEPQ, CodeObjects::ISEP, CodeObjects::ISEPQ, CodeObjects::METHODMATCH, CodeObjects::METHODNAMEMATCH, CodeObjects::NAMESPACEMATCH, CodeObjects::NSEP, CodeObjects::NSEPQ, CodeObjects::PROXY_MATCH

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CodeObjects::NamespaceMapper

#clear_separators, #default_separator, on_invalidate, #register_separator, #separators, #separators_for_type, #separators_match, #types_for_separator, #unregister_separator_by_type

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.



117
118
119
# File 'lib/yard/tags/types_explainer.rb', line 117

def initialize(string)
  @scanner = StringScanner.new(string)
end

Class Method Details

.parse(string) ⇒ Object



113
114
115
# File 'lib/yard/tags/types_explainer.rb', line 113

def self.parse(string)
  new(string).parse
end

Instance Method Details

#parseObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/yard/tags/types_explainer.rb', line 121

def parse
  types = []
  type = nil
  name = nil
  loop do
    found = false
    TOKENS.each do |token_type, match|
      # TODO: cleanup this code.
      # rubocop:disable Lint/AssignmentInCondition
      next unless (match.nil? && @scanner.eos?) || (match && token = @scanner.scan(match))
      found = true
      case token_type
      when :type_name
        raise SyntaxError, "expecting END, got name '#{token}'" if name
        name = token
      when :type_next
        raise SyntaxError, "expecting name, got '#{token}' at #{@scanner.pos}" if name.nil?
        type = Type.new(name) unless type
        types << type
        type = nil
        name = nil
      when :fixed_collection_start, :collection_start
        name ||= "Array"
        klass = token_type == :collection_start ? CollectionType : FixedCollectionType
        type = klass.new(name, parse)
      when :hash_collection_start
        name ||= "Hash"
        type = HashCollectionType.new(name, parse, parse)
      when :hash_collection_next, :hash_collection_end, :fixed_collection_end, :collection_end, :parse_end
        raise SyntaxError, "expecting name, got '#{token}'" if name.nil?
        type = Type.new(name) unless type
        types << type
        return types
      end
    end
    raise SyntaxError, "invalid character at #{@scanner.peek(1)}" unless found
  end
end