latychain 0.1.0__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.
- latychain-0.1.0/.gitignore +22 -0
- latychain-0.1.0/LICENSE +21 -0
- latychain-0.1.0/PKG-INFO +531 -0
- latychain-0.1.0/README.md +508 -0
- latychain-0.1.0/docs/api-reference.md +230 -0
- latychain-0.1.0/docs/guide.md +244 -0
- latychain-0.1.0/pyproject.toml +44 -0
- latychain-0.1.0/src/latychain/ChainDotRule.py +30 -0
- latychain-0.1.0/src/latychain/__init__.py +71 -0
- latychain-0.1.0/src/latychain/_atoms.py +532 -0
- latychain-0.1.0/src/latychain/_chain.py +415 -0
- latychain-0.1.0/src/latychain/_hook.py +282 -0
- latychain-0.1.0/test/_test_sugar.py +86 -0
- latychain-0.1.0/test/run_all.py +30 -0
- latychain-0.1.0/test/test_core.py +288 -0
latychain-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 fnsii
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
latychain-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: latychain
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Chain-structured data and pattern matching with .xxx.yyy syntax
|
|
5
|
+
Project-URL: Homepage, https://github.com/fnsii/latychain
|
|
6
|
+
Project-URL: Repository, https://github.com/fnsii/latychain
|
|
7
|
+
Project-URL: Documentation, https://github.com/fnsii/latychain#readme
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/fnsii/latychain/issues
|
|
9
|
+
Author: laty contributors
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: chain,dsl,path,pattern-matching
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# latychain
|
|
25
|
+
|
|
26
|
+
**Chain-structured data and pattern matching with `.xxx.yyy` syntax.**
|
|
27
|
+
|
|
28
|
+
`latychain` provides two core types — [`Chain`](#chain) (immutable ordered container) and [`ChainRuleAtom`](#chainruleatom) (rule atoms) — plus an optional **import hook** that enables the concise `.xxx.yyy` syntax for constructing chains.
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
import latychain.ChainDotRule # enable .xxx.yyy sugar
|
|
32
|
+
from latychain import Chain
|
|
33
|
+
|
|
34
|
+
# ── Data chain ──
|
|
35
|
+
heading = .heading.h1 # → Chain(['heading', 'h1'])
|
|
36
|
+
|
|
37
|
+
# ── Rule chain ──
|
|
38
|
+
rule = .any(0).uuu.rex(r'x\d') # → Chain([any(0), 'uuu', rex(...)])
|
|
39
|
+
|
|
40
|
+
# ── Nested rules ──
|
|
41
|
+
r2 = .any(0).enum(
|
|
42
|
+
.hi.rex(r'x[0-9]'),
|
|
43
|
+
.wuhu.apply(f)
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# ── Matching ──
|
|
47
|
+
.x.uuu.x1.match(rule) # True
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Table of Contents
|
|
53
|
+
|
|
54
|
+
- [Why latychain?](#why-latychain)
|
|
55
|
+
- [Installation](#installation)
|
|
56
|
+
- [Quick Start](#quick-start)
|
|
57
|
+
- [Chain](#chain)
|
|
58
|
+
- [Construction](#construction)
|
|
59
|
+
- [Operations](#operations)
|
|
60
|
+
- [Methods](#methods)
|
|
61
|
+
- [ChainRuleAtom](#chainruleatom)
|
|
62
|
+
- [any — arbitrary elements](#any--arbitrary-elements)
|
|
63
|
+
- [rex — regex match](#rex--regex-match)
|
|
64
|
+
- [enum — choice](#enum--choice)
|
|
65
|
+
- [apply — custom predicate](#apply--custom-predicate)
|
|
66
|
+
- [long — string length](#long--string-length)
|
|
67
|
+
- [un — negation](#un--negation)
|
|
68
|
+
- [ext — optional segment](#ext--optional-segment)
|
|
69
|
+
- [Matching](#matching)
|
|
70
|
+
- [Full match vs partial match](#full-match-vs-partial-match)
|
|
71
|
+
- [Backtracking engine](#backtracking-engine)
|
|
72
|
+
- [`.xxx.yyy` Syntax Sugar](#xxxyyy-syntax-sugar)
|
|
73
|
+
- [How it works](#how-it-works)
|
|
74
|
+
- [What is (and isn't) transformed](#what-is-and-isnt-transformed)
|
|
75
|
+
- [Nested expressions](#nested-expressions)
|
|
76
|
+
- [Examples](#examples)
|
|
77
|
+
- [HTML headings](#html-headings)
|
|
78
|
+
- [Path permissions](#path-permissions)
|
|
79
|
+
- [Log classification](#log-classification)
|
|
80
|
+
- [API Reference](#api-reference)
|
|
81
|
+
- [Design & Implementation](#design--implementation)
|
|
82
|
+
- [Development](#development)
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Why latychain?
|
|
87
|
+
|
|
88
|
+
Many domains deal with **hierarchical path-like data**: CSS selectors, filesystem paths, JSON paths, routing rules, config keys, log categories, etc. Representing these as plain strings is error-prone; representing them as lists is verbose.
|
|
89
|
+
|
|
90
|
+
`latychain` gives you:
|
|
91
|
+
|
|
92
|
+
- **Immutability** — chains are hashable, thread-safe, usable as dict keys
|
|
93
|
+
- **Pattern matching** — declarative rules with backtracking, regex, custom predicates
|
|
94
|
+
- **Concise syntax** — `.xxx.yyy` reads naturally as a path
|
|
95
|
+
- **No external dependencies** — pure Python, uses only standard library
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Installation
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pip install latychain
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Requires Python 3.10+.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Quick Start
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import latychain.ChainDotRule
|
|
113
|
+
from latychain import Chain
|
|
114
|
+
|
|
115
|
+
# ── Construct data chains ──
|
|
116
|
+
path = .user.profile.avatar
|
|
117
|
+
# → Chain(['user', 'profile', 'avatar'])
|
|
118
|
+
|
|
119
|
+
# ── Construct rule chains ──
|
|
120
|
+
rule = .any(0).enum(
|
|
121
|
+
.admin.any(0),
|
|
122
|
+
.user.any(0)
|
|
123
|
+
).rex(r'\d+')
|
|
124
|
+
|
|
125
|
+
# ── Match ──
|
|
126
|
+
.user.login.123.match(rule) # True
|
|
127
|
+
.admin.delete.456.match(rule) # True
|
|
128
|
+
.guest.abc.match(rule) # False
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Chain
|
|
134
|
+
|
|
135
|
+
`Chain` is an **immutable, ordered container**. Elements can be plain strings (data) or `ChainRuleAtom` instances (rules).
|
|
136
|
+
|
|
137
|
+
### Construction
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from latychain import Chain
|
|
141
|
+
|
|
142
|
+
Chain() # empty chain
|
|
143
|
+
Chain(['a']) # single element
|
|
144
|
+
Chain(['a', 'b', 'c']) # multi-element
|
|
145
|
+
Chain([ChainRuleAtom.any(0)]) # with rule atoms
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Or with the `.xxx.yyy` sugar:
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
import latychain.ChainDotRule
|
|
152
|
+
|
|
153
|
+
.a.b.c # → Chain(['a', 'b', 'c'])
|
|
154
|
+
.any(0).uuu.rex(r'x\d') # → Chain([any(0), 'uuu', rex(...)])
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Operations
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
c = Chain(['a', 'b', 'c'])
|
|
161
|
+
|
|
162
|
+
len(c) # 3
|
|
163
|
+
c[0] # 'a'
|
|
164
|
+
c[-1] # 'c'
|
|
165
|
+
list(c) # ['a', 'b', 'c']
|
|
166
|
+
c.elements # ('a', 'b', 'c')
|
|
167
|
+
|
|
168
|
+
str(c) # ".a.b.c"
|
|
169
|
+
repr(c) # "Chain(['a', 'b', 'c'])"
|
|
170
|
+
|
|
171
|
+
Chain(['a', 'b']) == Chain(['a', 'b']) # True
|
|
172
|
+
Chain(['a', 'b']) + Chain(['c', 'd']) # → Chain(['a','b','c','d'])
|
|
173
|
+
|
|
174
|
+
d = {Chain(['a']): 1} # hashable, usable as dict key
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Methods
|
|
178
|
+
|
|
179
|
+
```python
|
|
180
|
+
chain.to_list() # → list of elements
|
|
181
|
+
chain.startswith(prefix) # prefix match (partial)
|
|
182
|
+
chain.match(pattern) # full match (see Matching section)
|
|
183
|
+
chain.match(pattern, partial=True) # prefix match
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## ChainRuleAtom
|
|
189
|
+
|
|
190
|
+
`ChainRuleAtom` is the minimal unit of a rule pattern. All atoms are immutable and hashable.
|
|
191
|
+
|
|
192
|
+
| Factory | Purpose |
|
|
193
|
+
|---------|---------|
|
|
194
|
+
| [`any(min, max)`](#any--arbitrary-elements) | Match N arbitrary elements (with backtracking) |
|
|
195
|
+
| [`rex(pattern)`](#rex--regex-match) | Regex fullmatch on a single element |
|
|
196
|
+
| [`enum(*chains)`](#enum--choice) | Pick one of several alternatives |
|
|
197
|
+
| [`apply(func, long)`](#apply--custom-predicate) | Custom predicate on N elements |
|
|
198
|
+
| [`long(min, max)`](#long--string-length) | String length constraint |
|
|
199
|
+
| [`un(value)`](#un--negation) | Negation: not equal to value |
|
|
200
|
+
| [`ext(chain)`](#ext--optional-segment) | Optional segment (match or skip) |
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
from latychain import ChainRuleAtom
|
|
204
|
+
|
|
205
|
+
ChainRuleAtom.any(0)
|
|
206
|
+
ChainRuleAtom.rex(r'x\d')
|
|
207
|
+
ChainRuleAtom.enum(Chain(['a']), Chain(['b']))
|
|
208
|
+
ChainRuleAtom.apply(lambda c: len(c) > 2)
|
|
209
|
+
ChainRuleAtom.long(3, 5)
|
|
210
|
+
ChainRuleAtom.un('admin')
|
|
211
|
+
ChainRuleAtom.ext(Chain(['a', 'b']))
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### any — arbitrary elements
|
|
215
|
+
|
|
216
|
+
Match between `min` and `max` arbitrary elements. Non-greedy with backtracking.
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
.any() # at least 1
|
|
220
|
+
.any(0) # 0 or more
|
|
221
|
+
.any(2) # at least 2
|
|
222
|
+
.any(1, 3) # 1 to 3
|
|
223
|
+
.any(0, 5) # 0 to 5
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### rex — regex match
|
|
227
|
+
|
|
228
|
+
Regex `fullmatch` on a **single** string element.
|
|
229
|
+
|
|
230
|
+
```python
|
|
231
|
+
.rex(r'h[12]') # matches 'h1', 'h2'
|
|
232
|
+
.rex(r'\d+') # matches '123', '0'
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### enum — choice
|
|
236
|
+
|
|
237
|
+
Match **one** of several alternatives. Each alternative is a `Chain` (data or rule).
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
.enum(
|
|
241
|
+
.type.h1,
|
|
242
|
+
.type.h2
|
|
243
|
+
)
|
|
244
|
+
# matches .type.h1 or .type.h2
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### apply — custom predicate
|
|
248
|
+
|
|
249
|
+
Apply a user function to `long` consecutive elements. The function receives a `Chain` object.
|
|
250
|
+
|
|
251
|
+
```python
|
|
252
|
+
.apply(lambda seg: str(seg).startswith('.x'))
|
|
253
|
+
# single element starting with 'x'
|
|
254
|
+
|
|
255
|
+
.apply(lambda seg: len(seg) > 2, long=2)
|
|
256
|
+
# two consecutive elements, total chain length > 2
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### long — string length
|
|
260
|
+
|
|
261
|
+
Constrain the **string length** of a single element.
|
|
262
|
+
|
|
263
|
+
```python
|
|
264
|
+
.long(3) # exactly 3 characters
|
|
265
|
+
.long(2, 5) # 2 to 5 characters
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### un — negation
|
|
269
|
+
|
|
270
|
+
Match any single element **except** the given value.
|
|
271
|
+
|
|
272
|
+
```python
|
|
273
|
+
.un('admin') # matches 'user', 'guest'; does NOT match 'admin'
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
### ext — optional segment
|
|
277
|
+
|
|
278
|
+
Try to match the inner chain; if it fails, skip (consume 0 elements).
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
.a.ext(.pi).b
|
|
282
|
+
# matches .a.pi.b (ext matched)
|
|
283
|
+
# matches .a.b (ext skipped)
|
|
284
|
+
# does NOT match .a.x.b
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
## Matching
|
|
290
|
+
|
|
291
|
+
### Full match vs partial match
|
|
292
|
+
|
|
293
|
+
```python
|
|
294
|
+
data = .a.b.c.d
|
|
295
|
+
|
|
296
|
+
data.match(.a.b) # False — does not consume c.d
|
|
297
|
+
data.match(.a.b, partial=True) # True — prefix matches
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Backtracking engine
|
|
301
|
+
|
|
302
|
+
The matcher uses **depth-first backtracking with non-greedy priority**. `any()` tries shorter matches first, then longer ones if the rest of the pattern fails.
|
|
303
|
+
|
|
304
|
+
```
|
|
305
|
+
Rule: .any(0).uuu.rex(r'x\d')
|
|
306
|
+
Data: .pre.uuu.x1
|
|
307
|
+
|
|
308
|
+
Attempts:
|
|
309
|
+
any=0 → uuu ≠ 'pre' → backtrack
|
|
310
|
+
any=1 → uuu = 'uuu' ✓ → rex(r'x\d') matches 'x1' ✓ → success
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
---
|
|
314
|
+
|
|
315
|
+
## `.xxx.yyy` Syntax Sugar
|
|
316
|
+
|
|
317
|
+
### Enabling
|
|
318
|
+
|
|
319
|
+
```python
|
|
320
|
+
import latychain.ChainDotRule
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
This registers a **meta path finder** (import hook) that transforms all subsequently loaded `.py` files. Only needs to be done once, at the entry point.
|
|
324
|
+
|
|
325
|
+
### How it works
|
|
326
|
+
|
|
327
|
+
The import hook uses Python's `tokenize` module to safely identify `.xxx` expressions and transform them into `Chain([...])` calls at compile time (not runtime).
|
|
328
|
+
|
|
329
|
+
| Source | Transformed to |
|
|
330
|
+
|--------|---------------|
|
|
331
|
+
| `.heading.h1` | `Chain(['heading', 'h1'])` |
|
|
332
|
+
| `.any(0).uuu` | `Chain([ChainRuleAtom.any(0), 'uuu'])` |
|
|
333
|
+
| `.any(0).uuu.rex(r'x\d')` | `Chain([ChainRuleAtom.any(0), 'uuu', ChainRuleAtom.rex(r'x\d')])` |
|
|
334
|
+
|
|
335
|
+
**Rule**: segments without `()` become strings; segments with `()` become `ChainRuleAtom.xxx()` calls.
|
|
336
|
+
|
|
337
|
+
### What is (and isn't) transformed
|
|
338
|
+
|
|
339
|
+
| Code | Transformed? | Reason |
|
|
340
|
+
|------|-------------|--------|
|
|
341
|
+
| `.heading.h1` | ✅ Yes | chain expression |
|
|
342
|
+
| `.any(0).uuu` | ✅ Yes | chain expression |
|
|
343
|
+
| `obj.attr` | ❌ No | attribute access |
|
|
344
|
+
| `.5 + .3` | ❌ No | float literals |
|
|
345
|
+
| `func().attr` | ❌ No | method return value access |
|
|
346
|
+
| `"strings .here"` | ❌ No | inside string literals |
|
|
347
|
+
| `# comments .here` | ❌ No | inside comments |
|
|
348
|
+
|
|
349
|
+
### Nested expressions
|
|
350
|
+
|
|
351
|
+
Arguments inside `enum()`, `ext()`, etc. are recursively transformed:
|
|
352
|
+
|
|
353
|
+
```python
|
|
354
|
+
.enum(
|
|
355
|
+
.hi.rex(r'x[0-9]'),
|
|
356
|
+
.wuhu.apply(f)
|
|
357
|
+
)
|
|
358
|
+
# → Chain([ChainRuleAtom.enum(
|
|
359
|
+
# Chain(['hi', ChainRuleAtom.rex(r'x[0-9]')]),
|
|
360
|
+
# Chain(['wuhu', ChainRuleAtom.apply(f)])
|
|
361
|
+
# )])
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
---
|
|
365
|
+
|
|
366
|
+
## Examples
|
|
367
|
+
|
|
368
|
+
### HTML headings
|
|
369
|
+
|
|
370
|
+
```python
|
|
371
|
+
import latychain.ChainDotRule
|
|
372
|
+
|
|
373
|
+
heading_rule = .any(0).heading.rex(r'h[1-6]')
|
|
374
|
+
|
|
375
|
+
.heading.h1.match(heading_rule) # True
|
|
376
|
+
.body.heading.h3.match(heading_rule) # True
|
|
377
|
+
.heading.h7.match(heading_rule) # False
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Path permissions
|
|
381
|
+
|
|
382
|
+
```python
|
|
383
|
+
import latychain.ChainDotRule
|
|
384
|
+
|
|
385
|
+
# Allow /user/* and /admin/*, but reject /admin/secret
|
|
386
|
+
allow_rule = .any(0).enum(
|
|
387
|
+
.user.any(0),
|
|
388
|
+
.admin.un('secret').any(0)
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
.a.user.profile.match(allow_rule) # True
|
|
392
|
+
.a.admin.dashboard.match(allow_rule) # True
|
|
393
|
+
.a.admin.secret.match(allow_rule) # False
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Log classification
|
|
397
|
+
|
|
398
|
+
```python
|
|
399
|
+
import latychain.ChainDotRule
|
|
400
|
+
|
|
401
|
+
# Match error logs: YYYY.MM.DD.ERROR.xxx
|
|
402
|
+
error_pattern = (
|
|
403
|
+
.rex(r'\d{4}')
|
|
404
|
+
.rex(r'\d{2}')
|
|
405
|
+
.rex(r'\d{2}')
|
|
406
|
+
.ERROR
|
|
407
|
+
.any(0)
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
.2024.01.15.ERROR.timeout.match(error_pattern) # True
|
|
411
|
+
.2024.01.15.INFO.request.match(error_pattern) # False
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
---
|
|
415
|
+
|
|
416
|
+
## API Reference
|
|
417
|
+
|
|
418
|
+
### `Chain`
|
|
419
|
+
|
|
420
|
+
```python
|
|
421
|
+
class Chain:
|
|
422
|
+
def __init__(self, elements: Iterable = ()) -> None
|
|
423
|
+
|
|
424
|
+
# Read
|
|
425
|
+
def __getitem__(self, index: int) -> str | ChainRuleAtom
|
|
426
|
+
def __len__(self) -> int
|
|
427
|
+
def __iter__(self) -> Iterator
|
|
428
|
+
@property
|
|
429
|
+
def elements(self) -> tuple
|
|
430
|
+
|
|
431
|
+
# String
|
|
432
|
+
def __str__(self) -> str # ".a.b.c"
|
|
433
|
+
def __repr__(self) -> str # "Chain(['a', 'b', 'c'])"
|
|
434
|
+
|
|
435
|
+
# Value semantics
|
|
436
|
+
def __eq__(self, other) -> bool
|
|
437
|
+
def __hash__(self) -> int
|
|
438
|
+
def __bool__(self) -> bool
|
|
439
|
+
|
|
440
|
+
# Operations
|
|
441
|
+
def __add__(self, other) -> Chain
|
|
442
|
+
def match(self, pattern: Chain, partial: bool = False) -> bool
|
|
443
|
+
|
|
444
|
+
# Utilities
|
|
445
|
+
def to_list(self) -> list
|
|
446
|
+
def startswith(self, prefix: Chain) -> bool
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
### `ChainRuleAtom`
|
|
450
|
+
|
|
451
|
+
```python
|
|
452
|
+
class ChainRuleAtom:
|
|
453
|
+
@staticmethod
|
|
454
|
+
def any(min: int = 0, max: int = 0) -> ChainRuleAtom
|
|
455
|
+
@staticmethod
|
|
456
|
+
def rex(pattern: str) -> ChainRuleAtom
|
|
457
|
+
@staticmethod
|
|
458
|
+
def enum(*alternatives: Chain) -> ChainRuleAtom
|
|
459
|
+
@staticmethod
|
|
460
|
+
def apply(func: callable, long: int = 1) -> ChainRuleAtom
|
|
461
|
+
@staticmethod
|
|
462
|
+
def long(min: int, max: int | None = None) -> ChainRuleAtom
|
|
463
|
+
@staticmethod
|
|
464
|
+
def un(value: str) -> ChainRuleAtom
|
|
465
|
+
@staticmethod
|
|
466
|
+
def ext(chain: Chain | None = None) -> ChainRuleAtom
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
### `latychain.ChainDotRule`
|
|
470
|
+
|
|
471
|
+
```python
|
|
472
|
+
import latychain.ChainDotRule # registers the import hook globally
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
---
|
|
476
|
+
|
|
477
|
+
## Design & Implementation
|
|
478
|
+
|
|
479
|
+
Detailed documentation is in the [`docs/`](./docs/) directory:
|
|
480
|
+
|
|
481
|
+
| Document | Description |
|
|
482
|
+
|----------|-------------|
|
|
483
|
+
| [`docs/api-reference.md`](./docs/api-reference.md) | Complete API reference for Chain, ChainRuleAtom, and the import hook |
|
|
484
|
+
| [`docs/guide.md`](./docs/guide.md) | Usage guide with practical patterns, migration tips, and deep dives |
|
|
485
|
+
|
|
486
|
+
### Key design decisions
|
|
487
|
+
|
|
488
|
+
1. **Single type for data and rules** — `Chain` holds both strings and `ChainRuleAtom` instances, no separate DSL
|
|
489
|
+
2. **Compile-time transformation** — import hook uses `tokenize`, not runtime evaluation; safe and performant
|
|
490
|
+
3. **Backtracking engine** — non-greedy depth-first search for `any()` matching
|
|
491
|
+
4. **Immutability** — chains are hashable, thread-safe, usable as dict keys
|
|
492
|
+
|
|
493
|
+
---
|
|
494
|
+
|
|
495
|
+
## Development
|
|
496
|
+
|
|
497
|
+
### Setup
|
|
498
|
+
|
|
499
|
+
```bash
|
|
500
|
+
git clone <repo>
|
|
501
|
+
cd latychain
|
|
502
|
+
uv venv
|
|
503
|
+
source .venv/bin/activate # or .venv\Scripts\activate on Windows
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
### Running tests
|
|
507
|
+
|
|
508
|
+
```bash
|
|
509
|
+
uv run python test/run_all.py
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
### Project structure
|
|
513
|
+
|
|
514
|
+
```
|
|
515
|
+
latychain/
|
|
516
|
+
├── src/latychain/
|
|
517
|
+
│ ├── __init__.py # Public API: Chain, ChainRuleAtom
|
|
518
|
+
│ ├── _chain.py # Chain class + backtracking matcher
|
|
519
|
+
│ ├── _atoms.py # ChainRuleAtom + 7 rule atom types
|
|
520
|
+
│ ├── _hook.py # Import hook (tokenize transformer)
|
|
521
|
+
│ └── ChainDotRule.py # Entry point: import to enable sugar
|
|
522
|
+
├── test/
|
|
523
|
+
│ ├── run_all.py # Test runner
|
|
524
|
+
│ ├── test_core.py # Core API tests (30 cases)
|
|
525
|
+
│ └── _test_sugar.py # Sugar syntax integration tests
|
|
526
|
+
├── docs/
|
|
527
|
+
│ ├── api-reference.md # Complete API reference
|
|
528
|
+
│ └── guide.md # Usage guide and practical patterns
|
|
529
|
+
├── pyproject.toml # Project metadata
|
|
530
|
+
└── README.md # This file
|
|
531
|
+
```
|