2019-05-09 16:37:39 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
2021-09-30 12:52:08 +00:00
|
|
|
from ...core.coroutine import coroutine, STOOOP
|
2019-05-13 17:37:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@coroutine
|
|
|
|
def look_for_rdleaf(target):
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Coroutine which look to "{...}" which are RdLeaf
|
2019-05-13 17:37:34 +00:00
|
|
|
|
|
|
|
:example:
|
2021-09-30 12:52:08 +00:00
|
|
|
>>> from ...core.str2 import list_sink
|
2019-05-13 17:37:34 +00:00
|
|
|
>>> 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
|
|
|
|
|
2019-05-16 15:21:34 +00:00
|
|
|
stacking = False
|
2019-05-13 17:37:34 +00:00
|
|
|
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)
|
2019-05-09 16:37:39 +00:00
|
|
|
|
2019-05-09 14:46:24 +00:00
|
|
|
|
|
|
|
class RdLeaf:
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Random leaf"""
|
2019-05-09 14:46:24 +00:00
|
|
|
|
2019-05-09 16:37:39 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self._name = name
|
|
|
|
self.rdleaf = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def replace(self, computed):
|
2019-05-13 17:37:34 +00:00
|
|
|
return computed[self._name]
|
2019-05-09 16:37:39 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2019-05-13 17:37:34 +00:00
|
|
|
return "{" + self._name + "}"
|
2019-05-09 16:37:39 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2019-05-13 17:37:34 +00:00
|
|
|
return f"<{self.__class__.__name__} {self._name}>"
|