Parser combinator library
Project description
Syncraft
Syncraft is a parser/generator combinator library for Python. It helps you
- Build grammars
- Parse SQL statement to AST
- Search AST by grammar
- Convert AST to dataclass
- Check constraints over the AST/dataclass
- Change dataclass and convert back to AST
Installation
pip
pip install syncraft
uv
uv add syncraft
Python 3.10+ is required.
Quickstart
!. Define grammar
from dataclasses import dataclass
from syncraft import literal, parse, generate
A = literal("a")
B = literal("b")
syntax = A + B # sequence
ast, _ = parse(syntax, "a b", dialect="sqlite")
gen, _ = generate(syntax, ast)
assert ast == gen
Collect parsed pieces into dataclasses using marks and .to():
from dataclasses import dataclass
from syncraft import literal
@dataclass
class Pair:
first: any
second: any
A = literal("a").mark("first")
B = literal("b").mark("second")
syntax = (A + B).to(Pair)
ast, _ = parse(syntax, "a b", dialect="sqlite")
value, invert = ast.bimap()
# value is Pair(first=VAR(a), second=VAR(b))
round_tripped, _ = generate(syntax, invert(value))
assert round_tripped == ast
Use the built‑in SQLite grammar snippets to parse statements:
from syncraft import parse
from syncraft.sqlite3 import select_stmt
ast, _ = parse(select_stmt, "select a from t where a > 1", dialect="sqlite")
Core ideas
- Syntax describes structure and transforms values; Algebra executes it.
- AST types: Then, Choice, Many, Marked, Collect, Nothing, Token.
- Operators:
+(both),>>(keep right),//(keep left),|(choice),~(optional),many(),sep_by(),between(). - Error model supports backtracking and commit (
cut()).
Documentation
-
Tutorials and API reference are built with MkDocs. Local preview:
- install dev deps (see
pyproject.tomldev group) - activate your venv and run
mkdocs serve
- install dev deps (see
-
Version injection: pages can use
{{ version }}. It is provided by mkdocs-macros viadocs/main.py, which resolves the version in this order:[project].versionfrompyproject.toml- installed package metadata (
importlib.metadata.version('syncraft')) - fallback
"0.0.0"
The macros plugin is configured in
mkdocs.ymlwithmodule_name: docs/main.
Contributing / Roadmap
- Improve performance and add benchmarks
- Expand tutorials and SQLite coverage examples
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 Distribution
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 syncraft-0.2.3.tar.gz.
File metadata
- Download URL: syncraft-0.2.3.tar.gz
- Upload date:
- Size: 42.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b17c1d64bc609349a362ed426b39885aba5dd009c54c1183dffb86f91ce5f552
|
|
| MD5 |
05825d00c2506af70a1f65f2870df01e
|
|
| BLAKE2b-256 |
c54cd5b6a41e3f6b5714ff151ac205b8e99303df3b3c5dfb40123f5b389408e5
|
File details
Details for the file syncraft-0.2.3-py3-none-any.whl.
File metadata
- Download URL: syncraft-0.2.3-py3-none-any.whl
- Upload date:
- Size: 43.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b760c8165cda2909e8e001bcde29dc07abeab6990189b7a965eb6784191cde1
|
|
| MD5 |
74c388bae66ffebe30b24395081bdeb9
|
|
| BLAKE2b-256 |
f2f13b266a9336622bbc1934de5efb425d3c004955d982cd9c88632c1cfcd1dc
|