Skip to content

Commit

Permalink
Merge pull request #527 from ydah/change-option
Browse files Browse the repository at this point in the history
Fix an error when specify `--define=foo`
  • Loading branch information
ydah authored Feb 5, 2025
2 parents 7fa74d8 + 5dfb6a3 commit 58b28b9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/lrama/grammar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def initialize(rule_counter, define = {})
@aux = Auxiliary.new
@no_stdlib = false
@locations = false
@define = define.map {|d| d.split('=') }.to_h
@define = define

append_special_symbols
end
Expand Down
7 changes: 6 additions & 1 deletion lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ def parse_by_option_parser(argv)
o.separator 'Tuning the Parser:'
o.on('-S', '--skeleton=FILE', 'specify the skeleton to use') {|v| @options.skeleton = v }
o.on('-t', '--debug', 'display debugging outputs of internal parser') {|v| @options.debug = true }
o.on('-D', '--define=NAME[=VALUE]', Array, "similar to '%define NAME VALUE'") {|v| @options.define = v }
o.on('-D', '--define=NAME[=VALUE]', Array, "similar to '%define NAME VALUE'") do |v|
@options.define = v.each_with_object({}) do |item, hash|
key, value = item.split('=', 2)
hash[key] = value
end
end
o.separator ''
o.separator 'Output:'
o.on('-H', '--header=[FILE]', 'also produce a header file named FILE') {|v| @options.header = true; @options.header_file = v }
Expand Down
24 changes: 18 additions & 6 deletions lib/lrama/options.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
# rbs_inline: enabled
# frozen_string_literal: true

module Lrama
# Command line options.
class Options
attr_accessor :skeleton, :header, :header_file,
:report_file, :outfile,
:error_recovery, :grammar_file,
:trace_opts, :report_opts,
:diagnostic, :y, :debug, :define,
:diagram, :diagram_file, :profile_opts
attr_accessor :skeleton #: String
attr_accessor :header #: bool
attr_accessor :header_file #: String?
attr_accessor :report_file #: String?
attr_accessor :outfile #: String
attr_accessor :error_recovery #: bool
attr_accessor :grammar_file #: String?
attr_accessor :trace_opts #: Hash[Symbol, bool]?
attr_accessor :report_opts #: Hash[Symbol, bool]?
attr_accessor :diagnostic #: bool
attr_accessor :y #: IO
attr_accessor :debug #: bool
attr_accessor :define #: Hash[String, String]
attr_accessor :diagram #: bool
attr_accessor :diagram_file #: String
attr_accessor :profile_opts #: Hash[Symbol, bool]?

# @rbs () -> void
def initialize
@skeleton = "bison/yacc.c"
@define = {}
Expand Down
23 changes: 22 additions & 1 deletion sig/lrama/options.rbs → sig/generated/lrama/options.rbs
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
# Generated from lib/lrama/options.rb with RBS::Inline

module Lrama
# Command line options.
class Options
attr_accessor skeleton: String
attr_accessor define: Hash[String, String]

attr_accessor header: bool

attr_accessor header_file: String?

attr_accessor report_file: String?

attr_accessor outfile: String

attr_accessor error_recovery: bool

attr_accessor grammar_file: String?

attr_accessor trace_opts: Hash[Symbol, bool]?

attr_accessor report_opts: Hash[Symbol, bool]?

attr_accessor diagnostic: bool

attr_accessor y: IO

attr_accessor debug: bool

attr_accessor define: Hash[String, String]

attr_accessor diagram: bool

attr_accessor diagram_file: String

attr_accessor profile_opts: Hash[Symbol, bool]?

# @rbs () -> void
def initialize: () -> void
end
end
31 changes: 31 additions & 0 deletions spec/lrama/option_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,35 @@
end
end
end

describe "@define" do
context "when define option is not passed" do
it "returns empty hash" do
option_parser = Lrama::OptionParser.new
option_parser.send(:parse, [fixture_path("command/basic.y")])
options = option_parser.instance_variable_get(:@options)
expect(options.define).to eq({})
end
end

context "when define option is passed" do
context "when value includes equal sign" do
it "returns hash of define options" do
option_parser = Lrama::OptionParser.new
option_parser.send(:parse, ["--define=foo=1", fixture_path("command/basic.y")])
options = option_parser.instance_variable_get(:@options)
expect(options.define).to eq({'foo' => "1"})
end
end

context "when value doesn't include equal sign" do
it "returns hash of define options" do
option_parser = Lrama::OptionParser.new
option_parser.send(:parse, ["--define=foo", fixture_path("command/basic.y")])
options = option_parser.instance_variable_get(:@options)
expect(options.define).to eq({'foo' => nil})
end
end
end
end
end

0 comments on commit 58b28b9

Please sign in to comment.