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