Class: YardToRbsInline::YardType::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/yard_to_rbs_inline/yard_type/scanner.rb

Defined Under Namespace

Classes: ScanError, Token

Constant Summary collapse

TOKEN_PATTERNS =
{
  generic_start: /</,
  generic_end: />/,
  tuple_start: /\(/,
  tuple_end: /\)/,
  duck_symbol: /#/,
  separator: /[,;]/,
  arrow: /=>/,
  hash_start: /\{/,
  hash_end: /\}/,
  name: /(::|\w)+/,
  symbol: /:\w+/,
  string: [/"[^"]*"/, /'[^']*'/],
  integer: /\d+/,
  spaces: /\s+/
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Scanner

: (String text) -> untyped



34
35
36
# File 'lib/yard_to_rbs_inline/yard_type/scanner.rb', line 34

def initialize(text)
  @text = text
end

Instance Attribute Details

#textObject (readonly)

: String



31
32
33
# File 'lib/yard_to_rbs_inline/yard_type/scanner.rb', line 31

def text
  @text
end

Instance Method Details

#to_racc_tokensObject

: () -> Array[[Symbol | boolish, String]]



39
40
41
42
43
44
45
# File 'lib/yard_to_rbs_inline/yard_type/scanner.rb', line 39

def to_racc_tokens
  tokens.map do |token| #$ [Symbol | bool, String]
    [token.kind.to_s.upcase.to_sym, token.content]
  end + [
    [false, "EOS"] #: [bool, String]
  ]
end

#tokensObject

: () -> Array



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/yard_to_rbs_inline/yard_type/scanner.rb', line 50

def tokens
  @tokens ||= begin
    scanner = StringScanner.new(text)
    tokens = [] #: Array[Token]

    scan_step = lambda do
      TOKEN_PATTERNS.each do |kind, pattern_or_patterns|
        Array(pattern_or_patterns).each do |pattern|
          next unless (matched = scanner.scan(pattern))

          tokens << Token.new(kind: kind, content: matched) unless kind == :spaces
          # For now, steep parse this as a return of the whole method
          return # steep:ignore
        end
      end

      raise ScanError, "Unknown keyword #{scanner.rest} (in #{text})"
    end

    scan_step.call until scanner.eos?

    tokens
  end
end