Пример #1
0
class ParsleyCompatibleParser(object):
    def __init__(self, grammar, receiver, bindings):
        self._grammar = grammar
        self._receiver = receiver
        self._bindings = bindings
        self._bindings['receiver'] = receiver

        self.compiler = Compiler(self._grammar)
        self._rules = {}
        self._setup()


    def getParseTree(self, name):
        # XXX this must also cache .compileRule, not just .getRule
        # but apparently some state is still held inside the parse tree?
        if name not in self._rules:
            self._rules[name] = self.compiler.compileRule(self.compiler.getRule(name))
        return self._rules[name]

    def _setup(self):
        parseTree = self.getParseTree(self._receiver.currentRule)
        self._interp = Interp(parseTree, self._setup, names=self._bindings)

    def receive(self, data):
        self._interp.receive(data)
Пример #2
0
class ParsleyCompatibleParser(object):
    def __init__(self, grammar, receiver, bindings):
        self._grammar = grammar
        self._receiver = receiver
        self._bindings = bindings
        self._bindings['receiver'] = receiver

        self.compiler = Compiler(self._grammar)
        self._rules = {}
        self._setup()

    def getParseTree(self, name):
        # XXX this must also cache .compileRule, not just .getRule
        # but apparently some state is still held inside the parse tree?
        if name not in self._rules:
            self._rules[name] = self.compiler.compileRule(
                self.compiler.getRule(name))
        return self._rules[name]

    def _setup(self):
        parseTree = self.getParseTree(self._receiver.currentRule)
        self._interp = Interp(parseTree, self._setup, names=self._bindings)

    def receive(self, data):
        self._interp.receive(data)
Пример #3
0
    def test_reuseMany(self):
        receiver = NetstringsReceiver()

        compiler = Compiler("""
            a = 'a'*:string 'b' -> receiver.netstringReceived(string)
        """)
        rule = compiler.getRule('a')
        parseTree = compiler.compileRule(rule)
        interp = Interp(parseTree, names={'receiver': receiver})
        interp.receive('aab')
        assert receiver.strings == [['a', 'a']]
        interp = Interp(parseTree, names={'receiver': receiver})
        interp.receive('aaab')
        assert receiver.strings == [['a', 'a'], ['a', 'a', 'a']]
Пример #4
0
    def test_reuseParseTree_newInterp(self):
        receiver = NetstringsReceiver()

        compiler = Compiler(netstringsGrammar)
        rule = compiler.getRule('receiveNetstring')
        parseTree = compiler.compileRule(rule)

        interp = Interp(parseTree, names={'receiver': receiver})
        interp.receive('11:aaaaaaaaaaa,')
        assert receiver.strings == ['aaaaaaaaaaa']
        interp = Interp(parseTree, names={'receiver': receiver})
        interp.receive('2:bb,')

        assert receiver.strings == ['aaaaaaaaaaa', 'bb']
Пример #5
0
def getParseTree(source, name=None):
    compiler = Compiler(source)
    return compiler.compileRule(compiler.getRule(name))
Пример #6
0
def getParseTree(source, name=None):
    compiler = Compiler(source)
    return compiler.compileRule(compiler.getRule(name))