Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
diff pygments_lexer_pseudocode2/bases.py @ 56:661461fb4dfc
Make the "py-name" rules parameterized: allow to provide the token type.
For this to work the implemting function must be global, because the
access to the class is not yet possible at construction time.
So consistently make some previons LexBase members module globals.
Make some LexBase members module globals consistently.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 27 Apr 2026 12:37:27 +0200 |
| parents | 5bfa9113d3c4 |
| children | 7153e945a3d6 |
line wrap: on
line diff
--- a/pygments_lexer_pseudocode2/bases.py Sun Apr 26 19:19:55 2026 +0200 +++ b/pygments_lexer_pseudocode2/bases.py Mon Apr 27 12:37:27 2026 +0200 @@ -5,7 +5,7 @@ # :- r"""Some common bases for the lexers.""" -__all__ = ["LexBase"] +__all__ = ["LexBase", "uni_name", "py_innerstring_rules", "py_name_rules"] from pygments import unistring @@ -13,6 +13,53 @@ from pygments.token import Error, Name, Number, String, Comment +# +# SPDX-SnippetBegin +# SPDX-License-Identifier: BSD-2-Clause +# SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team +# SPDX-SnippetCopyrightText: Copyright 2026 by Franz Glasner +# + +uni_name = "[%s][%s]*" % (unistring.xid_start, unistring.xid_continue) + + +def py_innerstring_rules(ttype): + return [ + # the old style '%s' % (...) string formatting (still valid in Py3) + (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' + '[hlL]?[E-GXc-giorsaux%]', String.Interpol), + # the new style '{}'.format(...) string formatting + (r'\{' + r'((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name + r'(\![sra])?' # conversion + r'(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?' + r'\}', String.Interpol), + # + # backslashes, quotes and formatting signs must be parsed + # one at a time + # + (r'[^\\\'"%{\n]+', ttype), + (r'[\'"\\]', ttype), + # unhandled string formatting sign + (r'%|(\{{1,2})', ttype) + # newlines are an error (use "nl" state) + ] + + +def py_name_rules(ttype, deco_ttype=Name.Decorator): + return [ + # We recognize decorator syntax here + (r'@' + uni_name, deco_ttype), + # + # Python's new matrix multiplication operator: + # not used here in pseudocode + # (r'@', Operator), + (uni_name, ttype), + ] + +# SPDX-SnippetEnd + + class LexBase(RegexLexer): """A base that defines some common lexer states. @@ -21,37 +68,6 @@ """ -# -# SPDX-SnippetBegin -# SPDX-License-Identifier: BSD-2-Clause -# SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team -# - - uni_name = "[%s][%s]*" % (unistring.xid_start, unistring.xid_continue) - - def py_innerstring_rules(ttype): - return [ - # the old style '%s' % (...) string formatting (still valid in Py3) - (r'%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?' - '[hlL]?[E-GXc-giorsaux%]', String.Interpol), - # the new style '{}'.format(...) string formatting - (r'\{' - r'((\w+)((\.\w+)|(\[[^\]]+\]))*)?' # field name - r'(\![sra])?' # conversion - r'(\:(.?[<>=\^])?[-+ ]?#?0?(\d+)?,?(\.\d+)?[E-GXb-gnosx%]?)?' - r'\}', String.Interpol), - # - # backslashes, quotes and formatting signs must be parsed - # one at a time - # - (r'[^\\\'"%{\n]+', ttype), - (r'[\'"\\]', ttype), - # unhandled string formatting sign - (r'%|(\{{1,2})', ttype) - # newlines are an error (use "nl" state) - ] -# SPDX-SnippetEnd - tokens = { # # These states are borrowed from Pygment's Python lexer. @@ -123,15 +139,7 @@ ], 'py-strings-single': py_innerstring_rules(String.Single), 'py-strings-double': py_innerstring_rules(String.Double), - 'py-name': [ - # We recognize decorator syntax here - (r'@' + uni_name, Name.Decorator), - # - # Python's new matrix multiplication operator: - # not used here in pseudocode - # (r'@', Operator), - (uni_name, Name), - ], + 'py-name': py_name_rules(Name.Entity), # SPDX-SnippetEnd # This snippet is from the Pygments' documentation "Write your own lexer" 'multiline-nested-comment': [
