lib/ebnf/ll1/scanner.rb in ebnf-1.0.2 vs lib/ebnf/ll1/scanner.rb in ebnf-1.1.0

- old
+ new

@@ -16,26 +16,40 @@ ## # @return [IO, StringIO] attr_reader :input ## - # Create a scanner, from an IO or String + # If we don't have an IO input, simply use StringScanner directly + # @private + def self.new(input, options = {}) + input ||= "" + if input.respond_to?(:read) + scanner = self.allocate + scanner.send(:initialize, input, options) + else + if input.encoding != Encoding::UTF_8 + input = input.dup if input.frozen? + input.force_encoding(Encoding::UTF_8) + end + StringScanner.new(input) + end + end + + ## + # Create a scanner, from an IO # # @param [String, IO, #read] input # @param [Hash{Symbol => Object}] options # @option options[Integer] :high_water (HIGH_WATER) # @option options[Integer] :low_water (LOW_WATER) # @return [Scanner] def initialize(input, options = {}) @options = options.merge(high_water: HIGH_WATER, low_water: LOW_WATER) - if input.respond_to?(:read) - @input = input - super("") - feed_me - else - super(encode_utf8 input.to_s) - end + @input = input + super("") + feed_me + self end ## # Returns the "rest" of the line, or the next line if at EOL (i.e. everything after the scan pointer). # If there is no more data (eos? = true), it returns "". \ No newline at end of file