diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2023-11-03 19:55:52 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2023-11-03 19:55:52 +0200 |
| commit | abf6cbdecc8eb006a859205dee7500c2b70c0998 (patch) | |
| tree | 4938cfaf270b6c83a77767de968845f704049205 /src/tokenizer.py | |
| parent | a3b677bac00060393cc66f1bb3b350413282a4ae (diff) | |
Fix macros, add string I/O helpers
Diffstat (limited to 'src/tokenizer.py')
| -rw-r--r-- | src/tokenizer.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/tokenizer.py b/src/tokenizer.py new file mode 100644 index 0000000..2049461 --- /dev/null +++ b/src/tokenizer.py @@ -0,0 +1,32 @@ +def tokenize(line: str) -> list[str]: + cur: str = "" + result: list[str] = [] + is_py_expr = False + idx = 0 + while idx < len(line): + c = line[idx] + if not is_py_expr and c == "$": + is_py_expr = True + 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 + result.append(cur) + cur = "" + elif is_py_expr: + 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 |
