def do_tgrep_with_callback(root, pattern, callback, **kwargs): new_root = None for match_node, context in tgrep(root, pattern, with_context=True, **kwargs): debug("Callback %s matched", callback.__name__) if context: # only supply a context if the expression binds variables # smash the case, variables in tgrep expressions are case insensitive result = callback(match_node, **smash_key_case(context)) else: result = callback(match_node) # a new root will be returned if one has been installed if result: new_root = result return new_root or root
def multi_tgrep(deriv, query_callback_map): if not query_callback_map: raise RuntimeError('No query expressions given.') initialise() if _tgrep_debug: for expression in query_callback_map.keys(): debug("Lexing %s", expression) lex.input(expression) for tok in iter(lex.token, None): debug("\t%s %s", tok.type, tok.value) queries = [yacc.parse(expression) for expression in query_callback_map.keys()] for node in nodes(deriv): for query_expr, query_str in izip(queries, query_callback_map.keys()): context = Context() if query_expr.is_satisfied_by(node, context): if context: query_callback_map[query_str](node, **smash_key_case(context)) else: query_callback_map[query_str](node)
def multi_tgrep(deriv, query_callback_map): if not query_callback_map: raise RuntimeError('No query expressions given.') initialise() if _tgrep_debug: for expression in query_callback_map.keys(): debug("Lexing %s", expression) lex.input(expression) for tok in iter(lex.token, None): debug("\t%s %s", tok.type, tok.value) queries = [ yacc.parse(expression) for expression in query_callback_map.keys() ] for node in nodes(deriv): for query_expr, query_str in izip(queries, query_callback_map.keys()): context = Context() if query_expr.is_satisfied_by(node, context): if context: query_callback_map[query_str](node, **smash_key_case(context)) else: query_callback_map[query_str](node)