Skip to main content

typeset-py

An embedded DSL for defining source code pretty printers.

typeset-py is the Python binding for the typeset Rust crate.

Installation

pip install typeset-soren-n

The package installs the module typeset:

import typeset

layout = typeset.text('hello') + typeset.text('world')
print(layout.compile().render(2, 80))
# hello world

Concept

The layout language is designed such that it fits well over a structurally recursive pass of some inductive data-structure; an abstract representation of the thing you wish to pretty print.

A layout is a tree of text literals composed together with either padded, unpadded compositions or with a line-break. The layout solver will select compositions in a layout and convert them into line-breaks, in order to make the layout fit within a given layout buffer width. It will do this in a greedy way, fitting as many literals on a line as possible. While doing so it will respect the annotated properties that the compositions are constructed under.

The solver being an abstract concept, is concretely implemented via two methods: a compiler, Layout.compile, which produces an immutable optimized layout called a Document; and a renderer, Document.render, which takes arguments for indentation and buffer width and produces the final text output.

Composition

Layouts compose through four families of binary constructors:

pad(left, right)        # "left right"  - separated by a space
unpad(left, right)      # "leftright"   - no separation
line(left, right)       # forced line-break between left and right
fix_pad(left, right)    # padded, and never broken at the seam
fix_unpad(left, right)  # unpadded, and never broken at the seam

The three most common compositions are also available as Python operators on Layout, mirroring the DSL syntax:

a + b   # pad(a, b)
a & b   # unpad(a, b)
a @ b   # line(a, b)

Null constructor

Sometimes in a data-structure there can be optional data (e.g. of type 'string option'), which when omitted should not have a layout. To make this case easy to handle, the null element of layout composition is available.

def layout_option(maybe_string: str | None) -> Layout:
  match maybe_string:
    case None: return null()
    case data: return text(data)

The null will be eliminated from the layout by the compiler, and will not be rendered, e.g:

foobar = text('foo') & null() & text('bar')

When rendering foobar, when the layout fits in the layout buffer, the result will be:

       7
       |
foobar |
       |

Word literal constructor

These are the visible terminals that we are typesetting.

foo = text('foo')

When rendering foo, when the layout fits in the layout buffer, the result will be:

    4
    |
foo |
    |

It will simply overflow the buffer when it does not:

  2
  |
fo|o
  |

Fix constructor

Sometimes you need to render a part of some layout as inline, i.e. that its compositions should not be broken; this is what the fix constructor is for. In other words a fixed layout is treated as a literal.

foobar = fix(text('foo') + text('bar'))

When rendering the fixed layout foobar, when the layout fits in the layout buffer, the result will be:

        8
        |
foo bar |
        |

It will overflow the buffer when it does not:

  2
  |
fo|o bar
  |

Grp constructor

The grp constructor prevents the solver from breaking its compositions, as long as there are compositions to the left of the group which could still be broken. This is useful when you need part of the layout to be treated as an item.

foobarbaz = text('foo') & grp(text('bar') & text('baz'))

When rendering foobarbaz, when the layout fits in the layout buffer, the result will be:

          10
          |
foobarbaz |
          |

If one of the literals does not fit within the layout buffer, the result will be:

       7
       |
foo    |
barbaz |
       |

In contrast, had the group not been annotated, the result would have been:

       7
       |
foobar |
baz    |
       |

Since the composition between bar and baz was not guarded, and the layout solver is greedy and wants to fit as many literals on the same line as possible without overflowing the buffer. If the group still does not fit within the layout buffer, the group will be broken and the result will be:

    4
    |
foo |
bar |
baz |
    |

Seq constructor

The seq constructor forces the solver to break all of its compositions as soon as one of them is broken. This is useful when you have data that is a sequence or is list-like in nature; when one item in the sequence is put on a new line, then so should the rest of the items in the sequence.

foobarbaz = seq(text('foo') & text('bar') & text('baz'))

When rendering foobarbaz, when the layout fits in the layout buffer, the result will be:

          10
          |
foobarbaz |
          |

If one of the literals does not fit within the layout buffer, the result will be:

       7
       |
