]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/pgen2/tokenize.py
edk2: Remove AppPkg, StdLib, StdLibPrivateInternalFiles
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / lib2to3 / pgen2 / tokenize.py
diff --git a/AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/pgen2/tokenize.py b/AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/pgen2/tokenize.py
deleted file mode 100644 (file)
index 5ac8f3a..0000000
+++ /dev/null
@@ -1,500 +0,0 @@
-# Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Python Software Foundation.\r
-# All rights reserved.\r
-\r
-"""Tokenization help for Python programs.\r
-\r
-generate_tokens(readline) is a generator that breaks a stream of\r
-text into Python tokens.  It accepts a readline-like method which is called\r
-repeatedly to get the next line of input (or "" for EOF).  It generates\r
-5-tuples with these members:\r
-\r
-    the token type (see token.py)\r
-    the token (a string)\r
-    the starting (row, column) indices of the token (a 2-tuple of ints)\r
-    the ending (row, column) indices of the token (a 2-tuple of ints)\r
-    the original line (string)\r
-\r
-It is designed to match the working of the Python tokenizer exactly, except\r
-that it produces COMMENT tokens for comments and gives type OP for all\r
-operators\r
-\r
-Older entry points\r
-    tokenize_loop(readline, tokeneater)\r
-    tokenize(readline, tokeneater=printtoken)\r
-are the same, except instead of generating tokens, tokeneater is a callback\r
-function to which the 5 fields described above are passed as 5 arguments,\r
-each time a new token is found."""\r
-\r
-__author__ = 'Ka-Ping Yee <ping@lfw.org>'\r
-__credits__ = \\r
-    'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'\r
-\r
-import string, re\r
-from codecs import BOM_UTF8, lookup\r
-from lib2to3.pgen2.token import *\r
-\r
-from . import token\r
-__all__ = [x for x in dir(token) if x[0] != '_'] + ["tokenize",\r
-           "generate_tokens", "untokenize"]\r
-del token\r
-\r
-try:\r
-    bytes\r
-except NameError:\r
-    # Support bytes type in Python <= 2.5, so 2to3 turns itself into\r
-    # valid Python 3 code.\r
-    bytes = str\r
-\r
-def group(*choices): return '(' + '|'.join(choices) + ')'\r
-def any(*choices): return group(*choices) + '*'\r
-def maybe(*choices): return group(*choices) + '?'\r
-\r
-Whitespace = r'[ \f\t]*'\r
-Comment = r'#[^\r\n]*'\r
-Ignore = Whitespace + any(r'\\\r?\n' + Whitespace) + maybe(Comment)\r
-Name = r'[a-zA-Z_]\w*'\r
-\r
-Binnumber = r'0[bB][01]*'\r
-Hexnumber = r'0[xX][\da-fA-F]*[lL]?'\r
-Octnumber = r'0[oO]?[0-7]*[lL]?'\r
-Decnumber = r'[1-9]\d*[lL]?'\r
-Intnumber = group(Binnumber, Hexnumber, Octnumber, Decnumber)\r
-Exponent = r'[eE][-+]?\d+'\r
-Pointfloat = group(r'\d+\.\d*', r'\.\d+') + maybe(Exponent)\r
-Expfloat = r'\d+' + Exponent\r
-Floatnumber = group(Pointfloat, Expfloat)\r
-Imagnumber = group(r'\d+[jJ]', Floatnumber + r'[jJ]')\r
-Number = group(Imagnumber, Floatnumber, Intnumber)\r
-\r
-# Tail end of ' string.\r
-Single = r"[^'\\]*(?:\\.[^'\\]*)*'"\r
-# Tail end of " string.\r
-Double = r'[^"\\]*(?:\\.[^"\\]*)*"'\r
-# Tail end of ''' string.\r
-Single3 = r"[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"\r
-# Tail end of """ string.\r
-Double3 = r'[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'\r
-Triple = group("[ubUB]?[rR]?'''", '[ubUB]?[rR]?"""')\r
-# Single-line ' or " string.\r
-String = group(r"[uU]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*'",\r
-               r'[uU]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*"')\r
-\r
-# Because of leftmost-then-longest match semantics, be sure to put the\r
-# longest operators first (e.g., if = came before ==, == would get\r
-# recognized as two instances of =).\r
-Operator = group(r"\*\*=?", r">>=?", r"<<=?", r"<>", r"!=",\r
-                 r"//=?", r"->",\r
-                 r"[+\-*/%&|^=<>]=?",\r
-                 r"~")\r
-\r
-Bracket = '[][(){}]'\r
-Special = group(r'\r?\n', r'[:;.,`@]')\r
-Funny = group(Operator, Bracket, Special)\r
-\r
-PlainToken = group(Number, Funny, String, Name)\r
-Token = Ignore + PlainToken\r
-\r
-# First (or only) line of ' or " string.\r
-ContStr = group(r"[uUbB]?[rR]?'[^\n'\\]*(?:\\.[^\n'\\]*)*" +\r
-                group("'", r'\\\r?\n'),\r
-                r'[uUbB]?[rR]?"[^\n"\\]*(?:\\.[^\n"\\]*)*' +\r
-                group('"', r'\\\r?\n'))\r
-PseudoExtras = group(r'\\\r?\n', Comment, Triple)\r
-PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)\r
-\r
-tokenprog, pseudoprog, single3prog, double3prog = map(\r
-    re.compile, (Token, PseudoToken, Single3, Double3))\r
-endprogs = {"'": re.compile(Single), '"': re.compile(Double),\r
-            "'''": single3prog, '"""': double3prog,\r
-            "r'''": single3prog, 'r"""': double3prog,\r
-            "u'''": single3prog, 'u"""': double3prog,\r
-            "b'''": single3prog, 'b"""': double3prog,\r
-            "ur'''": single3prog, 'ur"""': double3prog,\r
-            "br'''": single3prog, 'br"""': double3prog,\r
-            "R'''": single3prog, 'R"""': double3prog,\r
-            "U'''": single3prog, 'U"""': double3prog,\r
-            "B'''": single3prog, 'B"""': double3prog,\r
-            "uR'''": single3prog, 'uR"""': double3prog,\r
-            "Ur'''": single3prog, 'Ur"""': double3prog,\r
-            "UR'''": single3prog, 'UR"""': double3prog,\r
-            "bR'''": single3prog, 'bR"""': double3prog,\r
-            "Br'''": single3prog, 'Br"""': double3prog,\r
-            "BR'''": single3prog, 'BR"""': double3prog,\r
-            'r': None, 'R': None,\r
-            'u': None, 'U': None,\r
-            'b': None, 'B': None}\r
-\r
-triple_quoted = {}\r
-for t in ("'''", '"""',\r
-          "r'''", 'r"""', "R'''", 'R"""',\r
-          "u'''", 'u"""', "U'''", 'U"""',\r
-          "b'''", 'b"""', "B'''", 'B"""',\r
-          "ur'''", 'ur"""', "Ur'''", 'Ur"""',\r
-          "uR'''", 'uR"""', "UR'''", 'UR"""',\r
-          "br'''", 'br"""', "Br'''", 'Br"""',\r
-          "bR'''", 'bR"""', "BR'''", 'BR"""',):\r
-    triple_quoted[t] = t\r
-single_quoted = {}\r
-for t in ("'", '"',\r
-          "r'", 'r"', "R'", 'R"',\r
-          "u'", 'u"', "U'", 'U"',\r
-          "b'", 'b"', "B'", 'B"',\r
-          "ur'", 'ur"', "Ur'", 'Ur"',\r
-          "uR'", 'uR"', "UR'", 'UR"',\r
-          "br'", 'br"', "Br'", 'Br"',\r
-          "bR'", 'bR"', "BR'", 'BR"', ):\r
-    single_quoted[t] = t\r
-\r
-tabsize = 8\r
-\r
-class TokenError(Exception): pass\r
-\r
-class StopTokenizing(Exception): pass\r
-\r
-def printtoken(type, token, start, end, line): # for testing\r
-    (srow, scol) = start\r
-    (erow, ecol) = end\r
-    print "%d,%d-%d,%d:\t%s\t%s" % \\r
-        (srow, scol, erow, ecol, tok_name[type], repr(token))\r
-\r
-def tokenize(readline, tokeneater=printtoken):\r
-    """\r
-    The tokenize() function accepts two parameters: one representing the\r
-    input stream, and one providing an output mechanism for tokenize().\r
-\r
-    The first parameter, readline, must be a callable object which provides\r
-    the same interface as the readline() method of built-in file objects.\r
-    Each call to the function should return one line of input as a string.\r
-\r
-    The second parameter, tokeneater, must also be a callable object. It is\r
-    called once for each token, with five arguments, corresponding to the\r
-    tuples generated by generate_tokens().\r
-    """\r
-    try:\r
-        tokenize_loop(readline, tokeneater)\r
-    except StopTokenizing:\r
-        pass\r
-\r
-# backwards compatible interface\r
-def tokenize_loop(readline, tokeneater):\r
-    for token_info in generate_tokens(readline):\r
-        tokeneater(*token_info)\r
-\r
-class Untokenizer:\r
-\r
-    def __init__(self):\r
-        self.tokens = []\r
-        self.prev_row = 1\r
-        self.prev_col = 0\r
-\r
-    def add_whitespace(self, start):\r
-        row, col = start\r
-        assert row <= self.prev_row\r
-        col_offset = col - self.prev_col\r
-        if col_offset:\r
-            self.tokens.append(" " * col_offset)\r
-\r
-    def untokenize(self, iterable):\r
-        for t in iterable:\r
-            if len(t) == 2:\r
-                self.compat(t, iterable)\r
-                break\r
-            tok_type, token, start, end, line = t\r
-            self.add_whitespace(start)\r
-            self.tokens.append(token)\r
-            self.prev_row, self.prev_col = end\r
-            if tok_type in (NEWLINE, NL):\r
-                self.prev_row += 1\r
-                self.prev_col = 0\r
-        return "".join(self.tokens)\r
-\r
-    def compat(self, token, iterable):\r
-        startline = False\r
-        indents = []\r
-        toks_append = self.tokens.append\r
-        toknum, tokval = token\r
-        if toknum in (NAME, NUMBER):\r
-            tokval += ' '\r
-        if toknum in (NEWLINE, NL):\r
-            startline = True\r
-        for tok in iterable:\r
-            toknum, tokval = tok[:2]\r
-\r
-            if toknum in (NAME, NUMBER):\r
-                tokval += ' '\r
-\r
-            if toknum == INDENT:\r
-                indents.append(tokval)\r
-                continue\r
-            elif toknum == DEDENT:\r
-                indents.pop()\r
-                continue\r
-            elif toknum in (NEWLINE, NL):\r
-                startline = True\r
-            elif startline and indents:\r
-                toks_append(indents[-1])\r
-                startline = False\r
-            toks_append(tokval)\r
-\r
-cookie_re = re.compile("coding[:=]\s*([-\w.]+)")\r
-\r
-def _get_normal_name(orig_enc):\r
-    """Imitates get_normal_name in tokenizer.c."""\r
-    # Only care about the first 12 characters.\r
-    enc = orig_enc[:12].lower().replace("_", "-")\r
-    if enc == "utf-8" or enc.startswith("utf-8-"):\r
-        return "utf-8"\r
-    if enc in ("latin-1", "iso-8859-1", "iso-latin-1") or \\r
-       enc.startswith(("latin-1-", "iso-8859-1-", "iso-latin-1-")):\r
-        return "iso-8859-1"\r
-    return orig_enc\r
-\r
-def detect_encoding(readline):\r
-    """\r
-    The detect_encoding() function is used to detect the encoding that should\r
-    be used to decode a Python source file. It requires one argment, readline,\r
-    in the same way as the tokenize() generator.\r
-\r
-    It will call readline a maximum of twice, and return the encoding used\r
-    (as a string) and a list of any lines (left as bytes) it has read\r
-    in.\r
-\r
-    It detects the encoding from the presence of a utf-8 bom or an encoding\r
-    cookie as specified in pep-0263. If both a bom and a cookie are present, but\r
-    disagree, a SyntaxError will be raised. If the encoding cookie is an invalid\r
-    charset, raise a SyntaxError.  Note that if a utf-8 bom is found,\r
-    'utf-8-sig' is returned.\r
-\r
-    If no encoding is specified, then the default of 'utf-8' will be returned.\r
-    """\r
-    bom_found = False\r
-    encoding = None\r
-    default = 'utf-8'\r
-    def read_or_stop():\r
-        try:\r
-            return readline()\r
-        except StopIteration:\r
-            return bytes()\r
-\r
-    def find_cookie(line):\r
-        try:\r
-            line_string = line.decode('ascii')\r
-        except UnicodeDecodeError:\r
-            return None\r
-\r
-        matches = cookie_re.findall(line_string)\r
-        if not matches:\r
-            return None\r
-        encoding = _get_normal_name(matches[0])\r
-        try:\r
-            codec = lookup(encoding)\r
-        except LookupError:\r
-            # This behaviour mimics the Python interpreter\r
-            raise SyntaxError("unknown encoding: " + encoding)\r
-\r
-        if bom_found:\r
-            if codec.name != 'utf-8':\r
-                # This behaviour mimics the Python interpreter\r
-                raise SyntaxError('encoding problem: utf-8')\r
-            encoding += '-sig'\r
-        return encoding\r
-\r
-    first = read_or_stop()\r
-    if first.startswith(BOM_UTF8):\r
-        bom_found = True\r
-        first = first[3:]\r
-        default = 'utf-8-sig'\r
-    if not first:\r
-        return default, []\r
-\r
-    encoding = find_cookie(first)\r
-    if encoding:\r
-        return encoding, [first]\r
-\r
-    second = read_or_stop()\r
-    if not second:\r
-        return default, [first]\r
-\r
-    encoding = find_cookie(second)\r
-    if encoding:\r
-        return encoding, [first, second]\r
-\r
-    return default, [first, second]\r
-\r
-def untokenize(iterable):\r
-    """Transform tokens back into Python source code.\r
-\r
-    Each element returned by the iterable must be a token sequence\r
-    with at least two elements, a token number and token value.  If\r
-    only two tokens are passed, the resulting output is poor.\r
-\r
-    Round-trip invariant for full input:\r
-        Untokenized source will match input source exactly\r
-\r
-    Round-trip invariant for limited intput:\r
-        # Output text will tokenize the back to the input\r
-        t1 = [tok[:2] for tok in generate_tokens(f.readline)]\r
-        newcode = untokenize(t1)\r
-        readline = iter(newcode.splitlines(1)).next\r
-        t2 = [tok[:2] for tokin generate_tokens(readline)]\r
-        assert t1 == t2\r
-    """\r
-    ut = Untokenizer()\r
-    return ut.untokenize(iterable)\r
-\r
-def generate_tokens(readline):\r
-    """\r
-    The generate_tokens() generator requires one argment, readline, which\r
-    must be a callable object which provides the same interface as the\r
-    readline() method of built-in file objects. Each call to the function\r
-    should return one line of input as a string.  Alternately, readline\r
-    can be a callable function terminating with StopIteration:\r
-        readline = open(myfile).next    # Example of alternate readline\r
-\r
-    The generator produces 5-tuples with these members: the token type; the\r
-    token string; a 2-tuple (srow, scol) of ints specifying the row and\r
-    column where the token begins in the source; a 2-tuple (erow, ecol) of\r
-    ints specifying the row and column where the token ends in the source;\r
-    and the line on which the token was found. The line passed is the\r
-    logical line; continuation lines are included.\r
-    """\r
-    lnum = parenlev = continued = 0\r
-    namechars, numchars = string.ascii_letters + '_', '0123456789'\r
-    contstr, needcont = '', 0\r
-    contline = None\r
-    indents = [0]\r
-\r
-    while 1:                                   # loop over lines in stream\r
-        try:\r
-            line = readline()\r
-        except StopIteration:\r
-            line = ''\r
-        lnum = lnum + 1\r
-        pos, max = 0, len(line)\r
-\r
-        if contstr:                            # continued string\r
-            if not line:\r
-                raise TokenError, ("EOF in multi-line string", strstart)\r
-            endmatch = endprog.match(line)\r
-            if endmatch:\r
-                pos = end = endmatch.end(0)\r
-                yield (STRING, contstr + line[:end],\r
-                       strstart, (lnum, end), contline + line)\r
-                contstr, needcont = '', 0\r
-                contline = None\r
-            elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':\r
-                yield (ERRORTOKEN, contstr + line,\r
-                           strstart, (lnum, len(line)), contline)\r
-                contstr = ''\r
-                contline = None\r
-                continue\r
-            else:\r
-                contstr = contstr + line\r
-                contline = contline + line\r
-                continue\r
-\r
-        elif parenlev == 0 and not continued:  # new statement\r
-            if not line: break\r
-            column = 0\r
-            while pos < max:                   # measure leading whitespace\r
-                if line[pos] == ' ': column = column + 1\r
-                elif line[pos] == '\t': column = (column//tabsize + 1)*tabsize\r
-                elif line[pos] == '\f': column = 0\r
-                else: break\r
-                pos = pos + 1\r
-            if pos == max: break\r
-\r
-            if line[pos] in '#\r\n':           # skip comments or blank lines\r
-                if line[pos] == '#':\r
-                    comment_token = line[pos:].rstrip('\r\n')\r
-                    nl_pos = pos + len(comment_token)\r
-                    yield (COMMENT, comment_token,\r
-                           (lnum, pos), (lnum, pos + len(comment_token)), line)\r
-                    yield (NL, line[nl_pos:],\r
-                           (lnum, nl_pos), (lnum, len(line)), line)\r
-                else:\r
-                    yield ((NL, COMMENT)[line[pos] == '#'], line[pos:],\r
-                           (lnum, pos), (lnum, len(line)), line)\r
-                continue\r
-\r
-            if column > indents[-1]:           # count indents or dedents\r
-                indents.append(column)\r
-                yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)\r
-            while column < indents[-1]:\r
-                if column not in indents:\r
-                    raise IndentationError(\r
-                        "unindent does not match any outer indentation level",\r
-                        ("<tokenize>", lnum, pos, line))\r
-                indents = indents[:-1]\r
-                yield (DEDENT, '', (lnum, pos), (lnum, pos), line)\r
-\r
-        else:                                  # continued statement\r
-            if not line:\r
-                raise TokenError, ("EOF in multi-line statement", (lnum, 0))\r
-            continued = 0\r
-\r
-        while pos < max:\r
-            pseudomatch = pseudoprog.match(line, pos)\r
-            if pseudomatch:                                # scan for tokens\r
-                start, end = pseudomatch.span(1)\r
-                spos, epos, pos = (lnum, start), (lnum, end), end\r
-                token, initial = line[start:end], line[start]\r
-\r
-                if initial in numchars or \\r
-                   (initial == '.' and token != '.'):      # ordinary number\r
-                    yield (NUMBER, token, spos, epos, line)\r
-                elif initial in '\r\n':\r
-                    newline = NEWLINE\r
-                    if parenlev > 0:\r
-                        newline = NL\r
-                    yield (newline, token, spos, epos, line)\r
-                elif initial == '#':\r
-                    assert not token.endswith("\n")\r
-                    yield (COMMENT, token, spos, epos, line)\r
-                elif token in triple_quoted:\r
-                    endprog = endprogs[token]\r
-                    endmatch = endprog.match(line, pos)\r
-                    if endmatch:                           # all on one line\r
-                        pos = endmatch.end(0)\r
-                        token = line[start:pos]\r
-                        yield (STRING, token, spos, (lnum, pos), line)\r
-                    else:\r
-                        strstart = (lnum, start)           # multiple lines\r
-                        contstr = line[start:]\r
-                        contline = line\r
-                        break\r
-                elif initial in single_quoted or \\r
-                    token[:2] in single_quoted or \\r
-                    token[:3] in single_quoted:\r
-                    if token[-1] == '\n':                  # continued string\r
-                        strstart = (lnum, start)\r
-                        endprog = (endprogs[initial] or endprogs[token[1]] or\r
-                                   endprogs[token[2]])\r
-                        contstr, needcont = line[start:], 1\r
-                        contline = line\r
-                        break\r
-                    else:                                  # ordinary string\r
-                        yield (STRING, token, spos, epos, line)\r
-                elif initial in namechars:                 # ordinary name\r
-                    yield (NAME, token, spos, epos, line)\r
-                elif initial == '\\':                      # continued stmt\r
-                    # This yield is new; needed for better idempotency:\r
-                    yield (NL, token, spos, (lnum, pos), line)\r
-                    continued = 1\r
-                else:\r
-                    if initial in '([{': parenlev = parenlev + 1\r
-                    elif initial in ')]}': parenlev = parenlev - 1\r
-                    yield (OP, token, spos, epos, line)\r
-            else:\r
-                yield (ERRORTOKEN, line[pos],\r
-                           (lnum, pos), (lnum, pos+1), line)\r
-                pos = pos + 1\r
-\r
-    for indent in indents[1:]:                 # pop remaining indent levels\r
-        yield (DEDENT, '', (lnum, 0), (lnum, 0), '')\r
-    yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')\r
-\r
-if __name__ == '__main__':                     # testing\r
-    import sys\r
-    if len(sys.argv) > 1: tokenize(open(sys.argv[1]).readline)\r
-    else: tokenize(sys.stdin.readline)\r