Skip to content

Commit

Permalink
fix(twilight): UnionMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
GreyElaina committed Dec 26, 2023
1 parent 39bdcc0 commit 0e1f55e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions avilla/twilight/twilight.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def _src(self) -> str:
class UnionMatch(RegexMatch):
"""多重匹配"""

pattern: List[str]
pattern: List[Union[str, RegexMatch]]
"""匹配的选择项"""

def __init__(
Expand All @@ -247,20 +247,20 @@ def __init__(
optional (bool, optional): 匹配是否可选. Defaults to False.
"""
super().__init__("", optional)
self.pattern: List[str] = []
self.pattern: List[Union[str, RegexMatch]] = []
for p in pattern:
if isinstance(p, str):
self.pattern.append(re.escape(p))
elif isinstance(p, RegexMatch):
self.pattern.append(p._src)
self.pattern.append(p)
else:
self.pattern.extend([re.escape(i) for i in p])
self.optional = optional
self.help(f"在 {self.pattern} 中选择一项")

@property
def _src(self) -> str:
return f"{'|'.join(i for i in self.pattern)}"
return f"{'|'.join(i if isinstance(i, str) else i._src for i in self.pattern)}"


class ElementMatch(RegexMatch):
Expand Down Expand Up @@ -557,7 +557,7 @@ def match(
res = None
else:
accept_element = isinstance(match, ElementMatch) or (
isinstance(match, MatchUnion) and any(isinstance(i, ElementMatch) for i in match.matches)
isinstance(match, UnionMatch) and any(isinstance(i, ElementMatch) for i in match.pattern)
)
if group[0] == "\x02" and group[-1] == "\x03" and accept_element:
res = elem_mapping[group[1:-1].split("_")[0]]
Expand Down

0 comments on commit 0e1f55e

Please sign in to comment.