From 58047569f81e2fa095c438eb7aba303734a102c4 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 28 Jun 2017 00:03:14 +0200 Subject: [PATCH] add error handling: parser now throws exceptions --- src/JobShopParser/__init__.py | 2 +- src/JobShopParser/jobshop2_parser.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/JobShopParser/__init__.py b/src/JobShopParser/__init__.py index b407706..9ac12e1 100644 --- a/src/JobShopParser/__init__.py +++ b/src/JobShopParser/__init__.py @@ -80,7 +80,7 @@ class JobShopVisitor(arpeggio.PTNodeVisitor): if len(job_numbers) % 2 ==1: raise ParseError("Odd number of numbers in job data") # returns list of (duration, machine) tuples - return list(zip(job_numbers[1::2], job_numbers[0::2])) # [::2] returns only every second element of the list + return list(zip(job_numbers[1::2], job_numbers[0::2])) # [::2] returns only every second element of the list def visit_problem_data(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem: if self.debug: diff --git a/src/JobShopParser/jobshop2_parser.py b/src/JobShopParser/jobshop2_parser.py index 7a4c4b1..a9399e9 100644 --- a/src/JobShopParser/jobshop2_parser.py +++ b/src/JobShopParser/jobshop2_parser.py @@ -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""" - parse_tree = parser.parse(inputdata) + 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()