JobShopScheduling/src/Parser/js2_style.py

54 lines
1.6 KiB
Python

import arpeggio
from arpeggio.cleanpeg import ParserPEG
from typing import Union
from . import grammar, JobShopVisitor, JobShopProblem, ParseError
class JobShop2Visitor(JobShopVisitor):
def visit_job_shop2(
self, node: arpeggio.ParseTreeNode,
children: arpeggio.SemanticActionResults) -> JobShopProblem:
if self.debug:
print("job_shop2:\nchildren:", children)
problem = children[0]
if self.debug:
print("returning a", type(problem))
return problem
def parse_string(inputdata: str) -> JobShopProblem:
"""parse string of jobshop2-formatted data
(single problem instance, no name & description)
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_file(filename: Union[str, bytes]) -> JobShopProblem:
"""Open file with jobshop2-formatted data
(single problem instance, no name & description),
parse it and return JobShopProblem
Raises a ParseError exception on errors"""
with open(filename) as datafile:
inputdata: str = datafile.read()
return parse_string(inputdata)
def main():
print(parse_file('../inputdata/jobshop2/ta13'))
parser = ParserPEG(grammar, "job_shop2", skipws=False)
if __name__ == "__main__":
main()