Module: MilkTea::Lexer::Recovery

Included in:
MilkTea::Lexer
Defined in:
lib/milk_tea/core/lexer/recovery.rb

Overview

Error-recovery heuristics: detecting a top-level declaration line to resynchronize on after an unterminated grouping or heredoc.

Instance Method Summary collapse

Instance Method Details

#top_level_resync_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/milk_tea/core/lexer/recovery.rb', line 8

def top_level_resync_line?(line)
  return false if line.strip.empty?
  return false if leading_space_count(line).positive?

  first_word = line.strip.split(/\s+/, 3)[0]
  second_word = line.strip.split(/\s+/, 3)[1]

  case first_word
  when *TOP_LEVEL_RESYNC_PREFIXES
    true
  when "async"
    second_word == "function"
  when "public"
    %w[function struct union enum flags variant type const var opaque interface extending attribute event].include?(second_word)
  when "foreign", "external"
    second_word == "function"
  else
    false
  end
end