@@ -9,37 +9,10 @@ class Symbol:
9
9
name : str
10
10
11
11
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
-
39
12
@dataclass
40
13
class Var :
41
14
"""Something that isn't a literal in a pattern, that binds to a variable"""
42
- var : Symbol
15
+ var : Symbol | None
43
16
cls : type
44
17
use_cls : bool = True
45
18
@@ -61,9 +34,41 @@ class Repeat:
61
34
def __post_init__ (self ):
62
35
if self .max is not None :
63
36
assert self .max >= self .min
37
+ assert self .what
64
38
65
39
66
40
@dataclass
67
41
class Alternate :
68
42
"""Value used in patterns to indicate multiple options."""
69
43
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