Module: Cucumber::Parser::TreetopExt

Included in:
Treetop::Runtime::CompiledParser
Defined in:
lib/cucumber/parser/treetop_ext.rb

Constant Summary collapse

FILE_COLON_LINE_PATTERN =
/^([\w\W]*?):([\d:]+)$/

Instance Method Summary collapse

Instance Method Details

#parse_file(file) ⇒ Object

Parses a file and returns a Cucumber::Ast



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cucumber/parser/treetop_ext.rb', line 19

def parse_file(file)
  _, path, lines = *FILE_COLON_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

#parse_or_fail(s, file = nil, line_offset = 0) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/cucumber/parser/treetop_ext.rb', line 39

def parse_or_fail(s, file=nil, line_offset=0)
  parse_tree = parse(s)
  if parse_tree.nil?
    raise Cucumber::Parser::SyntaxError.new(self, file, line_offset)
  else
    ast = parse_tree.build
    ast.file = file
    ast
  end
end