13
13
14
14
import numpy as np
15
15
from 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 )
19
19
20
20
import matplotlib as mpl
21
21
from . import cbook
@@ -1695,70 +1695,72 @@ class _MathStyle(enum.Enum):
1695
1695
1696
1696
def __init__ (self ):
1697
1697
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.)
1699
1737
p .accent = Forward ()
1700
- p .ambi_delim = Forward ()
1701
1738
p .auto_delim = Forward ()
1702
1739
p .binom = Forward ()
1703
1740
p .customspace = Forward ()
1704
- p .end_group = Forward ()
1705
- p .float_literal = Forward ()
1706
- p .font = Forward ()
1707
1741
p .frac = Forward ()
1708
1742
p .dfrac = Forward ()
1709
1743
p .function = Forward ()
1710
1744
p .genfrac = Forward ()
1711
1745
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 ()
1717
1746
p .operatorname = Forward ()
1718
1747
p .overline = Forward ()
1719
1748
p .overset = Forward ()
1720
1749
p .placeable = Forward ()
1721
1750
p .required_group = Forward ()
1722
- p .right_delim = Forward ()
1723
1751
p .simple = Forward ()
1724
1752
p .simple_group = Forward ()
1725
- p .single_symbol = Forward ()
1726
- p .accentprefixed = Forward ()
1727
- p .space = Forward ()
1728
1753
p .sqrt = Forward ()
1729
- p .start_group = Forward ()
1730
1754
p .subsuper = Forward ()
1731
- p .symbol = Forward ()
1732
- p .symbol_name = Forward ()
1733
1755
p .token = Forward ()
1734
1756
p .underset = Forward ()
1735
- p .unknown_symbol = Forward ()
1736
1757
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.
1744
1759
1745
- p .float_literal <<= Regex (r"[-+]?([0-9]+\.?[0-9]*|\.[0-9]+)" )
1746
-
1747
- p .space <<= oneOf (self ._space_widths )("space" )
1748
1760
p .customspace <<= r"\hspace" - (
1749
1761
"{" + p .float_literal ("space" ) + "}"
1750
1762
| Error (r"Expected \hspace{n}" ))
1751
1763
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
-
1762
1764
p .accent <<= (
1763
1765
"\\ "
1764
1766
+ oneOf ([* self ._accent_map , * self ._wide_accents ])("accent" )
@@ -1769,14 +1771,9 @@ def __init__(self):
1769
1771
"{" + ZeroOrMore (p .simple | p .unknown_symbol )("name" ) + "}"
1770
1772
| Error (r"Expected \operatorname{name}" ))
1771
1773
1772
- p .start_group <<= (
1773
- Optional (r"\math" + oneOf (self ._fontnames )("font" )) + "{" )
1774
- p .end_group <<= "}"
1775
1774
p .group <<= (
1776
1775
p .start_group + ZeroOrMore (p .token )("group" ) + p .end_group )
1777
1776
1778
- p .font <<= "\\ " + oneOf (self ._fontnames )("font" )
1779
-
1780
1777
p .simple_group <<= "{" + ZeroOrMore (p .token )("group" ) + "}"
1781
1778
p .required_group <<= "{" + OneOrMore (p .token )("group" ) + "}"
1782
1779
@@ -1790,10 +1787,6 @@ def __init__(self):
1790
1787
p .required_group ("num" ) + p .required_group ("den" )
1791
1788
| Error (r"Expected \binom{num}{den}" ))
1792
1789
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
-
1797
1790
p .genfrac <<= r"\genfrac" - (
1798
1791
"{" + Optional (p .ambi_delim | p .left_delim )("ldelim" ) + "}"
1799
1792
+ "{" + Optional (p .ambi_delim | p .right_delim )("rdelim" ) + "}"
@@ -1820,8 +1813,6 @@ def __init__(self):
1820
1813
p .simple_group ("annotation" ) + p .simple_group ("body" )
1821
1814
| Error (r"Expected \underset{annotation}{body}" ))
1822
1815
1823
- p .unknown_symbol <<= Regex (r"\\[A-Za-z]*" )("name" )
1824
-
1825
1816
p .placeable <<= (
1826
1817
p .accentprefixed # Must be before accent so named symbols that are
1827
1818
# prefixed with an accent name work
@@ -1872,15 +1863,14 @@ def __init__(self):
1872
1863
| Error ("Expected a delimiter" ))
1873
1864
)
1874
1865
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 = (
1882
1871
p .non_math + ZeroOrMore (p .math_string + p .non_math ) + StringEnd ()
1883
1872
)
1873
+ set_names_and_parse_actions () # for leaf definitions.
1884
1874
1885
1875
self ._expression = p .main
1886
1876
self ._math_expression = p .math
0 commit comments