Module: Ibex::CLIDebug

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

Overview

Safe parser-table simulation subcommand.

Instance Method Summary collapse

Instance Method Details

#debug_options(arguments) ⇒ debug_options

RBS:

  • (Array[String] arguments) -> debug_options

Parameters:

  • arguments (Array[String])

Returns:



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

def debug_options(arguments)
  settings = {
    paths: [],
    format: "text",
    max_steps: TableSimulation::Simulator::DEFAULT_MAX_STEPS,
    max_stack: TableSimulation::Simulator::DEFAULT_MAX_STACK
  } #: debug_options
  parser = OptionParser.new do |options|
    options.banner = "Usage: ibex debug AUTOMATON.json [TOKEN...]"
    options.on("--format=FORMAT", %w[text json], "text or json") { |value| settings[:format] = value }
    options.on("--max-steps=N", Integer, "maximum simulated actions") do |value|
      settings[:max_steps] = positive_debug_budget(value, "max-steps")
    end
    options.on("--max-stack=N", Integer, "maximum simulated state stack depth") do |value|
      settings[:max_stack] = positive_debug_budget(value, "max-stack")
    end
  end
  settings[:paths] = parser.parse(arguments)
  settings
end

#positive_debug_budget(value, name) ⇒ Integer

RBS:

  • (Integer value, String name) -> Integer

Parameters:

  • value (Integer)
  • name (String)

Returns:

  • (Integer)


68
69
70
71
72
# File 'lib/ibex/cli/debug.rb', line 68

def positive_debug_budget(value, name)
  return value if value.positive?

  raise OptionParser::InvalidArgument, "--#{name} must be positive"
end

#run_debug_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
41
42
43
# File 'lib/ibex/cli/debug.rb', line 21

def run_debug_command(arguments)
  settings = debug_options(arguments)
  operands = settings.fetch(:paths)
  raise Ibex::Error, "(cli):1:1: debug requires an Automaton IR file" if operands.empty?

  path = operands.first
  value = IR::Validator.validate(File.read(path))
  raise Ibex::Error, "#{path}:1:1: debug requires Automaton IR" unless value.is_a?(IR::Automaton)

  simulator = TableSimulation::Simulator.new(
    value,
    max_steps: settings.fetch(:max_steps),
    max_stack: settings.fetch(:max_stack)
  )
  tokens = operands.drop(1)
  interactive = tokens.empty?
  format = settings.fetch(:format)
  result = interactive ? simulate_debug_input(simulator, format) : simulator.simulate(tokens)
  write_debug_result(result, format, steps_written: interactive && format == "text")
  result.status == :accepted ? 0 : 1
rescue ArgumentError => e
  raise Ibex::Error, "(debug):1:1: #{e.message}"
end

#simulate_debug_input(simulator, format) ⇒ TableSimulation::Result

RBS:

  • (TableSimulation::Simulator simulator, String format) -> TableSimulation::Result

Parameters:

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ibex/cli/debug.rb', line 75

def simulate_debug_input(simulator, format)
  session = simulator.start
  while (line = @stdin.gets)
    spelling = line.strip
    break if spelling.empty?

    steps = session.push(spelling)
    write_debug_steps(steps) if format == "text"
    break if session.status
  end
  written = session.steps.length
  result = session.finish
  write_debug_steps(result.steps.drop(written)) if format == "text"
  result
end

#write_debug_result(result, format, steps_written:) ⇒ void

This method returns an undefined value.

RBS:

  • (TableSimulation::Result result, String format, steps_written: bool) -> void

Parameters:



92
93
94
95
96
97
98
99
# File 'lib/ibex/cli/debug.rb', line 92

def write_debug_result(result, format, steps_written:)
  if format == "json"
    @stdout.write(result.to_json)
  else
    write_debug_steps(result.steps) unless steps_written
    @stdout.puts("status=#{result.status}")
  end
end

#write_debug_steps(steps) ⇒ void

This method returns an undefined value.

RBS:

  • (Array[TableSimulation::Step] steps) -> void

Parameters:



102
103
104
# File 'lib/ibex/cli/debug.rb', line 102

def write_debug_steps(steps)
  steps.each { |step| @stdout.puts(TableSimulation::Text.render_step(step)) }
end