custom regex dsl designed in rply for simplicity
Project description
- 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))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rexdsl-0.0.3.tar.gz
(3.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rexdsl-0.0.3.tar.gz.
File metadata
- Download URL: rexdsl-0.0.3.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32ce9fab71bc38f420f1fe8dac6e919e7552c5760efbede6e883fd28f8cf6d6f
|
|
| MD5 |
06aeea80e123b38eddc191489555a81d
|
|
| BLAKE2b-256 |
87026e42ee15578531b278dac0cac225e7f28e2d4e5ebfadb43fd965cc3a3767
|
File details
Details for the file rexdsl-0.0.3-py3-none-any.whl.
File metadata
- Download URL: rexdsl-0.0.3-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
866a63139cd51d43ca9d3068b1934358e71e4dc2805cb5c61c55bfe59d9c346c
|
|
| MD5 |
90c52954e90777cc26579633f01b41d4
|
|
| BLAKE2b-256 |
41071f68dbaa928ff50814314897addff83dcb4068235c220f2cdafa57d29126
|