comparison pygments_lexer_pseudocode2/bases.py @ 73:a2a56d08b860

FIX: Python 2 needs a "staticmethod" on "op_fixed()" when called via the LexBase. Python 3 does not allow this. Implement proper decoration depending on the Python version.
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 28 Apr 2026 19:29:28 +0200
parents 206017a08ed7
children cd79d2c76347
comparison
equal deleted inserted replaced
72:206017a08ed7 73:a2a56d08b860
6 r"""Some common bases for the lexers.""" 6 r"""Some common bases for the lexers."""
7 7
8 __all__ = ["LexBase", "uni_name", "py_innerstring_rules", "py_name_rules"] 8 __all__ = ["LexBase", "uni_name", "py_innerstring_rules", "py_name_rules"]
9 9
10 10
11 import sys
12
11 from pygments import unistring 13 from pygments import unistring
12 from pygments.lexer import RegexLexer, combined, bygroups, include 14 from pygments.lexer import RegexLexer, combined, bygroups, include
13 from pygments.token import (Comment, Error, Name, Number, Other, String) 15 from pygments.token import (Comment, Error, Name, Number, Other, String)
16
17
18 PY2 = sys.version_info[0] <= 2
14 19
15 20
16 # 21 #
17 # SPDX-SnippetBegin 22 # SPDX-SnippetBegin
18 # SPDX-License-Identifier: BSD-2-Clause 23 # SPDX-License-Identifier: BSD-2-Clause
19 # SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team 24 # SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team
20 # SPDX-SnippetCopyrightText: Copyright 2026 by Franz Glasner 25 # SPDX-SnippetCopyrightText: Copyright 2026 by Franz Glasner
21 # 26 #
22 27
23 uni_name = "[%s][%s]*" % (unistring.xid_start, unistring.xid_continue) 28 uni_name = "[%s][%s]*" % (unistring.xid_start, unistring.xid_continue)
29
30
31 """PY3 allows no @staticmethod but PY2 needs it."""
32 if PY2:
33 _staticmethod = staticmethod
34 else:
35 def _staticmethod(fn):
36 return fn
24 37
25 38
26 def py_innerstring_rules(ttype): 39 def py_innerstring_rules(ttype):
27 return [ 40 return [
28 # the old style '%s' % (...) string formatting (still valid in Py3) 41 # the old style '%s' % (...) string formatting (still valid in Py3)
71 def op_ignore(lexer, match, ctx=None): 84 def op_ignore(lexer, match, ctx=None):
72 """Unconditionally ignore the match.""" 85 """Unconditionally ignore the match."""
73 if False: 86 if False:
74 yield match.start(), Other, "" 87 yield match.start(), Other, ""
75 88
89 @_staticmethod
76 def op_fixed(toktype, value): 90 def op_fixed(toktype, value):
77 """Unconditionally yield a given token type and value.""" 91 """Unconditionally yield a given token type and value."""
78 92
79 def _op_fixed(lexer, match, ctx=None): 93 def _op_fixed(lexer, match, ctx=None):
80 yield match.start(), toktype, value 94 yield match.start(), toktype, value