def _read_from(self:object, tr:TextReader, **options) -> ReadResult: if self.mode == "first": for th in self.thseq: rslt = tr.read_thing(th, **options) if rslt.is_fullmatch(): return rslt return Nomatch([], self) elif self.mode == "longer": maxl = -1 mid = -1 i = 0 for th in self.thseq: rslt = tr.read_thing(th, **options) if rslt.is_fullmatch(): l = len(thing_as_string(rslt.readedlist)) if l > maxl: maxl = l mid = i tr.push_back(rslt.readedlist) i += 1 if mid < 0: return Nomatch([], self) else: rslt = tr.read_thing(self.thseq[mid]) return rslt
def _read_from(self:object, tr:TextReader, **options) -> ReadResult: acc = "" for th in self.thseq: rslt = tr.read_thing(th, **options) if rslt.is_fullmatch(): acc += thing_as_string(rslt.readedlist) else: tr.push_back(acc) return ReadResult(rslt.state, acc, self) break return Fullmatch(acc, self)
def _read_from(self, tr, **options) -> ReadResult: if isinstance(tr, TextReader): rslt = tr.read_thing(self.pattern, **options) if rslt.is_fullmatch(): string = thing_as_string(rslt.readedlist) rslt.readedlist = [Token(string, self)] rslt.readed_object = self return rslt elif isinstance(tr, TokenReader): rslt = tr.read_next(**options) if rslt.is_fullmatch(): readed_tok = rslt.readedlist[0] if readed_tok == self: rslt.readed_object = self return rslt else: tr.push_back(rslt.readedlist) return Nomatch([], self) else: return Nomatch([], self)
def _read_from(self:object, tr:TextReader, **options) -> ReadResult: n_chars = 0 acc = "" if 'char' == self.mode: while True: if self.max_chars >= 0 and n_chars >= self.max_chars: break ch = tr.read_next(**options) if not ch.is_fullmatch(): break if None is not re.match(self.regexp, thing_as_string(ch.readedlist)): acc += thing_as_string(ch.readedlist) else: tr.push_back(ch.readedlist) break n_chars += 1 elif 'string' == self.mode: acc = "" while True: if self.max_chars >= 0 and n_chars >= self.max_chars: break ch = tr.read_next(**options) if not ch.is_fullmatch(): break if None is re.match(self.regexp, acc + thing_as_string(ch.readedlist)): tr.push_back(ch.readedlist) break else: acc += thing_as_string(ch.readedlist) n_chars += 1 else: return Nomatch([], self) if n_chars >= self.min_chars: return Fullmatch(thing_as_string(acc), self) else: tr.push_back(acc) return Nomatch(thing_as_string(acc), self)
def _as_string(self): return thing_as_string(self.subnodes)
def _as_string(self): return thing_as_string(self.thing)