parent
ba91f321e2
commit
92ddb2740e
3 changed files with 7 additions and 7 deletions
57
src/Parser/js1_style.py
Normal file
57
src/Parser/js1_style.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
from arpeggio.cleanpeg import ParserPEG
|
||||
from typing import List, Tuple, Sequence, Optional, Union
|
||||
import arpeggio
|
||||
|
||||
from . import grammar, ParseError, JobShopProblem, JobShopVisitor
|
||||
|
||||
class JobShop1Visitor(JobShopVisitor):
|
||||
"""instanciated for semantic analysis of parse_tree"""
|
||||
|
||||
def visit_problem_instance(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> JobShopProblem:
|
||||
if self.debug:
|
||||
print("problem_instance\nchildren:", children)
|
||||
problem: JobShopProblem = children[3]
|
||||
problem.name = children[0]
|
||||
problem.description = children[2]
|
||||
return problem
|
||||
|
||||
def visit_instance_list(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> List[JobShopProblem]:
|
||||
if self.debug:
|
||||
print("instance_list\nchildren:", children)
|
||||
return list(filter(lambda x: isinstance(x, JobShopProblem), children))
|
||||
|
||||
def visit_skip_preface(self, node: arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> List[JobShopProblem]:
|
||||
if self.debug:
|
||||
print("skip_preface\nchildren:", children)
|
||||
return list(filter(lambda x: type(x) is list, children))[0]
|
||||
|
||||
# ignore eol nodes
|
||||
def visit_eol(self, node:arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> None:
|
||||
return None
|
||||
|
||||
# ignore trim_ws nodes
|
||||
def visit_trim_ws(self, node:arpeggio.ParseTreeNode, children: arpeggio.SemanticActionResults) -> None:
|
||||
return None
|
||||
|
||||
def parse_jobshop1_string(inputdata: str) -> JobShopProblem:
|
||||
"""parse string with jobshop1-formatted data (multiple problem instances)
|
||||
and return list of JobShopProblem s"""
|
||||
parse_tree = parser.parse(inputdata)
|
||||
return arpeggio.visit_parse_tree(parse_tree, JobShop1Visitor())
|
||||
|
||||
def parse_jobshop1_file(filename: Union[str, bytes]) -> List[JobShopProblem]:
|
||||
"""Open file with jobshop1-formatted data (multiple problem instances),
|
||||
parse it and return list of JobShopProblem s"""
|
||||
|
||||
with open(filename) as datafile:
|
||||
inputdata: str = datafile.read()
|
||||
return parse_jobshop1_string(inputdata)
|
||||
|
||||
|
||||
def main():
|
||||
print(parse_jobshop1_file("../inputdata/jobshop1.txt"))
|
||||
|
||||
parser = ParserPEG(grammar, "job_shop1", skipws=False)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue