@@ -24,20 +24,21 @@ netstrings protocol is very simple::
24
24
25
25
This stream contains two netstrings: ``spam ``, and ``eggs ``. The data is
26
26
prefixed with one or more ASCII digits followed by a ``: ``, and suffixed with a
27
- ``, ``. So, a Parsley grammar to match a netstring would look like::
27
+ ``, ``. So, a Parsley grammar to match a netstring would look like:
28
28
29
- nonzeroDigit = digit:x ?(x != '0')
30
- digits = <'0' | nonzeroDigit digit*>:i -> int(i)
31
-
32
- netstring = digits:length ':' <anything{length}>:string ',' -> string
29
+ .. literalinclude :: _static/listings/tutorial3-netstrings.py
30
+ :start-after: grammar =
31
+ :end-before: receiveNetstring
33
32
34
33
:func: `~parsley.makeProtocol ` takes, in addition to a grammar, a factory for a
35
34
"sender" and a factory for a "receiver". In the system of objects managed by
36
35
the ``ParserProtocol ``, the sender is in charge of writing data to the wire,
37
36
and the receiver has methods called on it by the Parsley rules. To demonstrate
38
- it, here is the final piece needed in the Parsley grammar for netstrings::
37
+ it, here is the final piece needed in the Parsley grammar for netstrings:
39
38
40
- receiveNetstring = netstring:string -> receiver.netstringReceived(string)
39
+ .. literalinclude :: _static/listings/tutorial3-netstrings.py
40
+ :start-after: netstring =
41
+ :end-before: """
41
42
42
43
The receiver is always available in Parsley rules with the name ``receiver ``,
43
44
allowing Parsley rules to call methods on it.
@@ -87,10 +88,11 @@ and echos the same netstrings back:
87
88
:pyobject: NetstringReceiver
88
89
89
90
Putting it all together, the Protocol is constructed using the grammar, sender
90
- factory, and receiver factory::
91
+ factory, and receiver factory:
91
92
92
- NetstringProtocol = makeProtocol(
93
- grammar, NetstringSender, NetstringReceiver)
93
+ .. literalinclude :: _static/listings/tutorial3-netstrings.py
94
+ :start-after: self.sender.sendNetstring
95
+ :end-before: class
94
96
95
97
:download: `The complete script is also available for download.
96
98
<_static/listings/tutorial3-netstrings.py>`
@@ -144,12 +146,9 @@ The corresponding receiver and again, constructing the Protocol:
144
146
.. literalinclude :: _static/listings/tutorial3-netstring-reversal.py
145
147
:pyobject: SplitNetstringReceiver
146
148
147
- .. code-block :: python
148
-
149
- NetstringProtocol = makeProtocol(
150
- grammar,
151
- stack(NetstringReversalWrapper, NetstringSender),
152
- stack(NetstringSplittingWrapper, SplitNetstringReceiver))
149
+ .. literalinclude :: _static/listings/tutorial3-netstring-reversal.py
150
+ :start-after: begin protocol definition
151
+ :end-before: SplitNetstringReceiver
153
152
154
153
:download: `The complete script is also available for download.
155
154
<_static/listings/tutorial3-netstring-reversal.py>`
@@ -166,13 +165,11 @@ As mentioned before, it's possible to change the current rule. Imagine a
166
165
3:foo,3;bar,4:spam,4;eggs,
167
166
168
167
That is, the protocol alternates between using ``: `` and using ``; `` delimiting
169
- data length and the data. The amended grammar would look something like this::
170
-
171
- nonzeroDigit = digit:x ?(x != '0')
172
- digits = <'0' | nonzeroDigit digit*>:i -> int(i)
168
+ data length and the data. The amended grammar would look something like this:
173
169
174
- colon = digits:length ':' <anything{length}>:string ',' -> receiver.netstringReceived(':', string)
175
- semicolon = digits:length ';' <anything{length}>:string ',' -> receiver.netstringReceived(';', string)
170
+ .. literalinclude :: _static/listings/tutorial3-netstrings2.py
171
+ :start-after: grammar =
172
+ :end-before: """
176
173
177
174
Changing the current rule is as simple as changing the ``currentRule ``
178
175
attribute on the receiver. So, the ``netstringReceived `` method could look like
0 commit comments