Skip to content

Commit cdbeedb

Browse files
committed
Push more stuff into sphinx literal includes.
It did require some contortions, but I think this is better? It's definitely more maintainable than line numbers. At worst I can add more # begin whatever blocks.
1 parent 6904f2e commit cdbeedb

File tree

4 files changed

+20
-30
lines changed

4 files changed

+20
-30
lines changed

doc/_static/listings/tutorial3-netstring-reversal.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88

99
grammar = """
10-
1110
nonzeroDigit = digit:x ?(x != '0')
1211
digits = <'0' | nonzeroDigit digit*>:i -> int(i)
1312
1413
netstring = digits:length ':' <anything{length}>:string ',' -> string
1514
1615
receiveNetstring = netstring:string -> receiver.netstringReceived(string)
17-
1816
"""
1917

2018

@@ -65,7 +63,7 @@ def netstringFirstHalfReceived(self, string):
6563
def netstringSecondHalfReceived(self, string):
6664
pass
6765

68-
66+
pass # begin protocol definition
6967
NetstringProtocol = makeProtocol(
7068
grammar,
7169
stack(NetstringReversalWrapper, NetstringSender),

doc/_static/listings/tutorial3-netstrings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77

88

99
grammar = """
10-
1110
nonzeroDigit = digit:x ?(x != '0')
1211
digits = <'0' | nonzeroDigit digit*>:i -> int(i)
1312
1413
netstring = digits:length ':' <anything{length}>:string ',' -> string
15-
1614
receiveNetstring = netstring:string -> receiver.netstringReceived(string)
17-
1815
"""
1916

2017

doc/_static/listings/tutorial3-netstrings2.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88

99
grammar = """
10-
1110
nonzeroDigit = digit:x ?(x != '0')
1211
digits = <'0' | nonzeroDigit digit*>:i -> int(i)
1312
netstring :delimiter = digits:length delimiter <anything{length}>:string ',' -> string
1413
1514
colon = digits:length ':' <anything{length}>:string ',' -> receiver.netstringReceived(':', string)
1615
semicolon = digits:length ';' <anything{length}>:string ',' -> receiver.netstringReceived(';', string)
17-
1816
"""
1917

2018

doc/tutorial3.rst

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ netstrings protocol is very simple::
2424

2525
This stream contains two netstrings: ``spam``, and ``eggs``. The data is
2626
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:
2828

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
3332

3433
:func:`~parsley.makeProtocol` takes, in addition to a grammar, a factory for a
3534
"sender" and a factory for a "receiver". In the system of objects managed by
3635
the ``ParserProtocol``, the sender is in charge of writing data to the wire,
3736
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:
3938

40-
receiveNetstring = netstring:string -> receiver.netstringReceived(string)
39+
.. literalinclude:: _static/listings/tutorial3-netstrings.py
40+
:start-after: netstring =
41+
:end-before: """
4142

4243
The receiver is always available in Parsley rules with the name ``receiver``,
4344
allowing Parsley rules to call methods on it.
@@ -87,10 +88,11 @@ and echos the same netstrings back:
8788
:pyobject: NetstringReceiver
8889

8990
Putting it all together, the Protocol is constructed using the grammar, sender
90-
factory, and receiver factory::
91+
factory, and receiver factory:
9192

92-
NetstringProtocol = makeProtocol(
93-
grammar, NetstringSender, NetstringReceiver)
93+
.. literalinclude:: _static/listings/tutorial3-netstrings.py
94+
:start-after: self.sender.sendNetstring
95+
:end-before: class
9496

9597
:download:`The complete script is also available for download.
9698
<_static/listings/tutorial3-netstrings.py>`
@@ -144,12 +146,9 @@ The corresponding receiver and again, constructing the Protocol:
144146
.. literalinclude:: _static/listings/tutorial3-netstring-reversal.py
145147
:pyobject: SplitNetstringReceiver
146148

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
153152

154153
:download:`The complete script is also available for download.
155154
<_static/listings/tutorial3-netstring-reversal.py>`
@@ -166,13 +165,11 @@ As mentioned before, it's possible to change the current rule. Imagine a
166165
3:foo,3;bar,4:spam,4;eggs,
167166

168167
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:
173169

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: """
176173

177174
Changing the current rule is as simple as changing the ``currentRule``
178175
attribute on the receiver. So, the ``netstringReceived`` method could look like

0 commit comments

Comments
 (0)