Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #28 from ajm188/add-swift-example
Browse files Browse the repository at this point in the history
Adds swift example. EXPR might not be 100% valid
  • Loading branch information
ajm188 committed Aug 26, 2016
2 parents c5c97fd + 1cad3dd commit c6427f5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,19 @@ Example of:
- using `in_string` to determine if the match location is inside of a string
- `originalTextFor` to make grammar elements parse to the original text that matched them
- `STRING` to match any valid string

`undebt.examples.swift`
-----------------------------------
(`Source
<https://github.com/Yelp/undebt/blob/master/undebt/examples/swift.py>`_)

Transforms uses of `if let where` from Swift 2.2 to the updated syntax in Swift
3.0.

Example of:

- using `tokens_as_dict` to assert multiple possible dictionary keys
- `EXPR` to match a Python expression
- Note: It's possible that this won't match all Swift expressions; if you are
concerned about this, you should use an EXPR pattern that corresponds to
the Swift grammar.
27 changes: 27 additions & 0 deletions tests/examples/swift_test.py
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])
41 changes: 41 additions & 0 deletions undebt/examples/swift.py
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 + "{"

0 comments on commit c6427f5

Please sign in to comment.