Beispiel #1
0
def compile_q_objects(search):
    compiler = wrapGrammar(DjangoCompiler)
    try:
        qs = compiler(search).expr()
        return qs, None
    except (BadDirective, ParseError) as why:
        return None, why.formatReason()
Beispiel #2
0
def compile_q_objects(search):
    compiler = wrapGrammar(DjangoCompiler)
    try:
        qs = compiler(search).expr()
        return qs, None
    except (BadDirective, ParseError) as why:
        return None, str(why)
Beispiel #3
0
def compile_q_objects(search):
    search = search.strip()
    compiler = wrapGrammar(DjangoCompiler)
    try:
        qs = compiler(search).expr()
        return qs, None
    except (BadDirective, ParseError) as why:
        return None, str(why)
    except (BadType, ParseError) as why:
        return None, str(why)
Beispiel #4
0
Datei: edn.py Projekt: jml/edn
def _make_edn_grammar(tagged_value_handler):
    # XXX: At last, my pact with the dark lord is fulfilled.
    #
    # I can't find any obvious way to specify bindings at the same time as
    # specifying input.  Making a new grammar for every set of handlers is
    # expensive.  This hideous alternative seems to work.
    class _specialized_edn(_unwrapped_edn):
        pass

    globals = dict(_specialized_edn.globals)
    globals.update({"TaggedValue": tagged_value_handler})
    _specialized_edn.globals = globals
    return wrapGrammar(_specialized_edn)
Beispiel #5
0
def _make_edn_grammar(tagged_value_handler):
    # XXX: At last, my pact with the dark lord is fulfilled.
    #
    # I can't find any obvious way to specify bindings at the same time as
    # specifying input.  Making a new grammar for every set of handlers is
    # expensive.  This hideous alternative seems to work.
    class _specialized_edn(_unwrapped_edn):
        pass

    globals = dict(_specialized_edn.globals)
    globals.update({'TaggedValue': tagged_value_handler})
    _specialized_edn.globals = globals
    return wrapGrammar(_specialized_edn)
Beispiel #6
0
def load_grammar(compile=True):
    if compile:
        # re/compile if necessary:
        if not os.path.exists(COMPILED_GRAMMAR_FILE) or \
           os.path.getmtime(GRAMMAR_FILE) > os.path.getmtime(COMPILED_GRAMMAR_FILE):
            compile_grammar()
        # load it like this - there doesn't seem to be a better user API available:
        import kgtk.kypher.grammar_compiled as cgc
        cgram = cgc.createParserClass(ometa.grammar.OMetaBase, {})
        return parsley.wrapGrammar(cgram)
    else:
        # rebuild the grammar from scratch:
        return parsley.makeGrammar(KYPHER_GRAMMAR, {})
Beispiel #7
0
Datei: edn.py Projekt: jml/edn
def _make_edn_grammar(tagged_value_handler):
    # XXX: At last, my pact with the dark lord is fulfilled.
    #
    # I can't find any obvious way to specify bindings at the same time as
    # specifying input.  Making a new grammar for every set of handlers is
    # expensive.  This hideous alternative seems to work.
    class _specialized_edn(_unwrapped_edn):
        pass

    globals = dict(_specialized_edn.globals)
    globals.update({"TaggedValue": tagged_value_handler})
    _specialized_edn.globals = globals
    return wrapGrammar(_specialized_edn)


edn = wrapGrammar(_unwrapped_edn)


def loads(string, handlers=None):
    if handlers is None:
        handlers = BUILTIN_READ_HANDLERS
    grammar = _make_edn_grammar(partial(make_tagged_value, handlers))
    return grammar(string).edn()


def _dump_bool(obj):
    if obj:
        return "true"
    else:
        return "false"
Beispiel #8
0
def make_debug_compiler():
    return wrapGrammar(DebugCompiler)
Beispiel #9
0
 def __init__(self, *args, **kwargs):
     self.bindings = self.setupBindings()
     self.grammar = wrapGrammar(
         loadGrammar(parseproto.dns, "grammar", self.bindings))
Beispiel #10
0
def make_debug_compiler():
    return wrapGrammar(DebugCompiler)
