Skip to content

Commit

Permalink
Merge pull request #66 from pocke/nest-cond-preprop
Browse files Browse the repository at this point in the history
Fix nested conditional preprocessors
  • Loading branch information
okkez committed Aug 15, 2019
2 parents f01a552 + a148a08 commit 66e749d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/bitclust/preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def current_cond
end

def cond_init
@state_stack = [State.new(nil, :toplevel)]
@state_stack = [State.new(true, :toplevel)]
end

def cond_toplevel?
Expand All @@ -158,13 +158,13 @@ def cond_toplevel?

def cond_push(bool)
last = @state_stack.last
@state_stack.push(State.new(last.current, bool))
@state_stack.push(last.next(bool, :condition))
end

def cond_invert
b = @state_stack.pop.processing?
last = @state_stack.last
@state_stack.push(State.new(last.current, !b))
@state_stack.push(last.next(!b, :condition))
end

def cond_pop
Expand Down Expand Up @@ -254,7 +254,7 @@ def samplecode_end

def samplecode_push(description)
last = @state_stack.last
@state_stack.push(State.new(last.current, :samplecode))
@state_stack.push(last.next(true, :samplecode))
end

def samplecode_pop
Expand All @@ -276,24 +276,25 @@ def scan_error(msg)
class State
attr_reader :current

def initialize(previous, current)
@previous = previous
@current = current
def initialize(is_processing, label)
@is_processing = is_processing
@label = label
end

def next(is_processing, label)
State.new(@is_processing && is_processing, label)
end

def toplevel?
@current == :toplevel
@label == :toplevel
end

def processing?
toplevel? ||
(@current == true && @previous != false) ||
(@current == :samplecode && @previous == true) ||
(@current == :samplecode && @previous == :toplevel)
@is_processing
end

def samplecode?
@current == :samplecode
@label == :samplecode
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions test/test_preprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ def test_complex_condition
assert_equal(expected, ret.join)
end

def test_nested_condition
params = { 'version' => '2.4.0' }
src = <<~'HERE'
#@until 2.4.0
#@since 1.8.7
#@since 1.9.3
#@since 2.0.0
Not display here!
#@end
#@end
#@end
#@end
Display here!
HERE
expected = <<~HERE
Display here!
HERE
ret = Preprocessor.wrap(StringIO.new(src), params).to_a
assert_equal(expected, ret.join)
end

sub_test_case("samplecode") do

def test_samplecode
Expand Down

0 comments on commit 66e749d

Please sign in to comment.