feat: подготовка к разбору сделана
This commit is contained in:
@ -5,22 +5,46 @@ class RulesParser:
|
|||||||
TRANSFORM = r'->'
|
TRANSFORM = r'->'
|
||||||
B_TRANSFORM = r'->\|'
|
B_TRANSFORM = r'->\|'
|
||||||
NEWLINE = '\n'
|
NEWLINE = '\n'
|
||||||
IGNORE = r'\s'
|
IGNORE = r'\s+'
|
||||||
COMMENTS = r'//.+$'
|
COMMENTS = r'//.+$'
|
||||||
EMPTY = r'\$'
|
EMPTY = r'\$'
|
||||||
|
|
||||||
def parse(self, file: str) -> OrderedDict[str, str]:
|
def parse(self, file: str):
|
||||||
"""
|
"""
|
||||||
Parsing file according to syntax, specified
|
Parsing file according to syntax, specified
|
||||||
in class variables. Hardcoded for now (and forever)
|
in class variables. Hardcoded for now (and forever)
|
||||||
"""
|
"""
|
||||||
with open(file, 'r') as f:
|
with open(file, 'r') as f:
|
||||||
text = f.read()
|
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:
|
def _remove_comments(self, src: str) -> str:
|
||||||
"""
|
"""
|
||||||
removes comments from end of lines and returns
|
removes comments from end of lines and returns
|
||||||
cleaned text
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user