foo    |
bar    |
baz    |
       |

Since the compositions were part of a sequence; i.e when one of them broke, they all broke.

Nest constructor

The nest constructor is simply there to provide an extra level of indentation for all literals that it ranges over. The width of each level of indentation is given as a parameter to the render function.

foobarbaz = text('foo') & nest(text('bar') & text('baz'))

When rendering foobarbaz with a indentation width of 2, when the layout fits in the layout buffer, the result will be:

          10
          |
foobarbaz |
          |

If one of the literals does not fit within the layout buffer, the result will be;

       7
       |
foobar |
  baz  |
       |

And when the layout buffer will only hold one of the literals, the result will be:

    4
    |
foo |
  ba|r
  ba|z
    |

In this case bar and baz will overflow the layout buffer because of the given indentation.

Pack constructor

The pack constructor defines an indentation level, but implicitly sets the indentation width to the index of the first literal in the layout it annotates. This is e.g. useful if you are pretty printing terms in a lisp-like language, where all other arguments to an application is often 'indented' to the same buffer index as the first argument.

foobarbaz = text('foo') & pack(text('bar') & text('baz'))

When rendering foobarbaz, when the layout fits in the layout buffer, the result will be:

          10
          |
foobarbaz |
          |

When one of the literals do not fit, the result will be:

       7
       |
foobar |
   baz |
       |

When the layout buffer will only hold one literal, the result will be:

    4
    |
foo |
bar |
baz |
    |

The calculation of which buffer index to indent to is:

max((indent_level * indent_width), mark)

I.e the mark index will only be chosen if it is greater than the current indentation.

Forced linebreak composition

The forced linebreak composition does just that, it is a pre-broken composition.

foobar = line(text('foo'), text('bar'))

When rendering foobar, whether or not the layout fits in the layout buffer, the result will be:

        8
        |
foo     |
bar     |
        |

Infix fixed compositions

The infix fixed compositions are syntactic sugar for compositions where the rightmost literal of the left operand, and the leftmost literal of the right operand are fixed together. I.e. the two following layouts are equivalent:

foobarbaz1 = text('foo') + fix_unpad(text('bar'), text('baz'))
foobarbaz2 = text('foo') + fix(text('bar') & text('baz'))

The example above might make it seem trivial, and that infix fixed compositions do not give you much value; but remember that you are composing layouts, not just literals. As such normalising the infix fixed composition is actually quite challenging since there are many different cases to consider when the fix is 'sunk in place' in the layout tree; this is part of what the compiler is responsible for.

Infix fixed compositions are useful when you need to fix a literal to the beginning or end of some other layout, e.g. separators between items in a sequence or list-like data structure. Without this feature you would again need to use an accumulator variable if you want to fix to the next literal, and probably need continuations if you want to fix to the last literal.

Joins

Three folds over a list of layouts; an empty list is null():

join_with_spaces(items)   # padded compositions: 'a b c', or one per line when broken
join_with_commas(items)   # each comma fixed to the item before it: 'a, b, c'
join_with_lines(items)    # forced linebreaks: one item per line

For example a function call whose arguments align under the first when they do not fit:

args = pack(seq(join_with_commas([text('x'), text('y'), text('z')])))
call = text('f(') & fix_unpad(args, text(')'))
document = call.compile()
document.render(2, 80)  # 'f(x, y, z)'
document.render(2, 6)   # 'f(x,\n  y,\n  z)'

A blank line is a linebreak onto the empty layout: a @ null() @ b.

Compiling the layout

Your custom layout function (pretty printer) will build a layout, which you then need to compile and render:

document = layout.compile()
print(document.render(2, 80))

I.e. the layout is compiled into a document ready for rendering, which is then rendered with arguments for indentation width and layout buffer width; in the above case indentation width is 2 and the layout buffer width is 80.

The reason for splitting the solver into compile and render, is in case the result is to be displayed in a buffer where the width is variable; i.e. you will not need to re-compile the layout between renderings using varying buffer width. For one-shot formatting, layout.compile().render(2, 80) is the one-shot form.

No layout is too deep: a chain built by a loop or a reduce over a hundred thousand items compiles, prints and frees without touching the native stack, with depth costing heap instead.

