Sha256: 6cb2047557282648823b1762f53ad7c2148a4747c0e2cc0bf662ccfe07640cbe

Contents?: true

Size: 1.93 KB

Versions: 10

Compression:

Stored size: 1.93 KB

Contents

require 'pathname'

module FixtureFox
  class Tokenizer
    # Minimum path to source file. Note that this is a ruby file if the source
    # was inline
    attr_reader :file

    # Array of tokenized lines
    attr_reader :lines

    def initialize(file_or_text)
      @lines = []

      # FIXME: Why @dev_proc ?
      if file_or_text =~ /\s/
        if caller[0] =~ /^(.*):(\d+):in `.*/
          file, @lineno = $1, $2.to_i - 1
          @file = Pathname.new(Pathname.new(file).expand_path).relative_path_from(Pathname.getwd).to_s
          @dev_proc = lambda { |&block| StringIO.open(file_or_text, &block) }
        else
          raise "Oops"
        end
      else
        @lineno = 0
        @file = file_or_text
        @dev_proc = lambda { |&block| File.open(@file, "r", &block) }
      end
    end

    def call
      # Read source
      source = @dev_proc.call { |dev|
        dev.readlines.take_while { |l| l !~ /^__END__\s*$/ }
      }

      # Find initial indent
      while !source.empty? && source.first =~ /^\s*(?:#.*)?$/
        source.shift
        @lineno += 1
      end
      !source.empty? or return []
      @initial_indent = source.first[/\A */].size

      while !source.empty? 
        line = source.shift
        @lineno += 1

        # Ignore blank lines and comments
        next if line =~ /^\s*(?:#.*)?$/

        # Find indent
        indent = line[/\A */].size - @initial_indent
        indent >= 0 or error("Illegal indent")

        # Strip prefixed and suffixed blanks
        line.strip!

        # Create
        if line =~ /^@/
          @lines << DirectiveLine.new(@file, @lineno, @initial_indent, indent, line)
        else
          @lines << Line.new(@file, @lineno, @initial_indent, indent, line)
        end
      end

      @lines
    end

    def dump(long: false)
      @lines.each { |l| puts l.to_s(long: long) }
    end

    def error(msg)
      raise ParseError.new(@file, @lineno, @initial_indent, msg)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fixture_fox-0.2.6 lib/fixture_fox/tokenizer.rb
fixture_fox-0.2.5 lib/fixture_fox/tokenizer.rb
fixture_fox-0.2.4 lib/fixture_fox/tokenizer.rb
fixture_fox-0.2.3 lib/fixture_fox/tokenizer.rb
fixture_fox-0.2.2 lib/fixture_fox/tokenizer.rb
fixture_fox-0.2.1 lib/fixture_fox/tokenizer.rb
fixture_fox-0.2.0 lib/fixture_fox/tokenizer.rb
fixture_fox-0.1.3 lib/fixture_fox/tokenizer.rb
fixture_fox-0.1.2 lib/fixture_fox/tokenizer.rb
fixture_fox-0.1.1 lib/fixture_fox/tokenizer.rb