diff --git a/src/parser.py b/src/parser.py index 808bfd1..966adbc 100755 --- a/src/parser.py +++ b/src/parser.py @@ -1,38 +1,10 @@ #!/bin/env python import argparse import pprint -import re -from abc import abstractmethod -from typing import override import grammar as grammar import visitor as visitor - - -class Preprocessor: - @abstractmethod - def process(self, program: str): - pass - - -class CommentsPreprocessor(Preprocessor): - @override - def process(self, program: str): - cleaned_program = map( - lambda line: re.sub(r"#.*$", "", line), program.splitlines() - ) - return "\n".join(cleaned_program) - - -class PipelinePreprocessor(Preprocessor): - def __init__(self, preprocessor_actions): - self.preprocessor_actions = preprocessor_actions - - @override - def process(self, program: str): - for action in self.preprocessor_actions: - program = action.process(program) - return program +from preprocessor import PipelinePreprocessor, CommentsPreprocessor class ImperivmParser: diff --git a/src/preprocessor.py b/src/preprocessor.py new file mode 100644 index 0000000..56679c3 --- /dev/null +++ b/src/preprocessor.py @@ -0,0 +1,27 @@ +import re +from abc import ABC, abstractmethod +from typing import override, List + + +class Preprocessor(ABC): + @abstractmethod + def process(self, program: str) -> str: + pass + + +class CommentsPreprocessor(Preprocessor): + @override + def process(self, program: str): + cleaned_program = map(lambda line: re.sub(r"#.*$", "", line), program.splitlines()) + return "\n".join(cleaned_program) + + +class PipelinePreprocessor(Preprocessor): + def __init__(self, preprocessor_actions: List[Preprocessor]): + self.preprocessor_actions = preprocessor_actions + + @override + def process(self, program: str): + for action in self.preprocessor_actions: + program = action.process(program) + return program \ No newline at end of file