class YARD::Tags::TypesExplainer::Parser
@private
Constants
- TOKENS
Public Class Methods
Source
# File lib/yard/tags/types_explainer.rb, line 270 def initialize(string) @scanner = StringScanner.new(string) end
Source
# File lib/yard/tags/types_explainer.rb, line 266 def self.parse(string) new(string).parse end
Public Instance Methods
Source
# File lib/yard/tags/types_explainer.rb, line 275 def parse(until_tokens: [:parse_end]) parse_until(until_tokens).first end
@return [Array(Boolean, Array<Type>)] - finished, types
Private Instance Methods
Source
# File lib/yard/tags/types_explainer.rb, line 524 def create_type(name) if name[0, 1] == ":" || (name[0, 1] =~ /['"]/ && name[-1, 1] =~ /['"]/) LiteralType.new(name) elsif name[0, 1] == "#" DuckType.new(name) else Type.new(name) end end
Source
# File lib/yard/tags/types_explainer.rb, line 519 def finish_group(conjuncts, last_type) return last_type if conjuncts.empty? GroupType.new(conjuncts + [last_type]) end
Combines the ‘|`-separated conjuncts of one order-dependent-list slot (`A | B` in `Array(A | B, C)`) with the slot’s final type into a single {GroupType}, the same representation ‘[…]` produces - so `Array(A | B, C)` and `Array([A | B], C)` describe the same type.
@param conjuncts [Array<Type>] the conjuncts seen so far (may be empty) @param last_type [Type] the final conjunct in the slot @return [Type] the combined type for this slot
Source
# File lib/yard/tags/types_explainer.rb, line 500 def finish_intersection(conjuncts, last_type) return last_type if conjuncts.empty? all_types = conjuncts + [last_type] if all_types.all? {|t| t.is_a?(DuckType) } DuckType.new(all_types.map(&:name).join(' & ')) else IntersectionType.new(all_types) end end
Combines the conjuncts of an ‘A & B & …` intersection with the final type in the chain. Consecutive duck-types (`#foo & bar`) collapse into a single {DuckType} listing all of the methods, to match the pre-existing duck-type convention; any other mix of types becomes an {IntersectionType}.
@param conjuncts [Array<Type>] the conjuncts seen so far (may be empty) @param last_type [Type] the final conjunct in the chain @return [Type] the combined type for this union slot
Source
# File lib/yard/tags/types_explainer.rb, line 471 def parse_collection(name, fixed:) nested_types, = parse_until( [:fixed_collection_end, :collection_end, :parse_end], slot_pipe: true ) if fixed FixedCollectionType.new(name, nested_types) elsif name == "Hash" && nested_types.size == 2 # `Hash<KeyType, ValueType>` is documented as positional # (slot 0 = key type, slot 1 = value type), matching the # dedicated `Hash{K=>V}` syntax - not an implicit union. HashCollectionType.new(name, [nested_types[0]], [nested_types[1]]) else # Whether these type parameters get described as a union # (Array, Set) or neutrally (everything else) doesn't # affect parsing, so it's not decided here - see # {CollectionType#union?}. CollectionType.new(name, nested_types) end end
Parses the type parameters of a ‘name<…>` or `name(…)` that has just been opened, in either a type list or a hash’s keys.
Both a fixed tuple’s ‘(…)` and a parameterized type’s ‘<…>` treat `,` as separating positional type parameters, and a bare `|` as grouping alternatives within just one of them (`finish_group`) - parsing doesn’t need to know whether ‘name` means an implicit union. Whether a name’s type parameters get described as a union (Array, Set) or neutrally (everything else) is purely a rendering choice, made below.
@param name [String] the collection’s name @param fixed [Boolean] whether it was opened with ‘(` rather than `<` @return [Type] the parsed collection type
Source
# File lib/yard/tags/types_explainer.rb, line 394 def parse_hash_collection(name) key_value_pairs = [] current_keys = [] key_name = nil key_type = nil intersection_conjuncts = [] finished = false # Finalizes whatever key is pending (if any) into `current_keys`, # the same way `parse_until` finalizes a union member - `&` binds # tighter than the `,`/`|` (synonyms here, as everywhere outside # `(...)`) that separates keys. finalize_key = lambda do next if key_name.nil? key_type = create_type(key_name) unless key_type current_keys << finish_intersection(intersection_conjuncts, key_type) intersection_conjuncts = [] key_name = nil key_type = nil end parse_with_handlers do |token_type, token| case token_type when :type_name raise SyntaxError, "expecting END, got name '#{token}'" if key_name key_name = token when :intersect raise SyntaxError, "expecting name, got '&' at #{@scanner.pos}" if key_name.nil? key_type = create_type(key_name) unless key_type intersection_conjuncts << key_type key_name = nil key_type = nil when :type_next, :union # ',' and '|' are synonyms here, as everywhere outside '(...)' raise SyntaxError, "expecting name, got '#{token}' at #{@scanner.pos}" if key_name.nil? finalize_key.call when :fixed_collection_start, :collection_start key_name ||= "Array" key_type = parse_collection(key_name, fixed: token_type == :fixed_collection_start) when :hash_collection_start key_name ||= "Hash" key_type = parse_hash_collection(key_name) when :hash_collection_value # => - current keys map to the next value(s) finalize_key.call raise SyntaxError, "no keys before =>" if current_keys.empty? values, end_token = parse_until([:hash_collection_value_end, :hash_collection_end, :parse_end]) key_value_pairs << [current_keys, values] current_keys = [] finished = end_token != :hash_collection_value_end when :hash_collection_end, :parse_end # End of hash finished = true when :whitespace # Ignore whitespace end [finished, HashCollectionType.new(name, key_value_pairs)] end end
Source
# File lib/yard/tags/types_explainer.rb, line 306 def parse_until(until_tokens, slot_pipe: false) current_parsed_types = [] type = nil name = nil finished = false end_token = nil intersection_conjuncts = [] slot_conjuncts = [] types = parse_with_handlers do |token_type, token| case token_type when *until_tokens raise SyntaxError, "expecting name, got '#{token}'" if name.nil? type = create_type(name) unless type slot = finish_intersection(intersection_conjuncts, type) slot = finish_group(slot_conjuncts, slot) if slot_pipe current_parsed_types << slot intersection_conjuncts = [] slot_conjuncts = [] finished = true end_token = token_type when :type_name raise SyntaxError, "expecting END, got name '#{token}'" if name name = token when :intersect raise SyntaxError, "expecting name, got '&' at #{@scanner.pos}" if name.nil? type = create_type(name) unless type intersection_conjuncts << type name = nil type = nil when :type_next raise SyntaxError, "expecting name, got '#{token}' at #{@scanner.pos}" if name.nil? type = create_type(name) unless type slot = finish_intersection(intersection_conjuncts, type) slot = finish_group(slot_conjuncts, slot) if slot_pipe current_parsed_types << slot intersection_conjuncts = [] slot_conjuncts = [] name = nil type = nil when :union raise SyntaxError, "expecting name, got '|' at #{@scanner.pos}" if name.nil? type = create_type(name) unless type combined = finish_intersection(intersection_conjuncts, type) intersection_conjuncts = [] if slot_pipe slot_conjuncts << combined else current_parsed_types << combined end name = nil type = nil when :fixed_collection_start, :collection_start name ||= "Array" type = parse_collection(name, fixed: token_type == :fixed_collection_start) when :group_start raise SyntaxError, "'[' cannot follow a type name" if name nested_types, = parse_until([:group_end, :parse_end]) type = GroupType.new(nested_types) name = "Group" when :hash_collection_start name ||= "Hash" type = parse_hash_collection(name) end [finished, current_parsed_types] end [types, end_token] end
@param slot_pipe [Boolean] whether ‘,` separates positional type
parameters/slots (a fixed tuple's `(...)`, or a parameterized
type's `<...>`), in which case a bare `|` groups alternatives
within a single one of them instead of adding another
top-level item. False for containers whose whole
comma-separated list already means "any of these" (the top
level, `{...}`, and `[...]`) - there, `,` and `|` both just add
another alternative to that same list.
‘&` (intersection) is legal anywhere a type is expected (it never
needs this distinction), accumulated via
`intersection_conjuncts`/{#finish_intersection}, and always
binds tighter than whichever separator is active: `A & B, C` is
`(A & B), C` everywhere, and `Array(A & B | C, D)` is
`Array((A & B) | C, D)` - a 2-slot tuple whose first slot is
`(A & B) or C`.
‘[…]` groups a union (spelled with either `,` or `|`) into a
single type usable as one conjunct of an intersection at the top level, where a bare union would otherwise just add another independent top-level item instead: `[A | B] & C` groups `A` and `B` before intersecting with `C`, where `A | B & C` would parse as the two top-level items `A` and `B & C`.
@return [Array(Array<Type>, Symbol)] the parsed types and the
token that ended the list
Source
# File lib/yard/tags/types_explainer.rb, line 376 def parse_with_handlers 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 # @type [Array<Type>] finished, types = yield(token_type, token) return types if finished break end raise SyntaxError, "invalid character at #{@scanner.peek(1)}" unless found end nil end
@return [Array<Type>]