Sha256: 6d0d8675362321b237f397be321753c788511466109c6915a9353a8a240f6986

Contents?: true

Size: 1.2 KB

Versions: 14

Compression:

Stored size: 1.2 KB

Contents

require 'stringio'
require_relative 'token'
require_relative 'gherkin_line'

module Gherkin
  # The scanner reads a gherkin doc (typically read from a .feature file) and
  # creates a token for line. The tokens are passed to the parser, which outputs
  # an AST (Abstract Syntax Tree).
  #
  # If the scanner sees a # language header, it will reconfigure itself dynamically
  # to look for Gherkin keywords for the associated language. The keywords are defined
  # in gherkin-languages.json.
  class TokenScanner
    def initialize(source_or_io)
      @line_number = 0

      case(source_or_io)
      when String
        @io = StringIO.new(source_or_io)
      when StringIO, IO
        @io = source_or_io
      else
        fail ArgumentError, "Please a pass String, StringIO or IO. I got a #{source_or_io.class}"
      end
    end

    def read
      location = {line: @line_number += 1, column: 0}
      if @io.nil? || line = @io.gets
        gherkin_line = line ? GherkinLine.new(line, location[:line]) : nil
        Token.new(gherkin_line, location)
      else
        @io.close unless @io.closed? # ARGF closes the last file after final gets
        @io = nil
        Token.new(nil, location)
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cucumber-gherkin-18.1.1 lib/gherkin/token_scanner.rb
cucumber-gherkin-18.1.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-18.0.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-17.0.2 lib/gherkin/token_scanner.rb
cucumber-gherkin-17.0.1 lib/gherkin/token_scanner.rb
cucumber-gherkin-17.0.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-16.0.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-15.0.2 lib/gherkin/token_scanner.rb
cucumber-gherkin-15.0.1 lib/gherkin/token_scanner.rb
cucumber-gherkin-15.0.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-14.2.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-14.1.0 lib/gherkin/token_scanner.rb
cucumber-gherkin-14.0.1 lib/gherkin/token_scanner.rb
cucumber-gherkin-14.0.0 lib/gherkin/token_scanner.rb