summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiikka Salminen <miikka.salminen@gmail.com>2016-01-18 18:05:05 +0200
committerMiikka Salminen <miikka.salminen@gmail.com>2016-01-18 18:05:05 +0200
commit00ae857e205113d7f265ae58ea0ec7ec4b767c07 (patch)
tree016b56987cf8349bec8262bd62686c090e542a1f
parent0f500f75d258b131707f4601d8010089a570c436 (diff)
downloadpygments-00ae857e205113d7f265ae58ea0ec7ec4b767c07.tar.gz
Fixed new style string interpolation for Python 3.
-rw-r--r--pygments/lexers/python.py39
1 files changed, 22 insertions, 17 deletions
diff --git a/pygments/lexers/python.py b/pygments/lexers/python.py
index c05c8ae0..9433f7fd 100644
--- a/pygments/lexers/python.py
+++ b/pygments/lexers/python.py
@@ -213,6 +213,26 @@ class Python3Lexer(RegexLexer):
uni_name = "[%s][%s]*" % (uni.xid_start, uni.xid_continue)
+ def innerstring_rules(ttype):
+ return [
+ # 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]+', ttype),
+ (r'[\'"\\]', ttype),
+ # unhandled string formatting sign
+ (r'%|(\{{1,2})', ttype)
+ # newlines are an error (use "nl" state)
+ ]
+
tokens = PythonLexer.tokens.copy()
tokens['keywords'] = [
(words((
@@ -295,23 +315,8 @@ class Python3Lexer(RegexLexer):
(uni_name, Name.Namespace),
default('#pop'),
]
- tokens['strings'] = [
- # 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'%|(\{{1,2})', String)
- # newlines are an error (use "nl" state)
- ]
+ tokens['strings-single'] = innerstring_rules(String.Single)
+ tokens['strings-double'] = innerstring_rules(String.Double)
def analyse_text(text):
return shebang_matches(text, r'pythonw?3(\.\d)?')