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 492, Coroutines with async and await syntax #114

Merged
merged 3 commits into from
Jul 28, 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
2 changes: 2 additions & 0 deletions baron/formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class UnExpectedSpaceToken(BaronError):
GROUP_SPACE_AFTER = BOTH + (
"FROM",
"TILDE",
"ASYNC",
"AWAIT",
"RETURN",
"YIELD",
"WITH",
Expand Down
34 changes: 31 additions & 3 deletions baron/grammator.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,37 @@ def simple_stmt_semicolon(pack):
@pg.production("compound_stmt : classdef")
@pg.production("compound_stmt : with_stmt")
@pg.production("compound_stmt : decorated")
@pg.production("compound_stmt : async_stmt")
def small_and_compound_stmt(pack):
(statement,) = pack
return statement


@pg.production("async_maybe : ")
def async__(pack):
return {}


@pg.production("async_maybe : ASYNC")
@pg.production("async : ASYNC")
def async__(pack):
(async_, ) = pack
return {
"type": "async",
"formatting": async_.hidden_tokens_after

}


@pg.production("async_stmt : async with_stmt")
@pg.production("async_stmt : async for_stmt")
@pg.production("async_stmt : async funcdef")
def async_stmt(pack):
(async_, statement,) = pack
statement[0]["async"] = async_
return statement


if not print_function:
@pg.production("small_stmt : print_stmt")
def print_statement(pack):
Expand Down Expand Up @@ -179,6 +206,7 @@ def with_stmt(pack):
(with_, with_items, colon, suite) = pack
return [{
"type": "with",
"async": {},
"value": suite,
"first_formatting": with_.hidden_tokens_after,
"second_formatting": colon.hidden_tokens_before,
Expand Down Expand Up @@ -364,12 +392,12 @@ def decorator_call(pack):
"formatting": at.hidden_tokens_after,
}] + endl


@pg.production("funcdef : DEF NAME LEFT_PARENTHESIS parameters RIGHT_PARENTHESIS COLON suite")
@pg.production("funcdef : async_maybe DEF NAME LEFT_PARENTHESIS parameters RIGHT_PARENTHESIS COLON suite")
def function_definition(pack):
(def_, name, left_parenthesis, parameters, right_parenthesis, colon, suite) = pack
(async_maybe, def_, name, left_parenthesis, parameters, right_parenthesis, colon, suite) = pack
return [{
"type": "def",
"async": async_maybe,
"decorators": [],
"name": name.value,
"first_formatting": def_.hidden_tokens_after,
Expand Down
1 change: 1 addition & 0 deletions baron/grammator_control_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def for_stmt(pack,):
(for_, exprlist, in_, testlist, colon, suite) = pack
return [{
"type": "for",
"async": {},
"value": suite,
"iterator": exprlist,
"target": testlist,
Expand Down
13 changes: 13 additions & 0 deletions baron/grammator_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def binary_operator_node(pack):
"second_formatting": operator.hidden_tokens_after
}


@pg.production("factor : PLUS factor")
@pg.production("factor : MINUS factor")
@pg.production("factor : TILDE factor")
Expand Down Expand Up @@ -196,6 +197,18 @@ def power_atomtrailers(pack):
"value": atomtrailers
}

@pg.production("power : AWAIT atomtrailers")
def power_atomtrailers_await(pack):
(await_, atomtrailers,) = pack

return [{
"type": "await",
"formatting": await_.hidden_tokens_after
}, {
"type": "atomtrailers",
"value": atomtrailers
}]

@pg.production("atomtrailers : atom")
def atomtrailers_atom(pack):
(atom,) = pack
Expand Down
2 changes: 2 additions & 0 deletions baron/inner_formatting_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class GroupingError(BaronError):
"DOUBLE_SLASH",
"PLUS",
"MINUS",
"ASYNC",
"AWAIT",
"LEFT_SHIFT",
"RIGHT_SHIFT",
"AMPER",
Expand Down
13 changes: 13 additions & 0 deletions baron/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,18 @@ def child_by_key(node, key):
("bool", "with_parenthesis", False),
],

"async": [
("constant", "async", True),
("formatting", "formatting", True),

],
"await": [
("constant", "await", True),
("formatting", "formatting", True),

],
"def": [
("key", "async", "async"),
("list", "decorators", True),
("constant", "def", True),
("formatting", "first_formatting", True),
Expand Down Expand Up @@ -489,6 +500,7 @@ def child_by_key(node, key):
],

"with": [
("key", "async", "async"),
("constant", "with", True),
("formatting", "first_formatting", True),
("list", "contexts", True),
Expand Down Expand Up @@ -555,6 +567,7 @@ def child_by_key(node, key):
("key", "else", "else"),
],
"for": [
("key", "async", "async"),
("constant", "for", True),
("formatting", "first_formatting", True),
("key", "iterator", 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", "async", "await", "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")

TOKENS = (
(r'[a-zA-Z_]\w*', 'NAME'),
Expand Down
73 changes: 73 additions & 0 deletions tests/test_grammator.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def a () :
"type": "def",
"name": "a",
"decorators": [],
"async": {},
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [{"type": "space", "value": " "}],
"third_formatting": [],
Expand Down Expand Up @@ -428,6 +429,61 @@ def a () :
}
])


def test_funcdef_stmt_async():
"""
async def a():
pass
"""
parse_multi([
('ASYNC', 'async', [], [('SPACE', ' ')]),
('DEF', 'def', [], [('SPACE', ' ')]),
('NAME', 'a'),
('LEFT_PARENTHESIS', '(', [('SPACE', ' ')]),
('RIGHT_PARENTHESIS', ')'),
('COLON', ':', [], [('SPACE', ' ')]),
('ENDL', '\n', [], [('SPACE', ' ')]),
('INDENT', ''),
('PASS', 'pass'),
('ENDL', '\n'),
('DEDENT', ''),
], [
{
"type": "def",
"name": "a",
"decorators": [],
"async": {
"type": "async",
"formatting": [{"type": "space", "value": " "}],
},
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [{"type": "space", "value": " "}],
"third_formatting": [],
"fourth_formatting": [],
"fifth_formatting": [],
"sixth_formatting": [{"type": "space", "value": " "}],
"arguments": [],
"value": [
{
"type": "endl",
"value": "\n",
"formatting": [],
"indent": " "
},
{
"type": "pass",
},
{
"type": "endl",
"formatting": [],
"indent": "",
"value": "\n"
}
],
}
])


def test_funcdef_stmt_one_parameter_indent():
"""
def a ( x ) :
Expand All @@ -450,6 +506,7 @@ def a ( x ) :
"type": "def",
"name": "a",
"decorators": [],
"async": {},
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [{"type": "space", "value": " "}],
"third_formatting": [{"type": "space", "value": " "}],
Expand Down Expand Up @@ -510,6 +567,7 @@ def a ( x , ) :
{
"type": "def",
"decorators": [],
"async": {},
"name": "a",
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [{"type": "space", "value": " "}],
Expand Down Expand Up @@ -578,6 +636,7 @@ def a ( x=1 , ) :
{
"type": "def",
"decorators": [],
"async": {},
"name": "a",
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [{"type": "space", "value": " "}],
Expand Down Expand Up @@ -800,6 +859,7 @@ def a (*b):
"type": "def",
"name": "a",
"decorators": [],
"async": {},
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [],
"third_formatting": [],
Expand Down Expand Up @@ -859,6 +919,7 @@ def a (**b):
"type": "def",
"name": "a",
"decorators": [],
"async": {},
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [],
"third_formatting": [],
Expand Down Expand Up @@ -955,6 +1016,7 @@ def test_with_a():
"second_formatting": [],
"third_formatting": [{"type": "space", "value": " "}],
"type": "with",
"async": {},
"contexts": [
{
"type": "with_context_item",
Expand Down Expand Up @@ -1000,6 +1062,7 @@ def test_with_a_as_b():
"second_formatting": [],
"third_formatting": [{"type": "space", "value": " "}],
"type": "with",
"async": {},
"contexts": [
{
"type": "with_context_item",
Expand Down Expand Up @@ -1050,6 +1113,7 @@ def test_with_a_as_b_c():
"second_formatting": [],
"third_formatting": [{"type": "space", "value": " "}],
"type": "with",
"async": {},
"contexts": [
{
"type": "with_context_item",
Expand Down Expand Up @@ -1122,6 +1186,7 @@ def b(): pass
"type": "def",
"arguments": [],
"name": "b",
"async": {},
"decorators": [
{
"type": "decorator",
Expand Down Expand Up @@ -1188,6 +1253,7 @@ def b(): pass
"type": "def",
"arguments": [],
"name": "b",
"async": {},
"decorators": [
{
"type": "decorator",
Expand Down Expand Up @@ -1262,6 +1328,7 @@ def b(): pass
"type": "def",
"arguments": [],
"name": "b",
"async": {},
"decorators": [
{
"type": "decorator",
Expand Down Expand Up @@ -1348,6 +1415,7 @@ def b(): pass
"type": "def",
"arguments": [],
"name": "b",
"async": {},
"decorators": [
{
"type": "decorator",
Expand Down Expand Up @@ -1613,6 +1681,7 @@ def a((b,)): pass
],
"name": "a",
"decorators": [],
"async": {},
"value": [
{
"type": "pass",
Expand Down Expand Up @@ -1687,6 +1756,7 @@ def a((b,c)): pass
],
"name": "a",
"decorators": [],
"async": {},
"value": [
{
"type": "pass",
Expand Down Expand Up @@ -1747,6 +1817,7 @@ def a((b)): pass
],
"name": "a",
"decorators": [],
"async": {},
"value": [
{
"type": "pass",
Expand Down Expand Up @@ -1834,6 +1905,7 @@ def function_name((a,b)=c):\n pass\n
}
],
'decorators': [],
'async': {},
'fifth_formatting': [],
'first_formatting': [
{
Expand Down Expand Up @@ -1951,6 +2023,7 @@ def function_name((a,(x,y))=c):\n pass\n
}
],
'decorators': [],
'async': {},
'fifth_formatting': [],
'first_formatting': [
{
Expand Down
1 change: 1 addition & 0 deletions tests/test_grammator_control_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ def test_for_stmt_indent():
], [
{
"type": "for",
"async": {},
"first_formatting": [{"type": "space", "value": " "}],
"second_formatting": [{"type": "space", "value": " "}],
"third_formatting": [{"type": "space", "value": " "}],
Expand Down
11 changes: 11 additions & 0 deletions tests/test_grammator_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8489,6 +8489,17 @@ def test_expr_comma_list_3_items():
])


def test_await_a():
"await a"
parse_simple([
('AWAIT', 'await', [], [('SPACE', ' ')]),
('NAME', 'a'),
], [[
{'formatting': [{'type': 'space', 'value': ' '}], 'type': 'await'},
{'type': 'atomtrailers', 'value': [{'type': 'name', 'value': 'a'}]},
]])


def test_implicit_tuple_space():
"a, b , c"
parse_simple([
Expand Down