Skip to main content

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:

    1. install dev deps (see pyproject.toml dev group)
    2. activate your venv and run mkdocs serve
  • Version injection: pages can use {{ version }}. It is provided by mkdocs-macros via docs/main.py, which resolves the version in this order:

    • [project].version from pyproject.toml
    • installed package metadata (importlib.metadata.version('syncraft'))
    • fallback "0.0.0"

    The macros plugin is configured in mkdocs.yml with module_name: docs/main.

Contributing / Roadmap

  • Improve performance and add benchmarks
  • Expand tutorials and SQLite coverage examples

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

syncraft-0.2.4.tar.gz (42.9 kB view details)

Uploaded Source

Built Distribution

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

syncraft-0.2.4-py3-none-any.whl (43.0 kB view details)

Uploaded Python 3

File details

Details for the file syncraft-0.2.4.tar.gz.

File metadata

  • Download URL: syncraft-0.2.4.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

Hashes for syncraft-0.2.4.tar.gz
Algorithm Hash digest
SHA256 a5476532de2e2ae5c684ae6a6ec77535258b471bcc082c20a3bfbf596b3d23da
MD5 9953c288b2fb7ec031fadf929daf60a5
BLAKE2b-256 7a1037489489f0f39a6d101ad93d9cc0289721f096c8bdb79b51a42db08df443

See more details on using hashes here.

File details

Details for the file syncraft-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: syncraft-0.2.4-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

Hashes for syncraft-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 02a8baffcb13cc34391275a45e4cfd51a31a0c000fd87ec868895ef57c277829
MD5 0ce78eecda1f40e23ada7fd628fed664
BLAKE2b-256 6a317c4944e1a2d7075c54720df62efafc27d0d45d234802aae82c58d5093f3d

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