38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import arpeggio
|
|
from arpeggio.cleanpeg import ParserPEG
|
|
from typing import List, Tuple, Sequence, Optional, Union
|
|
|
|
from . import grammar, JobShopVisitor, JobShopProblem
|
|
|
|
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_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)
|
|
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"""
|
|
|
|
with open(filename) as datafile:
|
|
inputdata: str = datafile.read()
|
|
return parse_jobshop2_string(inputdata)
|
|
|
|
def main():
|
|
print(type(parse_jobshop2_file('../inputdata/jobshop2/ta13')))
|
|
|
|
parser = ParserPEG(grammar, "job_shop2", skipws=False)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|