feat: Switch method to tree

This commit is contained in:
lwad 2024-03-07 23:50:15 +00:00
parent 77bbac5915
commit 1a6c65bdd4
1 changed files with 48 additions and 58 deletions

106
main.py
View File

@ -1,66 +1,56 @@
#! /usr/bin/env python
def putchar(string, highlight=None):
match highlight:
case "red":
return f"\033[31;1;1m{string}\033[0m"
case "green":
return f"\033[32;1;1m{string}\033[0m"
case "blue":
return f"\033[34;1;1m{string}\033[0m"
case _:
return string
from pprint import pprint
with open("/etc/nixos/components/programs/imunes.nix", "r") as file:
stack = []
prev_char = None
prev_prev_char = None
sequential = 0
prev_sequential = 0
while (line := file.readline()):
for char in line:
format = None
def getTopNode(nodes):
node = nodes
while isinstance(node[-1], list):
node = node[-1]
return node
if len(stack) != 0 and stack[-1][0] == "\"":
if char == "\"":
if prev_char != "\\":
stack.pop()
format = "green"
def getAlmostTopNode(nodes):
node = nodes
while isinstance(node[-1], list):
parent = node
node = node[-1]
return parent
separators = {"{": (",", ";")}
nodes = [""]
top_node = getTopNode(nodes)
openers = ("{", "(", "[")
closers = ("}", ")", "]")
last_opener = None
def stripTree(node):
if isinstance(node, str):
return
for index in range(len(node)):
if isinstance(node[index], str):
node[index] = node[index].strip(" \n")
else:
stripTree(node[index])
try:
with open("/etc/nixos/components/programs/imunes.nix", "r") as file:
while (line := file.readline()):
for char in line:
if char not in closers:
getTopNode(nodes)[-1] += char
if char in openers:
getTopNode(nodes).append([""])
else:
if char == "{":
if prev_char == "$":
if prev_prev_char != "\\":
stack.append(("}", (",", ";")))
format = "blue"
else:
if len(stack) != 0 and stack[-1][0] == "''":
if char == "'":
prev_sequential = sequential
sequential += 1
elif char == "{" and prev_char == "$" and prev_sequential % 3 != 2:
stack.append(("}", (",", ";")))
format = "blue"
else:
if sequential % 3 == 2 and char != "$":
stack.pop()
format = "green"
prev_sequential = sequential
sequential = 0
else:
if len(stack) != 0 and char in stack[-1][0]:
stack.pop()
format = "blue"
getAlmostTopNode(nodes).append("")
getTopNode(nodes)[-1] += char
if char == "'" and prev_char == "'":
format = "green"
stack.append(("''", ()))
stripTree(nodes)
if char == "\"":
format = "green"
stack.append(("\"", ()))
print(putchar(char, format), end="")
prev_prev_char = prev_char
prev_char = char
pprint(nodes)
except BrokenPipeError:
exit(0)