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