类 SyntaxSuggest::LeftRightLexCount
Find
基于词法计数的语法不匹配
用于检测缺少的元素对,每个关键字都需要一个 end,每个 ‘{’ 需要一个 ‘}’ 等等。
示例
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 # => "}"
常量
- PAIRS
公共类方法
new() 点击切换源代码
# 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
公共实例方法
count_end() 点击切换源代码
# File lib/syntax_suggest/left_right_lex_count.rb, line 44 def count_end @end_count += 1 end
count_for_char(char) 点击切换源代码
# File lib/syntax_suggest/left_right_lex_count.rb, line 101 def count_for_char(char) @count_for_char[char] end
count_kw() 点击切换源代码
# File lib/syntax_suggest/left_right_lex_count.rb, line 40 def count_kw @kw_count += 1 end
count_lex(lex) 点击切换源代码
统计源代码字符
示例
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
# 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
missing() 点击切换源代码
返回一个包含缺少的语法字符或 ‘“end”` 或 `“keyword”` 的数组
left_right.missing # => ["}"]
# 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
私有实例方法
missing_keyword_end() 点击切换源代码
关键字需要 ends,ends 需要关键字
如果我们有更多关键字,则缺少一个 ‘end`;如果我们有更多 `end`-s,则缺少一个关键字
# File lib/syntax_suggest/left_right_lex_count.rb, line 148 def missing_keyword_end case @kw_count <=> @end_count when 1 "end" when 0 nil when -1 "keyword" end end
missing_pairs() 点击切换源代码
像 ‘{` 这样的开始字符需要像 `}` 这样的结束字符。
当检测到不匹配计数时,建议缺少的成员。
例如,如果有 3 个 ‘}` 和只有两个 `{`,则返回 `“{”`
# File lib/syntax_suggest/left_right_lex_count.rb, line 131 def missing_pairs PAIRS.map do |(left, right)| case @count_for_char[left] <=> @count_for_char[right] when 1 right when 0 nil when -1 left end end end
missing_pipe() 点击切换源代码
管道成对出现。如果管道数量为奇数,则缺少一个
# File lib/syntax_suggest/left_right_lex_count.rb, line 162 def missing_pipe if @count_for_char["|"].odd? "|" end end