8000 add some more Thompson code that doesn't work · dragoncoder047/pickle@98fa7d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98fa7d3

Browse files
add some more Thompson code that doesn't work
1 parent 287cd0e commit 98fa7d3

File tree

2 files changed

+220
-170
lines changed

2 files changed

+220
-170
lines changed

python/pickle_lang/object.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,10 @@ class Symbol:
99
name: str
1010

1111

12-
@dataclass
13-
@functools.total_ordering
14-
class Pattern:
15-
"""The core object that manages pattern-matching."""
16-
precedence: int
17-
pattern: list
18-
handler: Callable[[dict[Symbol, Any]], Any]
19-
right: bool = False
20-
macro: bool = False
21-
22-
def __lt__(self, other):
23-
if isinstance(other, Pattern):
24-
return other.precedence < self.precedence
25-
return NotImplemented
26-
27-
def __eq__(self, other):
28-
if isinstance(other, Pattern):
29-
return other.precedence == self.precedence
30-
return NotImplemented
31-
32-
def __len__(self):
33-
return len(self.pattern)
34-
35-
def __iter__(self):
36-
yield from self.pattern
37-
38-
3912
@dataclass
4013
class Var:
4114
"""Something that isn't a literal in a pattern, that binds to a variable"""
42-
var: Symbol
15+
var: Symbol | None
4316
cls: type
4417
use_cls: bool = True
4518

@@ -61,9 +34,41 @@ class Repeat:
6134
def __post_init__(self):
6235
if self.max is not None:
6336
assert self.max >= self.min
37+
assert self.what
6438

6539

6640
@dataclass
6741
class Alternate:
6842
"""Value used in patterns to indicate multiple options."""
6943
options: list[Any]
44+
45+
def __post_init__(self):
46+
assert self.options
47+
48+
49+
@dataclass
50+
@functools.total_ordering
51+
class Pattern:
52+
"""The core object that manages pattern-matching."""
53+
precedence: int
54+
pattern: list[Var | Space | Repeat | Alternate]
55+
handler: Callable[[dict[Symbol, Any]], Any]
56+
right: bool = False
57+
macro: bool = False
58+
greedy: bool = True
59+
60+
def __lt__(self, other):
61+
if isinstance(other, Pattern):
62+
return other.precedence < self.precedence
63+
return NotImplemented
64+
65+
def __eq__(self, other):
66+
if isinstance(other, Pattern):
67+
return other.precedence == self.precedence
68+
return NotImplemented
69+
70+
def __len__(self):
71+
return len(self.pattern)
72+
73+
def __iter__(self):
74+
yield from self.pattern

0 commit comments

Comments
 (0)
0