Logo Search packages:      
Sourcecode: qm version File versions

def qm::test::classes::dejagnu_test::DejaGNUTest::BuildExecutable::_ParseTclWords (   self,
  s,
  variables = {} 
) [private]

Separate 's' into words, in the same way that Tcl would.

's' -- A string.

'variables' -- A map from variable names to values.  If Tcl
variable substitutions are encountered in 's', the
corresponding value from 'variables' will be used.

returns -- A sequence of strings, each of which is a Tcl
word.

Command substitution is not supported and results in an
exceptions.  Invalid inputs (like the string consisting of a
single quote) also result in exceptions.

See 'Tcl and the Tk Toolkit', by John K. Ousterhout, copyright
1994 by Addison-Wesley Publishing Company, Inc. for details
about the syntax of Tcl.

Definition at line 290 of file dejagnu_test.py.

00290                                             {}):
        """Separate 's' into words, in the same way that Tcl would.

        's' -- A string.

        'variables' -- A map from variable names to values.  If Tcl
        variable substitutions are encountered in 's', the
        corresponding value from 'variables' will be used.

        returns -- A sequence of strings, each of which is a Tcl
        word.

        Command substitution is not supported and results in an
        exceptions.  Invalid inputs (like the string consisting of a
        single quote) also result in exceptions.
        
        See 'Tcl and the Tk Toolkit', by John K. Ousterhout, copyright
        1994 by Addison-Wesley Publishing Company, Inc. for details
        about the syntax of Tcl."""

        # There are no words yet.
        words = []
        # There is no current word.
        word = None
        # We are not processing a double-quoted string.
        in_double_quoted_string = 0
        # Nor are we processing a brace-quoted string.
        in_brace_quoted_string = 0
        # Iterate through all of the characters in s.
        n = 0
        while n < len(s):
            # See what the next character is.
            c = s[n]
            # A "$" indicates variable substitution.
            if c == "$" and not in_brace_quoted_string:
                k = n + 1
                if s[k] == "{":
                    # The name of the variable is enclosed in braces.
                    start = k + 1
                    finish = s.index("}", start)
                    n = finish + 1
                    var = s[start:finish]
                    v = variables[var]
                else:
                    # The following letters, numbers, and underscores make
                    # up the variable name.
                    start = k
                    while (k < len(s)
                           and (s[k].isalnum() or s[k] == "_")):
                        k += 1
                    n = k
                    finish = k
                    if start < finish:
                        var = s[start:finish]
                        v = variables[var]
                    else:
                        v = "$"
                if word is None:
                    word = v
                else:
                    word += v
                continue
            # A "[" indicates command substitution.
            elif (c == "[" and not in_brace_quoted_string
                  and n < len(s) + 1 and s[n + 1] != "]"):
                raise QMException, "Tcl command substitution is unsupported."
            # A double-quote indicates the beginning of a double-quoted
            # string.
            elif c == '"' and not in_brace_quoted_string:
                # We are now entering a new double-quoted string, or
                # leaving the old one.
                in_double_quoted_string = not in_double_quoted_string
                # Skip the quote.
                n += 1
                # The quote starts the word.
                if word is None:
                    word = ""
            # A "{" indicates the beginning of a brace-quoted string.
            elif c == '{' and not in_double_quoted_string:
                # If that's not the opening quote, add it to the
                # string.
                if in_brace_quoted_string:
                    if word is not None:
                        word = word + "{"
                    else:
                        word = "{"
                # The quote starts the word.
                if word is None:
                    word = ""
                # We are entering a brace-quoted string.
                in_brace_quoted_string += 1
                # Skip the brace.
                n += 1
            elif c == '}' and in_brace_quoted_string:
                # Leave the brace quoted string.
                in_brace_quoted_string -= 1
                # Skip the brace.
                n += 1
                # If that's not the closing quote, add it to the
                # string.
                if in_brace_quoted_string:
                    if word is not None:
                        word = word + "}"
                    else:
                        word = "}"
            # A backslash-newline is translated into a space.
            elif c == '\\' and len(s) > 1 and s[1] == '\n':
                # Skip the backslash and the newline.
                n += 2
                # Now, skip tabs and spaces.
                while n < len(s) and (s[n] == ' ' or s[n] == '\t'):
                    n += 1
                # Now prepend one space.
                n -= 1
                s[n] = " "
            # A backslash indicates backslash-substitution.
            elif c == '\\' and not in_brace_quoted_string:
                # There should be a character following the backslash.
                if len(s) == 1:
                    raise QMException, "Invalid Tcl string."
                # Skip the backslash.
                n += 1
                # See what the next character is.
                c = s[n]
                # If it's a control character, use the same character
                # in Python.
                if c in ["a", "b", "f", "n", "r", "t", "v"]:
                    c = eval('"\%s"' % c)
                    n += 1
                # "\x" indicates a hex literal.
                elif c == "x":
                    if (n < len(s)
                        and s[n + 1] in ["0", "1", "2", "3", "4", "5",
                                         "6", "7", "8", "9", "a", "b",
                                         "c", "d", "e", "f"]):
                        raise QMException, "Unsupported Tcl escape."
                    n += 1
                # "\d" where "d" is a digit indicates an octal literal.
                elif c.isdigit():
                    raise QMException, "Unsupported Tcl escape."
                # Any other character just indicates the character
                # itself.
                else:
                    n += 1
                # Add it to the current word.
                if word is not None:
                    word = word + c
                else:
                    word = c
            # A space or tab indicates a word separator.
            elif ((c == ' ' or c == '\t')
                  and not in_double_quoted_string
                  and not in_brace_quoted_string):
                # Add the current word to the list of words.
                if word is not None:
                    words.append(word)
                # Skip over the space.
                n += 1
                # Keep skipping while the leading character of s is
                # a space or tab.
                while n < len(s) and (s[n] == ' ' or s[n] == '\t'):
                    n += 1
                # Start the next word.
                word = None
            # Any other character is just added to the current word.
            else:
                if word is not None:
                    word = word + c
                else:
                    word = c
                n += 1

        # If we were working on a word when we reached the end of
        # the string, add it to the list.
        if word is not None:
            words.append(word)

        return words
        return words


Generated by  Doxygen 1.6.0   Back to index