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