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', 'b', 'c', ['123', 'e', 'f', [], 'x']]
Atoms
a, b, c in the above example are called atoms.
Atoms are parsed using two different rules:
- Every sequence that does not contain a whitespace, opening of closing parenthesis or the comment character is an atom.
- 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 thedelimsparameter, you can customize the delimiters and their escape sequences.
Details
The parameters of read are:
file_like: An object that is file like i.e. provides areadmethod.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 mapDEFAULT_DELIMSspecifies delimiters for strings and symbols.comment_char: The character that starts a single line comment. The default value is;.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 python -m pytest.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinysexpr-1.3-py3-none-any.whl.
File metadata
- Download URL: tinysexpr-1.3-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59ac9431374f4b29eb494a88ec8379c9eae87d59c5148d5e0888e4de4471aacf
|
|
| MD5 |
24cb20ed160e71d44e7889f123082eed
|
|
| BLAKE2b-256 |
531928aeaa9c1d55b3934e257537cf6f0dc4e964a5fdd4aa1804b7af84cba049
|