Commit 20635758 authored by Savva Golubitsky's avatar Savva Golubitsky

topology regexp and line after line processing

parent fcfb8126
...@@ -8,7 +8,7 @@ scope = [ ...@@ -8,7 +8,7 @@ scope = [
"__END__" "__END__"
] ]
class Stmt(): class Parms():
__slots__=( __slots__=(
'module', 'module',
'entry_func', 'entry_func',
...@@ -28,33 +28,91 @@ class Stmt(): ...@@ -28,33 +28,91 @@ class Stmt():
stri += s+": {}, ".format(getattr(self, s)) stri += s+": {}, ".format(getattr(self, s))
return stri 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"...}}] # ent = [{"name":{"module":"val" , "entr_func":"val"...}}]
dotfile = open("./testgr.adot", "r").read() file = open("./testgr.adot", "r")
graphcheck = re.match(r"\A(digraph)\s+\w*\n{(.*\s)*}\Z", dotfile) dot = file.read()
graphcheck = re.fullmatch(r"\A(digraph)\s+\w*\n{(.*\s)*}\Z", dot)
if graphcheck is None: if graphcheck is None:
print("Incorrect graph, check syntax!") print("Incorrect graph, check syntax!")
exit() 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__ считается объявлением # Все что до __BEGIN__ считается объявлением
beg = re.search((scope[0]), dotfile).start() beg = re.search((scope[0]), dotfile).start()
stmts = {} entities = {}
rawstmts = re.findall(r"\w+\s\[.*\]", dotfile[:beg]) #Парсинг свойств переходов и состояний
for raw in rawstmts: rawents = re.findall(r"\w+\s*?\[.*\]", dotfile[:beg])
res = raw.split(r" ", 1) for raw in rawents:
st = Stmt() res = re.split(r"\s+", raw, 1)
rs =res[1][1:-1].split(r", ") ent = Parms()
rs =re.split(r",\s*", res[1][1:-1]) #.split(r", ")
for r in rs: for r in rs:
r=r.split(r"=", 1) r=r.split(r"=", 1)
for s in st.__slots__: for s in ent.__slots__:
if s == r[0]: if s == r[0]:
setattr(st, s, r[1]) setattr(ent, s, r[1])
stmts[res[0]] = st entities[res[0]] = ent
for s in entities:
print(s, entities[s])
for s in stmts: #Парсинг топологии графа
print(s, stmts[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