Skip to content

Commit

Permalink
Merge pull request #879 from FichteForks/python_unpacking-operators
Browse files Browse the repository at this point in the history
[Python] Match unpacking operators
  • Loading branch information
wbond authored Apr 5, 2017
2 parents b506d85 + c94e370 commit c786b73
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 44 deletions.
103 changes: 61 additions & 42 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,7 @@ contexts:
- match: \)
scope: punctuation.section.arguments.end.python
pop: true
- include: keyword-arguments
- match: ','
scope: punctuation.separator.parameters.python
- include: inline-for
- include: expressions
- include: arguments
# item access
- match: '\s*(\[)'
captures:
Expand Down Expand Up @@ -395,6 +391,21 @@ contexts:
- match: '@'
scope: keyword.operator.matrix.python

allow-unpack-operators:
# Match unpacking operators, if present
- include: comments
- match: \*{3,}
scope: invalid.illegal.syntax.python
pop: true
- match: \*\*
scope: keyword.operator.unpacking.mapping.python
pop: true
- match: \*
scope: keyword.operator.unpacking.sequence.python
pop: true
- match: (?!\s|#)
pop: true

classes:
- match: '^\s*(class)\b'
captures:
Expand Down Expand Up @@ -465,7 +476,7 @@ contexts:
set:
- match: \(
scope: meta.function.parameters.python punctuation.section.parameters.begin.python
set: function-parameters
set: [function-parameters, allow-unpack-operators]

function-parameters:
- meta_content_scope: meta.function.parameters.python
Expand All @@ -475,14 +486,15 @@ contexts:
- include: comments
- match: ','
scope: punctuation.separator.parameters.python
push: allow-unpack-operators
- match: '(?==)'
set:
- match: '='
scope: keyword.operator.assignment.python
set:
- meta_scope: meta.function.parameters.default-value.python
- match: '(?=[,)])'
set: function-parameters
set: [function-parameters, allow-unpack-operators]
- include: expressions
- match: '(?=:)'
set:
Expand Down Expand Up @@ -552,11 +564,7 @@ contexts:
- meta_content_scope: meta.annotation.arguments.python
- match: (?=\))
pop: true
- include: keyword-arguments
- match: ','
scope: punctuation.separator.arguments.python
- include: inline-for
- include: expressions
- include: arguments

item-access:
- match: '(?={{path}}\s*\[)'
Expand Down Expand Up @@ -597,29 +605,52 @@ contexts:
- meta_content_scope: meta.function-call.arguments.python
- match: (?=\))
pop: true
- include: keyword-arguments
- match: ','
scope: punctuation.separator.arguments.python
- include: inline-for
- include: expressions
- include: arguments

lambda:
- match: \b(lambda)(?=\s|:|$)
scope: storage.type.function.inline.python
arguments:
- include: keyword-arguments
- match: ','
scope: punctuation.separator.arguments.python
push: allow-unpack-operators
- include: inline-for
- include: expressions

keyword-arguments:
- match: '(?={{identifier}}\s*=(?!=))'
push:
- meta_scope: meta.function.inline.python
- meta_content_scope: meta.function.inline.parameters.python
- include: line-continuation-or-pop
- match: '\:'
scope: punctuation.section.function.begin.python
pop: true
- match: ','
scope: punctuation.separator.parameters.python
- include: keyword-arguments
- match: '='
scope: keyword.operator.assignment.python
set:
- match: (?=\s*[,):])
pop: true
- include: expressions
- include: illegal-names
- match: '{{identifier}}'
scope: variable.parameter.python

lambda:
- match: \b(lambda)(?=\s|:|$)
scope: storage.type.function.inline.python
push: [lambda-parameters, allow-unpack-operators]

lambda-parameters:
- meta_scope: meta.function.inline.python
- meta_content_scope: meta.function.inline.parameters.python
- include: line-continuation-or-pop
- match: '\:'
scope: punctuation.section.function.begin.python
pop: true
- match: ','
scope: punctuation.separator.parameters.python
push: allow-unpack-operators
- include: keyword-arguments
- include: illegal-names
- match: '{{identifier}}'
scope: variable.parameter.python
- match: '\S'
scope: invalid.illegal.expected-parameter.python

lists:
- match: '(\[)(\s*(\]))\b'
captures:
Expand All @@ -635,6 +666,7 @@ contexts:
set: after-expression
- match: ','
scope: punctuation.separator.list.python
push: allow-unpack-operators
- include: inline-for
- include: expressions

Expand All @@ -654,6 +686,7 @@ contexts:
set: after-expression
- match: ','
scope: punctuation.separator.dictionary-or-set.python
push: allow-unpack-operators
- match: ':'
scope: punctuation.separator.key-value.python
- include: inline-for
Expand Down Expand Up @@ -793,20 +826,6 @@ contexts:
scope: invalid.illegal.name.python
pop: true

keyword-arguments:
- match: '(?={{identifier}}\s*=(?!=))'
push:
- include: line-continuation-or-pop
- match: '='
scope: keyword.operator.assignment.python
set:
- match: (?=\s*[,):])
pop: true
- include: expressions
- include: illegal-names
- match: '{{identifier}}'
scope: variable.parameter.python

language-variables:
- match: \b(self|cls)\b
scope: variable.language.python
Expand Down
33 changes: 31 additions & 2 deletions Python/syntax_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,19 @@
# ^ punctuation.accessor.dot
# ^^^^^^^^^ - support
# ^^^^ variable.function
#

call(2**10, *range(10), **dict(), * *{}, ***a)
# ^^ keyword.operator.arithmetic
# ^ keyword.operator.unpacking.sequence.python
# ^^ keyword.operator.unpacking.mapping.python
# ^ keyword.operator.unpacking.sequence.python
# ^ - keyword.operator.unpacking
# ^^^ invalid.illegal.syntax.python

if p.type not in ('NUMBER', 'INTEGER'):
# ^^ keyword.operator - meta.function-call invalid

call(from='no')
call(from='no', from_='yes')
#^^^^^^^^^^^^^^ meta.function-call
# ^^^^ invalid.illegal.name
# ^ keyword.operator.assignment
Expand Down Expand Up @@ -186,6 +193,14 @@ def _():
# ^^ invalid.illegal.name
# ^^ invalid.illegal.name

lambda *a, **kwa, ab*, * *: (a, kwa)
# ^ keyword.operator.unpacking.sequence.python
# ^ variable.parameter.python
# ^^^ variable.parameter.python
# ^^ keyword.operator.unpacking.mapping.python
# ^ invalid.illegal.expected-parameter.python
# ^ invalid.illegal.expected-parameter.python

lambda
# ^^^^^^ storage.type.function.inline

Expand Down Expand Up @@ -413,6 +428,11 @@ async def coroutine(param1):
# ^ entity.name.function
pass

def func(*args, other_arg=2**10, **kwargs):
# ^ keyword.operator.unpacking.sequence.python
# ^^ keyword.operator.arithmetic.python
# ^^ keyword.operator.unpacking.mapping.python
pass


##################
Expand Down Expand Up @@ -666,6 +686,15 @@ def f(): pass
# ^^^^^ keyword.other.await.python


l = [1 * 2, 2**10, *result]
# ^ keyword.operator.arithmetic.python
# ^^ keyword.operator.arithmetic.python
# ^ keyword.operator.unpacking.sequence.python

d = {1: 3**4, **dict_}
# ^^ keyword.operator.arithmetic.python
# ^^ keyword.operator.unpacking.mapping.python

##################
# Exception handling
##################
Expand Down

0 comments on commit c786b73

Please sign in to comment.