From a9d5071ff7fb664ab30fe7235e38bb2656d9ac56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20=C5=A0est=C3=A1k=20=27v6ak?= Date: Tue, 28 May 2024 10:59:46 +0200 Subject: [PATCH] patterns: export to Python regex, which will be helpful for implementing #1 and maybe #2 --- pyi3l/patterns.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/pyi3l/patterns.py b/pyi3l/patterns.py index e71046e..fbf10a4 100644 --- a/pyi3l/patterns.py +++ b/pyi3l/patterns.py @@ -1,6 +1,6 @@ from dataclasses import dataclass from abc import ABC, abstractmethod -from typing import List +from typing import List, Optional from .util import pcre_quote from functools import reduce @@ -8,6 +8,9 @@ class Pattern(ABC): @abstractmethod def to_pcre(self): pass + + @abstractmethod + def to_python_re(self): pass @abstractmethod def map_chars(self, f): pass @@ -72,7 +75,10 @@ class Literal(Pattern): def to_pcre(self): return pcre_quote(self.s) - + + def to_python_re(self): + return re.escape(self.s) + def map_chars(self, f): return Literal("".join(map(f, self.s))) @@ -85,6 +91,9 @@ def __add__(self, other: "Pattern"): class Anything(Pattern): def to_pcre(self): return ".*" + + def to_python_re(self): + return ".*" def map_chars(self, f): return self @@ -96,6 +105,9 @@ class AnyOf(Pattern): def to_pcre(self): return "(" + "|".join(map(lambda p: p.to_pcre(), self.variants)) + ")" + def to_python_re(self): + return "(" + "|".join(map(lambda p: p.to_python_re(), self.variants)) + ")" + def map_chars(self, f): return AnyOf( variants=list(map(lambda p: p.map_chars(f), self.variants)), @@ -115,6 +127,9 @@ def __add__(self, other: "Pattern"): def to_pcre(self): return "".join(map(lambda p: p.to_pcre(), self.subpatterns)) + + def to_python_re(self): + return "".join(map(lambda p: p.to_python_re(), self.subpatterns)) def map_chars(self, f): return CompoundPattern( @@ -132,9 +147,16 @@ class Raw(Pattern): # Please use composable PCRE regexes (without modifiers, without ^ and $) # ^ and $ will be added automatically afterwards pattern: str - + python_re: Optional[str] + def to_pcre(self): return self.pattern - + + def to_python_re(self): + if self.python_re is not None: + return self.python_re + else: + raise NotImplementedError(f'No Python regex alternative for {self.pattern}') + def map_chars(self, f): raise Error(f"Cannot map chars of raw pattern {self}")