aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_asm/tokenizer.py
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2024-02-21 12:07:07 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2024-02-21 12:07:07 +0200
commit927c018f12e73c254cec32c0f60e1c01b9fc0319 (patch)
tree21fbb975e3567cb0d9ab3fb34519c50325571d6c /atk16_asm/tokenizer.py
parenta0b4a1a6107e4dc905b21858d8ac76c47293cfb4 (diff)
Refactor names
Diffstat (limited to 'atk16_asm/tokenizer.py')
-rw-r--r--atk16_asm/tokenizer.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/atk16_asm/tokenizer.py b/atk16_asm/tokenizer.py
new file mode 100644
index 0000000..cf31b56
--- /dev/null
+++ b/atk16_asm/tokenizer.py
@@ -0,0 +1,47 @@
+def tokenize(line: str, retain_curlies = False) -> list[str]:
+ cur: str = ""
+ result: list[str] = []
+ is_py_expr = False
+ is_string = False
+ idx = 0
+ while idx < len(line):
+ c = line[idx]
+ if not is_py_expr and c == "$":
+ is_py_expr = True
+ if retain_curlies:
+ cur += "${"
+ idx += 1
+ elif is_py_expr and c == "$":
+ raise Exception("Unexpected start of python expr while already parsing a python expression:\n" + line)
+ elif not is_py_expr and c == "}":
+ raise Exception("Unexpected end of python expr while not parsing a python expression:\n" + line)
+ elif is_py_expr and c == "}":
+ is_py_expr = False
+ if retain_curlies:
+ cur += c
+ result.append(cur)
+ cur = ""
+ elif is_py_expr:
+ cur += c
+ elif not is_string and c == "\"":
+ cur += "\""
+ is_string = True
+ elif is_string and c == "\"":
+ cur += "\""
+ is_string = False
+ result.append(cur)
+ cur = ""
+ elif is_string:
+ cur += c
+ elif c == " " or c == "\t":
+ result.append(cur)
+ cur = ""
+ else:
+ cur += c
+
+ idx += 1
+
+ if len(cur) > 0:
+ result.append(cur)
+
+ return [r for r in result if r != ""] \ No newline at end of file