Skip to content

Commit

Permalink
Merge pull request #7505 from dalthviz/fixes_issue_7324
Browse files Browse the repository at this point in the history
PR: Add support for numeric literals with underscores highlighting in Python 3.6
  • Loading branch information
ccordoba12 authored Jul 19, 2018
2 parents 703928e + 025c12f commit d8897f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
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

0 comments on commit d8897f6

Please sign in to comment.