Beispiel #11
0
def _make_edn_grammar(tagged_value_handler):
    # XXX: At last, my pact with the dark lord is fulfilled.
    #
    # I can't find any obvious way to specify bindings at the same time as
    # specifying input.  Making a new grammar for every set of handlers is
    # expensive.  This hideous alternative seems to work.
    class _specialized_edn(_unwrapped_edn):
        pass

    globals = dict(_specialized_edn.globals)
    globals.update({'TaggedValue': tagged_value_handler})
    _specialized_edn.globals = globals
    return wrapGrammar(_specialized_edn)


edn = wrapGrammar(_unwrapped_edn)


def loads(string, handlers=None):
    if handlers is None:
        handlers = BUILTIN_READ_HANDLERS
    grammar = _make_edn_grammar(partial(make_tagged_value, handlers))
    return grammar(string).edn()


def _dump_bool(obj):
    if obj:
        return 'true'
    else:
        return 'false'
Beispiel #12
0
                output.append(chunk.data)
            elif chunk.tag.name == "QuasiExprHole":
                v = vals[chunk.args[0].data]
                output.append(v)
            else:
                raise TypeError("didn't expect %r in string template" % chunk)

        pt = PrecedenceTemplate(self.nextPrecedence, output)
        self.nextPrecedence = None
        return pt, None

PythonExpressionUnparser = TreeTransformerGrammar.makeGrammar(
    open("expression_unparser.parsley").read(),
    'PythonExpressionUnparser').createParserClass(PrecedenceTransformer, globals())


PythonStatementUnparser = TreeTransformerGrammar.makeGrammar(
    open("statement_unparser.parsley").read(),
    'PythonStatementUnparser').createParserClass(TreeTransformerBase, globals())


if __name__ == "__main__":
    from terml.parser import parseTerm as term
    f = open(sys.argv[1]).read()
    g = wrapGrammar(PythonParser)
    stmts = g(f).file_input()
    from pprint import pprint
    pprint(stmts)
    pt = PythonStatementUnparser.transform(stmts)[0]
    print pt
Beispiel #13
0
 def _rebuild_parser(self):
     self.parser = parsley.wrapGrammar(self.grammar)
 def __init__(self, *args, **kwargs):
     self.bindings = self.setupBindings()
     self.grammar = wrapGrammar(loadGrammar(parseproto.dns, "grammar",
                                            self.bindings))
Beispiel #15
0
                for host in second_hosts:
                    stdout.write(str(host))
            if first_groups:
                print '### Groups found only in the first config ###'
                for group in first_groups:
                    stdout.write(str(group))
            if second_groups:
                print '### Groups found only in the second config ###'
                for group in second_groups:
                    stdout.write(str(group))
            if first_classes:
                print '### Classes found only in the first config ###'
                for klass in first_classes:
                    stdout.write(str(klass))
            if second_classes:
                print '### Classes found only in the second config ###'
                for klass in second_classes:
                    stdout.write(str(klass))






iscgrammar = wrapGrammar(DhcpConfigContext)

def compare(file1, file2):
    parse1 = iscgrammar(strip_comments(open(file1))).GlobalParse()
    parse2 = iscgrammar(strip_comments(open(file2))).GlobalParse()
    parse1.diff(parse2)
Beispiel #16
0

Desugarer = TreeTransformerGrammar.makeGrammar(
    open("desugar_augmented_assignment.parsley").read(),
    "Desugarer").createParserClass(TreeTransformerBase, {"t": t})


PythonExpressionUnparser = TreeTransformerGrammar.makeGrammar(
    open("expression_unparser.parsley").read(),
    'PythonExpressionUnparser').createParserClass(PrecedenceTransformer, globals())


PythonStatementUnparser = TreeTransformerGrammar.makeGrammar(
    open("statement_unparser.parsley").read(),
    'PythonStatementUnparser').createParserClass(TreeTransformerBase, globals())


if __name__ == "__main__":
    from terml.parser import parseTerm as term
    f = open(sys.argv[1]).read()
    g = wrapGrammar(PythonParser)
    # stmts = g(sys.argv[1] + '\n').file_input()
    stmts = g(f).file_input()
    from pprint import pprint
    pprint(stmts)
#    import pdb; pdb.set_trace()
    ds = Desugarer.transform(stmts)[0]
    print ds
    pt = PythonStatementUnparser.transform(ds)[0]
    print pt