19
19
20
20
import numpy as np
21
21
from pyparsing import (
22
- Empty , Forward , Literal , NotAny , oneOf , OneOrMore , Optional ,
22
+ Empty , Forward , Literal , Group , NotAny , OneOrMore , Optional ,
23
23
ParseBaseException , ParseException , ParseExpression , ParseFatalException ,
24
24
ParserElement , ParseResults , QuotedString , Regex , StringEnd , ZeroOrMore ,
25
- pyparsing_common , Group )
25
+ pyparsing_common , nested_expr , one_of )
26
26
27
27
import matplotlib as mpl
28
28
from . import cbook
31
31
from .font_manager import FontProperties , findfont , get_font
32
32
from .ft2font import FT2Font , FT2Image , Kerning , LoadFlags
33
33
34
- from packaging .version import parse as parse_version
35
- from pyparsing import __version__ as pyparsing_version
36
- if parse_version (pyparsing_version ).major < 3 :
37
- from pyparsing import nestedExpr as nested_expr
38
- else :
39
- from pyparsing import nested_expr
40
34
41
35
if T .TYPE_CHECKING :
42
36
from collections .abc import Iterable
43
37
from .ft2font import Glyph
44
38
45
- ParserElement .enablePackrat ()
39
+ ParserElement .enable_packrat ()
46
40
_log = logging .getLogger ("matplotlib.mathtext" )
47
41
48
42
@@ -1745,7 +1739,7 @@ def Error(msg: str) -> ParserElement:
1745
1739
def raise_error (s : str , loc : int , toks : ParseResults ) -> T .Any :
1746
1740
raise ParseFatalException (s , loc , msg )
1747
1741
1748
- return Empty ().setParseAction (raise_error )
1742
+ return Empty ().set_parse_action (raise_error )
1749
1743
1750
1744
1751
1745
class ParserState :
@@ -1981,10 +1975,10 @@ def set_names_and_parse_actions() -> None:
1981
1975
# token, placeable, and auto_delim are forward references which
1982
1976
# are left without names to ensure useful error messages
1983
1977
if key not in ("token" , "placeable" , "auto_delim" ):
1984
- val .setName (key )
1978
+ val .set_name (key )
1985
1979
# Set actions
1986
1980
if hasattr (self , key ):
1987
- val .setParseAction (getattr (self , key ))
1981
+ val .set_parse_action (getattr (self , key ))
1988
1982
1989
1983
# Root definitions.
1990
1984
@@ -2007,24 +2001,24 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
2007
2001
)
2008
2002
2009
2003
p .float_literal = Regex (r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)" )
2010
- p .space = oneOf (self ._space_widths )("space" )
2004
+ p .space = one_of (self ._space_widths )("space" )
2011
2005
2012
- p .style_literal = oneOf (
2006
+ p .style_literal = one_of (
2013
2007
[str (e .value ) for e in self ._MathStyle ])("style_literal" )
2014
2008
2015
2009
p .symbol = Regex (
2016
2010
r"[a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|\U00000080-\U0001ffff]"
2017
2011
r"|\\[%${}\[\]_|]"
2018
2012
+ r"|\\(?:{})(?![A-Za-z])" .format (
2019
2013
"|" .join (map (re .escape , tex2uni )))
2020
- )("sym" ).leaveWhitespace ()
2014
+ )("sym" ).leave_whitespace ()
2021
2015
p .unknown_symbol = Regex (r"\\[A-Za-z]+" )("name" )
2022
2016
2023
2017
p .font = csnames ("font" , self ._fontnames )
2024
- p .start_group = Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{"
2018
+ p .start_group = Optional (r"\math" + one_of (self ._fontnames )("font" )) + "{"
2025
2019
p .end_group = Literal ("}" )
2026
2020
2027
- p .delim = oneOf (self ._delims )
2021
+ p .delim = one_of (self ._delims )
2028
2022
2029
2023
# Mutually recursive definitions. (Minimizing the number of Forward
2030
2024
# elements is important for speed.)
@@ -2085,7 +2079,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
2085
2079
r"\underset" ,
2086
2080
p .optional_group ("annotation" ) + p .optional_group ("body" ))
2087
2081
2088
- p .text = cmd (r"\text" , QuotedString ('{' , '\\ ' , endQuoteChar = "}" ))
2082
+ p .text = cmd (r"\text" , QuotedString ('{' , '\\ ' , end_quote_char = "}" ))
2089
2083
2090
2084
p .substack = cmd (r"\substack" ,
2091
2085
nested_expr (opener = "{" , closer = "}" ,
@@ -2094,7 +2088,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
2094
2088
2095
2089
p .subsuper = (
2096
2090
(Optional (p .placeable )("nucleus" )
2097
- + OneOrMore (oneOf (["_" , "^" ]) - p .placeable )("subsuper" )
2091
+ + OneOrMore (one_of (["_" , "^" ]) - p .placeable )("subsuper" )
2098
2092
+ Regex ("'*" )("apostrophes" ))
2099
2093
| Regex ("'+" )("apostrophes" )
2100
2094
| (p .named_placeable ("nucleus" ) + Regex ("'*" )("apostrophes" ))
@@ -2143,8 +2137,8 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
2143
2137
2144
2138
# Leaf definitions.
2145
2139
p .math = OneOrMore (p .token )
2146
- p .math_string = QuotedString ('$' , '\\ ' , unquoteResults = False )
2147
- p .non_math = Regex (r"(?:(?:\\[$])|[^$])*" ).leaveWhitespace ()
2140
+ p .math_string = QuotedString ('$' , '\\ ' , unquote_results = False )
2141
+ p .non_math = Regex (r"(?:(?:\\[$])|[^$])*" ).leave_whitespace ()
2148
2142
p .main = (
2149
2143
p .non_math + ZeroOrMore (p .math_string + p .non_math ) + StringEnd ()
2150
2144
)
@@ -2167,15 +2161,15 @@ def parse(self, s: str, fonts_object: Fonts, fontsize: float, dpi: float) -> Hli
2167
2161
ParserState (fonts_object , 'default' , 'rm' , fontsize , dpi )]
2168
2162
self ._em_width_cache : dict [tuple [str , float , float ], float ] = {}
2169
2163
try :
2170
- result = self ._expression .parseString (s )
2164
+ result = self ._expression .parse_string (s )
2171
2165
except ParseBaseException as err :
2172
2166
# explain becomes a plain method on pyparsing 3 (err.explain(0)).
2173
2167
raise ValueError ("\n " + ParseException .explain (err , 0 )) from None
2174
2168
self ._state_stack = []
2175
2169
self ._in_subscript_or_superscript = False
2176
2170
# prevent operator spacing from leaking into a new expression
2177
2171
self ._em_width_cache = {}
2178
- ParserElement .resetCache ()
2172
+ ParserElement .reset_cache ()
2179
2173
return T .cast (Hlist , result [0 ]) # Known return type from main.
2180
2174
2181
2175
def get_state (self ) -> ParserState :
@@ -2191,13 +2185,13 @@ def push_state(self) -> None:
2191
2185
self ._state_stack .append (self .get_state ().copy ())
2192
2186
2193
2187
def main (self , toks : ParseResults ) -> list [Hlist ]:
2194
- return [Hlist (toks .asList ())]
2188
+ return [Hlist (toks .as_list ())]
2195
2189
2196
2190
def math_string (self , toks : ParseResults ) -> ParseResults :
2197
- return self ._math_expression .parseString (toks [0 ][1 :- 1 ], parseAll = True )
2191
+ return self ._math_expression .parse_string (toks [0 ][1 :- 1 ], parse_all = True )
2198
2192
2199
2193
def math (self , toks : ParseResults ) -> T .Any :
2200
- hlist = Hlist (toks .asList ())
2194
+ hlist = Hlist (toks .as_list ())
2201
2195
self .pop_state ()
2202
2196
return [hlist ]
2203
2197
@@ -2210,7 +2204,7 @@ def non_math(self, toks: ParseResults) -> T.Any:
2210
2204
self .get_state ().font = mpl .rcParams ['mathtext.default' ]
2211
2205
return [hlist ]
2212
2206
2213
- float_literal = staticmethod (pyparsing_common .convertToFloat )
2207
+ float_literal = staticmethod (pyparsing_common .convert_to_float )
2214
2208
2215
2209
def text (self , toks : ParseResults ) -> T .Any :
2216
2210
self .push_state ()
@@ -2809,7 +2803,7 @@ def auto_delim(self, toks: ParseResults) -> T.Any:
2809
2803
return self ._auto_sized_delimiter (
2810
2804
toks ["left" ],
2811
2805
# if "mid" in toks ... can be removed when requiring pyparsing 3.
2812
- toks ["mid" ].asList () if "mid" in toks else [],
2806
+ toks ["mid" ].as_list () if "mid" in toks else [],
2813
2807
toks ["right" ])
2814
2808
2815
2809
def boldsymbol (self , toks : ParseResults ) -> T .Any :
0 commit comments