22Implementation details for :mod:`.mathtext`.
33"""
44
5+ import copy
56from collections import namedtuple
67import enum
78import functools
@@ -1781,6 +1782,43 @@ def raise_error(s, loc, toks):
17811782 return empty
17821783
17831784
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+
17841822class Parser :
17851823 """
17861824 A pyparsing-based parser for strings containing math expressions.
@@ -2126,7 +2164,7 @@ def parse(self, s, fonts_object, fontsize, dpi):
21262164 Returns the parse tree of `Node` instances.
21272165 """
21282166 self ._state_stack = [
2129- self . State (fonts_object , 'default' , 'rm' , fontsize , dpi )]
2167+ ParserState (fonts_object , 'default' , 'rm' , fontsize , dpi )]
21302168 self ._em_width_cache = {}
21312169 try :
21322170 result = self ._expression .parseString (s )
@@ -2140,47 +2178,6 @@ def parse(self, s, fonts_object, fontsize, dpi):
21402178 self ._expression .resetCache ()
21412179 return result [0 ]
21422180
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-
21842181 def get_state (self ):
21852182 """Get the current `State` of the parser."""
21862183 return self ._state_stack [- 1 ]
0 commit comments