Пример #1
0
        def __init__(self, typename: str, instantiations: ParseResults):
            self.typename = typename

            if instantiations:
                self.instantiations = instantiations.asList()
            else:
                self.instantiations = []
Пример #2
0
def text_from_parsed(parsetree: ParseResults,
                     formatted: bool = True,
                     indent_width: int = 4) -> str:
    nested_list = parsetree.asList()
    flattened_list = flatten(nested_list)
    plain_str = " ".join(flattened_list)
    if not formatted:
        return plain_str

    # Forget my feeble efforts for now (below) and use sqlparse:
    return format_sql(plain_str, reindent=True, indent_width=indent_width)
Пример #3
0
 def from_parse_result(parse_result: ParseResults):
     """Return the result of parsing."""
     if parse_result:
         return ArgumentList(parse_result.asList())
     else:
         return ArgumentList([])
Пример #4
0
 def __init__(self, t: ParseResults):
     self.typename = Typename(t.asList())
Пример #5
0
def tree2str(t: ParseResults) -> str:
    out = []
    if isinstance(t, ParseResults):
        for e in t.asList():
            out.append(tree2str(e))
        for k, v in sorted(t.items()):
            out.append(tree2str(v))

    elif isinstance(t, CompValue):
        if t.name == 'PrefixDecl' and 'iri' in t:
            pref = t['prefix'] if 'prefix' in t else ''
            out.append('PREFIX %s: %s\n' % (pref, t['iri'].n3()))

        elif t.name.endswith('Query') or t.name.startswith('Sub'):
            ret = []
            for k, v in t.items():
                if k == 'modifier':
                    ret.append(v)
                elif k == 'projection':
                    ret.append(list2str(v, ele2str, joiner=' '))
                elif k == 'template':
                    ret.append('{%s}' % list2str(v, triple2str))
                elif k == 'limitoffset':
                    for k_, v_ in v.items():
                        ret.append('%s %s \n' % (k_.upper(), tree2str(v_)))
                elif k in fixed_recursion:
                    ret.append(fixed_recursion[k] % (tree2str(v)))
                else:
                    ret.append(tree2str(v))
            query_type = t.name[:-5] if t.name.endswith(
                'Query') else t.name[3:]
            out.append('%s %s' % (query_type.upper(), ' '.join(ret)))

        elif t.name == 'TriplesBlock' and 'triples' in t:
            for tri in t['triples']:
                out.append('%s\n' % (triple2str(tri)))
        elif t.name == 'OptionalGraphPattern' and 'graph' in t:
            out.append('OPTIONAL {%s} .\n' % tree2str(t['graph']))
        elif t.name == 'InlineData' and 'var' in t and 'value' in t:
            out.append(
                'Values (%s) {%s} .\n' %
                (ele2str(t['var']),
                 list2str(
                     t['value'], ele2str, prefix='(', suffix=')',
                     joiner='\n')))
        elif t.name == 'GroupOrUnionGraphPattern' and 'graph' in t:
            if len(t['graph']) > 1:
                ret = []
                for x in t['graph']:
                    ret.append(tree2str(x))
                out.append('{\n%s\n}' % ('\n} UNION {\n'.join(ret)))
            elif len(t['graph']) == 1:
                out.append('{\n%s\n}' % tree2str(t['graph']))
        elif t.name in ['Filter']:
            out.append('\n%s ( %s ) .\n' %
                       (t.name.upper(), ele2str(t['expr'])))
        elif t.name == 'OrderCondition' and 'order' in t and 'expr' in t:
            out.append('%s(%s)' % (ele2str(t['order']), ele2str(t['expr'])))

        else:
            for k, v in t.items():
                out.append(tree2str(v))
    elif isinstance(t, dict):
        for k, v in t.items():
            out.append(tree2str(v))
    elif isinstance(t, list):
        for e in t:
            out.append(tree2str(e))
    else:
        out.append("%s " % (ele2str(t)))
    return "".join(out)