ThirdParty/Pygments/pygments/lexers/python.py

changeset 4697
c2e9bf425554
parent 4172
4f20dba37ab6
child 5072
aab59042fefb
--- a/ThirdParty/Pygments/pygments/lexers/python.py	Sun Jan 24 16:15:58 2016 +0100
+++ b/ThirdParty/Pygments/pygments/lexers/python.py	Sun Jan 24 19:28:37 2016 +0100
@@ -5,7 +5,7 @@
 
     Lexers for Python and related languages.
 
-    :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
+    :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
 
@@ -35,13 +35,27 @@
     filenames = ['*.py', '*.pyw', '*.sc', 'SConstruct', 'SConscript', '*.tac', '*.sage']
     mimetypes = ['text/x-python', 'application/x-python']
 
+    def innerstring_rules(ttype):
+        return [
+            # the old style '%s' % (...) string formatting
+            (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
+             '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol),
+            # backslashes, quotes and formatting signs must be parsed one at a time
+            (r'[^\\\'"%\n]+', ttype),
+            (r'[\'"\\]', ttype),
+            # unhandled string formatting sign
+            (r'%', ttype),
+            # newlines are an error (use "nl" state)
+        ]
+
     tokens = {
         'root': [
             (r'\n', Text),
             (r'^(\s*)([rRuU]{,2}"""(?:.|\n)*?""")', bygroups(Text, String.Doc)),
             (r"^(\s*)([rRuU]{,2}'''(?:.|\n)*?''')", bygroups(Text, String.Doc)),
             (r'[^\S\n]+', Text),
-            (r'#.*$', Comment),
+            (r'\A#!.+$', Comment.Hashbang),
+            (r'#.*$', Comment.Single),
             (r'[]{}:(),;[]', Punctuation),
             (r'\\\n', Text),
             (r'\\', Text),
@@ -56,14 +70,14 @@
              'import'),
             include('builtins'),
             include('backtick'),
-            ('(?:[rR]|[uU][rR]|[rR][uU])"""', String, 'tdqs'),
-            ("(?:[rR]|[uU][rR]|[rR][uU])'''", String, 'tsqs'),
-            ('(?:[rR]|[uU][rR]|[rR][uU])"', String, 'dqs'),
-            ("(?:[rR]|[uU][rR]|[rR][uU])'", String, 'sqs'),
-            ('[uU]?"""', String, combined('stringescape', 'tdqs')),
-            ("[uU]?'''", String, combined('stringescape', 'tsqs')),
-            ('[uU]?"', String, combined('stringescape', 'dqs')),
-            ("[uU]?'", String, combined('stringescape', 'sqs')),
+            ('(?:[rR]|[uU][rR]|[rR][uU])"""', String.Double, 'tdqs'),
+            ("(?:[rR]|[uU][rR]|[rR][uU])'''", String.Single, 'tsqs'),
+            ('(?:[rR]|[uU][rR]|[rR][uU])"', String.Double, 'dqs'),
+            ("(?:[rR]|[uU][rR]|[rR][uU])'", String.Single, 'sqs'),
+            ('[uU]?"""', String.Double, combined('stringescape', 'tdqs')),
+            ("[uU]?'''", String.Single, combined('stringescape', 'tsqs')),
+            ('[uU]?"', String.Double, combined('stringescape', 'dqs')),
+            ("[uU]?'", String.Single, combined('stringescape', 'sqs')),
             include('name'),
             include('numbers'),
         ],
@@ -154,38 +168,27 @@
             (r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
              r'U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})', String.Escape)
         ],
-        'strings': [
-            (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
-             '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol),
-            (r'[^\\\'"%\n]+', String),
-            # quotes, percents and backslashes must be parsed one at a time
-            (r'[\'"\\]', String),
-            # unhandled string formatting sign
-            (r'%', String)
-            # newlines are an error (use "nl" state)
-        ],
-        'nl': [
-            (r'\n', String)
-        ],
+        'strings-single': innerstring_rules(String.Single),
+        'strings-double': innerstring_rules(String.Double),
         'dqs': [
-            (r'"', String, '#pop'),
+            (r'"', String.Double, '#pop'),
             (r'\\\\|\\"|\\\n', String.Escape),  # included here for raw strings
-            include('strings')
+            include('strings-double')
         ],
         'sqs': [
-            (r"'", String, '#pop'),
+            (r"'", String.Single, '#pop'),
             (r"\\\\|\\'|\\\n", String.Escape),  # included here for raw strings
-            include('strings')
+            include('strings-single')
         ],
         'tdqs': [
-            (r'"""', String, '#pop'),
-            include('strings'),
-            include('nl')
+            (r'"""', String.Double, '#pop'),
+            include('strings-double'),
+            (r'\n', String.Double)
         ],
         'tsqs': [
-            (r"'''", String, '#pop'),
-            include('strings'),
-            include('nl')
+            (r"'''", String.Single, '#pop'),
+            include('strings-single'),
+            (r'\n', String.Single)
         ],
     }
 
