comparison 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
comparison
equal deleted inserted replaced
285:afbca50b7dc1 286:051c8877ee22
16 import re 16 import re
17 17
18 import pygments.util 18 import pygments.util
19 from pygments.lexer import bygroups, include, words 19 from pygments.lexer import bygroups, include, words
20 from pygments.token import (Comment, Error, Generic, Keyword, Name, Operator, 20 from pygments.token import (Comment, Error, Generic, Keyword, Name, Operator,
21 Punctuation, Text, Whitespace) 21 Punctuation, Text, Whitespace,
22 string_to_tokentype)
22 23
23 # 24 #
24 # Relative imports do not work with pygments.lexers.load_lexer_from_file() 25 # Relative imports do not work with pygments.lexers.load_lexer_from_file()
25 # in all of our supported Python releases. 26 # in all of our supported Python releases.
26 # 27 #
235 if ctx: 236 if ctx:
236 ctx.pos = match.end() 237 ctx.pos = match.end()
237 238
238 def op_explicit_tokentype_ex_start(lexer, match, ctx): 239 def op_explicit_tokentype_ex_start(lexer, match, ctx):
239 needed_css = match.group("type") 240 needed_css = match.group("type")
240 ctx.explicit_token_type = REVERSED_STANDARD_TYPES.get(needed_css, None) 241 toktype = REVERSED_STANDARD_TYPES.get(needed_css, None)
241 if ctx.explicit_token_type is None: 242 if toktype is None:
243 if not lexer.strict_tokentype:
244 toktype = string_to_tokentype(needed_css)
245 if toktype is None:
246 _logger.warning(
247 "Unhandled explicit token type: %s", needed_css)
248 else:
249 _logger.debug(
250 "Synthesized new token type: %s", needed_css)
251 else:
252 _logger.warning(
253 "Unhandled explicit token type: %s", needed_css)
254 if toktype is None:
242 # Be more error friendly 255 # Be more error friendly
243 ctx.explicit_token_type = Generic.Error 256 ctx.explicit_token_type = Generic.Error
244 _logger.warning("Unhandled explicit token type: %s", match.group())
245 yield match.start(), ctx.explicit_token_type, match.group() 257 yield match.start(), ctx.explicit_token_type, match.group()
258 else:
259 ctx.explicit_token_type = toktype
260 # Nothing to yield: just record the required token type
246 ctx.pos = match.end() 261 ctx.pos = match.end()
247 262
248 def op_explicit_tokentype_ex_value(lexer, match, ctx): 263 def op_explicit_tokentype_ex_value(lexer, match, ctx):
249 yield match.start(), ctx.explicit_token_type, match.group(1) 264 yield match.start(), ctx.explicit_token_type, match.group(1)
250 ctx.pos = match.end() 265 ctx.pos = match.end()
553 } 568 }
554 569
555 def __init__(self, **options): 570 def __init__(self, **options):
556 self.no_end = pygments.util.get_bool_opt( 571 self.no_end = pygments.util.get_bool_opt(
557 options, "no_end", default=False) 572 options, "no_end", default=False)
573 self.strict_tokentype = pygments.util.get_bool_opt(
574 options, "strict_tokentype", default=True)
558 self.symbol_gets = options.get("gets", None) 575 self.symbol_gets = options.get("gets", None)
559 if self.symbol_gets is None: 576 if self.symbol_gets is None:
560 self.symbol_gets = self.SYMBOLS["<-"] # Default: "⟵" # U+27F5 577 self.symbol_gets = self.SYMBOLS["<-"] # Default: "⟵" # U+27F5
561 self.symbol_remark = options.get("remark", None) 578 self.symbol_remark = options.get("remark", None)
562 if self.symbol_remark is None: 579 if self.symbol_remark is None: