diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-08-29 22:59:56 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-08-29 22:59:56 +0800 |
commit | d303447dc7a830489828be2e66ccf8c36af4aed6 (patch) | |
tree | 53c02938a9b85ff94a5621371faa360f5630a6d8 /projects/hackc/tokens.py | |
parent | 7f54baf2668a58f2908f8242b2fbafc65a7f684a (diff) |
hackc: backslash escape extension
Diffstat (limited to 'projects/hackc/tokens.py')
-rw-r--r-- | projects/hackc/tokens.py | 8 |
1 files changed, 6 insertions, 2 deletions
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) |