Registry / serialization / textparser

textparser

JSON →
library0.26.2pypypi✓ verified 24d ago

Textparser is a Python library designed for fast text parsing. It allows users to define token specifications using regular expressions and construct grammars to parse text into a structured parse tree. The project prioritizes parsing speed, as highlighted in its benchmarks. The current version is 0.24.0, released on April 16, 2022, with an infrequent release cadence.

pip install textparser
INSTALL
IMPORT
SIG · TEXTPARSER
T
textparser
serializationpythonv0.26.2
Install
1.5s avg
Import
45ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.26.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.038s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.034s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Parser
from textparser import Parser
Primary class for defining a parser.
Sequence
from textparser import Sequence
Commonly used grammar element for sequential patterns.

This 'Hello World' example demonstrates how to define a custom parser by subclassing `textparser.Parser`. It specifies token types with regular expressions in `token_specs` and defines a simple grammar using `textparser.Sequence` in `grammar` to parse the string 'Hello, World!' into a parse tree.

import textparser from textparser import Sequence class MyParser(textparser.Parser): def token_specs(self): return [ ('SKIP', r'[ \r\n\t]+'), ('WORD', r'\w+'), ('EMARK', '!', r'!'), ('COMMA', ',', r','), ('MISMATCH', r'.') ] def grammar(self): return Sequence('WORD', ',', 'WORD', '!') tree = MyParser().parse('Hello, World!') print('Tree:', tree) # Expected output: Tree: ['Hello', ',', 'World', '!']
Debug
Known issues
gotchaWhen defining token specifications with `(kind, name, re)`, ensure the `grammar` refers to the `name` instead of the `kind`. Using `kind` when `name` is provided will lead to parsing errors.
fix
If `token_specs` contains `('TYPE', 'friendly_name', r'regex')`, then `grammar` patterns should reference `'friendly_name'` not `'TYPE'`.
affects: All versions
gotchaThe structure of parse trees returned by `textparser` can vary, and additional post-processing may be required to fit specific application needs. Its primary goal is speed, not necessarily a universally consistent parse tree format across different grammars.
fix
Always inspect the output parse tree for your specific grammar and implement any necessary transformations or validations on the returned structure.
affects: All versions
gotchaThe library's last release was April 2022, indicating a slower development pace. While generally stable, users should be aware of potential future compatibility challenges with newer Python versions or lack of updates for new parsing paradigms.
fix
Monitor the GitHub repository for updates. Test thoroughly with new Python versions or complex scenarios before deploying in production environments. Consider contributing if specific features or fixes are needed.
affects: 0.24.0 and older
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'textparser'
The 'textparser' library has not been installed in the current Python environment.
fix
Run `pip install textparser` to install the library.
sre_constants.error: nothing to repeat at position 0
An invalid regular expression pattern was provided when defining a `textparser.tokens.Token`, often by starting a pattern with a quantifier like `*` or `+`.
fix
Ensure the regular expression pattern is valid. For example, quantifiers like `*` or `+` cannot appear at the beginning of a pattern without a preceding element to quantify.
KeyError: "Rule 'UNDEFINED_RULE' is not defined"
A grammar rule defined within a `textparser.grammar.Grammar` class refers to another rule or token that has not been defined in the grammar or lexer's token set.
fix
Ensure all referenced rules and tokens are properly defined within the `Grammar` class or provided to the `Lexer`.
AttributeError: module 'textparser' has no attribute 'parse'
The `parse` method is not directly available on the `textparser` module. It is a method of a `textparser.grammar.Grammar` instance, which must be created first.
fix
Instantiate a `textparser.grammar.Grammar` class with a `textparser.lexers.Lexer` instance, then call the `parse()` method on that grammar object.
Upgrade
Version history
0.26.2latest on PyPI · released Jun 20, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
1
Resources
textparser — pip install textparser · libregistry