aboutsummaryrefslogtreecommitdiffstats
path: root/src/tokenizer.py
blob: d9c60580b216a462833f68a28588a6eaabe749d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def tokenize(line: str) -> 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
      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 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)

  print(result)

  return [r for r in result if r != ""]