Module: Ibex::CLIBisonImport

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

Overview

CLI boundary for analysis-only Bison grammar import.

Instance Method Summary collapse

Instance Method Details

#add_bison_import_budgets(options, settings) ⇒ void

This method returns an undefined value.

RBS:

  • (OptionParser options, bison_import_options settings) -> void

Parameters:

  • options (OptionParser)
  • settings (bison_import_options)


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

def add_bison_import_budgets(options, settings)
  {
    "max-bytes" => :max_bytes,
    "max-tokens" => :max_tokens,
    "max-rules" => :max_rules,
    "max-actions" => :max_actions
  }.each do |name, key|
    options.on("--#{name}=N", Integer, "positive import budget") do |value|
      raise OptionParser::InvalidArgument, "--#{name} must be positive" unless value.positive?

      settings[key] = value
    end
  end
end

#bison_import_option_parser(settings) ⇒ OptionParser

RBS:

  • (bison_import_options settings) -> OptionParser

Parameters:

  • settings (bison_import_options)

Returns:

  • (OptionParser)


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

def bison_import_option_parser(settings)
  OptionParser.new do |options|
    options.banner = "Usage: ibex import bison [options] grammar.y"
    options.on("--format=FORMAT", %w[source json], "converted source or JSON report") do |value|
      settings[:format] = value
    end
    options.on("-o FILE", "--output=FILE", "write atomically to FILE") { |value| settings[:output] = value }
    options.on("--class=NAME", "generated analysis grammar class") { |value| settings[:class_name] = value }
    add_bison_import_budgets(options, settings)
    options.on("--help", "show help") { settings[:help] = true }
  end
end

#build_bison_import(path, settings) ⇒ BisonImport::Result

RBS:

  • (String path, bison_import_options settings) -> BisonImport::Result

Parameters:

  • path (String)
  • settings (bison_import_options)

Returns:



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ibex/cli/bison_import.rb', line 93

def build_bison_import(path, settings)
  BisonImport::Importer.new(
    File.binread(path),
    file: path,
    class_name: settings[:class_name],
    max_bytes: settings.fetch(:max_bytes),
    max_tokens: settings.fetch(:max_tokens),
    max_rules: settings.fetch(:max_rules),
    max_actions: settings.fetch(:max_actions)
  ).run
end

#default_bison_import_optionsbison_import_options

RBS:

  • () -> bison_import_options

Returns:

  • (bison_import_options)


52
53
54
55
56
57
58
59
60
# File 'lib/ibex/cli/bison_import.rb', line 52

def default_bison_import_options
  {
    format: "source",
    max_bytes: BisonImport::Importer::DEFAULT_MAX_BYTES,
    max_tokens: BisonImport::Importer::DEFAULT_MAX_TOKENS,
    max_rules: BisonImport::Importer::DEFAULT_MAX_RULES,
    max_actions: BisonImport::Importer::DEFAULT_MAX_ACTIONS
  }
end

#run_bison_import_command(arguments) ⇒ Integer

RBS:

  • (Array[String] arguments) -> Integer

Parameters:

  • arguments (Array[String])

Returns:

  • (Integer)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ibex/cli/bison_import.rb', line 28

def run_bison_import_command(arguments)
  kind = arguments.shift
  raise Ibex::Error, "(bison-import):1:1: import requires the `bison` format" unless kind == "bison"

  settings = default_bison_import_options
  parser = bison_import_option_parser(settings)
  paths = parser.parse(arguments)
  if settings[:help]
    @stdout.puts(parser)
    return 0
  end
  raise Ibex::Error, "(bison-import):1:1: import bison requires exactly one source file" unless paths.length == 1

  path = paths.fetch(0)
  result = build_bison_import(path, settings)
  rendered = settings.fetch(:format) == "json" ? "#{JSON.pretty_generate(result.to_h)}\n" : result.source
  write_bison_import(path, settings[:output], rendered)
  0
rescue BisonImport::BudgetExceeded => e
  @stdout.puts(JSON.pretty_generate({ ibex_report: "bison_import", schema_version: 1 }.merge(e.details)))
  2
end

#write_bison_import(input, output, source) ⇒ void

This method returns an undefined value.

RBS:

  • (String input, String? output, String source) -> void

Parameters:

  • input (String)
  • output (String, nil)
  • source (String)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ibex/cli/bison_import.rb', line 106

def write_bison_import(input, output, source)
  unless output
    @stdout.write(source)
    return
  end
  if same_file_target?(input, output)
    raise Ibex::Error, "(bison-import):1:1: import input and output paths must be distinct"
  end
  if File.symlink?(output) || (File.exist?(output) && File.stat(output).nlink > 1)
    raise Ibex::Error, "(bison-import):1:1: output refuses symlink aliases and files with multiple hard links"
  end

  atomic_write_ir(output, source)
end