Правки по lab1
This commit is contained in:
@@ -34,8 +34,15 @@ class LexemeType:
|
||||
|
||||
|
||||
class Lexer:
|
||||
def __init__(self, lexeme_types: Iterable[LexemeType]):
|
||||
def __init__(
|
||||
self,
|
||||
lexeme_types: Iterable[LexemeType],
|
||||
error_regex: str,
|
||||
skip_types: Iterable[str] = [],
|
||||
):
|
||||
self.lexeme_types = lexeme_types
|
||||
self.skip_types = skip_types
|
||||
self.error_regex = re.compile(r"\s*(" + error_regex + ")")
|
||||
|
||||
def analyze(self, text: str) -> list[Lexem]:
|
||||
lexems: list[Lexem] = []
|
||||
@@ -43,16 +50,16 @@ class Lexer:
|
||||
for lex_type in self.lexeme_types:
|
||||
lexem, new_text = lex_type.consume(text)
|
||||
if lexem:
|
||||
lexems.append(lexem)
|
||||
if lexem.type_name not in self.skip_types:
|
||||
lexems.append(lexem)
|
||||
text = new_text
|
||||
break
|
||||
else:
|
||||
error_lexeme, text = self._consume_error(text)
|
||||
lexems.append(error_lexeme)
|
||||
return lexems
|
||||
|
||||
def _consume_error(self, text: str) -> tuple[Lexem, str]:
|
||||
match = re.match(r"\s*(\S+)", text)
|
||||
match = self.error_regex.match(text)
|
||||
err_text = match.group(1) if match else text.strip()
|
||||
print(f"Недопустимая лексема: {err_text}")
|
||||
rest = text[match.end() :] if match else ""
|
||||
|
||||
Reference in New Issue
Block a user