Feat(random): random str parsing is done

This commit is contained in:
2019-05-13 19:37:34 +02:00
parent 040dfff0ec
commit 037fd9f68a
2 changed files with 74 additions and 8 deletions

View File

@@ -9,6 +9,46 @@
"""
"""
from ..coroutine import *
@coroutine
def look_for_rdleaf(target):
""" Coroutine which look to "{...}" which are RdLeaf
:example:
>>> from ..str2 import list_sink
>>> str2list = look_for_rdleaf(list_sink)
>>> for i in "{a}+{a*b}-2":
... str2list.send(i)
>>> a = str2list.throw(STOOOP)
>>> a
[<RdLeaf a>, '+', <RdLeaf a*b>, '-', '2']
"""
try:
target_ = target()
except TypeError:
target_ = target
try:
while True:
tok = yield
if tok == "{":
stack = ""
stacking = True
elif tok == "}":
target_.send(RdLeaf(stack))
stack = ""
stacking = False
else:
if stacking:
stack += tok
else:
target_.send(tok)
except STOOOP as err:
yield target_.throw(err)
class RdLeaf:
@@ -25,10 +65,10 @@ class RdLeaf:
return self._name
def replace(self, computed):
return computed[self.name]
return computed[self._name]
def __str__(self):
return "{" + self.name + "}"
return "{" + self._name + "}"
def __repr__(self):
return f"<{self.__class__.__name__} {self.__str__}>"
return f"<{self.__class__.__name__} {self._name}>"