add error handling: parser now throws exceptions

This commit is contained in:
Trolli Schmittlauch 2017-06-28 00:03:14 +02:00
parent c3953449ff
commit 58047569f8
2 changed files with 9 additions and 4 deletions

View file

@ -16,13 +16,18 @@ class JobShop2Visitor(JobShopVisitor):
def parse_jobshop2_string(inputdata: str) -> JobShopProblem:
"""parse string of jobshop2-formatted data (single problem instance, no name & description)
and return JobShopProblem"""
and return JobShopProblem
Raises a ParseError exception on errors"""
try:
parse_tree = parser.parse(inputdata)
except arpeggio.NoMatch as e:
raise ParseError("Error while parsing problem input data at line {}, col {}:\n Rules {} did not match.".format(e.line, e.col, e.rules))
return arpeggio.visit_parse_tree(parse_tree, JobShop2Visitor())
def parse_jobshop2_file(filename: Union[str, bytes]) -> JobShopProblem:
"""Open file with jobshop2-formatted data (single problem instance, no name & description),
parse it and return JobShopProblem"""
parse it and return JobShopProblem
Raises a ParseError exception on errors"""
with open(filename) as datafile:
inputdata: str = datafile.read()