138138 Combine , Group , Optional , Forward , NotAny , alphas , nums , alphanums , \
139139 StringStart , StringEnd , ParseFatalException , FollowedBy , Regex , \
140140 operatorPrecedence , opAssoc , ParseResults , Or , Suppress , oneOf , \
141- ParseException , MatchFirst , NoMatch
141+ ParseException , MatchFirst , NoMatch , Empty
142142
143143from matplotlib .afm import AFM
144144from matplotlib .cbook import enumerate , iterable , Bunch , get_realpath_and_stat , \
@@ -1787,6 +1787,14 @@ def vlist_out(self, box):
17871787##############################################################################
17881788# PARSER
17891789
1790+ def Error (msg ):
1791+ def raise_error (s , loc , toks ):
1792+ raise ParseFatalException (msg )
1793+
1794+ empty = Empty ()
1795+ empty .setParseAction (raise_error )
1796+ return empty
1797+
17901798class Parser (object ):
17911799 _binary_operators = Set (r'''
17921800 + *
@@ -1887,9 +1895,10 @@ def __init__(self):
18871895 ).setParseAction (self .space ).setName ('space' )
18881896
18891897 customspace = (Literal (r'\hspace' )
1890- + lbrace
1891- + float
1892- + rbrace
1898+ + (( lbrace
1899+ + float
1900+ + rbrace
1901+ ) | Error (r"Expected \hspace{n}" ))
18931902 ).setParseAction (self .customspace ).setName ('customspace' )
18941903
18951904 symbol = (Regex (r"([a-zA-Z0-9 +\-*/<>=:,.;!'@()])|(\\[%${}\[\]])" )
@@ -1926,8 +1935,8 @@ def __init__(self):
19261935 bslash
19271936 + Literal ("frac" )
19281937 )
1929- + group
1930- + group
1938+ + (( group + group )
1939+ | Error ( r"Expected \frac{num}{den}" ))
19311940 ).setParseAction (self .frac ).setName ("frac" )
19321941
19331942 sqrt = Group (
@@ -1946,7 +1955,7 @@ def __init__(self):
19461955 + Suppress (Literal ("]" )),
19471956 default = None
19481957 )
1949- + group
1958+ + ( group | Error ( "Expected \sqrt{value}" ))
19501959 ).setParseAction (self .sqrt ).setName ("sqrt" )
19511960
19521961 placeable << (accent
@@ -1955,7 +1964,7 @@ def __init__(self):
19551964 ^ group
19561965 ^ frac
19571966 ^ sqrt
1958- )
1967+ ) | Error ( "Expected symbol or group" )
19591968
19601969 simple << (space
19611970 | customspace
@@ -1983,12 +1992,12 @@ def __init__(self):
19831992 leftDelim = oneOf (r"( [ { \lfloor \langle \lceil" )
19841993 rightDelim = oneOf (r") ] } \rfloor \rangle \rceil" )
19851994 autoDelim << (Suppress (Literal (r"\left" ))
1986- + (leftDelim | ambiDelim )
1995+ + (( leftDelim | ambiDelim ) | Error ( "Expected a delimiter" ) )
19871996 + Group (
19881997 autoDelim
19891998 ^ OneOrMore (simple ))
19901999 + Suppress (Literal (r"\right" ))
1991- + (rightDelim | ambiDelim )
2000+ + (( rightDelim | ambiDelim ) | Error ( "Expected a delimiter" ) )
19922001 )
19932002
19942003 math = OneOrMore (
@@ -2007,7 +2016,8 @@ def __init__(self):
20072016 + ZeroOrMore (
20082017 Suppress (math_delim )
20092018 + math
2010- + Suppress (math_delim )
2019+ + (Suppress (math_delim )
2020+ | Error ("Expected end of math '$'" ))
20112021 + non_math
20122022 )
20132023 ) + StringEnd ()
0 commit comments