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 #16 from Yelp/add_expr_list
Browse files Browse the repository at this point in the history
Adds EXPR_LIST and EXPR_IND_LIST
  • Loading branch information
evhub committed Aug 24, 2016
2 parents e83d6e6 + fb7ec14 commit e6cfa29
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/source/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ Same as `WHITE` but also matches new lines.
**EXPR**
Matches any valid Python expression.

**EXPR_LIST, EXPR_IND_LIST**
Matches one or more `EXPR` separated by `COMMA` for `EXPR_LIST` or `COMMA_IND` for `EXPR_IND_LIST`.

**ATOM**
Matches a single valid Python atom (that is, an expression without operators).

Expand Down
36 changes: 36 additions & 0 deletions tests/pattern/python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from undebt.pattern.python import ATOM
from undebt.pattern.python import BINARY_OP
from undebt.pattern.python import EXPR
from undebt.pattern.python import EXPR_IND_LIST
from undebt.pattern.python import EXPR_LIST
from undebt.pattern.python import HEADER
from undebt.pattern.python import OP
from undebt.pattern.testing import assert_parse
Expand Down Expand Up @@ -75,6 +77,40 @@ def test_EXPR():
)


def test_EXPR_LIST():
assert_parse(
grammar=EXPR_LIST,
text="""
derp + herp, herp.a.derp - 1, herp[derp],
""",
tokens_list=[
["derp + herp, herp.a.derp - 1, herp[derp],"],
],
interval_list=[
(9, 50),
],
)


def test_EXPR_IND_LIST():
assert_parse(
grammar=EXPR_IND_LIST,
text="""
derp + herp,
herp.a.derp - 1,
herp[derp]
""",
tokens_list=[
["""derp + herp,
herp.a.derp - 1,
herp[derp]"""],
],
interval_list=[
(9, 65),
],
)


def test_HEADER():
assert_parse(
grammar=HEADER,
Expand Down
5 changes: 5 additions & 0 deletions undebt/pattern/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from undebt.pattern.common import BRACES
from undebt.pattern.common import BRACKETS
from undebt.pattern.common import COMMA
from undebt.pattern.common import COMMA_IND
from undebt.pattern.common import DOT
from undebt.pattern.common import DOTTED_NAME
from undebt.pattern.common import NAME
Expand Down Expand Up @@ -57,12 +59,15 @@
TRAILER = DOT + NAME | PARENS | BRACKETS
TRAILERS = condense(ZeroOrMore(TRAILER))


ATOM_BASE = NAME | NUM | PARENS | BRACKETS | BRACES | STRING
ATOM = condense(ATOM_BASE + TRAILERS)
UNARY_OP_ATOM = addspace(Optional(UNARY_OP) + ATOM)


EXPR = addspace(UNARY_OP_ATOM + ZeroOrMore(BINARY_OP + UNARY_OP_ATOM))
EXPR_LIST = condense(EXPR + ZeroOrMore(addspace(COMMA + EXPR)) + Optional(COMMA))
EXPR_IND_LIST = originalTextFor(EXPR + ZeroOrMore(COMMA_IND + EXPR) + Optional(COMMA_IND))


HEADER = originalTextFor(START_OF_FILE + ZeroOrMore(SKIP_TO_TEXT + (
Expand Down

0 comments on commit e6cfa29

Please sign in to comment.