From d303447dc7a830489828be2e66ccf8c36af4aed6 Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Mon, 29 Aug 2022 22:59:56 +0800 Subject: hackc: backslash escape extension --- projects/hackc/tokens.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'projects/hackc/tokens.py') diff --git a/projects/hackc/tokens.py b/projects/hackc/tokens.py index 7ae37ce..50c4173 100644 --- a/projects/hackc/tokens.py +++ b/projects/hackc/tokens.py @@ -35,7 +35,7 @@ class Token: self.column = column @classmethod - def from_line(cls, line: str, line_no: int, column: int): + def from_line(cls, line: str, line_no: int, column: int, extensions=[]): """Extract first token from line and return it as an instance of Token.""" if not line: return None @@ -47,7 +47,11 @@ class Token: if int_match is not None: return Token("integer", int_match.group(1), line_no, column) - str_match = re.match('(".*")', line) + if "escape" in extensions: + str_match = re.match(r'("(.|\\")+?[^\\]")', line) + else: + str_match = re.match('(".*?")', line) + if str_match is not None: return Token("string", str_match.group(1), line_no, column) -- cgit v1.2.3