1919
2020import numpy as np
2121from pyparsing import (
22- Empty , Forward , Literal , NotAny , oneOf , OneOrMore , Optional ,
22+ Empty , Forward , Literal , Group , NotAny , OneOrMore , Optional ,
2323 ParseBaseException , ParseException , ParseExpression , ParseFatalException ,
2424 ParserElement , ParseResults , QuotedString , Regex , StringEnd , ZeroOrMore ,
25- pyparsing_common , Group )
25+ pyparsing_common , nested_expr , one_of )
2626
2727import matplotlib as mpl
2828from . import cbook
3131from .font_manager import FontProperties , findfont , get_font
3232from .ft2font import FT2Font , FT2Image , Kerning , LoadFlags
3333
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
4034
4135if T .TYPE_CHECKING :
4236 from collections .abc import Iterable
4337 from .ft2font import Glyph
4438
45- ParserElement .enablePackrat ()
39+ ParserElement .enable_packrat ()
4640_log = logging .getLogger ("matplotlib.mathtext" )
4741
4842
@@ -1745,7 +1739,7 @@ def Error(msg: str) -> ParserElement:
17451739 def raise_error (s : str , loc : int , toks : ParseResults ) -> T .Any :
17461740 raise ParseFatalException (s , loc , msg )
17471741
1748- return Empty ().setParseAction (raise_error )
1742+ return Empty ().set_parse_action (raise_error )
17491743
17501744
17511745class ParserState :
@@ -1981,10 +1975,10 @@ def set_names_and_parse_actions() -> None:
19811975 # token, placeable, and auto_delim are forward references which
19821976 # are left without names to ensure useful error messages
19831977 if key not in ("token" , "placeable" , "auto_delim" ):
1984- val .setName (key )
1978+ val .set_name (key )
19851979 # Set actions
19861980 if hasattr (self , key ):
1987- val .setParseAction (getattr (self , key ))
1981+ val .set_parse_action (getattr (self , key ))
19881982
19891983 # Root definitions.
19901984
@@ -2007,24 +2001,24 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
20072001 )
20082002
20092003 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" )
20112005
2012- p .style_literal = oneOf (
2006+ p .style_literal = one_of (
20132007 [str (e .value ) for e in self ._MathStyle ])("style_literal" )
20142008
20152009 p .symbol = Regex (
20162010 r"[a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|\U00000080-\U0001ffff]"
20172011 r"|\\[%${}\[\]_|]"
20182012 + r"|\\(?:{})(?![A-Za-z])" .format (
20192013 "|" .join (map (re .escape , tex2uni )))
2020- )("sym" ).leaveWhitespace ()
2014+ )("sym" ).leave_whitespace ()
20212015 p .unknown_symbol = Regex (r"\\[A-Za-z]+" )("name" )
20222016
20232017 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" )) + "{"
20252019 p .end_group = Literal ("}" )
20262020
2027- p .delim = oneOf (self ._delims )
2021+ p .delim = one_of (self ._delims )
20282022
20292023 # Mutually recursive definitions. (Minimizing the number of Forward
20302024 # elements is important for speed.)
@@ -2085,7 +2079,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
20852079 r"\underset" ,
20862080 p .optional_group ("annotation" ) + p .optional_group ("body" ))
20872081
2088- p .text = cmd (r"\text" , QuotedString ('{' , '\\ ' , endQuoteChar = "}" ))
2082+ p .text = cmd (r"\text" , QuotedString ('{' , '\\ ' , end_quote_char = "}" ))
20892083
20902084 p .substack = cmd (r"\substack" ,
20912085 nested_expr (opener = "{" , closer = "}" ,
@@ -2094,7 +2088,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
20942088
20952089 p .subsuper = (
20962090 (Optional (p .placeable )("nucleus" )
2097- + OneOrMore (oneOf (["_" , "^" ]) - p .placeable )("subsuper" )
2091+ + OneOrMore (one_of (["_" , "^" ]) - p .placeable )("subsuper" )
20982092 + Regex ("'*" )("apostrophes" ))
20992093 | Regex ("'+" )("apostrophes" )
21002094 | (p .named_placeable ("nucleus" ) + Regex ("'*" )("apostrophes" ))
@@ -2143,8 +2137,8 @@ def csnames(group: str, names: Iterable[str]) -> Regex:
21432137
21442138 # Leaf definitions.
21452139 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 ()
21482142 p .main = (
21492143 p .non_math + ZeroOrMore (p .math_string + p .non_math ) + StringEnd ()
21502144 )
@@ -2167,15 +2161,15 @@ def parse(self, s: str, fonts_object: Fonts, fontsize: float, dpi: float) -> Hli
21672161 ParserState (fonts_object , 'default' , 'rm' , fontsize , dpi )]
21682162 self ._em_width_cache : dict [tuple [str , float , float ], float ] = {}
21692163 try :
2170- result = self ._expression .parseString (s )
2164+ result = self ._expression .parse_string (s )
21712165 except ParseBaseException as err :
21722166 # explain becomes a plain method on pyparsing 3 (err.explain(0)).
21732167 raise ValueError ("\n " + ParseException .explain (err , 0 )) from None
21742168 self ._state_stack = []
21752169 self ._in_subscript_or_superscript = False
21762170 # prevent operator spacing from leaking into a new expression
21772171 self ._em_width_cache = {}
2178- ParserElement .resetCache ()
2172+ ParserElement .reset_cache ()
21792173 return T .cast (Hlist , result [0 ]) # Known return type from main.
21802174
21812175 def get_state (self ) -> ParserState :
@@ -2191,13 +2185,13 @@ def push_state(self) -> None:
21912185 self ._state_stack .append (self .get_state ().copy ())
21922186
21932187 def main (self , toks : ParseResults ) -> list [Hlist ]:
2194- return [Hlist (toks .asList ())]
2188+ return [Hlist (toks .as_list ())]
21952189
21962190 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 )
21982192
21992193 def math (self , toks : ParseResults ) -> T .Any :
2200- hlist = Hlist (toks .asList ())
2194+ hlist = Hlist (toks .as_list ())
22012195 self .pop_state ()
22022196 return [hlist ]
22032197
@@ -2210,7 +2204,7 @@ def non_math(self, toks: ParseResults) -> T.Any:
22102204 self .get_state ().font = mpl .rcParams ['mathtext.default' ]
22112205 return [hlist ]
22122206
2213- float_literal = staticmethod (pyparsing_common .convertToFloat )
2207+ float_literal = staticmethod (pyparsing_common .convert_to_float )
22142208
22152209 def text (self , toks : ParseResults ) -> T .Any :
22162210 self .push_state ()
@@ -2809,7 +2803,7 @@ def auto_delim(self, toks: ParseResults) -> T.Any:
28092803 return self ._auto_sized_delimiter (
28102804 toks ["left" ],
28112805 # 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 [],
28132807 toks ["right" ])
28142808
28152809 def boldsymbol (self , toks : ParseResults ) -> T .Any :
0 commit comments