Module: Ibex::CLIExplain

Defined in:
lib/ibex/cli/explain.rb,
sig/ibex/cli/explain.rbs

Overview

Dedicated command-line view for selected parser conflicts.

Instance Method Summary collapse

Instance Method Details

#add_explain_search_options(options) ⇒ void

This method returns an undefined value.

RBS:

  • (OptionParser options) -> void

Parameters:

  • options (OptionParser)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ibex/cli/explain.rb', line 68

def add_explain_search_options(options)
  options.on("--counterexample-max-tokens=N", Integer, "maximum counterexample search token budget") do |value|
    @options[:counterexample_max_tokens] = positive_counterexample_limit(value, "--counterexample-max-tokens")
  end
  options.on(
    "--counterexample-max-configurations=N", Integer, "maximum counterexample search configuration budget"
  ) do |value|
    @options[:counterexample_max_configurations] = positive_counterexample_limit(
      value, "--counterexample-max-configurations"
    )
  end
end

#explain_option_parserOptionParser

RBS:

  • () -> OptionParser

Returns:

  • (OptionParser)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ibex/cli/explain.rb', line 43

def explain_option_parser
  OptionParser.new do |options|
    options.banner = "Usage: ibex explain [options] grammarfile"
    options.on("--state=N", Integer, "select one automaton state") { |value| @options[:explain_state] = value }
    options.on("--token=NAME", "select a grammar token name or exact display name") do |value|
      @options[:explain_token] = value
    end
    options.on("--format=FORMAT", %w[text json], "text or json (default: text)") do |value|
      @options[:explain_format] = value
    end
    options.on("--algorithm=NAME", %w[slr lalr ielr lr1], "parser construction algorithm") do |value|
      set_configuration_option(:algorithm, value.to_sym)
    end
    options.on("--entry-isolation", "build independent state sets for each start symbol") do
      set_configuration_option(:entry_isolation, true)
    end
    options.on("--mode=MODE", %w[default extended], "grammar mode") do |value|
      set_configuration_option(:mode, value.to_sym)
    end
    add_explain_search_options(options)
    options.on("--help", "show help") { @options[:help] = true }
  end
end

#run_explain_command(arguments) ⇒ Integer

RBS:

  • (Array[String] arguments) -> Integer

Parameters:

  • arguments (Array[String])

Returns:

  • (Integer)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ibex/cli/explain.rb', line 21

def run_explain_command(arguments)
  parser = explain_option_parser
  remaining = parser.parse(arguments)
  return print_help(parser) if @options[:help]

  path = input_path(remaining)
  grammar = activate_analysis_grammar(normalize_grammar_path(path))
  automaton = LALR::Builder.new(
    grammar, algorithm: configuration_value("parser.algorithm"),
             entry_isolation: configuration_value("parser.entries") == :isolated
  ).build
  explanation = Codegen::Explain.new(
    automaton, state: @options[:explain_state], token: @options[:explain_token],
               max_tokens: @options.fetch(:counterexample_max_tokens),
               max_configurations: @options.fetch(:counterexample_max_configurations)
  )
  output = @options[:explain_format] == "json" ? JSON.pretty_generate(explanation.to_h) : explanation.render_text
  @stdout.puts(output)
  0
end