<s> from surlex . grammar import Parser , RegexScribe , get_all_nodes , MacroTagNode <EOL> from surlex . macros import MacroRegistry , DefaultMacroRegistry <EOL> import re <EOL> class Surlex ( object ) : <EOL> def __init__ ( self , surlex , macro_registry = DefaultMacroRegistry ( ) ) : <EOL> self . translated = False <EOL> self . surlex = surlex <EOL> self . macro_registry = macro_registry <EOL> def translate ( self ) : <EOL> self . parser = Parser ( self . surlex ) <EOL> self . node_list = self . parser . get_node_list ( ) <EOL> self . scribe = RegexScribe ( <EOL> self . node_list , <EOL> self . macro_registry , <EOL> ) <EOL> self . regex = self . scribe . translate ( ) <EOL> return self . regex <EOL> @ property <EOL> def groupmacros ( self ) : <EOL> macros = { } <EOL> if not self . translated : <EOL> self . translate ( ) <EOL> for node in get_all_nodes ( self . node_list ) : <EOL> if isinstance ( node , MacroTagNode ) : <EOL> macros [ node . name ] = node . macro <EOL> return macros <EOL> @ property <EOL> def to_regex ( self ) : <EOL> if not self . translated : <EOL> self . translate ( ) <EOL> return self . regex <EOL> def match ( self , subject ) : <EOL> m = re . match ( self . to_regex , subject ) <EOL> if m : <EOL> return m . groupdict ( ) <EOL> register_macro = DefaultMacroRegistry . register <EOL> def surlex_to_regex ( surlex ) : <EOL> parser = Parser ( surlex ) <EOL> scribe = RegexScribe ( parser . get_node_list ( ) ) <EOL> return scribe . translate ( ) <EOL> def parsed_surlex_object ( surlex ) : <EOL> object = Surlex ( surlex ) <EOL> object . translate ( ) <EOL> return object <EOL> def match ( surlex , subject ) : <EOL> return Surlex ( surlex ) . match ( subject ) </s>
