Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes some trivial formatting #462

Merged
merged 8 commits into from
Jul 22, 2024
10 changes: 5 additions & 5 deletions lib/lrama/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def compute_yydefact

# If no default_reduction_rule, default behavior is an
# error then replace ErrorActionNumber with zero.
if !state.default_reduction_rule
unless state.default_reduction_rule
actions.map! do |e|
if e == ErrorActionNumber
0
Expand Down Expand Up @@ -303,17 +303,17 @@ def compute_yydefgoto
end

@states.nterms.each do |nterm|
if !(states = nterm_to_next_states[nterm])
default_goto = 0
not_default_gotos = []
else
if (states = nterm_to_next_states[nterm])
default_state = states.map(&:last).group_by {|s| s }.max_by {|_, v| v.count }.first
default_goto = default_state.id
not_default_gotos = []
states.each do |from_state, to_state|
next if to_state.id == default_goto
not_default_gotos << [from_state.id, to_state.id]
end
else
default_goto = 0
not_default_gotos = []
end

k = nterm_number_to_sequence_number(nterm.number)
Expand Down
14 changes: 7 additions & 7 deletions lib/lrama/counterexamples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ def find_shift_conflict_shortest_state_items(reduce_path, conflict_state, confli
break
end

if !si.item.beginning_of_rule?
if si.item.beginning_of_rule?
key = [si.state, si.item.lhs]
@reverse_productions[key].each do |item|
state_item = StateItem.new(si.state, item)
queue << (sis + [state_item])
end
else
key = [si, si.item.previous_sym]
@reverse_transitions[key].each do |prev_target_state_item|
next if prev_target_state_item.state != prev_state_item.state
Expand All @@ -185,12 +191,6 @@ def find_shift_conflict_shortest_state_items(reduce_path, conflict_state, confli
queue.clear
break
end
else
key = [si.state, si.item.lhs]
@reverse_productions[key].each do |item|
state_item = StateItem.new(si.state, item)
queue << (sis + [state_item])
end
end
end
else
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/grammar/parameterizing_rule/resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def find_rule(token)
end

def find_inline(token)
@rules.select { |rule| rule.name == token.s_value && rule.is_inline }.last
@rules.reverse.find { |rule| rule.name == token.s_value && rule.is_inline }
end

def created_lhs(lhs_s_value)
Expand Down
10 changes: 3 additions & 7 deletions lib/lrama/grammar/rule_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ def initialize(rule_counter, midrule_action_counter, parameterizing_rule_resolve
end

def add_rhs(rhs)
if !@line
@line = rhs.line
end
@line ||= rhs.line

flush_user_code

@rhs << rhs
end

def user_code=(user_code)
if !@line
@line = user_code&.line
end
@line ||= user_code&.line

flush_user_code

Expand Down Expand Up @@ -73,7 +69,7 @@ def has_inline_rules?
def resolve_inline_rules
resolved_builders = []
rhs.each_with_index do |token, i|
if inline_rule = @parameterizing_rule_resolver.find_inline(token)
if (inline_rule = @parameterizing_rule_resolver.find_inline(token))
inline_rule.rhs_list.each do |inline_rhs|
rule_builder = RuleBuilder.new(@rule_counter, @midrule_action_counter, @parameterizing_rule_resolver, lhs_tag: lhs_tag)
if token.is_a?(Lexer::Token::InstantiateRule)
Expand Down
10 changes: 5 additions & 5 deletions lib/lrama/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Lexer
attr_reader :head_line, :head_column, :line
attr_accessor :status, :end_symbol

SYMBOLS = ['%{', '%}', '%%', '{', '}', '\[', '\]', '\(', '\)', '\,', ':', '\|', ';']
SYMBOLS = ['%{', '%}', '%%', '{', '}', '\[', '\]', '\(', '\)', '\,', ':', '\|', ';'].freeze
PERCENT_TOKENS = %w(
%union
%token
Expand Down Expand Up @@ -41,7 +41,7 @@ class Lexer
%no-stdlib
%inline
%locations
)
).freeze

def initialize(grammar_file)
@grammar_file = grammar_file
Expand Down Expand Up @@ -74,7 +74,7 @@ def location
end

def lex_token
while !@scanner.eos? do
until @scanner.eos? do
case
when @scanner.scan(/\n/)
newline
Expand Down Expand Up @@ -129,7 +129,7 @@ def lex_c_code
code = ''
reset_first_position

while !@scanner.eos? do
until @scanner.eos? do
case
when @scanner.scan(/{/)
code += @scanner.matched
Expand Down Expand Up @@ -166,7 +166,7 @@ def lex_c_code
private

def lex_comment
while !@scanner.eos? do
until @scanner.eos? do
case
when @scanner.scan(/\n/)
newline
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/lexer/token/user_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _references
scanner = StringScanner.new(s_value)
references = []

while !scanner.eos? do
until scanner.eos? do
case
when reference = scan_reference(scanner)
references << reference
Expand Down
10 changes: 5 additions & 5 deletions lib/lrama/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def parse(argv)
@options.report_opts = validate_report(@report)
@options.grammar_file = argv.shift

if !@options.grammar_file
unless @options.grammar_file
abort "File should be specified\n"
end

Expand Down Expand Up @@ -106,8 +106,8 @@ def parse_by_option_parser(argv)
end
end

ALIASED_REPORTS = { cex: :counterexamples }
VALID_REPORTS = %i[states itemsets lookaheads solved counterexamples rules terms verbose]
ALIASED_REPORTS = { cex: :counterexamples }.freeze
VALID_REPORTS = %i[states itemsets lookaheads solved counterexamples rules terms verbose].freeze

def validate_report(report)
h = { grammar: true }
Expand Down Expand Up @@ -138,11 +138,11 @@ def aliased_report_option(opt)
locations scan parse automaton bitsets closure
grammar rules actions resource sets muscles
tools m4-early m4 skeleton time ielr cex
]
].freeze
NOT_SUPPORTED_TRACES = %w[
locations scan parse bitsets grammar resource
sets muscles tools m4-early m4 skeleton ielr cex
]
].freeze

