def test_sort_vid_split():
    assert sort_vid_split('x1') == ('x', '1')
    assert sort_vid_split('event10') == ('event', '10')
    assert sort_vid_split('ref-ind2') == ('ref-ind', '2')
    with pytest.raises(ValueError): sort_vid_split('x')
    with pytest.raises(ValueError): sort_vid_split('1')
    with pytest.raises(ValueError): sort_vid_split('1x')
示例#2
0
def test_sort_vid_split():
    assert sort_vid_split('x1') == ('x', '1')
    assert sort_vid_split('event10') == ('event', '10')
    assert sort_vid_split('ref-ind2') == ('ref-ind', '2')
    with pytest.raises(ValueError):
        sort_vid_split('x')
    with pytest.raises(ValueError):
        sort_vid_split('1')
    with pytest.raises(ValueError):
        sort_vid_split('1x')
示例#3
0
def _encode_variable(v, varprops=None):
    if varprops is None: varprops = {}
    srt, vid = sort_vid_split(v)
    var = etree.Element('var', vid=vid, sort=srt)
    if v in varprops:
        var.extend(_encode_extrapair(key, val) for key, val in varprops[v])
        del varprops[v]
    return var
示例#4
0
def encode_variable(v, varprops=None):
    if varprops is None: varprops = {}
    srt, vid = sort_vid_split(v)
    var = etree.Element('var', vid=vid, sort=srt)
    if v in varprops:
        var.extend(encode_extrapair(key, val)
                   for key, val in varprops[v])
        del varprops[v]
    return var
示例#5
0
    def apply(self, var, props, reverse=False):
        """
        Apply the VPM to variable *var* and properties *props*.

        Args:
            var: a variable
            props: a dictionary mapping properties to values
            reverse: if `True`, apply the rules in reverse (e.g. from
                grammar-external to grammar-internal forms)
        Returns:
            a tuple (v, p) of the mapped variable and properties
        """
        vs, vid = sort_vid_split(var)
        if reverse:
            # variable type mapping is disabled in reverse
            # tms = [(b, op, a) for a, op, b in self._typemap if op in _RL_OPS]
            tms = []
        else:
            tms = [(a, op, b) for a, op, b in self._typemap if op in _LR_OPS]
        for src, op, tgt in tms:
            if _valmatch([vs], src, op, None, self._semi, 'variables'):
                vs = vs if tgt == ['*'] else tgt[0]
                break
        newvar = '{}{}'.format(vs, vid)

        newprops = {}
        for featsets, valmap in self._propmap:
            if reverse:
                tgtfeats, srcfeats = featsets
                pms = [(b, op, a) for a, op, b in valmap if op in _RL_OPS]
            else:
                srcfeats, tgtfeats = featsets
                pms = [(a, op, b) for a, op, b in valmap if op in _LR_OPS]
            vals = [props.get(f) for f in srcfeats]
            for srcvals, op, tgtvals in pms:
                if _valmatch(vals, srcvals, op, vs, self._semi, 'properties'):
                    for i, featval in enumerate(zip(tgtfeats, tgtvals)):
                        k, v = featval
                        if v == '*':
                            print(i, len(vals), vals, k, v)
                            if i < len(vals) and vals[i] is not None:
                                newprops[k] = vals[i]
                        elif v != '!':
                            newprops[k] = v
                    break

        return newvar, newprops
示例#6
0
def read_variable(tokens, sort=None, variables=None):
    """
    Read and return the variable and update a property dict.
    Fail if the sort does not match the expected.
    """
    # var [ vartype PROP : val ... ]
    if variables is None:
        variables = defaultdict(list)
    var = tokens.popleft()
    srt, vid = sort_vid_split(var)
    # consider something like not(srt <= sort) in the case of subsumptive sorts
    if sort is not None and srt != sort:
        raise XDE('Variable {} has sort "{}", expected "{}"'.format(var, srt, sort))
    vartype, props = read_props(tokens)
    if vartype is not None and srt != vartype:
        raise XDE('Variable "{}" and its cvarsort "{}" are not the same.'.format(var, vartype))
    if srt == "h" and props:
        raise XDE('Handle variable "{}" has a non-empty property set {}.'.format(var, props))
    variables[var].extend(props)
    return (var, props)
示例#7
0
def read_variable(tokens, sort=None, variables=None):
    """
    Read and return the variable and update a property dict.
    Fail if the sort does not match the expected.
    """
    # var [ vartype PROP : val ... ]
    if variables is None:
        variables = defaultdict(list)
    var = tokens.popleft()
    srt, vid = sort_vid_split(var)
    # consider something like not(srt <= sort) in the case of subsumptive sorts
    if sort is not None and srt != sort:
        raise XDE('Variable {} has sort "{}", expected "{}"'
                  .format(var, srt, sort))
    vartype, props = read_props(tokens)
    if vartype is not None and srt != vartype:
        raise XDE('Variable "{}" and its cvarsort "{}" are not the same.'
                  .format(var, vartype))
    if srt == 'h' and props:
        raise XDE('Handle variable "{}" has a non-empty property set {}.'
                  .format(var, props))
    variables[var].extend(props)
    return (var, props)
示例#8
0
def encode_label(label):
    srt, vid = sort_vid_split(label)
    return etree.Element('label', vid=vid)