1.1 --- a/libxml2macro.py Mon Jan 24 02:16:16 2005 +0000
1.2 +++ b/libxml2macro.py Mon Jan 24 10:36:41 2005 +0000
1.3 @@ -33,7 +33,7 @@
1.4
1.5 # Processing functions.
1.6
1.7 -def process_nodes(root_node, prefix):
1.8 +def process_nodes(root_node, prefix=None):
1.9
1.10 """
1.11 Under the 'root_node', process all suitable expression nodes which employ
1.12 @@ -41,14 +41,40 @@
1.13 """
1.14
1.15 for node in root_node.getChildNodes():
1.16 - # Identify suitable names and add such nodes
1.17 - if isinstance(node, compiler.ast.CallFunc):
1.18 +
1.19 + # Find imports and discover if they are really used to define the
1.20 + # special prefix.
1.21 +
1.22 + if isinstance(node, compiler.ast.Import):
1.23 + prefix = process_import(node, root_node) or prefix
1.24 +
1.25 + # Identify suitable names and add replacement nodes.
1.26 +
1.27 + elif isinstance(node, compiler.ast.CallFunc):
1.28 process_callfunc(node, prefix)
1.29 elif isinstance(node, compiler.ast.Getattr):
1.30 process_getattr(node, prefix, root_node)
1.31 else:
1.32 process_nodes(node, prefix)
1.33
1.34 +def process_import(node, parent):
1.35 +
1.36 + """
1.37 + Process the Import 'node' searching for the special incantation required to
1.38 + set the prefix. Remove compliant nodes from their 'parent' node. If no new
1.39 + prefix is found, return None.
1.40 + """
1.41 +
1.42 + for name, alias in node.names:
1.43 + if name == "libxml2macro":
1.44 +
1.45 + # Remove this node from its parent.
1.46 +
1.47 + parent.nodes.remove(node)
1.48 + return alias
1.49 +
1.50 + return None
1.51 +
1.52 def process_callfunc(node, prefix):
1.53
1.54 """
1.55 @@ -56,7 +82,13 @@
1.56 'prefix'.
1.57 """
1.58
1.59 + # Check the prefix.
1.60 +
1.61 + if prefix is None:
1.62 + return
1.63 +
1.64 # Check the target.
1.65 +
1.66 target = node.node
1.67 if isinstance(target, compiler.ast.Getattr):
1.68 if process_getattr(target, prefix, node):
1.69 @@ -95,6 +127,11 @@
1.70 processed and where a replacement of such arguments is occurring.
1.71 """
1.72
1.73 + # Check the prefix.
1.74 +
1.75 + if prefix is None:
1.76 + return
1.77 +
1.78 # Detected cases:
1.79 # node.attr plus node.attr.attr
1.80 # (obj.node).attr plus (obj.node).attr.attr
1.81 @@ -167,16 +204,27 @@
1.82
1.83 # Check the expression as a simple name:
1.84 # node.attr
1.85 +
1.86 if isinstance(node.expr, compiler.ast.Name) and node.expr.name.startswith(prefix):
1.87 return 1
1.88 +
1.89 # Check the attribute name of child expressions:
1.90 # (obj.node).attr
1.91 +
1.92 elif isinstance(node.expr, compiler.ast.Getattr) and node.expr.attrname.startswith(prefix):
1.93 return 1
1.94 else:
1.95 return 0
1.96
1.97 -def process_file(filename, prefix="x2_"):
1.98 +def include_import(module):
1.99 +
1.100 + """
1.101 + Include an import statement in 'module' so make the macro library available.
1.102 + """
1.103 +
1.104 + module.node.nodes.insert(0, compiler.ast.From("libxml2dom.macrolib", [("*", None)]))
1.105 +
1.106 +def process_file(filename):
1.107
1.108 """
1.109 Process the module given by the specified 'filename'. The optional special
1.110 @@ -189,7 +237,11 @@
1.111
1.112 # Find references to special variables.
1.113
1.114 - process_nodes(module, prefix)
1.115 + process_nodes(module)
1.116 +
1.117 + # Add necessary imports.
1.118 +
1.119 + include_import(module)
1.120
1.121 # Write the module.
1.122