Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9c04e1

Browse files
committed
Lift Parser.State to toplevel as standalone class.
It doesn't need to be a nested class.
1 parent e145328 commit c9c04e1

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Implementation details for :mod:`.mathtext`.
33
"""
44

5+
import copy
56
from collections import namedtuple
67
import enum
78
import functools
@@ -1785,6 +1786,43 @@ def raise_error(s, loc, toks):
17851786
return empty
17861787

17871788

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+
17881826
class Parser:
17891827
"""
17901828
A pyparsing-based parser for strings containing math expressions.
@@ -2130,7 +2168,7 @@ def parse(self, s, fonts_object, fontsize, dpi):
21302168
Returns the parse tree of `Node` instances.
21312169
"""
21322170
self._state_stack = [
2133-
self.State(fonts_object, 'default', 'rm', fontsize, dpi)]
2171+
ParserState(fonts_object, 'default', 'rm', fontsize, dpi)]
21342172
self._em_width_cache = {}
21352173
try:
21362174
result = self._expression.parseString(s)
@@ -2144,47 +2182,6 @@ def parse(self, s, fonts_object, fontsize, dpi):
21442182
self._expression.resetCache()
21452183
return result[0]
21462184

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-
21882185
def get_state(self):
21892186
"""Get the current `State` of the parser."""
21902187
return self._state_stack[-1]

0 commit comments

Comments
 (0)