147147from matplotlib .pyparsing import Combine , Group , Optional , Forward , \
148148 Literal , OneOrMore , ZeroOrMore , ParseException , Empty , \
149149 ParseResults , Suppress , oneOf , StringEnd , ParseFatalException , \
150- FollowedBy , Regex
150+ FollowedBy , Regex , ParserElement
151+ # Enable packrat parsing
152+ ParserElement .enablePackrat ()
151153
152154from matplotlib .afm import AFM
153155from matplotlib .cbook import Bunch , get_realpath_and_stat , \
@@ -1982,7 +1984,7 @@ def __init__(self):
19821984 autoDelim = Forward ().setParseAction (self .auto_sized_delimiter )
19831985 self ._expression = Forward ().setParseAction (self .finish ).setName ("finish" )
19841986
1985- float = Regex (r"-?[0-9]+\.? [0-9]* " )
1987+ float = Regex (r"-?[0-9]*(\. [0-9]+)? " )
19861988
19871989 lbrace = Literal ('{' ).suppress ()
19881990 rbrace = Literal ('}' ).suppress ()
@@ -2001,14 +2003,13 @@ def __init__(self):
20012003 latex2efont = oneOf (['math' + x for x in self ._fontnames ])
20022004
20032005 space = (FollowedBy (bslash )
2004- + (Literal (r'\ ' )
2005- | Literal (r'\/' )
2006- | Literal (r'\,' )
2007- | Literal (r'\;' )
2008- | Literal (r'\quad' )
2009- | Literal (r'\qquad' )
2010- | Literal (r'\!' )
2011- )
2006+ + oneOf ([r'\ ' ,
2007+ r'\/' ,
2008+ r'\,' ,
2009+ r'\;' ,
2010+ r'\quad' ,
2011+ r'\qquad' ,
2012+ r'\!' ])
20122013 ).setParseAction (self .space ).setName ('space' )
20132014
20142015 customspace = (Literal (r'\hspace' )
@@ -2055,19 +2056,13 @@ def __init__(self):
20552056 + latex2efont )
20562057
20572058 frac = Group (
2058- Suppress (
2059- bslash
2060- + Literal ("frac" )
2061- )
2059+ Suppress (Literal (r"\frac" ))
20622060 + ((group + group )
20632061 | Error (r"Expected \frac{num}{den}" ))
20642062 ).setParseAction (self .frac ).setName ("frac" )
20652063
20662064 sqrt = Group (
2067- Suppress (
2068- bslash
2069- + Literal ("sqrt" )
2070- )
2065+ Suppress (Literal (r"\sqrt" ))
20712066 + Optional (
20722067 Suppress (Literal ("[" ))
20732068 + Group (
@@ -2096,9 +2091,7 @@ def __init__(self):
20962091 | subsuper
20972092 )
20982093
2099- subsuperop = (Literal ("_" )
2100- | Literal ("^" )
2101- )
2094+ subsuperop = oneOf (["_" , "^" ])
21022095
21032096 subsuper << Group (
21042097 ( Optional (placeable )
@@ -2127,8 +2120,7 @@ def __init__(self):
21272120 ^ simple
21282121 ).setParseAction (self .math ).setName ("math" )
21292122
2130- math_delim = (~ bslash
2131- + Literal ('$' ))
2123+ math_delim = ~ bslash + Literal ('$' )
21322124
21332125 non_math = Regex (r"(?:(?:\\[$])|[^$])*"
21342126 ).setParseAction (self .non_math ).setName ("non_math" ).leaveWhitespace ()
@@ -2144,8 +2136,6 @@ def __init__(self):
21442136 )
21452137 ) + StringEnd ()
21462138
2147- self ._expression .enablePackrat ()
2148-
21492139 self .clear ()
21502140
21512141 def clear (self ):
0 commit comments