@@ -213,11 +216,14 @@
     tokens = PythonLexer.tokens.copy()
     tokens['keywords'] = [
         (words((
-            'assert', 'break', 'continue', 'del', 'elif', 'else', 'except',
-            'finally', 'for', 'global', 'if', 'lambda', 'pass', 'raise',
-            'nonlocal', 'return', 'try', 'while', 'yield', 'yield from', 'as',
-            'with', 'True', 'False', 'None'), suffix=r'\b'),
+            'assert', 'async', 'await', 'break', 'continue', 'del', 'elif',
+            'else', 'except', 'finally', 'for', 'global', 'if', 'lambda', 'pass',
+            'raise', 'nonlocal', 'return', 'try', 'while', 'yield', 'yield from',
+            'as', 'with'), suffix=r'\b'),
          Keyword),
+        (words((
+            'True', 'False', 'None'), suffix=r'\b'),
+         Keyword.Constant),
     ]
     tokens['builtins'] = [
         (words((
@@ -241,7 +247,7 @@
             'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
             'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
             'NotImplementedError', 'OSError', 'OverflowError',
-            'PendingDeprecationWarning', 'ReferenceError',
+            'PendingDeprecationWarning', 'ReferenceError', 'ResourceWarning',
             'RuntimeError', 'RuntimeWarning', 'StopIteration',
             'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
             'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
@@ -267,6 +273,7 @@
     tokens['backtick'] = []
     tokens['name'] = [
         (r'@\w+', Name.Decorator),
+        (r'@', Operator),  # new matrix multiplication operator
         (uni_name, Name),
     ]
     tokens['funcname'] = [
@@ -288,13 +295,21 @@
         (uni_name, Name.Namespace),
         default('#pop'),
     ]
-    # don't highlight "%s" substitutions
     tokens['strings'] = [
-        (r'[^\\\'"%\n]+', String),
-        # quotes, percents and backslashes must be parsed one at a time
+        # the old style '%s' % (...) string formatting (still valid in Py3)
+        (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?'
+         '[hlL]?[diouxXeEfFgGcrs%]', String.Interpol),
+        # the new style '{}'.format(...) string formatting
+        (r'\{'
+         '((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name
+         '(\![sra])?'                      # conversion
+         '(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[bcdeEfFgGnosxX%]?)?'
+         '\}', String.Interpol),
+        # backslashes, quotes and formatting signs must be parsed one at a time
+        (r'[^\\\'"%\{\n]+', String),
         (r'[\'"\\]', String),
         # unhandled string formatting sign
-        (r'%', String)
+        (r'%|(\{{1,2})', String)
         # newlines are an error (use "nl" state)
     ]
 
@@ -404,8 +419,10 @@
 
     tokens = {
         'root': [
-            (r'^Traceback \(most recent call last\):\n',
-             Generic.Traceback, 'intb'),
+            # Cover both (most recent call last) and (innermost last)
+            # The optional ^C allows us to catch keyboard interrupt signals.
+            (r'^(\^C)?(Traceback.*\n)',
+             bygroups(Text, Generic.Traceback), 'intb'),
             # SyntaxError starts with this.
             (r'^(?=  File "[^"]+", line \d+)', Generic.Traceback, 'intb'),
             (r'^.*\n', Other),
@@ -498,6 +515,8 @@
             include('keywords'),
             (r'(def|property)(\s+)', bygroups(Keyword, Text), 'funcname'),
             (r'(cp?def)(\s+)', bygroups(Keyword, Text), 'cdef'),
+            # (should actually start a block with only cdefs)
+            (r'(cdef)(:)', bygroups(Keyword, Punctuation)),
             (r'(class|struct)(\s+)', bygroups(Keyword, Text), 'classname'),
             (r'(from)(\s+)', bygroups(Keyword, Text), 'fromimport'),
             (r'(c?import)(\s+)', bygroups(Keyword, Text), 'import'),
@@ -517,7 +536,7 @@
         'keywords': [
             (words((
                 'assert', 'break', 'by', 'continue', 'ctypedef', 'del', 'elif',
-                'else', 'except', 'except?', 'exec', 'finally', 'for', 'gil',
+                'else', 'except', 'except?', 'exec', 'finally', 'for', 'fused', 'gil',
                 'global', 'if', 'include', 'lambda', 'nogil', 'pass', 'print',
                 'raise', 'return', 'try', 'while', 'yield', 'as', 'with'), suffix=r'\b'),
              Keyword),
@@ -535,7 +554,7 @@
                 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'property',
                 'range', 'raw_input', 'reduce', 'reload', 'repr', 'reversed',
                 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod',
-                'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode',
+                'str', 'sum', 'super', 'tuple', 'type', 'unichr', 'unicode', 'unsigned',
                 'vars', 'xrange', 'zip'), prefix=r'(?<!\.)', suffix=r'\b'),
              Name.Builtin),
             (r'(?<!\.)(self|None|Ellipsis|NotImplemented|False|True|NULL'
@@ -543,13 +562,14 @@
             (words((
                 'ArithmeticError', 'AssertionError', 'AttributeError',
                 'BaseException', 'DeprecationWarning', 'EOFError', 'EnvironmentError',
-                'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError',
-                'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
-                'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
-                'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError',
-                'OverflowWarning', 'PendingDeprecationWarning', 'ReferenceError',
-                'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
-                'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
+                'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit',
+                'IOError', 'ImportError', 'ImportWarning', 'IndentationError',
+                'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
+                'MemoryError', 'NameError', 'NotImplemented', 'NotImplementedError',
+                'OSError', 'OverflowError', 'OverflowWarning',
+                'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError',
+                'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError',
+                'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError',
                 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError',
                 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError',
                 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning',

eric ide

mercurial