Ejemplo n.º 1
0
def string_to_tok_by_type(string=None, tok_types=None, mode="first" #or "longer"
                          , only_fullmatch=True):
    string = _or(string, "")
    tok_types = _or(tok_types, [])
    if mode == "first":
        tr = TextReader(string)
        for tt in tok_types:
            rslt = tr.read_thing(tt)
            if rslt.is_fullmatch():
                return rslt.readedlist[0]
            #tr.inp_buf_iter.re_iter()
        return None
    elif mode == "longer":
        ml = -1
        rt = None
        tr = TextReader(string)
        for tt in tok_types:
            rslt = tr.read_thing(tt)
            if rslt.is_fullmatch():
                tok = rslt.readedlist[0]
                tl = len(tok.string)
                if tl > ml:
                    ml = tl
                    rt = tok
                tr.inp_buf_iter.re_iter()
        return rt
    else:
        return None
Ejemplo n.º 2
0
 def __init__(self, tr:TextReader=None, token_types=None, tokens_skip=None
              , mode="first", n_prelook=25):
     self.text_reader = tr
     if isinstance(self.text_reader, BufferedIterator):
         self.text_reader = TextReader(self.text_reader)
     self.token_types = _or(token_types, [])
     self.tokens_skip = _or(tokens_skip, [])
     self.mode = mode # "first" or "longer"
     self.n_prelook = n_prelook
     self.buffer, self.backbuffer = deque([]), deque([])
     self.at_end = False
Ejemplo n.º 3
0
 def _read_from(self:object, tr:TextReader, **options) -> ReadResult:
     rslt = tr.read_thing(self.thing, **options)
     if rslt.is_fullmatch():
         flat = _or(get_from_nested_dict(options, self.name + ".read_from", "flat")
                    , self.flat)
         skip = _or(get_from_nested_dict(options, self.name + ".read_from", "skip")
                    , self.skip)
         # set_to_nested_dict(options, None, "BIReadable.read_from", "flat")
         # set_to_nested_dict(options, None, "BIReadable.read_from", "skip")
         if not flat:
             rslt.readedlist = [ParseNode(self.name, rslt.readedlist, skip=skip, flat_eq_name=self.flat_eq_name
                                          , parent=None, Type=self.type , priority=self.priority)]
     return rslt
Ejemplo n.º 4
0
def thing_pprint(thing=None, level=0, pref="", post="", end="\n", first_indent=True):
    pref = _or(pref, "")
    if isinstance(thing, ClsShow):
        thing.PPrint(level, pref, post, end, first_indent)
    elif isinstance(thing, dict):
        space = gen_indentation(level)
        if first_indent:
            print(space, end="")
        print(pref + "{")
        for (k,v) in thing.items():
            thing_pprint(k, level+1, post=" : ", end="")
            thing_pprint(v, 0)
        print(space + "}" + post, end=end)
    elif isinstance(thing, (list, tuple)):
        space = gen_indentation(level)
        openbr = "("
        closebr = ")"
        if isinstance(thing, list):
            openbr = "["
            closebr = "]"
        if first_indent:
            print(space, end="")
        print(pref + openbr)
        comma="  "
        for th in thing:
            thing_pprint(th, level+1, pref=comma)
            comma=", "
        print(space + closebr + post, end=end)
    else:
        space = gen_indentation(level)
        if first_indent:
            print(space, end="")
        print(pref + repr(thing) + post, end=end)
Ejemplo n.º 5
0
 def copy_with(self, name=None, thing=None, skip=None, flat=None
               , flat_eq_name=None, Type=None, priority=None, grammar=None
               , add_to_grammar=False):
     if self.name == name:
         add_to_grammar = False
     return Node(name=_or(name, self.name), thing=_or(thing, self.thing), skip=_or(skip, self.skip)
                 , flat=_or(flat, self.flat), flat_eq_name=_or(flat_eq_name, self.flat_eq_name)
                 , Type=_or(Type, self.type), priority=_or(priority, self.priority)
                 , grammar=_or(grammar, self.grammar), add_to_grammar=add_to_grammar)
Ejemplo n.º 6
0
 def __init__(self, *thseq, seq=None):
     self.thseq = []
     thseq = _or(seq, thseq)
     for th in thseq:
         if isinstance(th, self.__class__):
             self.thseq.extend(th.thseq)
         else:
             self.thseq.append(th)
Ejemplo n.º 7
0
 def __init__(self, *thseq, mode=None):
     self.thseq = []
     for th in thseq:
         if isinstance(th, self.__class__):
             self.thseq.extend(th.thseq)
         else:
             self.thseq.append(th)
     self.mode = _or(mode, "first")
Ejemplo n.º 8
0
 def __init__(self, name="", subnodes=None, skip=False, parent=None, Type="", priority=1, flat_eq_name=False):
     self.name = name
     self.flat_eq_name = flat_eq_name
     self.subnodes = []
     subnodes = list(_or(subnodes,[]))
     self.add_subnodes(subnodes)
     self.skip = skip
     self.parent = parent
     self.type = Type
     self.priority = priority
     self._ClsShow__no_repr = ["parent"]
Ejemplo n.º 9
0
 def __init__(self, rules=None):
     self.name_register = {}
     self.rules = list(_or(rules,[]))
Ejemplo n.º 10
0
 def unref(self, register=None):
     if None is not self.grammar:
         register = _or(register, self.grammar.name_register)
         return register.get(self.ref_name)
     return None
Ejemplo n.º 11
0
 def __init__(self, matrix=None, vertex_list=None, **kwargs):
     matrix = _or(matrix, csr_matrix, ([], ))
     self.vertex_list = vertex_list
     self.adjacency_matrix = None
     super(IncidenceMatrix, self).__init__(matrix, **kwargs)
Ejemplo n.º 12
0
 def __init__(self, string=None, Type=None):
     self.string = _or(string, "")
     self.type = _or(Type, TTYPE_EOF)
Ejemplo n.º 13
0
 def __init__(self, name=None, pattern=None):
     self.name = _or(name, "")
     self.pattern = pattern