def _read_lnk(tokens): """Read and return a tuple of the pred's lnk type and lnk value, if a pred lnk is specified.""" # < FROM : TO > or < FROM # TO > or < TOK... > or < @ EDGE > lnk = None if tokens[0] == '<': tokens.popleft() # we just checked this is a left angle if tokens[0] == '>': pass # empty <> brackets the same as no lnk specified # edge lnk: ['@', EDGE, ...] elif tokens[0] == '@': tokens.popleft() # remove the @ lnk = Lnk.edge(tokens.popleft()) # edge lnks only have one number # character span lnk: [FROM, ':', TO, ...] elif tokens[1] == ':': lnk = Lnk.charspan(tokens.popleft(), tokens[1]) tokens.popleft() # this should be the colon tokens.popleft() # and this is the cto # chart vertex range lnk: [FROM, '#', TO, ...] elif tokens[1] == '#': lnk = Lnk.chartspan(tokens.popleft(), tokens[1]) tokens.popleft() # this should be the hash tokens.popleft() # and this is the to vertex # tokens lnk: [(TOK,)+ ...] else: lnkdata = [] while tokens[0] != '>': lnkdata.append(int(tokens.popleft())) lnk = Lnk.tokens(lnkdata) _read_literals(tokens, '>') return lnk
def read_lnk(tokens): """Read and return a tuple of the pred's lnk type and lnk value, if a pred lnk is specified.""" # < FROM : TO > or < FROM # TO > or < TOK... > or < @ EDGE > lnk = None if tokens[0] == _left_angle: tokens.popleft() # we just checked this is a left angle if tokens[0] == _right_angle: pass # empty <> brackets the same as no lnk specified # edge lnk: ['@', EDGE, ...] elif tokens[0] == _at: tokens.popleft() # remove the @ lnk = Lnk.edge(tokens.popleft()) # edge lnks only have one number # character span lnk: [FROM, ':', TO, ...] elif tokens[1] == _colon: lnk = Lnk.charspan(tokens.popleft(), tokens[1]) tokens.popleft() # this should be the colon tokens.popleft() # and this is the cto # chart vertex range lnk: [FROM, '#', TO, ...] elif tokens[1] == _hash: lnk = Lnk.chartspan(tokens.popleft(), tokens[1]) tokens.popleft() # this should be the hash tokens.popleft() # and this is the to vertex # tokens lnk: [(TOK,)+ ...] else: lnkdata = [] while tokens[0] != _right_angle: lnkdata.append(int(tokens.popleft())) lnk = Lnk.tokens(lnkdata) validate_token(tokens.popleft(), _right_angle) return lnk
def testTokensLnk(self): lnk = Lnk.tokens([1, 2, 3]) assert lnk.type == Lnk.TOKENS assert lnk.data == (1, 2, 3) assert str(lnk) == '<1 2 3>' repr(lnk) # no error lnk = Lnk.tokens(['1']) assert lnk.data == (1,) # empty tokens list might be invalid, but accept for now lnk = Lnk.tokens([]) assert lnk.data == tuple() with pytest.raises(TypeError): Lnk.tokens(1) with pytest.raises(ValueError): Lnk.tokens(['a','b'])