From nobody Wed Apr 8 12:42:47 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29F173F8E11; Tue, 17 Mar 2026 18:09:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773770990; cv=none; b=dmSZ4YRCV0MwxnI5uq7W0XVkb3a8MhFDdUXwibBdAPytm158ZBJ544Q1Rgh5rM3Y8WJm0KP4ObOqGWMOziTe3HDLnxBH0ScGqInvMb+LWSeRCDky/lYOCp25RaLcfSYPCBJCt9LUptHGBxJQ2flw1BmAbwQZdkDoyt1EzBqsO2Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773770990; c=relaxed/simple; bh=PHXaltNZ4ol9wjdXwRcvdpg7f6YIjhlE0exg45N//uI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BayqnFcXGn61cA7zSAh3c4DfdbQnWphKBysR0hd4wjLeqZU4H2HZL1VuLk3gtx2+d6qZn/cOrfGTjtNc+lDXHFD/DjpHB87jDBeANsaA20+vtrJJwKjom4V+EXrenEs4JKjnQqS44xMPjxLIQlbNJliicMFVX8nGb+3pxOXlp8A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gZrXSmO8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gZrXSmO8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C34CC2BC86; Tue, 17 Mar 2026 18:09:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773770990; bh=PHXaltNZ4ol9wjdXwRcvdpg7f6YIjhlE0exg45N//uI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gZrXSmO8eUuqNsR8v0KCMp8jF81KEFRbJbqAYCDJ2UpCdHIATyteYnkX450rkHfgY V/R7LzUFsxScj+s2vqyXnulzQwxxKKx+sER7lhA+zDhYCX2l7YDlnOp04dtv88a+yD ySZQygOHGu/y6AjAlx3CpV/tVr0b8HnxeOtGICymjfk3I0imb1FkhcxR6r9OXN076s p7W+u2Ego4wP03zptj11rl/z6PvVZYvq0wEXoILGmFqJPHPB0UCd5MQXZt2YZiEP4H X1eNtVqzrJmMm+wP0dRsUQ8At2CK36Wf2Q75nMF/5m8PMEMQQZ+N/MYnK+WBbK7rww 8GCpMF9EVnSbw== Received: from mchehab by mail.kernel.org with local (Exim 4.99.1) (envelope-from ) id 1w2YrU-0000000H5Wi-1EkP; Tue, 17 Mar 2026 19:09:48 +0100 From: Mauro Carvalho Chehab To: Jonathan Corbet , Linux Doc Mailing List Cc: Mauro Carvalho Chehab , linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org, Aleksandr Loktionov , Randy Dunlap Subject: [PATCH v3 15/22] docs: kdoc_re: get rid of NestedMatch class Date: Tue, 17 Mar 2026 19:09:35 +0100 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: Mauro Carvalho Chehab Now that everything was converted to CMatch, we can get rid of the previous NestedMatch implementation. Signed-off-by: Mauro Carvalho Chehab --- tools/lib/python/kdoc/kdoc_re.py | 201 ------------------------------- 1 file changed, 201 deletions(-) diff --git a/tools/lib/python/kdoc/kdoc_re.py b/tools/lib/python/kdoc/kdoc_= re.py index 085b89a4547c..6f3ae28859ea 100644 --- a/tools/lib/python/kdoc/kdoc_re.py +++ b/tools/lib/python/kdoc/kdoc_re.py @@ -140,204 +140,3 @@ class KernRe: """ =20 return self.last_match.groups() - -#: Nested delimited pairs (brackets and parenthesis) -DELIMITER_PAIRS =3D { - '{': '}', - '(': ')', - '[': ']', -} - -#: compiled delimiters -RE_DELIM =3D KernRe(r'[\{\}\[\]\(\)]') - - -class NestedMatch: - """ - Finding nested delimiters is hard with regular expressions. It is - even harder on Python with its normal re module, as there are several - advanced regular expressions that are missing. - - This is the case of this pattern:: - - '\\bSTRUCT_GROUP(\\(((?:(?>[^)(]+)|(?1))*)\\))[^;]*;' - - which is used to properly match open/close parentheses of the - string search STRUCT_GROUP(), - - Add a class that counts pairs of delimiters, using it to match and - replace nested expressions. - - The original approach was suggested by: - - https://stackoverflow.com/questions/5454322/python-how-to-match-ne= sted-parentheses-with-regex - - Although I re-implemented it to make it more generic and match 3 types - of delimiters. The logic checks if delimiters are paired. If not, it - will ignore the search string. - """ - - # TODO: make NestedMatch handle multiple match groups - # - # Right now, regular expressions to match it are defined only up to - # the start delimiter, e.g.: - # - # \bSTRUCT_GROUP\( - # - # is similar to: STRUCT_GROUP\((.*)\) - # except that the content inside the match group is delimiter-aligned. - # - # The content inside parentheses is converted into a single replace - # group (e.g. r`\0'). - # - # It would be nice to change such definition to support multiple - # match groups, allowing a regex equivalent to: - # - # FOO\((.*), (.*), (.*)\) - # - # it is probably easier to define it not as a regular expression, but - # with some lexical definition like: - # - # FOO(arg1, arg2, arg3) - - def __init__(self, regex): - self.regex =3D KernRe(regex) - - def _search(self, line): - """ - Finds paired blocks for a regex that ends with a delimiter. - - The suggestion of using finditer to match pairs came from: - https://stackoverflow.com/questions/5454322/python-how-to-match-ne= sted-parentheses-with-regex - but I ended using a different implementation to align all three ty= pes - of delimiters and seek for an initial regular expression. - - The algorithm seeks for open/close paired delimiters and places th= em - into a stack, yielding a start/stop position of each match when the - stack is zeroed. - - The algorithm should work fine for properly paired lines, but will - silently ignore end delimiters that precede a start delimiter. - This should be OK for kernel-doc parser, as unaligned delimiters - would cause compilation errors. So, we don't need to raise excepti= ons - to cover such issues. - """ - - stack =3D [] - - for match_re in self.regex.finditer(line): - start =3D match_re.start() - offset =3D match_re.end() - string_char =3D None - escape =3D False - - d =3D line[offset - 1] - if d not in DELIMITER_PAIRS: - continue - - end =3D DELIMITER_PAIRS[d] - stack.append(end) - - for match in RE_DELIM.finditer(line[offset:]): - pos =3D match.start() + offset - - d =3D line[pos] - - if escape: - escape =3D False - continue - - if string_char: - if d =3D=3D '\\': - escape =3D True - elif d =3D=3D string_char: - string_char =3D None - - continue - - if d in ('"', "'"): - string_char =3D d - continue - - if d in DELIMITER_PAIRS: - end =3D DELIMITER_PAIRS[d] - - stack.append(end) - continue - - # Does the end delimiter match what is expected? - if stack and d =3D=3D stack[-1]: - stack.pop() - - if not stack: - yield start, offset, pos + 1 - break - - def search(self, line): - """ - This is similar to re.search: - - It matches a regex that it is followed by a delimiter, - returning occurrences only if all delimiters are paired. - """ - - for t in self._search(line): - - yield line[t[0]:t[2]] - - def sub(self, sub, line, count=3D0): - """ - This is similar to re.sub: - - It matches a regex that it is followed by a delimiter, - replacing occurrences only if all delimiters are paired. - - if the sub argument contains:: - - r'\0' - - it will work just like re: it places there the matched paired data - with the delimiter stripped. - - If count is different than zero, it will replace at most count - items. - """ - out =3D "" - - cur_pos =3D 0 - n =3D 0 - - for start, end, pos in self._search(line): - out +=3D line[cur_pos:start] - - # Value, ignoring start/end delimiters - value =3D line[end:pos - 1] - - # replaces \0 at the sub string, if \0 is used there - new_sub =3D sub - new_sub =3D new_sub.replace(r'\0', value) - - out +=3D new_sub - - # Drop end ';' if any - if pos < len(line) and line[pos] =3D=3D ';': - pos +=3D 1 - - cur_pos =3D pos - n +=3D 1 - - if count and count >=3D n: - break - - # Append the remaining string - l =3D len(line) - out +=3D line[cur_pos:l] - - return out - - def __repr__(self): - """ - Returns a displayable version of the class init. - """ - - return f'NestedMatch("{self.regex.regex.pattern}")' --=20 2.52.0