Example #1
0
 def translate(self):
     self.parser = Parser(self.surlex)
     self.node_list = self.parser.get_node_list()
     self.scribe = RegexScribe(
         self.node_list,
         self.macro_registry,
     )
     self.regex = self.scribe.translate()
     return self.regex
Example #2
0
class Surlex(object):
    def __init__(self, surlex, macro_registry=DefaultMacroRegistry()):
        self.translated = False
        self.surlex = surlex
        self.macro_registry = macro_registry

    def translate(self):
        self.parser = Parser(self.surlex)
        self.node_list = self.parser.get_node_list()
        self.scribe = RegexScribe(
            self.node_list,
            self.macro_registry,
        )
        self.regex = self.scribe.translate()
        return self.regex

    @property
    def groupmacros(self):
        macros = {}
        if not self.translated:
            self.translate()
        for node in get_all_nodes(self.node_list):
            if isinstance(node, MacroTagNode):
                macros[node.name] = node.macro
        return macros

    @property
    def to_regex(self):
        if not self.translated:
            self.translate()
        return self.regex

    def match(self, subject):
        m = re.match(self.to_regex, subject)
        if m:
            return m.groupdict()
Example #3
0
def surlex_to_regex(surlex):
    parser = Parser(surlex)
    scribe = RegexScribe(parser.get_node_list())
    return scribe.translate()