comparison pygments_lexer_pseudocode2/utils.py @ 288:298841bc4dee

Allow "normal" Pygments token names in "\ttX" ("Error", "Text.Whitespace", ...)
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 22 May 2026 12:32:38 +0200
parents ae5e741d2a9b
children
comparison
equal deleted inserted replaced
287:f506d752e801 288:298841bc4dee
5 # :- 5 # :-
6 r"""Some helper utility functions and data. 6 r"""Some helper utility functions and data.
7 7
8 """ 8 """
9 9
10 __all__ = ["REVERSED_STANDARD_TYPES"] 10 __all__ = [
11 "REVERSED_STANDARD_TYPES",
12 "string_to_defined_tokentype",
13 ]
11 14
12 15
13 import pygments.token 16 import pygments.token
14 17
15 18
17 20
18 # Side-effect on import 21 # Side-effect on import
19 for _toktype, _cssstyle in pygments.token.STANDARD_TYPES.items(): 22 for _toktype, _cssstyle in pygments.token.STANDARD_TYPES.items():
20 REVERSED_STANDARD_TYPES[_cssstyle] = _toktype 23 REVERSED_STANDARD_TYPES[_cssstyle] = _toktype
21 del _toktype, _cssstyle 24 del _toktype, _cssstyle
25
26
27 def string_to_defined_tokentype(s):
28 """Determine whether the token type `s` given as string is defined.
29
30 :param str s: A token type string as in
31 :py:func:`pygments.token.string_to_tokentype`.
32 :returns: An **existing** token if
33 :py:func:`pygments.token.string_to_tokentype`
34 would return an already existing token type,
35 :py:obj:`None` otherwise.
36 :rtype: :py:class:`pygments.token._TokenType` or :py:obj:`None`
37
38 This implementation is needed because
39 :py:func:`pygments.token.string_to_tokentype` synthesizes a new token
40 on not yet existing token types.
41 And :py:func:`is_token_subtype` works only on token instances.
42
43 """
44 ttype = pygments.token.Token
45 ttype_prefix = "Token."
46 if not s:
47 return ttype
48 for part in s.split("."):
49 for subtype in ttype.subtypes:
50 subtypename = str(subtype)
51 # Remove prefix
52 assert subtypename.startswith(ttype_prefix)
53 subtypename = subtypename[len(ttype_prefix):]
54 if subtypename == part:
55 ttype = subtype
56 ttype_prefix += "%s." % (part,)
57 break
58 else:
59 return None
60 return ttype