def validate_trace(trace)
h = {}
Expand Down
2 changes: 1 addition & 1 deletion lib/lrama/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def partial_file(file)
end

def template_dir
File.expand_path("../../../template", __FILE__)
File.expand_path('../../template', __dir__)
end

def string_array_to_string(ary)
Expand Down
14 changes: 7 additions & 7 deletions lib/lrama/states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def compute_includes_relation
# TODO: need to omit if state == state2 ?
@includes_relation[key] ||= []
@includes_relation[key] << [state.id, nterm.token_id]
break if !sym.nullable
break unless sym.nullable
i -= 1
end
end
Expand Down Expand Up @@ -384,7 +384,7 @@ def compute_look_ahead_sets
@states.each do |state|
rules.each do |rule|
ary = @lookback_relation[[state.id, rule.id]]
next if !ary
next unless ary

ary.each do |state2_id, nterm_token_id|
# q = state, A -> ω = rule, p = state2, A = nterm
Expand Down Expand Up @@ -427,7 +427,7 @@ def compute_shift_reduce_conflicts
sym = shift.next_sym

next unless reduce.look_ahead
next if !reduce.look_ahead.include?(sym)
next unless reduce.look_ahead.include?(sym)

# Shift/Reduce conflict
shift_prec = sym.precedence
Expand Down Expand Up @@ -491,17 +491,17 @@ def compute_reduce_reduce_conflicts
states.each do |state|
count = state.reduces.count

for i in 0...count do
(0...count).each do |i|
reduce1 = state.reduces[i]
next if reduce1.look_ahead.nil?

for j in (i+1)...count do
((i+1)...count).each do |j|
reduce2 = state.reduces[j]
next if reduce2.look_ahead.nil?

intersection = reduce1.look_ahead & reduce2.look_ahead

if !intersection.empty?
unless intersection.empty?
state.conflicts << State::ReduceReduceConflict.new(symbols: intersection, reduce1: reduce1, reduce2: reduce2)
end
end
Expand All @@ -513,7 +513,7 @@ def compute_default_reduction
states.each do |state|
next if state.reduces.empty?
# Do not set, if conflict exist
next if !state.conflicts.empty?
next unless state.conflicts.empty?
# Do not set, if shift with `error` exists.
next if state.shifts.map(&:next_sym).include?(@grammar.error_symbol)

Expand Down
28 changes: 14 additions & 14 deletions lib/lrama/states_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def report_conflicts(io)
messages << "#{cs[:reduce_reduce].count} reduce/reduce"
end

