diff pygments_lexer_pseudocode2/pseudocode.py @ 34:1f741934205e

Begin a new Pseudocode lexer using numbers and strings from Python
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 21 Apr 2026 19:40:08 +0200
parents
children d9a3551a1038
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pygments_lexer_pseudocode2/pseudocode.py	Tue Apr 21 19:40:08 2026 +0200
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+# :-
+# SPDX-FileCopyrightText: © 2026 Franz Glasner
+# SPDX-License-Identifier: MIT
+# :-
+r"""A pseudocode lexer along the lines of CTAN's algpseudocode or
+algpseudocodex.
+
+"""
+
+__all__ = ["PseudocodeLexer"]
+
+
+import re
+
+from pygments.lexer import include
+from pygments.token import (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
+
+
+class PseudocodeLexer(LexBase):
+
+    """A pseudocode lexer along the lines of CTAN's algpseudocode or
+    algpseudocodex.
+
+    Some ideas (e.g. strings) are borrowed from Pygment's Python lexer.
+
+    """
+
+    name = "Pseudocode"
+    aliases = ["pseudocode", "pseudo", "algorithm", "algo"]
+    filenames = ["*.algo", "*.pseudocode"]
+    mimetypes = []
+    flags = re.MULTILINE
+
+    tokens = {
+        "root": [
+            (r"\n", Whitespace),
+            (r"\\\n", Text),
+            include("expr"),
+        ],
+        "expr": [
+            include("py-strings"),
+            include("py-numbers"),
+        ]
+    }