File: Synopsis/Processors/MacroFilter.py
 1#
 2# Copyright (C) 2005 Stefan Seefeld
 3# All rights reserved.
 4# Licensed to the public under the terms of the GNU LGPL (>= 2),
 5# see the file COPYING for details.
 6#
 7
 8from Synopsis.Processor import *
 9from Synopsis import ASG
10import re
11
12class MacroFilter(Processor, ASG.Visitor):
13    """A MacroFilter allows macros to be filtered, based on pattern matching.
14
15    Macros with matching names will be removed."""
16
17    pattern = Parameter('', 'Regular expression to match macro names with.')
18
19    def process(self, ir, **kwds):
20
21        self.set_parameters(kwds)
22        self._pattern = re.compile(self.pattern)
23        self.ir = self.merge_input(ir)
24
25        for decl in self.ir.asg.declarations[:]:
26            decl.accept(self)
27
28        return self.output_and_return_ir()
29
30    def visit_macro(self, node):
31
32        if self._pattern.match(node.name[-1]):
33            # Macros always live in the top-most scope.
34            self.ir.asg.declarations.remove(node)
35