changeset 85:ae5e741d2a9b

Optimize op_explicit_tokentype(): use a pref-computed reversed pygments.token.STANDARD_TYPES
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 30 Apr 2026 19:56:39 +0200
parents 3ac1c4502ad0
children 0c05dc09c9e2
files pygments_lexer_pseudocode2/algpseudocode.py pygments_lexer_pseudocode2/utils.py
diffstat 2 files changed, 24 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/pygments_lexer_pseudocode2/algpseudocode.py	Thu Apr 30 19:37:24 2026 +0200
+++ b/pygments_lexer_pseudocode2/algpseudocode.py	Thu Apr 30 19:56:39 2026 +0200
@@ -1,4 +1,3 @@
-
 # -*- coding: utf-8 -*-
 # :-
 # SPDX-FileCopyrightText: © 2026 Franz Glasner
@@ -18,14 +17,14 @@
 import pygments.util
 from pygments.lexer import bygroups, include, words
 from pygments.token import (Comment, Keyword, Name, Operator, Punctuation,
-                            Text, Whitespace,
-                            STANDARD_TYPES)
+                            Text, Whitespace)
 
 #
 # Relative imports do not work with pygments.lexers.load_lexer_from_file()
 # in all of our supported Python releases.
 #
 from pygments_lexer_pseudocode2.bases import LexBase
+from pygments_lexer_pseudocode2.utils import REVERSED_STANDARD_TYPES
 
 
 class AlgPseudocodeLexer(LexBase):
@@ -156,12 +155,7 @@
 
     def op_explicit_tokentype(lexer, match, ctx=None):
         needed_css = match.group("type")
-        for ttype, css in STANDARD_TYPES.items():
-            if css == needed_css:
-                toktype = ttype
-                break
-        else:
-            toktype = Text
+        toktype = REVERSED_STANDARD_TYPES.get(needed_css, Text)
         yield match.start(), toktype, match.group("character")
         if ctx:
             ctx.pos = match.end()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pygments_lexer_pseudocode2/utils.py	Thu Apr 30 19:56:39 2026 +0200
@@ -0,0 +1,21 @@
+# -*- coding: utf-8;  -*-
+# :-
+# SPDX-FileCopyrightText: © 2026 Franz Glasner
+# SPDX-License-Identifier: MIT
+# :-
+r"""Some helper utility functions and data.
+
+"""
+
+__all__ = ["REVERSED_STANDARD_TYPES"]
+
+
+import pygments.token
+
+
+REVERSED_STANDARD_TYPES = {}
+
+# Side-effect on import
+for _toktype, _cssstyle in pygments.token.STANDARD_TYPES.items():
+    REVERSED_STANDARD_TYPES[_cssstyle] = _toktype
+del _toktype, _cssstyle