{"id":3860,"library":"abnf","title":"ABNF Parsers","description":"ABNF is a Python package designed to generate parsers from Augmented Backus-Naur Form (ABNF) grammars, as specified in RFC 5234 and RFC 7405. While its primary use is to parse data defined in RFCs, it is capable of handling any valid ABNF grammar. The library originated from parsing HTTP headers in a web framework and has since been utilized in production environments. The current version is 2.4.1.","status":"active","version":"2.4.1","language":"en","source_language":"en","source_url":"https://github.com/declaresub/abnf","tags":["parser","ABNF","grammar","RFC","syntax"],"install":[{"cmd":"pip install abnf","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Required Python interpreter version.","package":"python","version":">=3.10"}],"imports":[{"symbol":"Rule","correct":"from abnf import Rule"},{"note":"Example of importing a pre-defined RFC grammar.","symbol":"rfc7232","correct":"from abnf.grammars import rfc7232"}],"quickstart":{"code":"from abnf import Rule\nfrom abnf.grammars import rfc7232\n\n# Example 1: Creating a custom rule and parsing (simplified)\n# This demonstrates the structure, actual parsing needs a Rule instance.\ncustom_rule_grammar_str = 'double-quoted-string = DQUOTE *(%x20-21 / %x23-7E / %x22.22) DQUOTE'\ncustom_rule_instance = Rule.create(custom_rule_grammar_str)\nprint(f\"Created custom rule: {custom_rule_instance.name}\")\n\n# Example 2: Parsing with a pre-defined RFC grammar\nsrc = 'W/\"moof\"'\n# Instantiate the ETag rule from rfc7232 grammar\netag_rule = rfc7232.Rule('ETag')\n\n# Parse the source string\nnode, start = etag_rule.parse(src)\n\nprint(f\"\\nParsed ETag: {src}\")\nprint(f\"Resulting AST node: {node}\")\nprint(f\"Parsing started at index: {start}\")","lang":"python","description":"Demonstrates how to create a custom ABNF rule and parse a string using a pre-defined grammar from the `abnf.grammars` module, such as RFC 7232."},"warnings":[{"fix":"Review grammar definitions and parsing logic for compatibility with the new backtracking implementation. Test thoroughly.","message":"Version 2.0.0 introduced significant changes to backtracking implementation, which constituted a breaking change in behavior. Code relying on previous backtracking mechanisms may need adjustment.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Monitor memory usage and consider contributing feedback or implementing custom cache management if performance degradation is observed. Consult the library's GitHub for potential updates or configuration options related to cache sizing.","message":"As of version 2.0.0, `Repetition` objects utilize an LRU cache (`ParseCache`) to store parse results, but the initial implementation does not set maximum sizes for these caches. This can lead to unbounded memory growth and long-term performance issues in applications with extensive or long-running parsing tasks.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Ensure that grammar files provided to `Rule.from_file` explicitly use CRLF line endings. Verify line endings with a text editor that can display them (e.g., VS Code, Notepad++).","message":"When loading grammars from text files using `Rule.from_file`, ABNF strictly expects CRLF (Carriage Return Line Feed) as a delimiter for rules. Many text editors silently convert CRLF to LF (Line Feed only), which can cause parsing failures or unexpected behavior when the grammar file is read by the library.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid relying on `Rule('rule-name')` always returning the identical Python object. Treat rule instantiation as potentially returning a new, but functionally equivalent, object.","message":"The library caches `Rule` objects (e.g., `Rule('rule-name')` will return the same object if the rule was previously created). While this behavior typically ensures consistency, the documentation advises against depending on it, suggesting that future versions or edge cases might alter this behavior.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}