{"id":2656,"library":"parsy","title":"Parsy","description":"Parsy is an easy-to-use parser combinator library for building parsers in pure Python. It provides a straightforward and Pythonic solution for parsing text without external dependencies, focusing on combining small parsers into complex ones. The library is highly mature and stable, with its current version being 2.2. Releases are active, often aligning with Python version lifecycle updates.","status":"active","version":"2.2","language":"en","source_language":"en","source_url":"https://github.com/python-parsy/parsy","tags":["parser","parser-combinator","parsing","dsl"],"install":[{"cmd":"pip install parsy","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"string","correct":"from parsy import string"},{"symbol":"regex","correct":"from parsy import regex"},{"symbol":"generate","correct":"from parsy import generate"},{"symbol":"ParseError","correct":"from parsy import ParseError"}],"quickstart":{"code":"from parsy import string, regex, generate, ParseError\nfrom datetime import date\n\n# Example 1: Simple date parsing using combinators\nyear = regex(r\"[0-9]{4}\").map(int)\nmonth = regex(r\"[0-9]{2}\").map(int)\nday = regex(r\"[0-9]{2}\").map(int)\ndash = string('-')\n\niso_date_parser = year.then(dash).then(month).then(dash).then(day)\n\ntry:\n    parsed_date_list = iso_date_parser.parse(\"2023-10-26\")\n    # The default behavior of .then() chain is to return the value of the *last* parser.\n    # To combine results, .map() or @generate is often used.\n    print(f\"Simple parse result (last element): {parsed_date_list}\")\nexcept ParseError as e:\n    print(f\"Parse Error: {e}\")\n\n# Example 2: More complex date parsing using the @generate decorator\n# This allows you to combine parsed values into a structured result.\n@generate\ndef full_date_parser():\n    y = yield year << dash\n    m = yield month << dash\n    d = yield day\n    return date(y, m, d)\n\ntry:\n    parsed_date_obj = full_date_parser.parse(\"2023-10-26\")\n    print(f\"Structured parse result (date object): {parsed_date_obj}\")\n    assert parsed_date_obj == date(2023, 10, 26)\n\n    # Example of a failure\n    full_date_parser.parse(\"2023/10/26\")\nexcept ParseError as e:\n    print(f\"Parse Error for '2023/10/26': {e}\")\n\n# Example 3: Using .map to transform simple results\nweekday_parser = string('Mon') | string('Tue') | string('Wed') # ...and so on\nweekday_mapping = {'Mon': 'Monday', 'Tue': 'Tuesday', 'Wed': 'Wednesday'}\nmapped_weekday_parser = weekday_parser.map(lambda s: weekday_mapping.get(s, s))\n\ntry:\n    print(f\"Mapped weekday: {mapped_weekday_parser.parse('Tue')}\")\nexcept ParseError as e:\n    print(f\"Parse Error: {e}\")\n","lang":"python","description":"This quickstart demonstrates basic parser creation using `string` and `regex` primitives, chaining with `then`, and transforming results with `map`. It also showcases the powerful `@generate` decorator for building parsers that yield intermediate results and return a structured object, which is crucial for complex grammars. Error handling with `ParseError` is also illustrated."},"warnings":[{"fix":"Upgrade Python to 3.9 or newer, or pin `parsy<2.2` in your project dependencies.","message":"Parsy version 2.2 (released 2025-09-12) dropped support for Python 3.7 and 3.8, which have reached their End-of-Life (EOL). Users on these Python versions must upgrade to Python 3.9+ or stick to an older `parsy` version (e.g., 2.1 or earlier).","severity":"breaking","affected_versions":">=2.2"},{"fix":"For basic parsing, default error messages are often sufficient. For advanced error reporting, consider using `desc()` on parsers to provide more context, or implement custom error processing logic around `ParseError`.","message":"While `parsy` provides good basic error messages, for highly detailed, end-user-facing error reporting in complex custom languages (e.g., for a programming language's compiler), significant extra effort might be required to customize the error output, or a more heavyweight parser generator might be a better fit.","severity":"gotcha","affected_versions":"All"},{"fix":"Evaluate `parsy`'s performance for your specific use case. If profiling reveals parsing as a bottleneck in high-performance scenarios, explore alternative parser generation libraries like Lark or PLY.","message":"Parsy excels at quickly writing clear, declarative parsers for relatively small to medium-sized languages. However, for applications with extremely demanding performance requirements or exceptionally large and complex grammars, other parser generators that use different parsing algorithms (e.g., LALR, PEG) might offer better performance characteristics.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}