2
2
Implementation details for :mod:`.mathtext`.
3
3
"""
4
4
5
+ import copy
5
6
from collections import namedtuple
6
7
import enum
7
8
import functools
@@ -1781,6 +1782,43 @@ def raise_error(s, loc, toks):
1781
1782
return empty
1782
1783
1783
1784
1785
+ class ParserState :
1786
+ """
1787
+ Parser state.
1788
+
1789
+ States are pushed and popped from a stack as necessary, and the "current"
1790
+ state is always at the top of the stack.
1791
+
1792
+ Upon entering and leaving a group { } or math/non-math, the stack is pushed
1793
+ and popped accordingly.
1794
+ """
1795
+
1796
+ def __init__ (self , font_output , font , font_class , fontsize , dpi ):
1797
+ self .font_output = font_output
1798
+ self ._font = font
1799
+ self .font_class = font_class
1800
+ self .fontsize = fontsize
1801
+ self .dpi = dpi
1802
+
1803
+ def copy (self ):
1804
+ return copy .copy (self )
1805
+
1806
+ @property
1807
+ def font (self ):
1808
+ return self ._font
1809
+
1810
+ @font .setter
1811
+ def font (self , name ):
1812
+ if name in ('rm' , 'it' , 'bf' ):
1813
+ self .font_class = name
1814
+ self ._font = name
1815
+
1816
+ def get_current_underline_thickness (self ):
1817
+ """Return the underline thickness for this state."""
1818
+ return self .font_output .get_underline_thickness (
1819
+ self .font , self .fontsize , self .dpi )
1820
+
1821
+
1784
1822
class Parser :
1785
1823
"""
1786
1824
A pyparsing-based parser for strings containing math expressions.
@@ -2126,7 +2164,7 @@ def parse(self, s, fonts_object, fontsize, dpi):
2126
2164
Returns the parse tree of `Node` instances.
2127
2165
"""
2128
2166
self ._state_stack = [
2129
- self . State (fonts_object , 'default' , 'rm' , fontsize , dpi )]
2167
+ ParserState (fonts_object , 'default' , 'rm' , fontsize , dpi )]
2130
2168
self ._em_width_cache = {}
2131
2169
try :
2132
2170
result = self ._expression .parseString (s )
@@ -2140,47 +2178,6 @@ def parse(self, s, fonts_object, fontsize, dpi):
2140
2178
self ._expression .resetCache ()
2141
2179
return result [0 ]
2142
2180
2143
- # The state of the parser is maintained in a stack. Upon
2144
- # entering and leaving a group { } or math/non-math, the stack
2145
- # is pushed and popped accordingly. The current state always
2146
- # exists in the top element of the stack.
2147
- class State :
2148
- """
2149
- Stores the state of the parser.
2150
-
2151
- States are pushed and popped from a stack as necessary, and
2152
- the "current" state is always at the top of the stack.
2153
- """
2154
- def __init__ (self , font_output , font , font_class , fontsize , dpi ):
2155
- self .font_output = font_output
2156
- self ._font = font
2157
- self .font_class = font_class
2158
- self .fontsize = fontsize
2159
- self .dpi = dpi
2160
-
2161
- def copy (self ):
2162
- return Parser .State (
2163
- self .font_output ,
2164
- self .font ,
2165
- self .font_class ,
2166
- self .fontsize ,
2167
- self .dpi )
2168
-
2169
- @property
2170
- def font (self ):
2171
- return self ._font
2172
-
2173
- @font .setter
2174
- def font (self , name ):
2175
- if name in ('rm' , 'it' , 'bf' ):
2176
- self .font_class = name
2177
- self ._font = name
2178
-
2179
- def get_current_underline_thickness (self ):
2180
- """Return the underline thickness for this state."""
2181
- return self .font_output .get_underline_thickness (
2182
- self .font , self .fontsize , self .dpi )
2183
-
2184
2181
def get_state (self ):
2185
2182
"""Get the current `State` of the parser."""
2186
2183
return self ._state_stack [- 1 ]
0 commit comments