Commit 20635758 authored by Savva Golubitsky's avatar Savva Golubitsky

topology regexp and line after line processing

parent fcfb8126
......@@ -8,7 +8,7 @@ scope = [
"__END__"
]
class Stmt():
class Parms():
__slots__=(
'module',
'entry_func',
......@@ -28,33 +28,91 @@ class Stmt():
stri += s+": {}, ".format(getattr(self, s))
return stri
#Props is line "[prop=smth, ...]"
def parm_from_props(props):
parm = Parms()
rs =re.split(r",\s*", props[:-1]) #.split(r", ")
for r in rs:
r=r.split(r"=", 1)
for s in parm.__slots__:
if s == r[0]:
setattr(parm, s, r[1])
return parm
def parm_from_entln(raw):
res = re.split(r"\[", raw, 1)
return res[0], parm_from_props(res[1])
def parm_from_topln(raw):
print(raw)
spl = re.split(r"\s*(=>|->|\[)\s*", raw)
print(spl)
# ent = [{"name":{"module":"val" , "entr_func":"val"...}}]
dotfile = open("./testgr.adot", "r").read()
graphcheck = re.match(r"\A(digraph)\s+\w*\n{(.*\s)*}\Z", dotfile)
file = open("./testgr.adot", "r")
dot = file.read()
graphcheck = re.fullmatch(r"\A(digraph)\s+\w*\n{(.*\s)*}\Z", dot)
if graphcheck is None:
print("Incorrect graph, check syntax!")
exit()
file.seek(0)
dotfile = file.readlines()
entities = {}
topology = {}
# ent_re - regular expr for edges, states, functions properties
ent_re = re.compile(r"^\w+\[.*\]$")
# top_re - regular expr for topology properties, most time consuming one
top_re = re.compile(r"^(\w+,?)+(->|=>)(\w+,?)+(\[(\w+=(\(?\w+,?\)?)+,?)+\])?")
# (r"^\w[\w\s,]*(->|=>)\s*\w[\w\s,=\[\]()]*$")
# com_re - regular expr for comment
com_re = re.compile(r"^\s*//.*$")
for i, ln in enumerate(dotfile):
ln = re.sub(r"\s", "", ln) #deleting all spaces
if com_re.match(ln) or len(ln)==0:
continue
elif ent_re.match(ln):
name, parm = parm_from_entln(ln)
entities[name] = parm
elif top_re.match(ln):
parm_from_topln(ln)
#for e in entities:
# print(e, entities[e])
exit()
# Все что до __BEGIN__ считается объявлением
beg = re.search((scope[0]), dotfile).start()
stmts = {}
entities = {}
rawstmts = re.findall(r"\w+\s\[.*\]", dotfile[:beg])
for raw in rawstmts:
res = raw.split(r" ", 1)
st = Stmt()
rs =res[1][1:-1].split(r", ")
#Парсинг свойств переходов и состояний
rawents = re.findall(r"\w+\s*?\[.*\]", dotfile[:beg])
for raw in rawents:
res = re.split(r"\s+", raw, 1)
ent = Parms()
rs =re.split(r",\s*", res[1][1:-1]) #.split(r", ")
for r in rs:
r=r.split(r"=", 1)
for s in st.__slots__:
for s in ent.__slots__:
if s == r[0]:
setattr(st, s, r[1])
stmts[res[0]] = st
setattr(ent, s, r[1])
entities[res[0]] = ent
for s in stmts:
print(s, stmts[s])
for s in entities:
print(s, entities[s])
#Парсинг топологии графа
rawstmts = re.findall(r"\s*([\w,\s]*(->|=>)[\w,\s]*(\[.*\])?)\n", dotfile[beg:])
print(rawstmts)
# for raw in rawstmts:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment