This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from ajm188/add-swift-example
Adds swift example. EXPR might not be 100% valid
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from undebt.examples import swift | ||
from undebt.pattern.testing import assert_transform | ||
|
||
|
||
def test_one_liner(): | ||
assert_transform( | ||
swift, | ||
'if let x = a where x == y {', | ||
['if let x = a, x == y {'], | ||
) | ||
|
||
|
||
def test_compound(): | ||
text = 'if let x = a, y = b, z = c where x == y && y != z {' | ||
expected = 'if let x = a, let y = b, let z = c, x == y && y != z {' | ||
assert_transform(swift, text, [expected]) | ||
|
||
|
||
def test_expr(): | ||
text = 'if let x = a + 1 / b where x == -y {' | ||
expected = 'if let x = a + 1 / b, x == -y {' | ||
assert_transform(swift, text, [expected]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from pyparsing import delimitedList | ||
from pyparsing import Keyword | ||
from pyparsing import Literal | ||
from pyparsing import SkipTo | ||
|
||
from undebt.pattern.common import NAME | ||
from undebt.pattern.python import EXPR | ||
from undebt.pattern.util import condense | ||
from undebt.pattern.util import tokens_as_dict | ||
|
||
|
||
if_ = Keyword("if") | ||
let = Keyword("let") | ||
where = Keyword("where") | ||
eq = Literal("=") | ||
|
||
assign = condense(NAME + eq + EXPR) | ||
|
||
grammar = ( | ||
if_ + let + delimitedList(assign)("let-bindings") | ||
+ where + SkipTo("{")("where-clause") | ||
) | ||
|
||
|
||
@tokens_as_dict(assert_keys=["let-bindings", "where-clause"]) | ||
def replace(tokens): | ||
assigns = tokens["let-bindings"] | ||
cond = tokens['where-clause'] | ||
|
||
def pretty_assign(assign): | ||
op = assign.index("=") | ||
l, r = assign[:op], assign[op + 1:] | ||
return "{0} = {1}".format(l, r) | ||
|
||
new_assigns = ", let ".join(map(pretty_assign, assigns)) | ||
return "if let " + new_assigns + ", " + cond + "{" |