SEBA-709 Handle parse errors at end of input
Change-Id: I9af5bc8007ec52608d05b1038fe23a6a20520d38
diff --git a/lib/xos-genx/xosgenx/generator.py b/lib/xos-genx/xosgenx/generator.py
index 278a309..307e914 100644
--- a/lib/xos-genx/xosgenx/generator.py
+++ b/lib/xos-genx/xosgenx/generator.py
@@ -261,31 +261,34 @@
try:
ast = parser.parse_string(inputs, debug=0)
except plyxproto.ParsingError as e:
- line, start, end = e.error_range
-
- ptr = XOSProcessor._find_last_nonempty_line(inputs, start)
-
- if start == 0:
- beginning = ""
- else:
- beginning = inputs[ptr: start - 1]
-
- line_end_char = inputs[start + end:].find("\n")
- line_end = inputs[line_end_char]
-
if e.message:
error = e.message
else:
error = "xproto parsing error"
+ if e.error_range is None:
+ # No line number information
+ print(error + "\n")
+ else:
+ line, start, end = e.error_range
- print(error + "\n" + Fore.YELLOW + "Line %d:" % line + Fore.WHITE)
- print(
- beginning
- + Fore.YELLOW
- + inputs[start - 1: start + end]
- + Fore.WHITE
- + line_end
- )
+ ptr = XOSProcessor._find_last_nonempty_line(inputs, start)
+
+ if start == 0:
+ beginning = ""
+ else:
+ beginning = inputs[ptr: start - 1]
+
+ line_end_char = inputs[start + end:].find("\n")
+ line_end = inputs[line_end_char]
+
+ print(error + "\n" + Fore.YELLOW + "Line %d:" % line + Fore.WHITE)
+ print(
+ beginning
+ + Fore.YELLOW
+ + inputs[start - 1: start + end]
+ + Fore.WHITE
+ + line_end
+ )
exit(1)
v = XOSProcessor._attach_parser(ast, args)