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.5.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.5-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vrox_lex-0.2.5.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.5.tar.gz
Algorithm Hash digest
SHA256 2370ae4de40ee7e7a682ea8b1ead2ec13f148784106d4213e77925a2813b997d
MD5 66eb3233ad375a642322f25be12ac548
BLAKE2b-256 487fbe69be8693e9a8836155b84931b882a28a7b89d1cb4e78024154dbfff5c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vrox_lex-0.2.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 71d67f140eb50285ff0663eb99108da1edc6bc7660b56988bea6375c949c55d1
MD5 ebdbca4aa62b13d26115d112295e35f3
BLAKE2b-256 743f5abbfe22288a7752b4765cc1d3236e1d5d03e06c82bd58e38cbc68e471cd

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