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

Fix nested conditional preprocessors #66

Merged
merged 1 commit into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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