1313
1414import numpy as np
1515from pyparsing import (
16- Empty , Forward , NotAny , oneOf , OneOrMore , Optional , ParseBaseException ,
17- ParseFatalException , ParserElement , ParseResults , QuotedString , Regex ,
18- StringEnd , ZeroOrMore , pyparsing_common )
16+ Empty , Forward , Literal , NotAny , oneOf , OneOrMore , Optional ,
17+ ParseBaseException , ParseFatalException , ParserElement , ParseResults ,
18+ QuotedString , Regex , StringEnd , ZeroOrMore , pyparsing_common )
1919
2020import matplotlib as mpl
2121from . import cbook
@@ -1695,70 +1695,72 @@ class _MathStyle(enum.Enum):
16951695
16961696 def __init__ (self ):
16971697 p = types .SimpleNamespace ()
1698- # All forward declarations are here
1698+
1699+ def set_names_and_parse_actions ():
1700+ for key , val in vars (p ).items ():
1701+ if not key .startswith ('_' ):
1702+ # Set names on everything -- very useful for debugging
1703+ val .setName (key )
1704+ # Set actions
1705+ if hasattr (self , key ):
1706+ val .setParseAction (getattr (self , key ))
1707+
1708+ # Root definitions.
1709+
1710+ p .float_literal = Regex (r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)" )
1711+ p .space = oneOf (self ._space_widths )("space" )
1712+
1713+ p .single_symbol = Regex (
1714+ r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
1715+ "\U00000080 -\U0001ffff " # unicode range
1716+ )("sym" )
1717+ p .accentprefixed = "\\ " + oneOf (self ._accentprefixed )("sym" )
1718+ p .symbol_name = (
1719+ oneOf ([rf"\{ sym } " for sym in tex2uni ])("sym" )
1720+ + Regex ("(?=[^A-Za-z]|$)" ).leaveWhitespace ())
1721+ p .symbol = (p .single_symbol | p .symbol_name ).leaveWhitespace ()
1722+ p .unknown_symbol = Regex (r"\\[A-Za-z]*" )("name" )
1723+
1724+ p .font = "\\ " + oneOf (self ._fontnames )("font" )
1725+ p .start_group = (
1726+ Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{" )
1727+ p .end_group = Literal ("}" )
1728+
1729+ p .ambi_delim = oneOf (self ._ambi_delim )
1730+ p .left_delim = oneOf (self ._left_delim )
1731+ p .right_delim = oneOf (self ._right_delim )
1732+
1733+ set_names_and_parse_actions () # for root definitions.
1734+
1735+ # Mutually recursive definitions. (Minimizing the number of Forward
1736+ # elements is important for speed.)
16991737 p .accent = Forward ()
1700- p .ambi_delim = Forward ()
17011738 p .auto_delim = Forward ()
17021739 p .binom = Forward ()
17031740 p .customspace = Forward ()
1704- p .end_group = Forward ()
1705- p .float_literal = Forward ()
1706- p .font = Forward ()
17071741 p .frac = Forward ()
17081742 p .dfrac = Forward ()
17091743 p .function = Forward ()
17101744 p .genfrac = Forward ()
17111745 p .group = Forward ()
1712- p .left_delim = Forward ()
1713- p .main = Forward ()
1714- p .math = Forward ()
1715- p .math_string = Forward ()
1716- p .non_math = Forward ()
17171746 p .operatorname = Forward ()
17181747 p .overline = Forward ()
17191748 p .overset = Forward ()
17201749 p .placeable = Forward ()
17211750 p .required_group = Forward ()
1722- p .right_delim = Forward ()
17231751 p .simple = Forward ()
17241752 p .simple_group = Forward ()
1725- p .single_symbol = Forward ()
1726- p .accentprefixed = Forward ()
1727- p .space = Forward ()
17281753 p .sqrt = Forward ()
1729- p .start_group = Forward ()
17301754 p .subsuper = Forward ()
1731- p .symbol = Forward ()
1732- p .symbol_name = Forward ()
17331755 p .token = Forward ()
17341756 p .underset = Forward ()
1735- p .unknown_symbol = Forward ()
17361757
1737- for key , val in vars (p ).items ():
1738- if not key .startswith ('_' ):
1739- # Set names on everything -- very useful for debugging
1740- val .setName (key )
1741- # Set actions
1742- if hasattr (self , key ):
1743- val .setParseAction (getattr (self , key ))
1758+ set_names_and_parse_actions () # for mutually recursive definitions.
17441759
1745- p .float_literal <<= Regex (r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)" )
1746-
1747- p .space <<= oneOf (self ._space_widths )("space" )
17481760 p .customspace <<= r"\hspace" - (
17491761 "{" + p .float_literal ("space" ) + "}"
17501762 | Error (r"Expected \hspace{n}" ))
17511763
1752- p .single_symbol <<= Regex (
1753- r"([a-zA-Z0-9 +\-*/<>=:,.;!\?&'@()\[\]|%s])|(\\[%%${}\[\]_|])" %
1754- "\U00000080 -\U0001ffff " # unicode range
1755- )("sym" )
1756- p .accentprefixed <<= "\\ " + oneOf (self ._accentprefixed )("sym" )
1757- p .symbol_name <<= (
1758- oneOf ([rf"\{ sym } " for sym in tex2uni ])("sym" )
1759- + Regex ("(?=[^A-Za-z]|$)" ).leaveWhitespace ())
1760- p .symbol <<= (p .single_symbol | p .symbol_name ).leaveWhitespace ()
1761-
17621764 p .accent <<= (
17631765 "\\ "
17641766 + oneOf ([* self ._accent_map , * self ._wide_accents ])("accent" )
@@ -1769,14 +1771,9 @@ def __init__(self):
17691771 "{" + ZeroOrMore (p .simple | p .unknown_symbol )("name" ) + "}"
17701772 | Error (r"Expected \operatorname{name}" ))
17711773
1772- p .start_group <<= (
1773- Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{" )
1774- p .end_group <<= "}"
17751774 p .group <<= (
17761775 p .start_group + ZeroOrMore (p .token )("group" ) + p .end_group )
17771776
1778- p .font <<= "\\ " + oneOf (self ._fontnames )("font" )
1779-
17801777 p .simple_group <<= "{" + ZeroOrMore (p .token )("group" ) + "}"
17811778 p .required_group <<= "{" + OneOrMore (p .token )("group" ) + "}"
17821779
@@ -1790,10 +1787,6 @@ def __init__(self):
17901787 p .required_group ("num" ) + p .required_group ("den" )
17911788 | Error (r"Expected \binom{num}{den}" ))
17921789
1793- p .ambi_delim <<= oneOf (self ._ambi_delim )
1794- p .left_delim <<= oneOf (self ._left_delim )
1795- p .right_delim <<= oneOf (self ._right_delim )
1796-
17971790 p .genfrac <<= r"\genfrac" - (
17981791 "{" + Optional (p .ambi_delim | p .left_delim )("ldelim" ) + "}"
17991792 + "{" + Optional (p .ambi_delim | p .right_delim )("rdelim" ) + "}"
@@ -1820,8 +1813,6 @@ def __init__(self):
18201813 p .simple_group ("annotation" ) + p .simple_group ("body" )
18211814 | Error (r"Expected \underset{annotation}{body}" ))
18221815
1823- p .unknown_symbol <<= Regex (r"\\[A-Za-z]*" )("name" )
1824-
18251816 p .placeable <<= (
18261817 p .accentprefixed # Must be before accent so named symbols that are
18271818 # prefixed with an accent name work
@@ -1872,15 +1863,14 @@ def __init__(self):
18721863 | Error ("Expected a delimiter" ))
18731864 )
18741865
1875- p .math <<= OneOrMore (p .token )
1876-
1877- p .math_string <<= QuotedString ('$' , '\\ ' , unquoteResults = False )
1878-
1879- p .non_math <<= Regex (r"(?:(?:\\[$])|[^$])*" ).leaveWhitespace ()
1880-
1881- p .main <<= (
1866+ # Leaf definitions.
1867+ p .math = OneOrMore (p .token )
1868+ p .math_string = QuotedString ('$' , '\\ ' , unquoteResults = False )
1869+ p .non_math = Regex (r"(?:(?:\\[$])|[^$])*" ).leaveWhitespace ()
1870+ p .main = (
18821871 p .non_math + ZeroOrMore (p .math_string + p .non_math ) + StringEnd ()
18831872 )
1873+ set_names_and_parse_actions () # for leaf definitions.
18841874
18851875 self ._expression = p .main
18861876 self ._math_expression = p .math
0 commit comments