initial parsing prototype

This commit is contained in:
randomuser 2021-06-29 13:07:22 -05:00
parent a897561c49
commit b154d12c1d
1 changed files with 54 additions and 0 deletions

54
uml.py Normal file
View File

@ -0,0 +1,54 @@
from fileinput import input as finput
class UntitledError: pass
class InternalError(UntitledError): pass
class ParsingError(UntitledError): pass
class MiscError(UntitledError): pass
class Element:
def __init__(self, element, data=None):
if not data: self.data = ""
else: self.data = data
self.element = element
def append(self, string, space=False):
if space: self.data += " "
self.data += string
def printchain(chain):
for i in chain:
if not i.data == None: print(i.data, i.element)
else: print(i.element)
chain = []
for i in finput():
i = i.rstrip()
# ignore blank lines
if len(i) == 0: continue
# lines with text
if i[0].isalpha(): chain.append(Element("p", i))
# we have a processing command
if not i[0].isalpha():
command = ""
params = ""
counter = 0
for j in i:
if not j.isalpha() \
and not j == ' ':
command += j
else:
if j == ' ':
params = i[counter + 1:]
else:
params = i[counter:]
break
counter += 1
if command[0] == "#": chain.append(Element("h" + str(len(command)), params))
elif command == "%": chain.append(Element("c", params))
elif command == "*": chain.append(Element("l", params))
elif command == "-": chain.append(Element("l", params))
elif command == ">": chain.append(Element("q", params))
elif command == "=": chain.append(Element("r", params))
printchain(chain)