class Yard2steep::Type::Parser

Public Class Methods

new(tokens) click to toggle source

@param [Array<String>] tokens

# File lib/yard2steep/type/parser.rb, line 11
def initialize(tokens)
  @tokens = tokens
  @types  = []
end
parse(tokens) click to toggle source

@param [Array<String>] tokens @return [Array<TypeBase>]

# File lib/yard2steep/type/parser.rb, line 6
def self.parse(tokens)
  Parser.new(tokens).parse
end

Public Instance Methods

parse() click to toggle source

@reutrn [Array<TypeBase>]

# File lib/yard2steep/type/parser.rb, line 17
def parse
  debug_print!(@tokens)

  return [] if (@tokens.size == 0)

  @types.push(parse_type)
  while @tokens.size > 0
    expect!(',')
    @types.push(parse_type)
  end

  UnionType.new(types: @types)
end

Private Instance Methods

debug_print!(message) click to toggle source

@param [String] message @return [void]

# File lib/yard2steep/type/parser.rb, line 144
def debug_print!(message)
  # TODO(south37) Add flag
  # print message
end
expect!(token) click to toggle source

@param [String] token

# File lib/yard2steep/type/parser.rb, line 127
def expect!(token)
  t = get
  Util.assert! { t == token }
end
get() click to toggle source

@return [String]

# File lib/yard2steep/type/parser.rb, line 133
def get
  @tokens.shift
end
parse_array() click to toggle source

@return [ArrayType]

# File lib/yard2steep/type/parser.rb, line 84
def parse_array
  case peek
  when '<'
    expect!('<')
    type = UnionType.new(
      types: parse_multiple_types('>')
    )
  when '('
    expect!('(')
    type = UnionType.new(
      types: parse_multiple_types(')')
    )
  else
    type = AnyType.new
  end

  ArrayType.new(type: type)
end
parse_hash() click to toggle source

@return [HashType]

# File lib/yard2steep/type/parser.rb, line 104
def parse_hash
  debug_print!("parse_hash, peek: #{peek}")

  case peek
  when '{'
    expect!('{')
    key = UnionType.new(types: parse_multiple_types('='))
    expect!('>')
    val = UnionType.new(types: parse_multiple_types('}'))

    HashType.new(
      key: key,
      val: val,
    )
  else
    HashType.new(
      key: AnyType.new,
      val: AnyType.new,
    )
  end
end
parse_multiple_types(term_s) click to toggle source

@param [String] term_s @return [Array<TypeBase>]

# File lib/yard2steep/type/parser.rb, line 61
def parse_multiple_types(term_s)
  t = peek
  if t == term_s
    expect!(term_s)
    return []
  end

  r = []
  r.push(parse_type)

  while t = peek
    break unless t == ','
    expect!(',')
    r.push(parse_type)
  end

  debug_print! "term_s: #{term_s}\n"
  expect!(term_s)

  r
end
parse_normal_type(t) click to toggle source

@param [String] t @return [NormalType]

# File lib/yard2steep/type/parser.rb, line 55
def parse_normal_type(t)
  NormalType.new(type: t)
end
parse_type() click to toggle source

@return [TypeBase]

# File lib/yard2steep/type/parser.rb, line 34
def parse_type
  t = get
  Util.assert! { t }

  case t
  when 'Array'
    r = parse_array
  when 'Hash'
    r = parse_hash
  else
    r = parse_normal_type(t)
  end

  debug_print! "type: #{r}\n"
  debug_print! "peek: #{peek}\n"

  r
end
peek() click to toggle source

@return [String]

# File lib/yard2steep/type/parser.rb, line 138
def peek
  @tokens[0]
end