Skip to content

Commit

Permalink
Move preprocessor logic to its own file (#17)
Browse files Browse the repository at this point in the history
* refactor: Move preprocessor logic to its own file

* chore: Minor change
  • Loading branch information
TheVaultdweller13 authored Jan 2, 2025
1 parent e89f418 commit 4908d52
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
30 changes: 1 addition & 29 deletions src/parser.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
27 changes: 27 additions & 0 deletions src/preprocessor.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4908d52

Please sign in to comment.