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')
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
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
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
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)
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)
def encode_label(label): srt, vid = sort_vid_split(label) return etree.Element('label', vid=vid)