if !messages.empty?
unless messages.empty?
has_conflict = true
io << "State #{state.id} conflicts: #{messages.join(', ')}\n"
end
Expand Down Expand Up @@ -139,7 +139,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
if lookaheads && item.end_of_rule?
reduce = state.find_reduce_by_item!(item)
look_ahead = reduce.selected_look_ahead
if !look_ahead.empty?
unless look_ahead.empty?
la = " [#{look_ahead.map(&:display_name).join(", ")}]"
end
end
Expand All @@ -159,7 +159,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
tmp.each do |term, state_id|
io << " #{term.display_name.ljust(max_len)} shift, and go to state #{state_id}\n"
end
io << "\n" if !tmp.empty?
io << "\n" unless tmp.empty?

# Report error caused by %nonassoc
nl = false
Expand All @@ -173,7 +173,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
nl = true
io << " #{name.ljust(max_len)} error (nonassociative)\n"
end
io << "\n" if !tmp.empty?
io << "\n" unless tmp.empty?

# Report reduces
nl = false
Expand Down Expand Up @@ -222,14 +222,14 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
tmp.each do |nterm, state_id|
io << " #{nterm.id.s_value.ljust(max_len)} go to state #{state_id}\n"
end
io << "\n" if !tmp.empty?
io << "\n" unless tmp.empty?

if solved
# Report conflict resolutions
state.resolved_conflicts.each do |resolved|
io << " #{resolved.report_message}\n"
end
io << "\n" if !state.resolved_conflicts.empty?
io << "\n" unless state.resolved_conflicts.empty?
end

if counterexamples && state.has_conflicts?
Expand Down Expand Up @@ -260,7 +260,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
direct_read_sets = @states.direct_read_sets
@states.nterms.each do |nterm|
terms = direct_read_sets[[state.id, nterm.token_id]]
next if !terms
next unless terms
next if terms.empty?

str = terms.map {|sym| sym.id.s_value }.join(", ")
Expand All @@ -272,7 +272,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
io << " [Reads Relation]\n"
@states.nterms.each do |nterm|
a = @states.reads_relation[[state.id, nterm.token_id]]
next if !a
next unless a

a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
Expand All @@ -286,7 +286,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
read_sets = @states.read_sets
@states.nterms.each do |nterm|
terms = read_sets[[state.id, nterm.token_id]]
next if !terms
next unless terms
next if terms.empty?

terms.each do |sym|
Expand All @@ -299,7 +299,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
io << " [Includes Relation]\n"
@states.nterms.each do |nterm|
a = @states.includes_relation[[state.id, nterm.token_id]]
next if !a
next unless a

a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
Expand All @@ -312,7 +312,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
io << " [Lookback Relation]\n"
@states.rules.each do |rule|
a = @states.lookback_relation[[state.id, rule.id]]
next if !a
next unless a

a.each do |state_id2, nterm_id2|
n = @states.nterms.find {|n| n.token_id == nterm_id2 }
Expand All @@ -327,7 +327,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
@states.nterms.each do |nterm|
terms = follow_sets[[state.id, nterm.token_id]]

next if !terms
next unless terms

terms.each do |sym|
io << " #{nterm.id.s_value} -> #{sym.id.s_value}\n"
Expand All @@ -341,7 +341,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
max_len = 0
@states.rules.each do |rule|
syms = @states.la[[state.id, rule.id]]
next if !syms
next unless syms

tmp << [rule, syms]
max_len = ([max_len] + syms.map {|s| s.id.s_value.length }).max
Expand All @@ -351,7 +351,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
io << " #{sym.id.s_value.ljust(max_len)} reduce using rule #{rule.id} (#{rule.lhs.id.s_value})\n"
end
end
io << "\n" if !tmp.empty?
io << "\n" unless tmp.empty?
end

# End of Report State
Expand Down
2 changes: 1 addition & 1 deletion spec/lrama/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
command = Lrama::Command.new
expect(command.run(o_option + [fixture_path("command/basic.y"), "--report-file=report.output"])).to be_nil
expect(File).to have_received(:open).with("report.output", "w+").once
expect(File.exist?("report.output")).to be_truthy
expect(File).to exist("report.output")
File.delete("report.output")
end
end
Expand Down
Loading