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