DSL and parsing

Additionally the typeset crate defines a small DSL, which allows you to write your layouts more succinctly (versus spelling out the full layout tree with the given constructors, which we've so far been doing throughout in this introduction!). parse reads it, with {i} standing for the i-th extra argument, and repr of a layout prints it:

layout = parse('{0} @ null @ {1}', fragment1, fragment2)
repr(parse('nest ("a" + "b")'))  # 'nest ("a" + "b")'

The full grammar is as such:

{i}       (Indexed variable for layout fragment substitution with index i)
null      (Constructor for the empty layout)
"x"       (Constructor for a word/text layout literal over a string x)
fix u     (Constructor for a fixed layout over a layout u)
grp u     (Constructor for a group layout over a layout u)
seq u     (Constructor for a sequence layout over a layout u)
nest u    (Constructor for a indented/nested layout over a layout u)
pack u    (Constructor for a indexed margin layout over a layout u)
u @ v     (Forced linebreak composition of layouts u and v)
u @@ v    (Forced double linebreak composition of layouts u and v)
u & v     (Unpadded composition of layouts u and v)
u !& v    (Infix fixed unpadded composition of layouts u and v)
u + v     (Padded composition of layouts u and v)
u !+ v    (Infix fixed padded composition of layouts u and v)

A unary constructor takes one primary and binds tighter than the binary operators (parenthesize to stack them: fix (grp u)); all binary operators share one precedence level and associate to the right; parentheses group. See docs/context/dsl-grammar.md for the full specification.

Examples

The test suite in tests/test_typeset.py doubles as a set of small, executable examples of every constructor and DSL form.

Metadata

Release files for typeset-soren-n 4.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for typeset-soren-n 4.0.1
File Size Uploaded Provenance Actions
typeset_soren_n-4.0.1.tar.gz 39.7 kB Without provenance Details for typeset_soren_n-4.0.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for typeset-soren-n 4.0.1
File Actions
typeset_soren_n-4.0.1-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for typeset_soren_n-4.0.1-cp310-abi3-win_amd64.whl
typeset_soren_n-4.0.1-cp310-abi3-win32.whl CPython 3.10 abi3 Windows x86-32 Details for typeset_soren_n-4.0.1-cp310-abi3-win32.whl
typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_x86_64.whl
typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_armv7l.whl CPython 3.10 abi3 Linux musl 1.2+ ARMv7l Details for typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_armv7l.whl
typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details for typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_aarch64.whl
typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl CPython 3.10 abi3 Linux glibc 2.17+ IBM System/390x Details for typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl CPython 3.10 abi3 Linux glibc 2.17+ PowerPC 64-le Details for typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl CPython 3.10 abi3 Linux glibc 2.17+ ARMv7l Details for typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
typeset_soren_n-4.0.1-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for typeset_soren_n-4.0.1-cp310-abi3-macosx_11_0_arm64.whl
typeset_soren_n-4.0.1-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details for typeset_soren_n-4.0.1-cp310-abi3-macosx_10_12_x86_64.whl

Total release size: 4.2 MB

Release files / typeset_soren_n-4.0.1.tar.gz

Download URL typeset_soren_n-4.0.1.tar.gz
Size 39.7 kB
Tags Source
SHA-256 checksum
How to use checksums
126a148e227ab4fef336ef0a391cb9a6b830c6d7db731924856df39cfad9897c
BLAKE2b-256 checksum
How to use checksums
040e8bcc301d8aafad31568b48863ea337e02def2d589a294e20be05b8e21c74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-win_amd64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-win_amd64.whl
Size 195.0 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
541992a88ad6a05e0bbbc844ac526e82ee997472242a18ef748619555b03367e
BLAKE2b-256 checksum
How to use checksums
a04c4bce0f9401a7de91c1947a946b218fee98fec9ed2dbdbca5f9522d991342
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-win32.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-win32.whl
Size 185.9 kB
Tags CPython 3.10 Windows x86-32 abi3
SHA-256 checksum
How to use checksums
ecfb56e68fab0925d6383b29de9c9ed16e8b37e39826322fbb851566d29be8f4
BLAKE2b-256 checksum
How to use checksums
c5cf83e5cd366b2fecc089ea51660371d5297b2694065ba7c0562e5fe0d75c4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_x86_64.whl
Size 517.3 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
a49bb6e62932c225c3019f6cd6c52a9467c947c4cde49d70f0d11a324e68f18e
BLAKE2b-256 checksum
How to use checksums
805ea4bf207b325de989099c4d9e26e9925a5a99827f332b4f44b4460d23b45d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_armv7l.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_armv7l.whl
Size 589.9 kB
Tags CPython 3.10 Linux musl 1.2+ ARMv7l abi3
SHA-256 checksum
How to use checksums
e2133899a3aa0befb9a42359a1326e91e3ea03727db06a2a263d55c937e41fb7
BLAKE2b-256 checksum
How to use checksums
30134e54f1c3d89d15f014328dd9fccf55b15807235ae6d3088421e965e4a56d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-musllinux_1_2_aarch64.whl
Size 482.7 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
c3373af7ea2b7d7f5f9259ce3a65edfa30f764cdfd853a8d85835e05a5996582
BLAKE2b-256 checksum
How to use checksums
bef7684f0e456e21041e6e5bd38b2432473374e5570bff271f1e618ce3b5bc72
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 305.8 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
ee337c8ac66163fae6bfc833c420ad7cc7cdaa126d65142c0fccde64c7935e06
BLAKE2b-256 checksum
How to use checksums
a88a768eb19182abe15428f737b621ab60aaf2090f20072640f60f7fb1195c85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Size 340.2 kB
Tags CPython 3.10 Linux glibc 2.17+ IBM System/390x abi3
SHA-256 checksum
How to use checksums
118941c3596a648797ebd76bae06f612cb3ed3ba8e9da018bfe4a7d662f0192b
BLAKE2b-256 checksum
How to use checksums
dd66ecdf656432dca85ea60cf9aa11ed1455ad28fc092f81a76e631a222b96ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Size 333.3 kB
Tags CPython 3.10 Linux glibc 2.17+ PowerPC 64-le abi3
SHA-256 checksum
How to use checksums
1d8e1c0dbf098c63d85abf673e7e44907c4973d7c8a0ccfa278ac6f830ff6c51
BLAKE2b-256 checksum
How to use checksums
3fdd99592390ca4890f1582b7d48d642de41f6a7252e779d807dad4b3fc2d9d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Size 314.6 kB
Tags CPython 3.10 Linux glibc 2.17+ ARMv7l abi3
SHA-256 checksum
How to use checksums
932917ebe268434436248a8a89fcfc5526857d1737cb23e376e689d0897c689a
BLAKE2b-256 checksum
How to use checksums
5deda22fad945b20f02ac99a7603508cae35f69150bad1688862fcb1ab83317d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 305.6 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
0446f8e170041dac359deb4b0e9d91ca1ad0b2e498a7d55866d6a68f861a1520
BLAKE2b-256 checksum
How to use checksums
82ecdfb3934ae1e12eb3d29fa283ffb03129e4f3cc2a3807e93ed0649ce89b54
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-macosx_11_0_arm64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-macosx_11_0_arm64.whl
Size 283.9 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fc70c9177f0143169f4b5c6fc967ea8cfac68c8565fc808934759f5d66659f9d
BLAKE2b-256 checksum
How to use checksums
106634570c68433dc3ca923c6345b67acf594b6b7d7f4016a8541173105b29ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Release files / typeset_soren_n-4.0.1-cp310-abi3-macosx_10_12_x86_64.whl

Download URL typeset_soren_n-4.0.1-cp310-abi3-macosx_10_12_x86_64.whl
Size 292.1 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
a3e763d4d096504d357780a8a82e211b2c9d3ecf7f1919a9dfb3243e6c3d29b6
BLAKE2b-256 checksum
How to use checksums
3c984363afd9d6bdbd9a845fb5ed8413a52d1d8ee3ad7bee8b19bf37a56d9265
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.15.0

Security for typeset-soren-n 4.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page