Skip to main content

A fast, customizable lexer for tokenizing text using regex rules.

Project description

📜 VroxLexer

VroxLexer is a simple yet powerful Python-based lexer designed to tokenize input text based on user-defined regular expression rules. It efficiently processes large files by reading in chunks and writing tokens directly to an output file.

✨ Features

  • Customizable Tokenization – Users can define custom tokenization rules using regular expressions.
  • Efficient File Processing – Processes large files efficiently by reading in chunks instead of loading the entire file into memory.
  • Regex-based Matching – Uses precompiled regular expressions for fast token detection.
  • Automatic Output Writing – Tokens are written directly to an output file in real time.
  • Callback Support – Allows users to specify a callback function that runs after tokenization is complete.
  • Extensible API – Easily add custom token rules for any syntax.

📦 Installation

pip install vrox-lex

🔗 Optional Dependency

For higher-level regex abstraction, you can install VroxRegex:

pip install vrox-regex

📝 Note: VroxLexer works without VroxRegex, but using it simplifies complex regex definitions.

🚀 Usage

1️⃣ Create a Lexer Instance

To start using VroxLexer, create an instance of it:

    from vrox_lexer import VroxLexer
    lexer = VroxLexer()

2️⃣ Add Tokenization Rules

Use the add_rule(name, regex) method to define token rules.

Method 1: One-by-One Rule Addition

lexer.add_rule("KEYWORD", r"\b(if|else|while|for|return)\b")
lexer.add_rule("NUMBER", r"\b\d+(\.\d+)?\b")
lexer.add_rule("STRING", r"\".*?\"|'.*?'")
lexer.add_rule("IDENTIFIER", r"\b[a-zA-Z_][a-zA-Z0-9_]*\b")
lexer.add_rule("OPERATOR", r"[+\-*/=]")
lexer.add_rule("PUNCTUATION", r"[\(\)\{\},;]")

Method 2: Chainable API for Better Readability (Recommended)

lexer = VroxLexer()\
        .add_rule("KEYWORD", r"\b(if|else|while|for|return)\b")\
        .add_rule("NUMBER", r"\b\d+(\.\d+)?\b")\
        .add_rule("STRING", r"\".*?\"|'.*?'")\
        .add_rule("IDENTIFIER", r"\b[a-zA-Z_][a-zA-Z0-9_]*\b")\
        .add_rule("OPERATOR", r"[+\-*/=]")\
        .add_rule("PUNCTUATION", r"[\(\)\{\},;]")

3️⃣ Tokenize Text from a File

def on_tokenization_complete(output_path):
    print(f"✔ Tokenization complete! Tokens saved in {output_path}")

lexer.tokenize("input.txt", "output.txt", on_tokenization_complete)

📢 Example Output (output.txt)

KEYWORD: if
IDENTIFIER: count
OPERATOR: =
NUMBER: 10
PUNCTUATION: ;

🔗 Final Notes

  • VroxLexer is lightweight, fast, and designed for extensibility.
  • Using precompiled regex improves performance significantly.
  • The chainable API makes rule definition more intuitive.
  • The callback function enables post-processing after tokenization.

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

vrox_lex-0.2.6.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

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

vrox_lex-0.2.6-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file vrox_lex-0.2.6.tar.gz.

File metadata

  • Download URL: vrox_lex-0.2.6.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for vrox_lex-0.2.6.tar.gz
Algorithm Hash digest
SHA256 b075bfb7b182eb80832a25f31eb5fa0af81bb232ce916b7fd4b387f94ea6e9d8
MD5 252036db387ba0aaaa40e97b3ef3d22b
BLAKE2b-256 9acd6d3a991412e0cc765a7003122ba364644ff7c1d2ccc51ac9170bf827d207

See more details on using hashes here.

File details

Details for the file vrox_lex-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: vrox_lex-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for vrox_lex-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8e151bd8a67ee36eefacd8c1a70ccbeb6cb06c7930c7ff3f828985749b2dba74
MD5 a046b97e04e47c2fe2c3e309fb0f4e41
BLAKE2b-256 869f614bc3b286160e302b47dfb17c460bd7a3e6295d5d5b72c96bd58aea4dca

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