Skip to content

Commit

Permalink
Support PEP 380, Syntax for Delegating to a Subgenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
odcinek committed Aug 4, 2017
1 parent 08c7561 commit d4b17fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
25 changes: 24 additions & 1 deletion baron/grammator_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def flow(pack):


@pg.production("return_stmt : RETURN")
@pg.production("yield_expr : YIELD")
def return_empty(pack):
(token,) = pack
return {
Expand All @@ -79,6 +78,18 @@ def return_empty(pack):
}


@pg.production("yield_expr : YIELD ")
def yield_expr(pack):
(yield_,) = pack
return {
"type": yield_.name.lower(),
"first_formatting": [],
"from": None,
"value": None,
"formatting": yield_.hidden_tokens_after,
}


@pg.production("break_stmt : BREAK")
@pg.production("continue_stmt : CONTINUE")
@pg.production("pass_stmt : PASS")
Expand Down Expand Up @@ -258,6 +269,18 @@ def return_testlist(pack):
"formatting": token.hidden_tokens_after,
}


@pg.production("yield_expr : YIELD FROM testlist")
def yield_from_expr(pack):
(yield_, from_, test) = pack
return {
"type": yield_.name.lower(),
"first_formatting": from_.hidden_tokens_after,
"from": True,
"value": test,
"formatting": yield_.hidden_tokens_after,
}

@pg.production("lambdef : LAMBDA COLON test")
@pg.production("old_lambdef : LAMBDA COLON old_test")
def lambdef(pack):
Expand Down
2 changes: 2 additions & 0 deletions baron/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ def child_by_key(node, key):
"yield": [
("constant", "yield", True),
("formatting", "formatting", True),
("constant", "from", "from"),
("formatting", "first_formatting", "from"),
("key", "value", "value"),
],
"yield_atom": [
Expand Down
23 changes: 22 additions & 1 deletion tests/test_grammator_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,28 @@ def test_yield():
"yield"
parse_simple([
('YIELD', 'yield'),
], [{"type": "yield", "value": None, "formatting": [],}])
], [{
"type": "yield",
"first_formatting": [],
"from": None,
"value": None,
"formatting": [],
}])


def test_yield_from():
"yield from a"
parse_simple([
('YIELD', 'yield', [], [('SPACE', ' ')]),
('FROM', 'from', [], [('SPACE', ' ')]),
('NAME', 'a'),
], [{
"type": "yield",
"first_formatting": [{"type": "space", "value": " "}],
"from": True,
"value": {"type": "name", "value": 'a',},
"formatting": [{"type": "space", "value": " "}]
,}])


def test_yield_a():
Expand Down

0 comments on commit d4b17fe

Please sign in to comment.