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

Commit c39d4d0

Browse files
committed
Add cPyparsing support
1 parent 9d7d869 commit c39d4d0

20 files changed

+86
-75
lines changed

docs/source/pyparsing.rst

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ While the `official pyparsing documentation`_ is a great resource, most of the m
1313

1414
.. _`official pyparsing documentation`: https://pythonhosted.org/pyparsing/
1515

16+
The recommended way to import `pyparsing` is via `undebt.pyparsing`. This has the advantage that if the faster `cPyparsing`_ is available, `undebt.pyparsing` will use it instead. To install `cPyparsing`_ automatically with your `undebt` installation, just `pip install undebt[cPyparsing]`.
17+
18+
.. _`cPyparsing`: https://github.com/evhub/cpyparsing
19+
1620
Operators
1721
---------
1822

setup.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
license="Copyright Yelp, Inc. 2016",
1818
url="https://github.com/Yelp/undebt",
1919
packages=find_packages(exclude=[
20-
"tests*",
21-
"docs*",
20+
"tests",
21+
"docs",
2222
]),
2323
install_requires=[
2424
'pyparsing',
@@ -30,6 +30,9 @@
3030
"mock",
3131
"coverage",
3232
],
33+
"cPyparsing": [
34+
"cPyparsing",
35+
],
3336
},
3437
entry_points={
3538
'console_scripts': [

tests/pattern/util_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import sys
77

88
import mock
9-
from pyparsing import Keyword
10-
from pyparsing import Literal
119

1210
from undebt.pattern.testing import assert_parse
1311
from undebt.pattern.util import debug
@@ -16,6 +14,8 @@
1614
from undebt.pattern.util import quoted
1715
from undebt.pattern.util import sequence
1816
from undebt.pattern.util import trailing_whitespace
17+
from undebt.pyparsing import Keyword
18+
from undebt.pyparsing import Literal
1919

2020

2121
if sys.version_info.major == 3:

undebt/examples/attribute_to_function.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Keyword
7-
from pyparsing import ZeroOrMore
8-
96
from undebt.pattern.common import BRACKETS
107
from undebt.pattern.common import DOT
118
from undebt.pattern.common import NAME
129
from undebt.pattern.common import PARENS
1310
from undebt.pattern.lang.python import ATOM_BASE
1411
from undebt.pattern.util import condense
1512
from undebt.pattern.util import tokens_as_list
13+
from undebt.pyparsing import Keyword
14+
from undebt.pyparsing import ZeroOrMore
1615

1716

1817
attribute = Keyword("attribute")

undebt/examples/class_inherit_object.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Keyword
7-
from pyparsing import Optional
8-
96
from undebt.pattern.common import COLON
107
from undebt.pattern.common import INDENT
118
from undebt.pattern.common import LPAREN
129
from undebt.pattern.common import NAME
1310
from undebt.pattern.common import RPAREN
1411
from undebt.pattern.util import tokens_as_list
12+
from undebt.pyparsing import Keyword
13+
from undebt.pyparsing import Optional
1514

1615

1716
grammar = INDENT + Keyword("class").suppress() + NAME + (Optional(LPAREN + RPAREN) + COLON).suppress()

undebt/examples/contextlib_nested.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
from __future__ import division
99
from __future__ import print_function
1010

11-
from pyparsing import Keyword
12-
from pyparsing import Optional
13-
from pyparsing import ZeroOrMore
14-
1511
from undebt.pattern.common import COLON
1612
from undebt.pattern.common import COMMA_IND
1713
from undebt.pattern.common import DOT
@@ -20,6 +16,9 @@
2016
from undebt.pattern.common import LPAREN_IND
2117
from undebt.pattern.lang.python import EXPR
2218
from undebt.pattern.util import tokens_as_dict
19+
from undebt.pyparsing import Keyword
20+
from undebt.pyparsing import Optional
21+
from undebt.pyparsing import ZeroOrMore
2322

2423

2524
expr_list = (

undebt/examples/deprecated_inequality_operator.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Literal
7-
86
from undebt.pattern.lang.python import UNARY_OP_ATOM
97
from undebt.pattern.util import tokens_as_list
8+
from undebt.pyparsing import Literal
109

1110

1211
grammar = UNARY_OP_ATOM + Literal("<>").suppress() + UNARY_OP_ATOM

undebt/examples/exec_function.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Keyword
7-
from pyparsing import Optional
8-
96
from undebt.pattern.common import COMMA
107
from undebt.pattern.common import INDENT
118
from undebt.pattern.lang.python import ATOM
129
from undebt.pattern.util import tokens_as_list
10+
from undebt.pyparsing import Keyword
11+
from undebt.pyparsing import Optional
1312

1413

1514
grammar = (

undebt/examples/hex_to_bitshift.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Combine
7-
from pyparsing import hexnums
8-
from pyparsing import Literal
9-
from pyparsing import Word
10-
116
from undebt.pattern.util import tokens_as_list
7+
from undebt.pyparsing import Combine
8+
from undebt.pyparsing import hexnums
9+
from undebt.pyparsing import Literal
10+
from undebt.pyparsing import Word
1211

1312
grammar = Combine(Literal("0x").suppress() + Word(hexnums))
1413

undebt/examples/method_to_function.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Keyword
7-
from pyparsing import ZeroOrMore
8-
96
from undebt.pattern.common import BRACKETS
107
from undebt.pattern.common import DOT
118
from undebt.pattern.common import LPAREN
@@ -15,6 +12,8 @@
1512
from undebt.pattern.lang.python import ATOM_BASE
1613
from undebt.pattern.util import condense
1714
from undebt.pattern.util import tokens_as_list
15+
from undebt.pyparsing import Keyword
16+
from undebt.pyparsing import ZeroOrMore
1817

1918

2019
method = Keyword("method")

undebt/examples/remove_needless_u_specifier.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Keyword
7-
from pyparsing import Literal
8-
from pyparsing import Optional
9-
from pyparsing import originalTextFor
10-
from pyparsing import ZeroOrMore
11-
126
from undebt.pattern.common import COMMA
137
from undebt.pattern.common import NAME
148
from undebt.pattern.common import STRING
159
from undebt.pattern.util import condense
1610
from undebt.pattern.util import in_string
1711
from undebt.pattern.util import tokens_as_list
12+
from undebt.pyparsing import Keyword
13+
from undebt.pyparsing import Literal
14+
from undebt.pyparsing import Optional
15+
from undebt.pyparsing import originalTextFor
16+
from undebt.pyparsing import ZeroOrMore
1817

1918

2019
unicode_literals = Keyword("unicode_literals")

undebt/examples/sqla_count.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Keyword
7-
from pyparsing import Literal
8-
96
from undebt.pattern.common import DOT
107
from undebt.pattern.common import LPAREN
118
from undebt.pattern.common import PARENS
129
from undebt.pattern.common import RPAREN
1310
from undebt.pattern.util import leading_whitespace
1411
from undebt.pattern.util import tokens_as_dict
1512
from undebt.pattern.util import trailing_whitespace
13+
from undebt.pyparsing import Keyword
14+
from undebt.pyparsing import Literal
1615

1716

1817
grammar = (

undebt/examples/swift.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import delimitedList
7-
from pyparsing import Keyword
8-
from pyparsing import Literal
9-
from pyparsing import SkipTo
10-
116
from undebt.pattern.common import NAME
127
from undebt.pattern.lang.python import EXPR
138
from undebt.pattern.util import condense
149
from undebt.pattern.util import tokens_as_dict
10+
from undebt.pyparsing import delimitedList
11+
from undebt.pyparsing import Keyword
12+
from undebt.pyparsing import Literal
13+
from undebt.pyparsing import SkipTo
1514

1615

1716
if_ = Keyword("if")

undebt/pattern/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import ParserElement
6+
from undebt.pyparsing import ParserElement
77

88

99
ParserElement.enablePackrat()

undebt/pattern/common.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55

66
import re
77

8-
from pyparsing import CharsNotIn
9-
from pyparsing import Literal
10-
from pyparsing import nestedExpr
11-
from pyparsing import Optional
12-
from pyparsing import originalTextFor
13-
from pyparsing import quotedString
14-
from pyparsing import Regex
15-
from pyparsing import SkipTo
16-
from pyparsing import StringEnd
17-
from pyparsing import StringStart
18-
from pyparsing import Word
19-
from pyparsing import ZeroOrMore
20-
218
from undebt.pattern import WHITESPACE_CHARS
229
from undebt.pattern import WHITESPACE_OR_NL_CHARS
2310
from undebt.pattern.util import condense
2411
from undebt.pattern.util import fixto
12+
from undebt.pyparsing import CharsNotIn
13+
from undebt.pyparsing import Literal
14+
from undebt.pyparsing import nestedExpr
15+
from undebt.pyparsing import Optional
16+
from undebt.pyparsing import originalTextFor
17+
from undebt.pyparsing import quotedString
18+
from undebt.pyparsing import Regex
19+
from undebt.pyparsing import SkipTo
20+
from undebt.pyparsing import StringEnd
21+
from undebt.pyparsing import StringStart
22+
from undebt.pyparsing import Word
23+
from undebt.pyparsing import ZeroOrMore
2524

2625

2726
ANY_CHAR = Regex(r".", re.DOTALL | re.U)

undebt/pattern/interface.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66
import sys
77

8-
from pyparsing import _trim_arity
9-
108
from undebt.cmd.logger import log
119
from undebt.pattern.util import attach
1210
from undebt.pattern.util import tokens_as_list
11+
from undebt.pyparsing import _trim_arity
1312

1413

1514
# required at the beginning of a string to be able to properly parse it

undebt/pattern/lang/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: utf-8 -*-

undebt/pattern/lang/python.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
from __future__ import division
44
from __future__ import print_function
55

6-
from pyparsing import Combine
7-
from pyparsing import Keyword
8-
from pyparsing import Literal
9-
from pyparsing import OneOrMore
10-
from pyparsing import Optional
11-
from pyparsing import originalTextFor
12-
from pyparsing import pythonStyleComment
13-
from pyparsing import SkipTo
14-
from pyparsing import Word
15-
from pyparsing import ZeroOrMore
16-
176
from undebt.pattern.common import BRACES
187
from undebt.pattern.common import BRACKETS
198
from undebt.pattern.common import COMMA
@@ -29,6 +18,16 @@
2918
from undebt.pattern.common import STRING
3019
from undebt.pattern.util import addspace
3120
from undebt.pattern.util import condense
21+
from undebt.pyparsing import Combine
22+
from undebt.pyparsing import Keyword
23+
from undebt.pyparsing import Literal
24+
from undebt.pyparsing import OneOrMore
25+
from undebt.pyparsing import Optional
26+
from undebt.pyparsing import originalTextFor
27+
from undebt.pyparsing import pythonStyleComment
28+
from undebt.pyparsing import SkipTo
29+
from undebt.pyparsing import Word
30+
from undebt.pyparsing import ZeroOrMore
3231

3332

3433
ASSIGN_OP = Combine((Word("~%^&*-+|/") | ~Literal("==")) + Literal("="))

undebt/pattern/util.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import functools
77
import operator
88

9-
from pyparsing import _trim_arity
10-
from pyparsing import col
11-
from pyparsing import Combine
12-
from pyparsing import line
13-
from pyparsing import originalTextFor
14-
from pyparsing import replaceWith
15-
169
from undebt.pattern import WHITESPACE_OR_NL_CHARS
10+
from undebt.pyparsing import _trim_arity
11+
from undebt.pyparsing import col
12+
from undebt.pyparsing import Combine
13+
from undebt.pyparsing import line
14+
from undebt.pyparsing import originalTextFor
15+
from undebt.pyparsing import replaceWith
1716

1817

1918
def attach(item, action):

undebt/pyparsing.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import absolute_import
3+
from __future__ import division
4+
from __future__ import print_function
5+
6+
"""
7+
Import from undebt.pyparsing instead of pyparsing to automatically
8+
get the performance benefits of cPyparsing when available and fall
9+
back to pyparsing when not.
10+
"""
11+
12+
try:
13+
from cPyparsing import * # NOQA
14+
from cPyparsing import _trim_arity # NOQA
15+
except ImportError:
16+
from pyparsing import * # NOQA
17+
from pyparsing import _trim_arity # NOQA

0 commit comments

Comments
 (0)