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

Skip to content

Commit 713919d

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

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
@@ -1777,6 +1778,43 @@ def raise_error(s, loc, toks):
17771778
return empty
17781779

17791780

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+
17801818
class Parser:
17811819
"""
17821820
A pyparsing-based parser for strings containing math expressions.
@@ -2122,7 +2160,7 @@ def parse(self, s, fonts_object, fontsize, dpi):
21222160
Returns the parse tree of `Node` instances.
21232161
"""
21242162
self._state_stack = [
2125-
self.State(fonts_object, 'default', 'rm', fontsize, dpi)]
2163+
ParserState(fonts_object, 'default', 'rm', fontsize, dpi)]
21262164
self._em_width_cache = {}
21272165
try:
21282166
result = self._expression.parseString(s)
@@ -2136,47 +2174,6 @@ def parse(self, s, fonts_object, fontsize, dpi):
21362174
self._expression.resetCache()
21372175
return result[0]
21382176

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

0 commit comments

Comments
 (0)