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

Support PEP 3104, Access to Names in Outer Scopes #112

Merged
merged 1 commit into from
Sep 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
1 change: 1 addition & 0 deletions baron/formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class UnExpectedSpaceToken(BaronError):
"RAISE",
"EXEC",
"GLOBAL",
"NONLOCAL",
"PRINT",
"INDENT",
"WHILE",
Expand Down
1 change: 1 addition & 0 deletions baron/grammator.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def simple_stmt_semicolon(pack):
@pg.production("small_stmt : assert_stmt")
@pg.production("small_stmt : raise_stmt")
@pg.production("small_stmt : global_stmt")
@pg.production("small_stmt : nonlocal_stmt")
@pg.production("compound_stmt : if_stmt")
@pg.production("compound_stmt : while_stmt")
@pg.production("compound_stmt : for_stmt")
Expand Down
10 changes: 10 additions & 0 deletions baron/grammator_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ def global_stmt(pack):
}


@pg.production("nonlocal_stmt : NONLOCAL names")
def nonlocal_stmt(pack):
(token, names) = pack
return {
"type": "nonlocal",
"formatting": token.hidden_tokens_after,
"value": names,
}


@pg.production("names : NAME")
def names_name(pack):
(name,) = pack
Expand Down
6 changes: 5 additions & 1 deletion baron/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ def child_by_key(node, key):
("formatting", "second_formatting", "as"),
("key", "as", "as"),
],

"nonlocal": [
("constant", "nonlocal", True),
("formatting", "formatting", True),
("list", "value", True),
],
"del": [
("constant", "del", True),
("formatting", "formatting", True),
Expand Down
2 changes: 1 addition & 1 deletion baron/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class UnknowItem(BaronError):
pass

KEYWORDS = ("and", "as", "assert", "break", "class", "continue", "def", "del", "elif", "else", "except", "exec", "finally", "for", "from", "global", "if", "import", "in", "is", "lambda", "not", "or", "pass", "print", "raise", "return", "try", "while", "with", "yield")
KEYWORDS = ("and", "as", "assert", "break", "class", "continue", "def", "del", "elif", "else", "except", "exec", "finally", "for", "from", "global", "nonlocal", "if", "import", "in", "is", "lambda", "not", "or", "pass", "print", "raise", "return", "try", "while", "with", "yield")

TOKENS = (
(r'[a-zA-Z_]\w*', 'NAME'),
Expand Down
12 changes: 12 additions & 0 deletions tests/test_formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,18 @@ def test_global_two():
])


def test_nonlocal():
"global a"
group([
('NONLOCAL', 'nonlocal'),
('SPACE', ' '),
('NAME', 'a'),
], [
('NONLOCAL', 'nonlocal', [], [('SPACE', ' ')]),
('NAME', 'a'),
])


def test_print():
"print"
group([
Expand Down