Skip to content

Commit 5f00d62

Browse files
committed
Represent CLI warnings and aborts as diagnostics
Route remaining user-facing command aborts and optional dependency warnings through Lrama::Diagnostic objects while preserving their existing stderr text. This closes the gap where some warning and error output bypassed the diagnostic model.
1 parent db864fd commit 5f00d62

6 files changed

Lines changed: 62 additions & 7 deletions

File tree

lib/lrama/command.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require_relative "diagnostic"
4+
35
module Lrama
46
class Command
57
LRAMA_LIB = File.realpath(File.join(File.dirname(__FILE__)))
@@ -12,7 +14,7 @@ def initialize(argv)
1214
@reporter = Reporter.new(**@options.report_opts)
1315
@warnings = Warnings.new(@logger, @options.warnings)
1416
rescue => e
15-
abort format_error_message(e.message)
17+
abort_with_diagnostic("command.option_error", e)
1618
end
1719

1820
def run
@@ -52,7 +54,7 @@ def build_grammar(text)
5254
grammar
5355
rescue => e
5456
raise e if @options.debug
55-
abort format_error_message(e.message)
57+
abort_with_diagnostic("command.grammar_error", e)
5658
end
5759

5860
def format_error_message(message)
@@ -61,6 +63,17 @@ def format_error_message(message)
6163
message.gsub(/.+/, "\e[1m\\&\e[m")
6264
end
6365

66+
def abort_with_diagnostic(id, exception)
67+
diagnostic = Lrama::Diagnostic.new(
68+
id: id,
69+
severity: :error,
70+
message: exception.message,
71+
details: { "exception_class" => exception.class.name }
72+
)
73+
74+
abort format_error_message(diagnostic.message)
75+
end
76+
6477
def merge_stdlib(grammar)
6578
return if grammar.no_stdlib
6679

lib/lrama/diagram.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# rbs_inline: enabled
22
# frozen_string_literal: true
33

4+
require_relative "diagnostic"
5+
46
module Lrama
57
class Diagram
68
class << self
@@ -15,7 +17,14 @@ def require_railroad_diagrams
1517
require "railroad_diagrams"
1618
true
1719
rescue LoadError
18-
warn "railroad_diagrams is not installed. Please run `bundle install`."
20+
diagnostic = Lrama::Diagnostic.new(
21+
id: "dependency.railroad_diagrams.missing",
22+
severity: :warning,
23+
message: "railroad_diagrams is not installed. Please run `bundle install`.",
24+
details: { "gem" => "railroad_diagrams" },
25+
suggestion: "Run `bundle install`."
26+
)
27+
$stderr.puts(diagnostic.message)
1928
false
2029
end
2130
end

lib/lrama/option_parser.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: true
33

44
require 'optparse'
5+
require_relative 'diagnostic'
56

67
module Lrama
78
# Handle option parsing for the command line interface.
@@ -35,11 +36,11 @@ def parse(argv)
3536
@options.grammar_file = argv.shift
3637

3738
unless @options.grammar_file
38-
abort "File should be specified\n"
39+
abort_with_diagnostic("command.missing_grammar_file", "File should be specified")
3940
end
4041

4142
if @options.grammar_file == '-'
42-
@options.grammar_file = argv.shift or abort "File name for STDIN should be specified\n"
43+
@options.grammar_file = argv.shift or abort_with_diagnostic("command.missing_stdin_filename", "File name for STDIN should be specified")
4344
else
4445
@options.y = File.open(@options.grammar_file, 'r')
4546
end
@@ -62,6 +63,17 @@ def parse(argv)
6263

6364
private
6465

66+
# @rbs (String id, String message) -> bot
67+
def abort_with_diagnostic(id, message)
68+
diagnostic = Lrama::Diagnostic.new(
69+
id: id,
70+
severity: :error,
71+
message: message
72+
)
73+
74+
abort diagnostic.message
75+
end
76+
6577
# @rbs (Array[String]) -> void
6678
def parse_by_option_parser(argv)
6779
::OptionParser.new do |o|

lib/lrama/reporter/profile/call_stack.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# rbs_inline: enabled
22
# frozen_string_literal: true
33

4+
require_relative "../../diagnostic"
5+
46
module Lrama
57
class Reporter
68
module Profile
@@ -36,7 +38,14 @@ def self.require_stackprof
3638
require "stackprof"
3739
true
3840
rescue LoadError
39-
warn "stackprof is not installed. Please run `bundle install`."
41+
diagnostic = Lrama::Diagnostic.new(
42+
id: "dependency.stackprof.missing",
43+
severity: :warning,
44+
message: "stackprof is not installed. Please run `bundle install`.",
45+
details: { "gem" => "stackprof" },
46+
suggestion: "Run `bundle install`."
47+
)
48+
$stderr.puts(diagnostic.message)
4049
false
4150
end
4251
end

lib/lrama/reporter/profile/memory.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# rbs_inline: enabled
22
# frozen_string_literal: true
33

4+
require_relative "../../diagnostic"
5+
46
module Lrama
57
class Reporter
68
module Profile
@@ -35,7 +37,14 @@ def self.require_memory_profiler
3537
require "memory_profiler"
3638
true
3739
rescue LoadError
38-
warn "memory_profiler is not installed. Please run `bundle install`."
40+
diagnostic = Lrama::Diagnostic.new(
41+
id: "dependency.memory_profiler.missing",
42+
severity: :warning,
43+
message: "memory_profiler is not installed. Please run `bundle install`.",
44+
details: { "gem" => "memory_profiler" },
45+
suggestion: "Run `bundle install`."
46+
)
47+
$stderr.puts(diagnostic.message)
3948
false
4049
end
4150
end

sig/generated/lrama/option_parser.rbs

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)