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)


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ibex/cli/explain.rb', line 63

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)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ibex/cli/explain.rb', line 40

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|
      @options[:algorithm] = value.to_sym
    end
    options.on("--entry-isolation", "build independent state sets for each start symbol") do
      @options[:entry_isolation] = true
    end
    options.on("--mode=MODE", %w[default extended], "grammar mode") { |value| @options[:mode] = value.to_sym }
    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)


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

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 = normalize_grammar_path(path)
  automaton = LALR::Builder.new(
    grammar, algorithm: @options[:algorithm] || :lalr, entry_isolation: @options[:entry_isolation] == true
  ).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