Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix error with pyparsing 3 for 3.5.x #21454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import numpy as np
from pyparsing import (
Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore,
Combine, Empty, Forward, Group, Literal, oneOf, OneOrMore,
Optional, ParseBaseException, ParseFatalException, ParserElement,
ParseResults, QuotedString, Regex, StringEnd, Suppress, ZeroOrMore)

Expand Down Expand Up @@ -2044,7 +2044,7 @@ def __init__(self):
p.accentprefixed <<= Suppress(p.bslash) + oneOf(self._accentprefixed)
p.symbol_name <<= (
Combine(p.bslash + oneOf(list(tex2uni)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also have been fixed by changing oneOf(list(tex2uni)) to oneOf(list(tex2uni), asKeyword=True) and removing the FollowedBy altogether? This has the benefit of reducing the number of pyparsing terms to be matched, which should translate to faster parsing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem of asKeyword is that "word boundary" needs a custom definition here (e.g. underscores and digits also separate words). I this this is configurable on pyparsing's side, but I'd rather just write our own regex (something like (?![a-zA-Z])) and be done with it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a better approach than trying to do custom keywords.

If you really want to collapse this down to a single Regex, then you could use oneOf to build a Regex for you of just the word choices, but then extract the generated regex pattern into a new Regex, something like Regex(r"\\(" + oneOf(text2uni).pattern + ")(?![a-zA-Z])"). (untested, ymmv, etc.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tex symbols should not have any metacharacters, so the middle part is probably even just "|".join(tex2uni).

Copy link

@ptmcg ptmcg Oct 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beware of this - oneOf also takes care of reordering longer entries before shorter in the event the shorter entry is a leading subset, and also deduping. You could just reverse sort the keys in tex2uni by length, but if there are known frequencies to some symbols vs others, and more frequent entries were placed first, oneOf would only reorder them to avoid masking entries, whereas sorting by longest-to-shortest would lose some of this priority ordering.

You can test this for yourself:

re.match(r"ab|abb", "abb")

will return "ab", not the longer and more desirable match "abb". And there are several cases in tex2uni of these kind of masking pairs.

Maybe keep it simple first, just do "|".join(sorted(set(tex2uni), key=len, reverse=True)) and then get clever with the frequency-based ordering as a later experiment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point, thanks for mentioning that.

+ FollowedBy(Regex("[^A-Za-z]").leaveWhitespace() | StringEnd())
+ Suppress(Regex("(?=[^A-Za-z]|$)").leaveWhitespace())
)
p.symbol <<= (p.single_symbol | p.symbol_name).leaveWhitespace()

Expand Down
8 changes: 5 additions & 3 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def test_fontinfo():
(r'$\leftF$', r'Expected a delimiter'),
(r'$\rightF$', r'Unknown symbol: \rightF'),
(r'$\left(\right$', r'Expected a delimiter'),
(r'$\left($', r'Expected "\right"'),
# PyParsing 2 uses double quotes, PyParsing 3 uses single quotes and an
# extra backslash.
(r'$\left($', re.compile(r'Expected ("|\'\\)\\right["\']')),
(r'$\dfrac$', r'Expected \dfrac{num}{den}'),
(r'$\dfrac{}{}$', r'Expected \dfrac{num}{den}'),
(r'$\overset$', r'Expected \overset{body}{annotation}'),
Expand Down Expand Up @@ -281,8 +283,8 @@ def test_fontinfo():
)
def test_mathtext_exceptions(math, msg):
parser = mathtext.MathTextParser('agg')

with pytest.raises(ValueError, match=re.escape(msg)):
match = re.escape(msg) if isinstance(msg, str) else msg
with pytest.raises(ValueError, match=match):
parser.parse(math)


Expand Down
1 change: 0 additions & 1 deletion requirements/doc/doc-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ ipython
ipywidgets
numpydoc>=0.8
packaging>=20
pyparsing<3.0.0
pydata-sphinx-theme>=0.6.0
sphinxcontrib-svg2pdfconverter>=1.1.0
sphinx-gallery>=0.10
Expand Down
1 change: 0 additions & 1 deletion requirements/testing/all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

certifi
coverage
pyparsing<3.0.0
pytest!=4.6.0,!=5.4.0
pytest-cov
pytest-rerunfailures
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def make_release_tree(self, base_dir, files):
"numpy>=1.17",
"packaging>=20.0",
"pillow>=6.2.0",
"pyparsing>=2.2.1,<3.0.0",
"pyparsing>=2.2.1",
"python-dateutil>=2.7",
] + (
# Installing from a git checkout.
Expand Down