Re: [Pyparsing] better parse error reporting
Brought to you by:
ptmcg
From: W. M. B. <de...@de...> - 2008-04-09 11:29:03
|
On Tue, Apr 08, 2008 at 08:18:32PM -0500, Paul McGuire wrote: > Pyparsing includes the line, lineno, and col builtins. You can convert a > location to the corresponding line of text by calling line(locn,string). Thanks for the hint! Because LastParseLoc might be slightly inaccurate, line() might return an empty line following the wrong input, but lineno() is helpful. E.g. in: string = open(filename).read() lineno = pyparsing.lineno(pyparsing.LastParseLoc, string) col = pyparsing.col(pyparsing.LastParseLoc, string) print >>sys.stderr, "%s:%d:%d: Parse Error!" % (filename, lineno, col) print >>sys.stderr, "\n".join(string.split("\n")[max(0, lineno - 3):lineno]) The split/join is prabably a bad idea, esp. for large input files, though. |