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

[Python] Match unpacking operators #879

Merged
merged 2 commits into from
Apr 5, 2017
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
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 @@ -553,11 +565,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 @@ -598,29 +606,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 @@ -636,6 +667,7 @@ contexts:
set: after-expression
- match: ','
scope: punctuation.separator.list.python
push: allow-unpack-operators
- include: inline-for
- include: expressions

Expand All @@ -655,6 +687,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 @@ -794,20 +827,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