22Implementation details for :mod:`.mathtext`.
33"""
44
5+ import copy
56from collections import namedtuple
67import enum
78import functools
@@ -1783,6 +1784,43 @@ def raise_error(s, loc, toks):
17831784 return empty
17841785
17851786
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+
17861824class Parser :
17871825 """
17881826 A pyparsing-based parser for strings containing math expressions.
@@ -2128,7 +2166,7 @@ def parse(self, s, fonts_object, fontsize, dpi):
21282166 Returns the parse tree of `Node` instances.
21292167 """
21302168 self ._state_stack = [
2131- self . State (fonts_object , 'default' , 'rm' , fontsize , dpi )]
2169+ ParserState (fonts_object , 'default' , 'rm' , fontsize , dpi )]
21322170 self ._em_width_cache = {}
21332171 try :
21342172 result = self ._expression .parseString (s )
@@ -2142,47 +2180,6 @@ def parse(self, s, fonts_object, fontsize, dpi):
21422180 self ._expression .resetCache ()
21432181 return result [0 ]
21442182
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-
21862183 def get_state (self ):
21872184 """Get the current `State` of the parser."""
21882185 return self ._state_stack [- 1 ]
0 commit comments