comparison pygments_lexer_pseudocode2/algpseudocode.py @ 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 d8ca835c74ea
comparison
equal deleted inserted replaced
84:3ac1c4502ad0 85:ae5e741d2a9b
1
2 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
3 # :- 2 # :-
4 # SPDX-FileCopyrightText: © 2026 Franz Glasner 3 # SPDX-FileCopyrightText: © 2026 Franz Glasner
5 # SPDX-License-Identifier: MIT 4 # SPDX-License-Identifier: MIT
6 # :- 5 # :-
16 import re 15 import re
17 16
18 import pygments.util 17 import pygments.util
19 from pygments.lexer import bygroups, include, words 18 from pygments.lexer import bygroups, include, words
20 from pygments.token import (Comment, Keyword, Name, Operator, Punctuation, 19 from pygments.token import (Comment, Keyword, Name, Operator, Punctuation,
21 Text, Whitespace, 20 Text, Whitespace)
22 STANDARD_TYPES)
23 21
24 # 22 #
25 # Relative imports do not work with pygments.lexers.load_lexer_from_file() 23 # Relative imports do not work with pygments.lexers.load_lexer_from_file()
26 # in all of our supported Python releases. 24 # in all of our supported Python releases.
27 # 25 #
28 from pygments_lexer_pseudocode2.bases import LexBase 26 from pygments_lexer_pseudocode2.bases import LexBase
27 from pygments_lexer_pseudocode2.utils import REVERSED_STANDARD_TYPES
29 28
30 29
31 class AlgPseudocodeLexer(LexBase): 30 class AlgPseudocodeLexer(LexBase):
32 31
33 """A pseudocode lexer along the lines of CTAN's algpseudocode or 32 """A pseudocode lexer along the lines of CTAN's algpseudocode or
154 153
155 return _op_symbol 154 return _op_symbol
156 155
157 def op_explicit_tokentype(lexer, match, ctx=None): 156 def op_explicit_tokentype(lexer, match, ctx=None):
158 needed_css = match.group("type") 157 needed_css = match.group("type")
159 for ttype, css in STANDARD_TYPES.items(): 158 toktype = REVERSED_STANDARD_TYPES.get(needed_css, Text)
160 if css == needed_css:
161 toktype = ttype
162 break
163 else:
164 toktype = Text
165 yield match.start(), toktype, match.group("character") 159 yield match.start(), toktype, match.group("character")
166 if ctx: 160 if ctx:
167 ctx.pos = match.end() 161 ctx.pos = match.end()
168 162
169 tokens = { 163 tokens = {