5
5
6
6
7
7
class ParserProtocol (Protocol ):
8
+ """
9
+ A Twisted ``Protocol`` subclass for parsing stream protocols.
10
+ """
11
+
12
+
8
13
def __init__ (self , grammar , senderFactory , receiverFactory , bindings ):
14
+ """
15
+ Initialize the parser.
16
+
17
+ :param grammar: An OMeta grammar to use for parsing.
18
+ :param senderFactory: A unary callable that returns a sender given a
19
+ transport.
20
+ :param receiverFactory: A unary callable that returns a receiver given
21
+ a sender.
22
+ :param bindings: A dict of additional globals for the grammar rules.
23
+ """
24
+
9
25
self .grammar = grammar
10
26
self .bindings = dict (bindings )
11
27
self .senderFactory = senderFactory
12
28
self .receiverFactory = receiverFactory
13
29
self .disconnecting = False
14
30
15
31
def connectionMade (self ):
32
+ """
33
+ Start parsing, since the connection has been established.
34
+ """
35
+
16
36
self .sender = self .senderFactory (self .transport )
17
37
self .receiver = self .receiverFactory (self .sender )
18
38
self .receiver .prepareParsing ()
19
39
self .parser = TrampolinedParser (
20
40
self .grammar , self .receiver , self .bindings )
21
41
22
42
def dataReceived (self , data ):
43
+ """
44
+ Receive and parse some data.
45
+
46
+ :param data: A ``str`` from Twisted.
47
+ """
48
+
23
49
if self .disconnecting :
24
50
return
25
51
@@ -31,6 +57,12 @@ def dataReceived(self, data):
31
57
return
32
58
33
59
def connectionLost (self , reason ):
60
+ """
61
+ Stop parsing, since the connection has been lost.
62
+
63
+ :param reason: A ``Failure`` instance from Twisted.
64
+ """
65
+
34
66
if self .disconnecting :
35
67
return
36
68
self .receiver .finishParsing (reason )
0 commit comments