Improve error reporting, by printing the errored line and a caret (^), taking care to display the beginning of the token the error is at.

This commit is contained in:
Sei Lisa 2016-01-02 03:39:47 +01:00
parent f97cef2a38
commit ceb442e931
2 changed files with 20 additions and 14 deletions

14
main.py
View file

@ -19,16 +19,22 @@
# This is the main executable program that imports the libraries.
from lslopt.lslparse import parser,EParse
from lslopt.lslparse import parser,EParse,fieldpos
from lslopt.lsloutput import outscript
from lslopt.lsloptimizer import optimizer
import sys, os, getopt, re
import lslopt.lslcommon
VERSION = '0.1.3alpha'
VERSION = '0.2.0beta'
def ReportError(script, e):
sys.stderr.write(script[fieldpos(script, '\n', e.lno):
fieldpos(script, '\n', e.lno+1)-1] + '\n')
sys.stderr.write(' ' * e.cno + '^\n')
sys.stderr.write(e[0] + '\n')
class UniConvScript(object):
'''Converts the script to Unicode, setting the properties required by
EParse to report a meaningful error position.
@ -467,6 +473,8 @@ def main():
# need the result.
UniConvScript(script).to_unicode()
except EParse as e:
# We don't call ReportError to prevent problems due to
# displaying invalid UTF-8
sys.stderr.write(e[0] + '\n')
return 1
script = PreparePreproc(script)
@ -502,7 +510,7 @@ def main():
try:
ts = p.parse(script, options)
except EParse as e:
sys.stderr.write(e[0] + '\n')
ReportError(script, e)
return 1
del p, script