Skip to content

Commit 195d909

Browse files
committed
Organize negative conditions
1 parent 4d86c2e commit 195d909

File tree

9 files changed

+42
-46
lines changed

9 files changed

+42
-46
lines changed

lib/lrama/context.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def compute_yydefact
255255

256256
# If no default_reduction_rule, default behavior is an
257257
# error then replace ErrorActionNumber with zero.
258-
if !state.default_reduction_rule
258+
unless state.default_reduction_rule
259259
actions.map! do |e|
260260
if e == ErrorActionNumber
261261
0
@@ -303,17 +303,17 @@ def compute_yydefgoto
303303
end
304304

305305
@states.nterms.each do |nterm|
306-
if !(states = nterm_to_next_states[nterm])
307-
default_goto = 0
308-
not_default_gotos = []
309-
else
306+
if (states = nterm_to_next_states[nterm])
310307
default_state = states.map(&:last).group_by {|s| s }.max_by {|_, v| v.count }.first
311308
default_goto = default_state.id
312309
not_default_gotos = []
313310
states.each do |from_state, to_state|
314311
next if to_state.id == default_goto
315312
not_default_gotos << [from_state.id, to_state.id]
316313
end
314+
else
315+
default_goto = 0
316+
not_default_gotos = []
317317
end
318318

319319
k = nterm_number_to_sequence_number(nterm.number)

