# encoding: utf-8

# Copyright (C) 2011 Clemens Fuchslocher <clemens@vakuumverpackt.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

module VoidMeter

	class BlockCommentDetector
		def initialize(open = '/*', close = '*/')
			@open = open
			@close = close
			@level = 0
			@comment = false
		end

		def consume(line)
			@comment = @level != 0
			@level += line.scan(@open).size
			@comment = @level != 0 || @comment
			@level -= line.scan(@close).size
			@comment = @level != 0 || @comment
			if line.include?(@open) && !line.lstrip.start_with?(@open)
				@comment = @level > 1
			end
			@comment
		end

		def block_comment?
			@comment
		end

		def reset
			@level = 0
			@comment = false
		end
	end

end
