Example #1
0
    def connectionMade(self):
        """
        Start parsing, since the connection has been established.
        """

        self.sender = self._senderFactory(self.transport)
        self.receiver = self._receiverFactory(self.sender)
        self.receiver.prepareParsing(self)
        self._parser = TrampolinedParser(
            self._grammar, self.receiver, self._bindings)
Example #2
0
 def test_dataNotFullyReceived(self):
     """
     Since the initial rule inside the grammar is not matched, the receiver
     shouldn't receive any byte.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b'foobarandnotreachdelimiter'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [])
Example #3
0
 def test_currentRuleWithArgs(self):
     """
     TrampolinedParser should be able to invoke curruent rule with args.
     """
     receiver = TrampolinedReceiver()
     receiver.currentRule = "witharg", "nice ", "day"
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = ' oh yes\r\n'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, ["nice day oh yes"])
Example #4
0
 def test_dataNotFullyReceived(self):
     """
     Since the initial rule inside the grammar is not matched, the receiver
     shouldn't receive any byte.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = 'foobarandnotreachdelimiter'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [])
 def test_currentRuleWithArgs(self):
     """
     TrampolinedParser should be able to invoke curruent rule with args.
     """
     receiver = TrampolinedReceiver()
     receiver.currentRule = "witharg", "nice ", "day"
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = ' oh yes\r\n'
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, ["nice day oh yes"])
Example #6
0
 def test_dataFullyReceived(self):
     """
     The receiver should receive the data according to the grammar.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = '\r\n'.join(('foo', 'bar', 'foo', 'bar'))
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, ['foo', 'bar', 'foo'])
     trampolinedParser.receive('\r\n')
     self.assertEqual(receiver.received, ['foo', 'bar', 'foo', 'bar'])
Example #7
0
    def connectionMade(self):
        """
        Start parsing, since the connection has been established.
        """

        self.sender = self._senderFactory(self.transport)
        self.receiver = self._receiverFactory(self.sender)
        self.receiver.prepareParsing(self)
        self._parser = TrampolinedParser(self._grammar, self.receiver, self._bindings)
Example #8
0
 def test_bindings(self):
     """
     The passed-in bindings should be accessible inside the grammar.
     """
     receiver = TrampolinedReceiver()
     grammar = r"""
         initial = digit:d (-> int(d)+SMALL_INT):val -> receiver.receive(val)
     """
     bindings = {'SMALL_INT': 3}
     TrampolinedParser(self._parseGrammar(grammar), receiver,
                       bindings).receive('0')
     self.assertEqual(receiver.received, [3])
Example #9
0
 def test_dataFullyReceived(self):
     """
     The receiver should receive the data according to the grammar.
     """
     receiver = TrampolinedReceiver()
     trampolinedParser = TrampolinedParser(self.grammar, receiver, {})
     buf = b'\r\n'.join((b'foo', b'bar', b'foo', b'bar'))
     for c in iterbytes(buf):
         trampolinedParser.receive(c)
     self.assertEqual(receiver.received, [b'foo', b'bar', b'foo'])
     trampolinedParser.receive('\r\n')
     self.assertEqual(receiver.received, [b'foo', b'bar', b'foo', b'bar'])
Example #10
0
class ParserProtocol(Protocol):
    """
    A Twisted ``Protocol`` subclass for parsing stream protocols.
    """


    def __init__(self, grammar, senderFactory, receiverFactory, bindings):
        """
        Initialize the parser.

        :param grammar: An OMeta grammar to use for parsing.
        :param senderFactory: A unary callable that returns a sender given a
                              transport.
        :param receiverFactory: A unary callable that returns a receiver given
                                a sender.
        :param bindings: A dict of additional globals for the grammar rules.
        """

        self._grammar = grammar
        self._bindings = dict(bindings)
        self._senderFactory = senderFactory
        self._receiverFactory = receiverFactory
        self._disconnecting = False

    def connectionMade(self):
        """
        Start parsing, since the connection has been established.
        """

        self.sender = self._senderFactory(self.transport)
        self.receiver = self._receiverFactory(self.sender)
        self.receiver.prepareParsing(self)
        self._parser = TrampolinedParser(
            self._grammar, self.receiver, self._bindings)

    def dataReceived(self, data):
        """
        Receive and parse some data.

        :param data: A ``str`` from Twisted.
        """

        if self._disconnecting:
            return

        try:
            self._parser.receive(data)
        except Exception:
            self.connectionLost(Failure())
            self.transport.abortConnection()
            return

    def connectionLost(self, reason):
        """
        Stop parsing, since the connection has been lost.

        :param reason: A ``Failure`` instance from Twisted.
        """

        if self._disconnecting:
            return
        self.receiver.finishParsing(reason)
        self._disconnecting = True
Example #11
0
 def _setupInterp(self):
     self._currentRule = self.receiver.currentRule
     return TrampolinedParser._setupInterp(self)
Example #12
0
    def receive(self, data):
        if self.receiver.currentRule != self._currentRule:
            if not self._interp.input.data:
                self._setupInterp()

        return TrampolinedParser.receive(self, data)
Example #13
0
class ParserProtocol(Protocol):
    """
    A Twisted ``Protocol`` subclass for parsing stream protocols.
    """

    def __init__(self, grammar, senderFactory, receiverFactory, bindings):
        """
        Initialize the parser.

        :param grammar: An OMeta grammar to use for parsing.
        :param senderFactory: A unary callable that returns a sender given a
                              transport.
        :param receiverFactory: A unary callable that returns a receiver given
                                a sender.
        :param bindings: A dict of additional globals for the grammar rules.
        """

        self._grammar = grammar
        self._bindings = dict(bindings)
        self._senderFactory = senderFactory
        self._receiverFactory = receiverFactory
        self._disconnecting = False

    def connectionMade(self):
        """
        Start parsing, since the connection has been established.
        """

        self.sender = self._senderFactory(self.transport)
        self.receiver = self._receiverFactory(self.sender)
        self.receiver.prepareParsing(self)
        self._parser = TrampolinedParser(self._grammar, self.receiver, self._bindings)

    def dataReceived(self, data):
        """
        Receive and parse some data.

        :param data: A ``str`` from Twisted.
        """

        if self._disconnecting:
            return

        try:
            self._parser.receive(data)
        except Exception:
            self.connectionLost(Failure())
            self.transport.abortConnection()
            return

    def connectionLost(self, reason):
        """
        Stop parsing, since the connection has been lost.

        :param reason: A ``Failure`` instance from Twisted.
        """

        if self._disconnecting:
            return
        self.receiver.finishParsing(reason)
        self._disconnecting = True
Example #14
0
 def _getMessageSetParser(self, topicName):
     if topicName not in self._parsers:
         receiver = MessageSetReceiver()
         self._parsers[topicName] = TrampolinedParser(grammar=self._messageSetGrammar,
         receiver=receiver, bindings=globalBindings)
     return self._parsers[topicName]
Example #15
0
    def receive(self, data):
        if self.receiver.currentRule != self._currentRule:
            if not self._interp.input.data:
                self._setupInterp()

        return TrampolinedParser.receive(self, data)
Example #16
0
 def _setupInterp(self):
     self._currentRule = self.receiver.currentRule
     return TrampolinedParser._setupInterp(self)
Example #17
0
 def _initializeParserProtocol(self):
     self._trampolinedParser = TrampolinedParser(grammar=getGrammar(
         self._parsleyGrammarPKG, self._parsleyGrammarName),
                                                 receiver=self,
                                                 bindings=self._bindings)