类 SyntaxSuggest::ExplainSyntax
根据语法错误的来源解释语法错误
示例
source = "def foo; puts 'lol'" # Note missing end explain ExplainSyntax.new( code_lines: CodeLine.from_source(source) ).call explain.errors.first # => "Unmatched keyword, missing `end' ?"
当无法通过词法计数确定错误时,解析器会针对输入运行,并返回原始错误。
示例
source = "1 * " # Note missing a second number explain ExplainSyntax.new( code_lines: CodeLine.from_source(source) ).call explain.errors.first # => "syntax error, unexpected end-of-input"
常量
- INVERSE
公共类方法
new(code_lines:) 点击切换源代码
# File lib/syntax_suggest/explain_syntax.rb, line 54 def initialize(code_lines:) @code_lines = code_lines @left_right = LeftRightLexCount.new @missing = nil end
公共实例方法
call() 点击切换源代码
# File lib/syntax_suggest/explain_syntax.rb, line 60 def call @code_lines.each do |line| line.lex.each do |lex| @left_right.count_lex(lex) end end self end
errors() 点击切换源代码
返回一个语法错误消息数组
如果未找到缺少的配对,则回退到原始错误消息
# File lib/syntax_suggest/explain_syntax.rb, line 109 def errors if missing.empty? return GetParseErrors.errors(@code_lines.map(&:original).join).uniq end missing.map { |miss| why(miss) } end
missing() 点击切换源代码
返回一个缺少元素的数组
例如,这
ExplainSyntax.new(code_lines: lines).missing # => ["}"]
将表明源代码中缺少“}`”字符
# File lib/syntax_suggest/explain_syntax.rb, line 79 def missing @missing ||= @left_right.missing end
why(miss) 点击切换源代码
将缺少的字符串转换为人类可理解的解释。
示例
explain.why("}") # => "Unmatched `{', missing `}' ?"
# File lib/syntax_suggest/explain_syntax.rb, line 91 def why(miss) case miss when "keyword" "Unmatched `end', missing keyword (`do', `def`, `if`, etc.) ?" when "end" "Unmatched keyword, missing `end' ?" else inverse = INVERSE.fetch(miss) { raise "Unknown explain syntax char or key: #{miss.inspect}" } "Unmatched `#{inverse}', missing `#{miss}' ?" end end