Skip to main content

A Parody of llparse written for writing C Parsers with Python

Project description

pyllparse

PyPI version PyPI - Downloads License: MIT

A python parody of the typescript library llparse.

I take no credit for the orginal work done by indutny and the other node-js contributors involved and I was originally very nervous about making this python library that I made public...

Links to the original library

Unlike the typescript library all 3 of llparse's libraries were combined in this version of the code for the sake of portability... I ended up mentioning how I did this a while back and I also had a concept for a C parser as well but I just didn't like it and I ended up using this instead but I also learned typescript as a bonus and It was alot of fun for me. I don't plan to make this into a real pypi library yet but I also didn't want to take away from the magic of the original source code that I borrowed from...

Looking back on my dead work 2 years later

I had this idea lay somewhat dormant for 2 years and now seeing that I wanted to write a new socks5 server parser writing it in python just made a little bit more sense to me, this way I don't have to stress over typescript or needing to relearn things I learned over 2 years ago. I have now made this project public on pypi for anybody who wanted to dig into it and access the code or generate your own parsers for C.

If your better at typescript. I reccommend sticking to the typescript version, no pressure. This library aims to not compete with llparse but to act as an alternative for users who prefer python over typescript/node-js, simillar to puppeteer or pyppeteer. Personally maybe we should make a rust crate for a rust version or at least someone will do it.

Warnings

  • It should be safe enough to generate code and stress-test it but if something doesn't seem right to you, be sure to throw an issue and share a typescript version of something that works in typescript but not in python, this way something unintended by the typescript developers can be solved.
  • Some code may look unfinished or may need further internal refactoring/polishing and with the other things I want to go out and do such as contributions to aiohttp and it's other libraries, cyares, winloop, aiothreading, aiocallback & deprecated-params and I also have an irl Part-time Job, ideas and pull requests are welcomed without hesitation. I maintain other projects listed above to prevent myself from experiencing burnout.
  • Pytest testsuite has not been made yet. If this is a concern to you, please throw an issue on github. (Pytests and new workflows soon!)
  • Some code might be spaghetti-code (because It's been 2 years since I touched much of the code if not at all) e.g. tests module should be moved away from the library into the github repo.
  • There's some research portions such as tools to help generate things for cython or tools to make mirrors of llhttp's code such as it's native c code (I was looking to see if it could be theoretically autogenerated). Feel free to use them but do it at your own risk. They maybe incompleted or not throughly stress-tested.

New Features

  • Throw me an issue if typescript llparse introduces something new that you want for me or another contributor to try and implement just seeing llparse add new features is exciting to me.

  • If you want a feature that typescript llparse doesn't have, be sure to try making a pull request over there as well and not just here, there's a good chance they will appericate you for helping over there too and your helping make llhttp better by doing so. :)

Why Did I Translate llparse to python?

  • I wanted to work with a langauge I was more familiar with
  • Better educate myself and others on how these great libraries like llhttp are made
  • Write faster C code that could do more than just a simple split function or a regex...
  • Make it easy for me or someone else to find a problem and solve it in typescript after testing it in python
  • Typescript takes 2 commands to run a script with node-js it while python only takes one cutting the time required tremendously...
  • The orginal project was MIT licensed.
  • I wanted to write my own C Parser tool with llhttp styled callbacks of my own using a language I was the most comfortable with using.
  • I didn't like Lemon Parser or Yacc all that much and a good ide for handling them in Visual Studio Code with error checking to my knowlegde does not exist.
  • The closest thing I got to what I wanted was a project named NMFU shorthand for no memory for you and even I had problems with writing things using that library...

This was the Code that inspired me to try and make a new pyi writer branch for cython and if it wasn't for llhttp existing as well as it's magical experience I would've never done what I did.

Unlike llparse in typescript, this version has more integrated and experimental features like automatically building the api seen in llhttp and I've added a few other things like the dot compiler from llparse_dot and I also made a brand new cython compiler for it making easy and simple to make pxd files to port your projects to cython

How to use

# The good old http_parser was borrowed from llparse.org to demonstrate this for you :)
from llparse import LLParse

p = LLParse("http_parser")
method = p.node("method")
beforeUrl = p.node("before_url")
urlSpan = p.span(p.code.span("on_url"))
url = p.node("url")
http = p.node("http")

# Add custom uint8_t property to the state
p.property("i8", "method")

# Store method inside a custom property
onMethod = p.invoke(p.code.store("method"), beforeUrl)

# Invoke custom C function
complete = p.invoke(
    p.code.match("on_complete"),
    {
        #  Restart
        0: method
    },
    p.error(4, "`on_complete` error"),
)

method.select(
    {
        "HEAD": 0,
        "GET": 1,
        "POST": 2,
        "PUT": 3,
        "DELETE": 4,
        "OPTIONS": 5,
        "CONNECT": 6,
        "TRACE": 7,
        "PATCH": 8,
    },
    onMethod,
).otherwise(p.error(5, "Expected method"))

beforeUrl.match(" ", beforeUrl).otherwise(urlSpan.start(url))

url.peek(" ", urlSpan.end(http)).skipTo(url)

http.match(" HTTP/1.1\r\n\r\n", complete).otherwise(
    p.error(6, "Expected HTTP/1.1 and two newlines")
)

c = p.build(method)
print(c.c)
open("http_parser.c", "w").write(c.c)
open("http_parser.h", "w").write(c.header)

Video Showcasing this library

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

llparse-0.1.3.tar.gz (50.0 kB view details)

Uploaded Source

Built Distribution

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

llparse-0.1.3-py3-none-any.whl (54.9 kB view details)

Uploaded Python 3

File details

Details for the file llparse-0.1.3.tar.gz.

File metadata

  • Download URL: llparse-0.1.3.tar.gz
  • Upload date:
  • Size: 50.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llparse-0.1.3.tar.gz
Algorithm Hash digest
SHA256 18161af11552f3e4da302b032b1074d018f39b8f32a78301bf5043ab8b29b1a9
MD5 3445d803b1b76e0d8aac9201e5eac5c9
BLAKE2b-256 365ca978755ba6ff01a4c89ad1d0083850cdda1aceade328d131cdd63f0a67db

See more details on using hashes here.

Provenance

The following attestation bundles were made for llparse-0.1.3.tar.gz:

Publisher: python-publish.yml on Vizonex/pyllparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llparse-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: llparse-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 54.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for llparse-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cd885fa003cf266a6d94c084cf9910ff4ceac8598e1db2c99096318071eef788
MD5 f2c197e4ca0792d52417c7669811cb52
BLAKE2b-256 c9895929127800338a1a0d45053a63609ecce3e55c9d8f52133d20d5ebddf7a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llparse-0.1.3-py3-none-any.whl:

Publisher: python-publish.yml on Vizonex/pyllparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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