Skip to main content

A very simple S-expression parser that tries to make as little fuss as possible.

Project description

S-Expression Parser

A very simple S-expression parser that tries to make as little fuss as possible.

Example

This module provides a single iterator

def read(file_like, coords=(1, 1), delims=DEFAULT_DELIMS, comment_char=';', atom_handler=lambda x: x)

that reads all S-expressions in the file_like. Reading

(a b c (123 e f () x))

returns a Sequence object that corresponds to this nesting:

['a', 'b', 'c', ['123', 'e', 'f', [], 'x']]

The sequence object has a field range that is a pair of row/col pairs that give the coordinates of the S-expression in the input. Furthermore, it has a tuple item_ranges that gives the coordinates of the i-th item of the S-expression.

Atoms

a, b, c in the above example are called atoms. Atoms are parsed using two different rules:

  1. Every sequence that does not contain a whitespace, opening of closing parenthesis or the comment character is an atom.
  2. Every sequence that starts and ends with a delimiter is an atom. The default delimiters are " for strings with the usual escape characters (\n, \t, \", \\, ...) and | without escape characters. Using the delims parameter, you can customize the delimiters and their escape sequences.

Details

The parameters of read are:

  1. file_like: An object that is file like i.e. provides a read method.
  2. delims: A map of delimiters used to surround atoms that contain spaces. Commonly these are double-quotes to represent strings as in "Hello" or vertical bars to allow for symbols that contain spaces as in |some symbol|. The map DEFAULT_DELIMS specifies delimiters for strings and symbols.
  3. comment_char: The character that starts a single line comment. The default value is ;.
  4. atom_handler: A function that is called when an atom is parsed. This function is passed a string that consists of the text of the parsed atom. The function can convert this string into something else and the returned value is used to construct the S-expression. For example, this allows for converting digit sequences into ints.

Testing

To run the tests, execute uv run pytest.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinysexpr-1.7.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tinysexpr-1.7.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file tinysexpr-1.7.0.tar.gz.

File metadata

  • Download URL: tinysexpr-1.7.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tinysexpr-1.7.0.tar.gz
Algorithm Hash digest
SHA256 27f4b0b9e7495abb92567561faf307480aaad50c55163dcc5c93feb78901c6ce
MD5 a8c040b39244d273bbb89f3700cf8e6d
BLAKE2b-256 182573d74988e64aa2a3100531ecf62f0b5fc2cccea1e7cfcfa92dc8ade08857

See more details on using hashes here.

File details

Details for the file tinysexpr-1.7.0-py3-none-any.whl.

File metadata

  • Download URL: tinysexpr-1.7.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tinysexpr-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eb1c415304fffd3a5f46bc196bd30750524ee7c3016cf6adad5fe8ace46517ce
MD5 964b12510cb3a1a3d7749602192fa370
BLAKE2b-256 937c02451798c4eca05592710ab3f0f4a6afea3648039dd867378219267b4344

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page