From: Paul Battley Date: 2006-07-30T02:46:24+09:00 Subject: Re: Wild ideas: Finding a missing 'end' On 29/07/06, Mat Schaffer wrote: > This code generates a parse error at the last line. Now most > languages do this, so it's not a big deal. But how frickin cool > would it be if a future version of ruby looked at the indenting and > made a guess as to where the missing end/} might be. Like this? --- def error_line(code) File.open('tmp.rb', 'w') do |io| io << code end s = `ruby -c tmp.rb 2>&1` if (s =~ /parse error/) return s[/\d+/].to_i else return false end end code = File.read(ARGV[0]) exit unless error_line(code) lines = code.split(/\n/) x = lines.length / 2 xmin = 0 xmax = lines.length while l = error_line(code) if l < x xmax = x elsif l > x xmin = x else break end x = xmin + (xmax - xmin)/2 code = (lines[0,x] + ['end'] + lines[x..-1]).join("\n") end puts("Error around line #{x+1}") ---- This is pretty rough code that I just threw together without much thought, but it seems to work. Its temporary file handling leaves a lot to be desired. Paul.