STRling 2.5.6__tar.gz → 2.5.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {strling-2.5.6 → strling-2.5.7}/PKG-INFO +23 -12
- {strling-2.5.6 → strling-2.5.7}/README.md +15 -2
- strling-2.5.7/pyproject.toml +53 -0
- strling-2.5.7/src/STRling/__init__.py +15 -0
- strling-2.5.7/src/STRling/core/__init__.py +39 -0
- strling-2.5.7/src/STRling/core/compiler.py +187 -0
- strling-2.5.7/src/STRling/core/ir.py +200 -0
- strling-2.5.7/src/STRling/core/nodes.py +228 -0
- strling-2.5.7/src/STRling/core/parser.py +783 -0
- strling-2.5.7/src/STRling/core/validator.py +62 -0
- strling-2.5.7/src/STRling/emitters/__init__.py +0 -0
- strling-2.5.7/src/STRling/emitters/pcre2.py +304 -0
- strling-2.5.7/src/STRling/simply/__init__.py +17 -0
- strling-2.5.7/src/STRling/simply/constructors.py +606 -0
- strling-2.5.7/src/STRling/simply/lookarounds.py +459 -0
- strling-2.5.7/src/STRling/simply/pattern.py +287 -0
- strling-2.5.7/src/STRling/simply/sets.py +481 -0
- strling-2.5.7/src/STRling/simply/static.py +593 -0
- {strling-2.5.6 → strling-2.5.7/src}/STRling.egg-info/PKG-INFO +23 -12
- strling-2.5.7/src/STRling.egg-info/SOURCES.txt +21 -0
- strling-2.5.6/MANIFEST.in +0 -2
- strling-2.5.6/STRling/__init__.py +0 -1
- strling-2.5.6/STRling/simply/__init__.py +0 -5
- strling-2.5.6/STRling/simply/constructors.py +0 -403
- strling-2.5.6/STRling/simply/lookarounds.py +0 -194
- strling-2.5.6/STRling/simply/pattern.py +0 -165
- strling-2.5.6/STRling/simply/sets.py +0 -319
- strling-2.5.6/STRling/simply/static.py +0 -351
- strling-2.5.6/STRling.egg-info/SOURCES.txt +0 -14
- strling-2.5.6/setup.py +0 -27
- {strling-2.5.6 → strling-2.5.7}/setup.cfg +0 -0
- {strling-2.5.6 → strling-2.5.7/src}/STRling.egg-info/dependency_links.txt +0 -0
- {strling-2.5.6 → strling-2.5.7/src}/STRling.egg-info/top_level.txt +0 -0
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: STRling
|
|
3
|
-
Version: 2.5.
|
|
4
|
-
|
|
3
|
+
Version: 2.5.7
|
|
4
|
+
Summary: Next-gen string pattern DSL & compiler
|
|
5
5
|
Author: TheCyberLocal
|
|
6
6
|
License: MIT
|
|
7
7
|
Project-URL: Homepage, https://github.com/TheCyberLocal/STRling
|
|
8
8
|
Project-URL: Documentation, https://github.com/TheCyberLocal/STRling/tree/main/docs
|
|
9
9
|
Project-URL: Issues, https://github.com/TheCyberLocal/STRling/issues
|
|
10
|
+
Keywords: strling,regex,regexp,regular expression,string,validation,pattern,parser,compiler,dsl,readable,beginner
|
|
10
11
|
Classifier: Programming Language :: Python :: 3
|
|
11
12
|
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Topic :: Software Development :: Compilers
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Classifier: Topic :: Text Processing
|
|
12
18
|
Requires-Python: >=3.8
|
|
13
19
|
Description-Content-Type: text/markdown
|
|
14
|
-
Dynamic: author
|
|
15
|
-
Dynamic: classifier
|
|
16
|
-
Dynamic: description
|
|
17
|
-
Dynamic: description-content-type
|
|
18
|
-
Dynamic: home-page
|
|
19
|
-
Dynamic: license
|
|
20
|
-
Dynamic: project-url
|
|
21
|
-
Dynamic: requires-python
|
|
22
20
|
|
|
23
|
-
# STRling
|
|
21
|
+
# STRling for Python (Quick Start)
|
|
24
22
|
|
|
25
23
|
## 🗺️ Overview
|
|
26
24
|
|
|
@@ -34,7 +32,7 @@ STRling makes string validation and matching **readable, safe, and consistent**
|
|
|
34
32
|
- **Reliable**: Built only on standard libraries.
|
|
35
33
|
- **Instructional Errors**: Explains what went wrong and how to fix it.
|
|
36
34
|
- **Consistent**: Works across frameworks and libraries without custom validators.
|
|
37
|
-
- **Multilingual**: Available across popular programming languages.
|
|
35
|
+
- **Multilingual**: Available across popular programming languages ([JavaScript](../javascript/README.md), and more coming soon).
|
|
38
36
|
|
|
39
37
|
## 💾 Installation
|
|
40
38
|
|
|
@@ -46,6 +44,8 @@ pip install STRling
|
|
|
46
44
|
|
|
47
45
|
## ✨ STRling in action!
|
|
48
46
|
|
|
47
|
+
### Basic Example: US Phone Number
|
|
48
|
+
|
|
49
49
|
```python
|
|
50
50
|
from STRling import simply as s
|
|
51
51
|
import re
|
|
@@ -80,6 +80,17 @@ for match in matches:
|
|
|
80
80
|
print("Last Part:", match.group("last_part"))
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
+
## 📚 Complete API Documentation
|
|
84
|
+
|
|
85
|
+
For comprehensive syntax reference and all available features, see the **[Python API Reference](docs/api_reference.md)**.
|
|
86
|
+
|
|
87
|
+
## 📖 Related Documentation
|
|
88
|
+
|
|
89
|
+
- **[Python API Reference](docs/api_reference.md)**: Complete API documentation for Python
|
|
90
|
+
- **[Developer Documentation Hub](../../docs/index.md)**: Architecture, philosophy, and development workflow
|
|
91
|
+
- **[Formal Specification](../../spec/README.md)**: Grammar and semantics reference
|
|
92
|
+
- **[JavaScript Binding](../javascript/README.md)**: STRling for JavaScript/Node.js
|
|
93
|
+
|
|
83
94
|
## 💖 Support
|
|
84
95
|
|
|
85
96
|
If you find STRling useful, consider [buying me a coffee](https://buymeacoffee.com/thecyberlocal).
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# STRling
|
|
1
|
+
# STRling for Python (Quick Start)
|
|
2
2
|
|
|
3
3
|
## 🗺️ Overview
|
|
4
4
|
|
|
@@ -12,7 +12,7 @@ STRling makes string validation and matching **readable, safe, and consistent**
|
|
|
12
12
|
- **Reliable**: Built only on standard libraries.
|
|
13
13
|
- **Instructional Errors**: Explains what went wrong and how to fix it.
|
|
14
14
|
- **Consistent**: Works across frameworks and libraries without custom validators.
|
|
15
|
-
- **Multilingual**: Available across popular programming languages.
|
|
15
|
+
- **Multilingual**: Available across popular programming languages ([JavaScript](../javascript/README.md), and more coming soon).
|
|
16
16
|
|
|
17
17
|
## 💾 Installation
|
|
18
18
|
|
|
@@ -24,6 +24,8 @@ pip install STRling
|
|
|
24
24
|
|
|
25
25
|
## ✨ STRling in action!
|
|
26
26
|
|
|
27
|
+
### Basic Example: US Phone Number
|
|
28
|
+
|
|
27
29
|
```python
|
|
28
30
|
from STRling import simply as s
|
|
29
31
|
import re
|
|
@@ -58,6 +60,17 @@ for match in matches:
|
|
|
58
60
|
print("Last Part:", match.group("last_part"))
|
|
59
61
|
```
|
|
60
62
|
|
|
63
|
+
## 📚 Complete API Documentation
|
|
64
|
+
|
|
65
|
+
For comprehensive syntax reference and all available features, see the **[Python API Reference](docs/api_reference.md)**.
|
|
66
|
+
|
|
67
|
+
## 📖 Related Documentation
|
|
68
|
+
|
|
69
|
+
- **[Python API Reference](docs/api_reference.md)**: Complete API documentation for Python
|
|
70
|
+
- **[Developer Documentation Hub](../../docs/index.md)**: Architecture, philosophy, and development workflow
|
|
71
|
+
- **[Formal Specification](../../spec/README.md)**: Grammar and semantics reference
|
|
72
|
+
- **[JavaScript Binding](../javascript/README.md)**: STRling for JavaScript/Node.js
|
|
73
|
+
|
|
61
74
|
## 💖 Support
|
|
62
75
|
|
|
63
76
|
If you find STRling useful, consider [buying me a coffee](https://buymeacoffee.com/thecyberlocal).
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "STRling"
|
|
7
|
+
version = "2.5.7"
|
|
8
|
+
description = "Next-gen string pattern DSL & compiler"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "TheCyberLocal" }]
|
|
13
|
+
keywords = [
|
|
14
|
+
"strling",
|
|
15
|
+
"regex",
|
|
16
|
+
"regexp",
|
|
17
|
+
"regular expression",
|
|
18
|
+
"string",
|
|
19
|
+
"validation",
|
|
20
|
+
"pattern",
|
|
21
|
+
"parser",
|
|
22
|
+
"compiler",
|
|
23
|
+
"dsl",
|
|
24
|
+
"readable",
|
|
25
|
+
"beginner"
|
|
26
|
+
]
|
|
27
|
+
classifiers = [
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"License :: OSI Approved :: MIT License",
|
|
30
|
+
"Development Status :: 4 - Beta",
|
|
31
|
+
"Intended Audience :: Developers",
|
|
32
|
+
"Topic :: Software Development :: Compilers",
|
|
33
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
34
|
+
"Topic :: Text Processing"
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/TheCyberLocal/STRling"
|
|
39
|
+
Documentation = "https://github.com/TheCyberLocal/STRling/tree/main/docs"
|
|
40
|
+
Issues = "https://github.com/TheCyberLocal/STRling/issues"
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
pythonpath = ["bindings/python/src"]
|
|
44
|
+
testpaths = ["tests"]
|
|
45
|
+
|
|
46
|
+
[tool.setuptools]
|
|
47
|
+
package-dir = {"" = "src"} # map package root to ./src (src layout)
|
|
48
|
+
|
|
49
|
+
# Optional but explicit:
|
|
50
|
+
[tool.setuptools.packages.find]
|
|
51
|
+
where = ["src"] # search here
|
|
52
|
+
include = ["STRling*"] # find STRling and subpackages
|
|
53
|
+
# exclude = ["tests*"] # (optional) keep tests out of the wheel
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""
|
|
2
|
+
STRling Binding - Main Entry Point
|
|
3
|
+
|
|
4
|
+
This is the root module for the STRling binding. It re-exports
|
|
5
|
+
the Simply API namespace, providing access to all public pattern construction
|
|
6
|
+
and manipulation functions.
|
|
7
|
+
|
|
8
|
+
The Simply API is the recommended way to use STRling, offering
|
|
9
|
+
a fluent, chainable interface for building regex patterns without dealing
|
|
10
|
+
with cryptic regex syntax.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from STRling import simply
|
|
14
|
+
|
|
15
|
+
__all__ = ["simply"]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from STRling.core.parser import parse, parse_to_artifact, ParseError
|
|
2
|
+
from STRling.core.nodes import (
|
|
3
|
+
Flags,
|
|
4
|
+
Node,
|
|
5
|
+
Alt,
|
|
6
|
+
Seq,
|
|
7
|
+
Lit,
|
|
8
|
+
Dot,
|
|
9
|
+
Anchor,
|
|
10
|
+
CharClass,
|
|
11
|
+
ClassLiteral,
|
|
12
|
+
ClassRange,
|
|
13
|
+
ClassEscape,
|
|
14
|
+
Quant,
|
|
15
|
+
Group,
|
|
16
|
+
Backref,
|
|
17
|
+
Look,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"parse",
|
|
22
|
+
"parse_to_artifact",
|
|
23
|
+
"ParseError",
|
|
24
|
+
"Flags",
|
|
25
|
+
"Node",
|
|
26
|
+
"Alt",
|
|
27
|
+
"Seq",
|
|
28
|
+
"Lit",
|
|
29
|
+
"Dot",
|
|
30
|
+
"Anchor",
|
|
31
|
+
"CharClass",
|
|
32
|
+
"ClassLiteral",
|
|
33
|
+
"ClassRange",
|
|
34
|
+
"ClassEscape",
|
|
35
|
+
"Quant",
|
|
36
|
+
"Group",
|
|
37
|
+
"Backref",
|
|
38
|
+
"Look",
|
|
39
|
+
]
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"""
|
|
2
|
+
STRling Compiler - AST to IR Transformation
|
|
3
|
+
|
|
4
|
+
This module implements the compiler that transforms Abstract Syntax Tree (AST)
|
|
5
|
+
nodes from the parser into an optimized Intermediate Representation (IR). The
|
|
6
|
+
compilation process includes:
|
|
7
|
+
- Lowering AST nodes to IR operations
|
|
8
|
+
- Flattening nested sequences and alternations
|
|
9
|
+
- Coalescing adjacent literal nodes for efficiency
|
|
10
|
+
- Ensuring quantifier children are properly grouped
|
|
11
|
+
- Analyzing and tracking regex features used
|
|
12
|
+
|
|
13
|
+
The IR is designed to be easily consumed by target emitters (e.g., PCRE2)
|
|
14
|
+
while maintaining semantic accuracy and enabling optimizations.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
from STRling.core import nodes as N, ir as IR
|
|
19
|
+
from typing import cast
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Compiler:
|
|
23
|
+
"""
|
|
24
|
+
Compiler for transforming AST nodes into optimized IR.
|
|
25
|
+
|
|
26
|
+
The Compiler class handles the complete transformation pipeline from parsed
|
|
27
|
+
AST to normalized IR, including feature detection for metadata generation.
|
|
28
|
+
|
|
29
|
+
AST -> IR lowering with normalization:
|
|
30
|
+
- Flatten nested Seq/Alt
|
|
31
|
+
- Coalesce adjacent Lit nodes
|
|
32
|
+
- Ensure quantifier children are grouped appropriately
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self):
|
|
36
|
+
self.features_used: set[str] = set()
|
|
37
|
+
|
|
38
|
+
def compile_with_metadata(self, root_node: N.Node) -> dict[str, object]:
|
|
39
|
+
"""
|
|
40
|
+
Compile an AST node and return IR with metadata.
|
|
41
|
+
|
|
42
|
+
This is the main entry point for compilation with full metadata tracking.
|
|
43
|
+
It performs lowering, normalization, and feature analysis.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
root_node: The root AST node to compile.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
Dictionary containing the compiled IR and metadata about features used.
|
|
50
|
+
"""
|
|
51
|
+
ir_root = self._lower(root_node)
|
|
52
|
+
ir_root = self._normalize(ir_root)
|
|
53
|
+
|
|
54
|
+
# New step: Analyze the final IR tree for special features
|
|
55
|
+
self._analyze_features(ir_root)
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
"ir": ir_root, # Return the IR object, not a dict
|
|
59
|
+
"metadata": {"features_used": sorted(list(self.features_used))},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
def _analyze_features(self, node: IR.IROp):
|
|
63
|
+
"""Recursively walk the IR tree and log features used."""
|
|
64
|
+
if isinstance(node, IR.IRGroup):
|
|
65
|
+
if node.atomic:
|
|
66
|
+
self.features_used.add("atomic_group")
|
|
67
|
+
if node.name is not None:
|
|
68
|
+
self.features_used.add("named_group")
|
|
69
|
+
if isinstance(node, IR.IRQuant) and node.mode == "Possessive":
|
|
70
|
+
self.features_used.add("possessive_quantifier")
|
|
71
|
+
if isinstance(node, IR.IRLook):
|
|
72
|
+
if node.dir == "Behind":
|
|
73
|
+
self.features_used.add("lookbehind")
|
|
74
|
+
elif node.dir == "Ahead":
|
|
75
|
+
self.features_used.add("lookahead")
|
|
76
|
+
if isinstance(node, IR.IRBackref):
|
|
77
|
+
self.features_used.add("backreference")
|
|
78
|
+
if isinstance(node, IR.IRCharClass):
|
|
79
|
+
# Check for Unicode property escapes in character class items
|
|
80
|
+
for item in node.items:
|
|
81
|
+
if isinstance(item, IR.IRClassEscape) and item.type == "UnicodeProperty":
|
|
82
|
+
self.features_used.add("unicode_property")
|
|
83
|
+
|
|
84
|
+
# --- Recurse into children ---
|
|
85
|
+
if isinstance(node, (IR.IRSeq, IR.IRAlt)):
|
|
86
|
+
for child in node.parts if isinstance(node, IR.IRSeq) else node.branches:
|
|
87
|
+
self._analyze_features(child)
|
|
88
|
+
elif isinstance(node, IR.IRQuant):
|
|
89
|
+
self._analyze_features(node.child)
|
|
90
|
+
elif isinstance(node, (IR.IRGroup, IR.IRLook)):
|
|
91
|
+
self._analyze_features(node.body)
|
|
92
|
+
|
|
93
|
+
def compile(self, root: N.Node) -> IR.IROp:
|
|
94
|
+
ir = self._lower(root)
|
|
95
|
+
ir = self._normalize(ir)
|
|
96
|
+
return ir
|
|
97
|
+
|
|
98
|
+
# ---------- Lowering (AST -> IR) ----------
|
|
99
|
+
def _lower(self, node: N.Node) -> IR.IROp:
|
|
100
|
+
t = type(node).__name__
|
|
101
|
+
if t == "Seq":
|
|
102
|
+
return IR.IRSeq([self._lower(p) for p in node.parts]) # type: ignore
|
|
103
|
+
if t == "Alt":
|
|
104
|
+
return IR.IRAlt([self._lower(b) for b in node.branches]) # type: ignore
|
|
105
|
+
if t == "Lit":
|
|
106
|
+
return IR.IRLit(node.value) # type: ignore
|
|
107
|
+
if t == "Dot":
|
|
108
|
+
return IR.IRDot()
|
|
109
|
+
if t == "Anchor":
|
|
110
|
+
return IR.IRAnchor(node.at) # type: ignore
|
|
111
|
+
if t == "CharClass":
|
|
112
|
+
items = []
|
|
113
|
+
for it in node.items: # type: ignore
|
|
114
|
+
it = cast(N.Node, it) # Cast to Node type to satisfy the type checker
|
|
115
|
+
it_t = type(it).__name__
|
|
116
|
+
it_t = type(it).__name__
|
|
117
|
+
if it_t == "ClassRange":
|
|
118
|
+
items.append(IR.IRClassRange(it.from_ch, it.to_ch)) # type: ignore
|
|
119
|
+
elif it_t == "ClassLiteral":
|
|
120
|
+
items.append(IR.IRClassLiteral(it.ch)) # type: ignore
|
|
121
|
+
elif it_t == "ClassEscape":
|
|
122
|
+
items.append(IR.IRClassEscape(it.type, it.property)) # type: ignore
|
|
123
|
+
else:
|
|
124
|
+
raise NotImplementedError(f"Unknown class item {it_t}")
|
|
125
|
+
return IR.IRCharClass(node.negated, items) # type: ignore
|
|
126
|
+
if t == "Quant":
|
|
127
|
+
return IR.IRQuant(self._lower(node.child), node.min, node.max, node.mode) # type: ignore
|
|
128
|
+
if t == "Group":
|
|
129
|
+
group_node = cast(N.Group, node)
|
|
130
|
+
return IR.IRGroup(
|
|
131
|
+
group_node.capturing,
|
|
132
|
+
self._lower(group_node.body),
|
|
133
|
+
getattr(group_node, "name", None),
|
|
134
|
+
getattr(group_node, "atomic", None),
|
|
135
|
+
) # type: ignore
|
|
136
|
+
if t == "Backref":
|
|
137
|
+
return IR.IRBackref(
|
|
138
|
+
getattr(node, "byIndex", None), getattr(node, "byName", None)
|
|
139
|
+
) # type: ignore
|
|
140
|
+
if t == "Look":
|
|
141
|
+
look_node = cast(N.Look, node)
|
|
142
|
+
return IR.IRLook(look_node.dir, look_node.neg, self._lower(look_node.body)) # type: ignore
|
|
143
|
+
raise NotImplementedError(f"No lowering for AST node {t}")
|
|
144
|
+
|
|
145
|
+
# ---------- Normalization ----------
|
|
146
|
+
def _normalize(self, node: IR.IROp) -> IR.IROp:
|
|
147
|
+
"""Flatten alt/seq and fuse adjacent literals."""
|
|
148
|
+
if isinstance(node, IR.IRSeq):
|
|
149
|
+
parts: list[IR.IROp] = []
|
|
150
|
+
for p in node.parts:
|
|
151
|
+
p_norm = self._normalize(p)
|
|
152
|
+
if isinstance(p_norm, IR.IRSeq):
|
|
153
|
+
parts.extend(p_norm.parts)
|
|
154
|
+
else:
|
|
155
|
+
parts.append(p_norm)
|
|
156
|
+
fused: list[IR.IROp] = []
|
|
157
|
+
buf = ""
|
|
158
|
+
for p in parts:
|
|
159
|
+
if isinstance(p, IR.IRLit):
|
|
160
|
+
buf += p.value
|
|
161
|
+
else:
|
|
162
|
+
if buf:
|
|
163
|
+
fused.append(IR.IRLit(buf))
|
|
164
|
+
buf = ""
|
|
165
|
+
fused.append(p)
|
|
166
|
+
if buf:
|
|
167
|
+
fused.append(IR.IRLit(buf))
|
|
168
|
+
return fused[0] if len(fused) == 1 else IR.IRSeq(fused)
|
|
169
|
+
if isinstance(node, IR.IRAlt):
|
|
170
|
+
branches: list[IR.IROp] = []
|
|
171
|
+
for b in node.branches:
|
|
172
|
+
b_norm = self._normalize(b)
|
|
173
|
+
if isinstance(b_norm, IR.IRAlt):
|
|
174
|
+
branches.extend(b_norm.branches)
|
|
175
|
+
else:
|
|
176
|
+
branches.append(b_norm)
|
|
177
|
+
return branches[0] if len(branches) == 1 else IR.IRAlt(branches)
|
|
178
|
+
if isinstance(node, IR.IRQuant):
|
|
179
|
+
child = self._normalize(node.child)
|
|
180
|
+
return IR.IRQuant(child, node.min, node.max, node.mode)
|
|
181
|
+
if isinstance(node, IR.IRGroup):
|
|
182
|
+
return IR.IRGroup(
|
|
183
|
+
node.capturing, self._normalize(node.body), node.name, node.atomic
|
|
184
|
+
)
|
|
185
|
+
if isinstance(node, IR.IRLook):
|
|
186
|
+
return IR.IRLook(node.dir, node.neg, self._normalize(node.body))
|
|
187
|
+
return node
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""
|
|
2
|
+
STRling Intermediate Representation (IR) Node Definitions
|
|
3
|
+
|
|
4
|
+
This module defines the complete set of IR node classes that represent
|
|
5
|
+
language-agnostic regex constructs. The IR serves as an intermediate layer
|
|
6
|
+
between the parsed AST and the target-specific emitters (e.g., PCRE2).
|
|
7
|
+
|
|
8
|
+
IR nodes are designed to be:
|
|
9
|
+
- Simple and composable
|
|
10
|
+
- Easy to serialize (via to_dict methods)
|
|
11
|
+
- Independent of any specific regex flavor
|
|
12
|
+
- Optimized for transformation and analysis
|
|
13
|
+
|
|
14
|
+
Each IR node corresponds to a fundamental regex operation (alternation,
|
|
15
|
+
sequencing, character classes, quantification, etc.) and can be serialized
|
|
16
|
+
to a dictionary representation for further processing or debugging.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
from dataclasses import dataclass
|
|
21
|
+
from typing import List, Optional, Union, Any, Dict
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class IROp:
|
|
25
|
+
"""
|
|
26
|
+
Base class for all IR operations.
|
|
27
|
+
|
|
28
|
+
All IR nodes extend this base class and must implement the to_dict() method
|
|
29
|
+
for serialization to a dictionary representation.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
33
|
+
"""
|
|
34
|
+
Serialize the IR node to a dictionary representation.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
The dictionary representation of this IR node.
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
NotImplementedError: If not implemented by subclass.
|
|
41
|
+
"""
|
|
42
|
+
raise NotImplementedError
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass
|
|
46
|
+
class IRAlt(IROp):
|
|
47
|
+
"""
|
|
48
|
+
Represents an alternation (OR) operation in the IR.
|
|
49
|
+
|
|
50
|
+
Matches any one of the provided branches. Equivalent to the | operator
|
|
51
|
+
in traditional regex syntax.
|
|
52
|
+
"""
|
|
53
|
+
branches: List["IROp"]
|
|
54
|
+
|
|
55
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
56
|
+
return {"ir": "Alt", "branches": [b.to_dict() for b in self.branches]}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass
|
|
60
|
+
class IRSeq(IROp):
|
|
61
|
+
parts: List["IROp"]
|
|
62
|
+
|
|
63
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
64
|
+
return {"ir": "Seq", "parts": [p.to_dict() for p in self.parts]}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@dataclass
|
|
68
|
+
class IRLit(IROp):
|
|
69
|
+
value: str
|
|
70
|
+
|
|
71
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
72
|
+
return {"ir": "Lit", "value": self.value}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@dataclass
|
|
76
|
+
class IRDot(IROp):
|
|
77
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
78
|
+
return {"ir": "Dot"}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass
|
|
82
|
+
class IRAnchor(IROp):
|
|
83
|
+
at: str
|
|
84
|
+
|
|
85
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
86
|
+
return {"ir": "Anchor", "at": self.at}
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass
|
|
90
|
+
class IRClassItem:
|
|
91
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
92
|
+
raise NotImplementedError("Subclasses must implement to_dict method")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@dataclass
|
|
96
|
+
class IRClassRange(IRClassItem):
|
|
97
|
+
from_ch: str
|
|
98
|
+
to_ch: str
|
|
99
|
+
|
|
100
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
101
|
+
return {"ir": "Range", "from": self.from_ch, "to": self.to_ch}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@dataclass
|
|
105
|
+
class IRClassLiteral(IRClassItem):
|
|
106
|
+
ch: str
|
|
107
|
+
|
|
108
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
109
|
+
return {"ir": "Char", "char": self.ch}
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass
|
|
113
|
+
class IRClassEscape(IRClassItem):
|
|
114
|
+
type: str
|
|
115
|
+
property: Optional[str] = None
|
|
116
|
+
|
|
117
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
118
|
+
d = {"ir": "Esc", "type": self.type}
|
|
119
|
+
if self.property:
|
|
120
|
+
d["property"] = self.property
|
|
121
|
+
return d
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@dataclass
|
|
125
|
+
class IRCharClass(IROp):
|
|
126
|
+
negated: bool
|
|
127
|
+
items: List[IRClassItem]
|
|
128
|
+
|
|
129
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
130
|
+
return {
|
|
131
|
+
"ir": "CharClass",
|
|
132
|
+
"negated": self.negated,
|
|
133
|
+
"items": [i.to_dict() for i in self.items],
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@dataclass
|
|
138
|
+
class IRQuant(IROp):
|
|
139
|
+
child: IROp
|
|
140
|
+
min: int
|
|
141
|
+
max: Union[int, str]
|
|
142
|
+
mode: str # Greedy|Lazy|Possessive
|
|
143
|
+
|
|
144
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
145
|
+
return {
|
|
146
|
+
"ir": "Quant",
|
|
147
|
+
"child": self.child.to_dict(),
|
|
148
|
+
"min": self.min,
|
|
149
|
+
"max": self.max,
|
|
150
|
+
"mode": self.mode,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@dataclass
|
|
155
|
+
class IRGroup(IROp):
|
|
156
|
+
capturing: bool
|
|
157
|
+
body: IROp
|
|
158
|
+
name: Optional[str] = None
|
|
159
|
+
atomic: Optional[bool] = None
|
|
160
|
+
|
|
161
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
162
|
+
d: Dict[str, Any] = {
|
|
163
|
+
"ir": "Group",
|
|
164
|
+
"capturing": self.capturing,
|
|
165
|
+
"body": self.body.to_dict(),
|
|
166
|
+
}
|
|
167
|
+
if self.name is not None:
|
|
168
|
+
d["name"] = self.name
|
|
169
|
+
if self.atomic is not None:
|
|
170
|
+
d["atomic"] = self.atomic
|
|
171
|
+
return d
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@dataclass
|
|
175
|
+
class IRBackref(IROp):
|
|
176
|
+
byIndex: Optional[int] = None
|
|
177
|
+
byName: Optional[str] = None
|
|
178
|
+
|
|
179
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
180
|
+
d: Dict[str, Any] = {"ir": "Backref"}
|
|
181
|
+
if self.byIndex is not None:
|
|
182
|
+
d["byIndex"] = self.byIndex
|
|
183
|
+
if self.byName is not None:
|
|
184
|
+
d["byName"] = self.byName
|
|
185
|
+
return d
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
@dataclass
|
|
189
|
+
class IRLook(IROp):
|
|
190
|
+
dir: str
|
|
191
|
+
neg: bool
|
|
192
|
+
body: IROp
|
|
193
|
+
|
|
194
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
195
|
+
return {
|
|
196
|
+
"ir": "Look",
|
|
197
|
+
"dir": self.dir,
|
|
198
|
+
"neg": self.neg,
|
|
199
|
+
"body": self.body.to_dict(),
|
|
200
|
+
}
|