From f2124dd88fd6782ce0b8bc9d5fd4fc788e539605 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Mon, 22 Jan 2024 14:32:56 -0600 Subject: [PATCH] Work around pyparsing diagnostic warnings Closes #25204 --- lib/matplotlib/_fontconfig_pattern.py | 4 ++-- lib/matplotlib/_mathtext.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/_fontconfig_pattern.py b/lib/matplotlib/_fontconfig_pattern.py index 6802c6ae24d7..a1341c633243 100644 --- a/lib/matplotlib/_fontconfig_pattern.py +++ b/lib/matplotlib/_fontconfig_pattern.py @@ -13,7 +13,7 @@ import re from pyparsing import ( - Optional, ParseException, Regex, StringEnd, Suppress, ZeroOrMore, oneOf) + Group, Optional, ParseException, Regex, StringEnd, Suppress, ZeroOrMore, oneOf) _family_punc = r'\\\-:,' @@ -61,7 +61,7 @@ def comma_separated(elem): size = Regex(r"([0-9]+\.?[0-9]*|\.[0-9]+)") name = Regex(r"[a-z]+") value = Regex(fr"([^{_value_punc}]|(\\[{_value_punc}]))*") - prop = (name + Suppress("=") + comma_separated(value)) | oneOf(_CONSTANTS) + prop = Group((name + Suppress("=") + comma_separated(value)) | oneOf(_CONSTANTS)) return ( Optional(comma_separated(family)("families")) + Optional("-" + comma_separated(size)("sizes")) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index 86091f79d5bb..6e4df209b1f9 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -2030,10 +2030,18 @@ def csnames(group: str, names: Iterable[str]) -> Regex: # elements is important for speed.) p.auto_delim = Forward() p.placeable = Forward() + p.named_placeable = Forward() p.required_group = Forward() p.optional_group = Forward() p.token = Forward() + # Workaround for placable being part of a cycle of definitions + # calling `p.placeable("name")` results in a copy, so not guaranteed + # to get the definition added after it is used. + # ref https://github.com/matplotlib/matplotlib/issues/25204 + # xref https://github.com/pyparsing/pyparsing/issues/95 + p.named_placeable <<= p.placeable + set_names_and_parse_actions() # for mutually recursive definitions. p.optional_group <<= "{" + ZeroOrMore(p.token)("group") + "}" @@ -2043,7 +2051,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex: p.accent = ( csnames("accent", [*self._accent_map, *self._wide_accents]) - - p.placeable("sym")) + - p.named_placeable("sym")) p.function = csnames("name", self._function_names) @@ -2089,7 +2097,7 @@ def csnames(group: str, names: Iterable[str]) -> Regex: + OneOrMore(oneOf(["_", "^"]) - p.placeable)("subsuper") + Regex("'*")("apostrophes")) | Regex("'+")("apostrophes") - | (p.placeable("nucleus") + Regex("'*")("apostrophes")) + | (p.named_placeable("nucleus") + Regex("'*")("apostrophes")) ) p.simple = p.space | p.customspace | p.font | p.subsuper