Sha256: e850b41a5e68e8263a833b77226357740f3ff82b6add0e681074639c66d88cda

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

require 'treetop'
require 'treetop/runtime'
require 'treetop/ruby_extensions'

module Cucumber
  module Parser
    module TreetopExt
      FILE_LINE_PATTERN = /^([\w\W]*?):([\d:]+)$/

      # Parses a file and returns a Cucumber::Ast
      def parse_file(file)
        _, path, lines = *FILE_LINE_PATTERN.match(file)
        if path
          lines = lines.split(':').map { |line| line.to_i }
        else
          path = file
          lines = []
        end

        loader = lambda { |io| parse_or_fail(io.read, path) }
        feature = if path =~ /^http/
          require 'open-uri'
          open(path, &loader)
        else
          File.open(path, Cucumber.file_mode('r'), &loader) 
        end
        feature.lines = lines
        feature
      end
    end

    class SyntaxError < StandardError
      def initialize(parser, file, line_offset)
        tf = parser.terminal_failures
        expected = tf.size == 1 ? tf[0].expected_string.inspect : "one of #{tf.map{|f| f.expected_string}.uniq*', '}"
        line = parser.failure_line + line_offset
        message = "#{file}:#{line}:#{parser.failure_column}: Parse error, expected #{expected}."
        super(message)
      end
    end
  end
end

module Treetop
  module Runtime
    class SyntaxNode
      def line
        input.line_of(interval.first)
      end
    end
    
    class CompiledParser
      include Cucumber::Parser::TreetopExt
      
      def parse_or_fail(s, file=nil, line=0)
        parse_tree = parse(s)
        if parse_tree.nil?
          raise Cucumber::Parser::SyntaxError.new(self, file, line)
        else
          ast = parse_tree.build
          ast.file = file
          ast
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
aslakhellesoy-cucumber-0.1.99.10 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.11 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.12 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.13 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.14 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.15 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.7 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.8 lib/cucumber/parser/treetop_ext.rb
aslakhellesoy-cucumber-0.1.99.9 lib/cucumber/parser/treetop_ext.rb