diff pygments_lexer_pseudocode2/lexers/algpseudocode.py @ 286:051c8877ee22

Implement lexer option "strict_tokentype". It allows the \ttX command to synthesize not yet existing token types.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 21 May 2026 09:32:35 +0200
parents afbca50b7dc1
children 298841bc4dee
line wrap: on
line diff
--- a/pygments_lexer_pseudocode2/lexers/algpseudocode.py	Wed May 20 20:35:37 2026 +0200
+++ b/pygments_lexer_pseudocode2/lexers/algpseudocode.py	Thu May 21 09:32:35 2026 +0200
@@ -18,7 +18,8 @@
 import pygments.util
 from pygments.lexer import bygroups, include, words
 from pygments.token import (Comment, Error, Generic, Keyword, Name, Operator,
-                            Punctuation, Text, Whitespace)
+                            Punctuation, Text, Whitespace,
+                            string_to_tokentype)
 
 #
 # Relative imports do not work with pygments.lexers.load_lexer_from_file()
@@ -237,12 +238,26 @@
 
     def op_explicit_tokentype_ex_start(lexer, match, ctx):
         needed_css = match.group("type")
-        ctx.explicit_token_type = REVERSED_STANDARD_TYPES.get(needed_css, None)
-        if ctx.explicit_token_type is None:
+        toktype = REVERSED_STANDARD_TYPES.get(needed_css, None)
+        if toktype is None:
+            if not lexer.strict_tokentype:
+                toktype = string_to_tokentype(needed_css)
+                if toktype is None:
+                    _logger.warning(
+                        "Unhandled explicit token type: %s", needed_css)
+                else:
+                    _logger.debug(
+                        "Synthesized new token type: %s", needed_css)
+            else:
+                _logger.warning(
+                    "Unhandled explicit token type: %s", needed_css)
+        if toktype is None:
             # Be more error friendly
             ctx.explicit_token_type = Generic.Error
-            _logger.warning("Unhandled explicit token type: %s", match.group())
             yield match.start(), ctx.explicit_token_type, match.group()
+        else:
+            ctx.explicit_token_type = toktype
+            # Nothing to yield: just record the required token type
         ctx.pos = match.end()
 
     def op_explicit_tokentype_ex_value(lexer, match, ctx):
@@ -555,6 +570,8 @@
     def __init__(self, **options):
         self.no_end = pygments.util.get_bool_opt(
             options, "no_end", default=False)
+        self.strict_tokentype = pygments.util.get_bool_opt(
+            options, "strict_tokentype", default=True)
         self.symbol_gets = options.get("gets", None)
         if self.symbol_gets is None:
             self.symbol_gets = self.SYMBOLS["<-"]   # Default: "⟵"  # U+27F5