Class: SyntaxSuggest::LeftRightLexCount

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_suggest/left_right_lex_count.rb

Overview

Find mis-matched syntax based on lexical count

Used for detecting missing pairs of elements each keyword needs an end, each ‘needs a ‘’ etc.

Example:

left_right = LeftRightLexCount.new
left_right.count_kw
left_right.missing.first
# => "end"

left_right = LeftRightLexCount.new
source = "{ a: b, c: d" # Note missing '}'
LexAll.new(source: source).each do |lex|
  left_right.count_lex(lex)
end
left_right.missing.first
# => "}"

Constant Summary collapse

PAIRS =
{
  "{" => "}",
  "[" => "]",
  "(" => ")"
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeLeftRightLexCount

Returns a new instance of LeftRightLexCount.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/syntax_suggest/left_right_lex_count.rb', line 25

def initialize
  @kw_count = 0
  @end_count = 0

  @count_for_char = {
    "{" => 0,
    "}" => 0,
    "[" => 0,
    "]" => 0,
    "(" => 0,
    ")" => 0,
    "|" => 0
  }
end

Instance Method Details

#count_endObject



44
45
46
# File 'lib/syntax_suggest/left_right_lex_count.rb', line 44

def count_end
  @end_count += 1
end

#count_for_char(char) ⇒ Object



101
102
103
# File 'lib/syntax_suggest/left_right_lex_count.rb', line 101

def count_for_char(char)
  @count_for_char[char]
end

#count_kwObject



40
41
42
# File 'lib/syntax_suggest/left_right_lex_count.rb', line 40

def count_kw
  @kw_count += 1
end

#count_lex(lex) ⇒ Object

Count source code characters

Example:

left_right = LeftRightLexCount.new
left_right.count_lex(LexValue.new(1, :on_lbrace, "{", Ripper::EXPR_BEG))
left_right.count_for_char("{")
# => 1
left_right.count_for_char("}")
# => 0


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/syntax_suggest/left_right_lex_count.rb', line 58

def count_lex(lex)
  case lex.type
  when :on_tstring_content
    # ^^^
    # Means it's a string or a symbol `"{"` rather than being
    # part of a data structure (like a hash) `{ a: b }`
    # ignore it.
  when :on_words_beg, :on_symbos_beg, :on_qwords_beg,
       :on_qsymbols_beg, :on_regexp_beg, :on_tstring_beg
    # ^^^
    # Handle shorthand syntaxes like `%Q{ i am a string }`
    #
    # The start token will be the full thing `%Q{` but we
    # need to count it as if it's a `{`. Any token
    # can be used
    char = lex.token[-1]
    @count_for_char[char] += 1 if @count_for_char.key?(char)
  when :on_embexpr_beg
    # ^^^
    # Embedded string expressions like `"#{foo} <-embed"`
    # are parsed with chars:
    #
    # `#{` as :on_embexpr_beg
    #  `}` as :on_embexpr_end
    #
    # We cannot ignore both :on_emb_expr_beg and :on_embexpr_end
    # because sometimes the lexer thinks something is an embed
    # string end, when it is not like `lol = }` (no clue why).
    #
    # When we see `#{` count it as a `{` or we will
    # have a mis-match count.
    #
    case lex.token
    when "\#{"
      @count_for_char["{"] += 1
    end
  else
    @end_count += 1 if lex.is_end?
    @kw_count += 1 if lex.is_kw?
    @count_for_char[lex.token] += 1 if @count_for_char.key?(lex.token)
  end
end

#missingObject

Returns an array of missing syntax characters or ‘“end”` or `“keyword”`

left_right.missing
# => ["}"]


110
111
112
113
114
115
116
# File 'lib/syntax_suggest/left_right_lex_count.rb', line 110

def missing
  out = missing_pairs
  out << missing_pipe
  out << missing_keyword_end
  out.compact!
  out
end