Skip to main content
  • domain specific language designed for regex
  • uses rply as engine

class Rex: def init(self, block: str): self._build_lexer() self._build_parser()

    tokens = self.lexer.lex(block)
    self.pattern = self.parser.parse(tokens)

def _build_lexer(self):
    rules = {
        "STRING":    r"'[^']*'",
        "NUMBER":    r"\d+",
        "OR":        r"(?i)or",
        "AND":       r"(?i)and",
        "SEMICOLON": r";",

        # constants
        "ALPHANUM":  r"_ALPHANUM",
    }

    lg = LexerGenerator()
    for name, pattern in rules.items():
        lg.add(name, pattern)

    lg.ignore(r"\s+")
    self.lexer = lg.build()

def _build_parser(self):
    pg = ParserGenerator(["STRING", "NUMBER", "ALPHANUM", "OR", "AND", "SEMICOLON"])

    # Start
    pg.production("start : sequences SEMICOLON")(lambda p: p[0])

    # OR sequences
    pg.production("sequences : sequence OR sequences")(lambda p: f"(?:{p[0]})|(?:{p[2]})")
    pg.production("sequences : sequence")(lambda p: p[0])

    # AND sequences
    pg.production("sequence : statement AND sequence")(lambda p: f"{p[0]}{p[2]}")
    pg.production("sequence : statement")(lambda p: p[0])

    # Unified statement handler
    def statement(p, x):
        tok_type = p[0].gettokentype()
        match tok_type:
            case "STRING":
                text = p[0].getstr()[1:-1]
            case "_ALPHANUM":
                text = r"[A-Za-z0-9]"
        count = int(p[1].getstr()) if x == 2 else 1
        return f"(?:{text}){{{count}}}"


    pg.production("statement : STRING NUMBER")(lambda p: statement(p, len(p)))
    pg.production("statement : STRING")(lambda p: statement(p, len(p)))
    pg.production("statement : ALPHANUM NUMBER")(lambda p: statement(p, len(p)))
    pg.production("statement : ALPHANUM")(lambda p: statement(p, len(p)))

    self.parser = pg.build()

def exists(self, block: str):
    return bool(regex.fullmatch(self.pattern, block))

Release files for rexdsl 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rexdsl 0.0.3
File Size Uploaded
rexdsl-0.0.3.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rexdsl 0.0.3
File Interpreter ABI Platform
rexdsl-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 7.0 kB

Release files / rexdsl-0.0.3.tar.gz

Download URL rexdsl-0.0.3.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
32ce9fab71bc38f420f1fe8dac6e919e7552c5760efbede6e883fd28f8cf6d6f
BLAKE2b-256 checksum
How to use checksums
87026e42ee15578531b278dac0cac225e7f28e2d4e5ebfadb43fd965cc3a3767
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.14

Release files / rexdsl-0.0.3-py3-none-any.whl

Download URL rexdsl-0.0.3-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
866a63139cd51d43ca9d3068b1934358e71e4dc2805cb5c61c55bfe59d9c346c
BLAKE2b-256 checksum
How to use checksums
41071f68dbaa928ff50814314897addff83dcb4068235c220f2cdafa57d29126
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.14

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page