Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add support for numeric literals with underscores highlighting in Python 3.6 #7505

Merged
merged 3 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spyder/py3compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

PY2 = sys.version[0] == '2'
PY3 = sys.version[0] == '3'
PY36_OR_MORE = sys.version_info[0] >= 3 and sys.version_info[1] >= 6

#==============================================================================
# Data types
Expand Down
33 changes: 26 additions & 7 deletions spyder/utils/syntaxhighlighters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from spyder import dependencies
from spyder.config.base import _
from spyder.config.main import CONF
from spyder.py3compat import builtins, is_text_string, to_text_string
from spyder.py3compat import (builtins, is_text_string, to_text_string,
PY36_OR_MORE)
from spyder.utils.sourcecode import CELL_LANGUAGES
from spyder.utils.workers import WorkerManager

Expand Down Expand Up @@ -294,12 +295,30 @@ def make_python_patterns(additional_keywords=[], additional_builtins=[]):
r"\bcls\b",
(r"^\s*@([a-zA-Z_][a-zA-Z0-9_]*)"
r"(\.[a-zA-Z_][a-zA-Z0-9_]*)*")])
number = any("number",
[r"\b[+-]?[0-9]+[lLjJ]?\b",
r"\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b",
r"\b[+-]?0[oO][0-7]+[lL]?\b",
r"\b[+-]?0[bB][01]+[lL]?\b",
r"\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?[jJ]?\b"])
number_regex = [r"\b[+-]?[0-9]+[lLjJ]?\b",
r"\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b",
r"\b[+-]?0[oO][0-7]+[lL]?\b",
r"\b[+-]?0[bB][01]+[lL]?\b",
r"\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?[jJ]?\b"]
# Needed to achieve correct highlighting in Python 3.6+
# See issue 7324
if PY36_OR_MORE:
# Based on
# https://github.com/python/cpython/blob/
# 81950495ba2c36056e0ce48fd37d514816c26747/Lib/tokenize.py#L117
# In order: Hexnumber, Binnumber, Octnumber, Decnumber,
# Pointfloat + Exponent, Expfloat, Imagnumber
number_regex = [
r"\b[+-]?0[xX](?:_?[0-9A-Fa-f])+[lL]?\b",
r"\b[+-]?0[bB](?:_?[01])+[lL]?\b",
r"\b[+-]?0[oO](?:_?[0-7])+[lL]?\b",
r"\b[+-]?(?:0(?:_?0)*|[1-9](?:_?[0-9])*)[lL]?\b",
r"\b((\.[0-9](?:_?[0-9])*')|\.[0-9](?:_?[0-9])*)"
"([eE][+-]?[0-9](?:_?[0-9])*)?[jJ]?\b",
r"\b[0-9](?:_?[0-9])*([eE][+-]?[0-9](?:_?[0-9])*)?[jJ]?\b",
r"\b[0-9](?:_?[0-9])*[jJ]\b"]
number = any("number", number_regex)

sqstring = r"(\b[rRuU])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
dqstring = r'(\b[rRuU])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
uf_sqstring = r"(\b[rRuU])?'[^'\\\n]*(\\.[^'\\\n]*)*(\\)$(?!')$"
Expand Down