From 927cd2a133a6f992da10846b8c245eef934beeb6 Mon Sep 17 00:00:00 2001 From: ElectronixTM Date: Sat, 15 Mar 2025 23:57:02 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BF=D0=BE=D0=B4=D0=B3=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BA=20=D1=80=D0=B0=D0=B7=D0=B1?= =?UTF-8?q?=D0=BE=D1=80=D1=83=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rulesparser.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/rulesparser.py b/src/rulesparser.py index cb629a2..dd65049 100644 --- a/src/rulesparser.py +++ b/src/rulesparser.py @@ -5,22 +5,46 @@ class RulesParser: TRANSFORM = r'->' B_TRANSFORM = r'->\|' NEWLINE = '\n' - IGNORE = r'\s' + IGNORE = r'\s+' COMMENTS = r'//.+$' EMPTY = r'\$' - def parse(self, file: str) -> OrderedDict[str, str]: + def parse(self, file: str): """ Parsing file according to syntax, specified in class variables. Hardcoded for now (and forever) """ with open(file, 'r') as f: text = f.read() + lines = self._get_lines(text) + self._parse_rule(lines[0]) + + def _parse_rule(self, line: str) -> tuple[str, str]: + """ + tries to parse rule according to set grammar + """ + print(re.split(self.IGNORE, line)) + + def _get_lines(self, src: str) -> list[str]: + """ + Get cleaned lines only with rules to parse + """ + text = self._remove_comments(src) + text = self._strip_lines(text) + lines = list(filter(lambda x: x != '', text.split(self.NEWLINE))) + return lines def _remove_comments(self, src: str) -> str: """ removes comments from end of lines and returns cleaned text """ - return re.sub(self.COMMENTS, "", src) + return re.sub(self.COMMENTS, '', src, flags=re.M) + def _strip_lines(self, src: str) -> str: + """ + Strips whitespaces at the end of lines + """ + result = re.sub(r' +$', '', src, flags=re.M) + # result = re.sub(r"\n+", r'\n', result) + return result