diff options
Diffstat (limited to 'src/py/javatoolkit/parser/helpers.py')
-rw-r--r-- | src/py/javatoolkit/parser/helpers.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/py/javatoolkit/parser/helpers.py b/src/py/javatoolkit/parser/helpers.py new file mode 100644 index 0000000..b11b08b --- /dev/null +++ b/src/py/javatoolkit/parser/helpers.py @@ -0,0 +1,57 @@ +# Copyright(c) 2006, James Le Cuirot <chewi@aura-online.co.uk> +# Copyright(c) 2004, Karl Trygve Kalleberg <karltk@gentoo.org> +# Copyright(c) 2004, Gentoo Foundation +# +# Licensed under the GNU General Public License, v2 +# +# $Header: $ + +def expand(root, expr, realroot = None): + """Evaluates a path expression on a given tree. + + @param root - the root of the tree + @param expr - the expression to resolve + + @return the expanded string + """ + + if realroot == None: + realroot = root + + expanded = "" + in_varref = False + varname = "" + + for i in range(len(expr)): + x = expr[i] + + if in_varref: + + if x == "}": + in_varref = False + expanded += expand(root, realroot.find_node(varname).value, realroot) + varname = "" + elif x != "{": + varname += expr[i] + + elif x == "$" and i < len(expr) and expr[i + 1] == "{": + in_varref = True + + else: + expanded += x + + return expanded + +def strip_varmarker(s): + """Strips away ${ and } in a variable expression. Idempotent if marker not found. + + Example: "${foo}" -> "foo" + Example: "foo" -> "foo" + """ + + if s.startswith("${") and s.endswith("}"): + return s[2:-1] + + return s + +# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap: |