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 function

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

that returns the read S-expression. Reading

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

returns a Sequence object that corresponds to this nesting:

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

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.4.2.tar.gz (2.8 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.4.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tinysexpr-1.4.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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.4.2.tar.gz
Algorithm Hash digest
SHA256 584af9a9bdb2d788d84e84207e4341df1c04e54203f754e047598fdef717faed
MD5 b5434938e87271e6e9a9ae87673fe251
BLAKE2b-256 6790906a73cdf929e4bb21417078697907b38eb7289c527be14008b3b11ac029

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tinysexpr-1.4.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c30dddc99f114458822b59bf0dadca94d3d9f917c680d477839bf7f7521d253a
MD5 c184694588acc065d6de27ccb52b4871
BLAKE2b-256 e5b287599a0585f37a1cf30dd7dcc86b971484778f4156ee3b29a5d1c454d890

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