lib/lrama/counterexamples.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ def find_shift_conflict_shortest_state_items(reduce_path, conflict_state, confli
173173
break
174174
end
175175

176-
if !si.item.beginning_of_rule?
176+
if si.item.beginning_of_rule?
177+
key = [si.state, si.item.lhs]
178+
@reverse_productions[key].each do |item|
179+
state_item = StateItem.new(si.state, item)
180+
queue << (sis + [state_item])
181+
end
182+
else
177183
key = [si, si.item.previous_sym]
178184
@reverse_transitions[key].each do |prev_target_state_item|
179185
next if prev_target_state_item.state != prev_state_item.state
@@ -185,12 +191,6 @@ def find_shift_conflict_shortest_state_items(reduce_path, conflict_state, confli
185191
queue.clear
186192
break
187193
end
188-
else
189-
key = [si.state, si.item.lhs]
190-
@reverse_productions[key].each do |item|
191-
state_item = StateItem.new(si.state, item)
192-
queue << (sis + [state_item])
193-
end
194194
end
195195
end
196196
else

lib/lrama/grammar/rule_builder.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ def initialize(rule_counter, midrule_action_counter, parameterizing_rule_resolve
2727
end
2828

2929
def add_rhs(rhs)
30-
if !@line
31-
@line = rhs.line
32-
end
30+
@line ||= rhs.line
3331

3432
flush_user_code
3533

3634
@rhs << rhs
3735
end
3836

3937
def user_code=(user_code)
40-
if !@line
41-
@line = user_code&.line
42-
end
38+
@line ||= user_code&.line
4339

4440
flush_user_code
4541

lib/lrama/lexer.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def location
7474
end
7575

7676
def lex_token
77-
while !@scanner.eos? do
77+
until @scanner.eos? do
7878
case
7979
when @scanner.scan(/\n/)
8080
newline
@@ -129,7 +129,7 @@ def lex_c_code
129129
code = ''
130130
reset_first_position
131131

132-
while !@scanner.eos? do
132+
until @scanner.eos? do
133133
case
134134
when @scanner.scan(/{/)
135135
code += @scanner.matched
@@ -166,7 +166,7 @@ def lex_c_code
166166
private
167167

168168
def lex_comment
169-
while !@scanner.eos? do
169+
until @scanner.eos? do
170170
case
171171
when @scanner.scan(/\n/)
172172
newline

lib/lrama/lexer/token/user_code.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def _references
1818
scanner = StringScanner.new(s_value)
1919
references = []
2020

21-
while !scanner.eos? do
21+
until scanner.eos? do
2222
case
2323
when reference = scan_reference(scanner)
2424
references << reference

lib/lrama/option_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def parse(argv)
1818
@options.report_opts = validate_report(@report)
1919
@options.grammar_file = argv.shift
2020

21-
if !@options.grammar_file
21+
unless @options.grammar_file
2222
abort "File should be specified\n"
2323
end
2424

lib/lrama/states.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def compute_includes_relation
349349
# TODO: need to omit if state == state2 ?
350350
@includes_relation[key] ||= []
351351
@includes_relation[key] << [state.id, nterm.token_id]
352-
break if !sym.nullable
352+
break unless sym.nullable
353353
i -= 1
354354
end
355355
end
@@ -384,7 +384,7 @@ def compute_look_ahead_sets
384384
@states.each do |state|
385385
rules.each do |rule|
386386
ary = @lookback_relation[[state.id, rule.id]]
387-
next if !ary
387+
next unless ary
388388

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

429429
next unless reduce.look_ahead
430-
next if !reduce.look_ahead.include?(sym)
430+
next unless reduce.look_ahead.include?(sym)
431431

432432
# Shift/Reduce conflict
433433
shift_prec = sym.precedence
@@ -501,7 +501,7 @@ def compute_reduce_reduce_conflicts
501501

502502
intersection = reduce1.look_ahead & reduce2.look_ahead
503503

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

lib/lrama/states_reporter.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def report_conflicts(io)
7878
messages << "#{cs[:reduce_reduce].count} reduce/reduce"
7979
end
8080

81-
if !messages.empty?
81+
unless messages.empty?
8282
has_conflict = true
8383
io << "State #{state.id} conflicts: #{messages.join(', ')}\n"
8484
end
@@ -139,7 +139,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
139139
if lookaheads && item.end_of_rule?
140140
reduce = state.find_reduce_by_item!(item)
141141
look_ahead = reduce.selected_look_ahead
142-
if !look_ahead.empty?
142+
unless look_ahead.empty?
143143
la = " [#{look_ahead.map(&:display_name).join(", ")}]"
144144
end
145145
end
@@ -159,7 +159,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
159159
tmp.each do |term, state_id|
160160
io << " #{term.display_name.ljust(max_len)} shift, and go to state #{state_id}\n"
161161
end
162-
io << "\n" if !tmp.empty?
162+
io << "\n" unless tmp.empty?
163163

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

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

227227
if solved
228228
# Report conflict resolutions
229229
state.resolved_conflicts.each do |resolved|
230230
io << " #{resolved.report_message}\n"
231231
end
232-
io << "\n" if !state.resolved_conflicts.empty?
232+
io << "\n" unless state.resolved_conflicts.empty?
233233
end
234234

235235
if counterexamples && state.has_conflicts?
@@ -260,7 +260,7 @@ def report_states(io, itemsets, lookaheads, solved, counterexamples, verbose)
260260
direct_read_sets = @states.direct_read_sets
261261
@states.nterms.each do |nterm|
262262
terms = direct_read_sets[[state.id, nterm.token_id]]
263-
next if !terms
263+
next unless terms
264264
next if terms.empty?
265265

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

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

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

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

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

330-
next if !terms
330+
next unless terms
331331

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

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

357357
# End of Report State

spec/spec_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def initialize(expected)
5151
def matches?(target)
5252
@target = target
5353

54-
if !@expected.is_a?(Lrama::Grammar::Symbol)
54+
unless @expected.is_a?(Lrama::Grammar::Symbol)
5555
@_failure_message = "expected #{@expected.inspect} to be Lrama::Grammar::Symbol"
5656
return false
5757
end
5858

59-
if !@target.is_a?(Lrama::Grammar::Symbol)
59+
unless @target.is_a?(Lrama::Grammar::Symbol)
6060
@_failure_message = "expected #{@target.inspect} to be Lrama::Grammar::Symbol"
6161
return false
6262
end
@@ -97,12 +97,12 @@ def initialize(expected)
9797
def matches?(target)
9898
@target = target
9999

100-
if !@expected.is_a?(Array)
100+
unless @expected.is_a?(Array)
101101
@_failure_message = "expected #{@expected.inspect} to be Array"
102102
return false
103103
end
104104

105-
if !@target.is_a?(Array)
105+
unless @target.is_a?(Array)
106106
@_failure_message = "expected #{@target.inspect} to be Array"
107107
return false
108108
end

0 commit comments

Comments
 (0)