view pygments_lexer_pseudocode2/pseudocode.py @ 35:d9a3551a1038

Basics of translating some keywords
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 22 Apr 2026 16:23:09 +0200
parents 1f741934205e
children 69522d4cafac
line wrap: on
line source

# -*- 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", "PseudocodeLexer_DE"]


import re

from pygments.lexer import include
from pygments.token import (Keyword, 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

    LANG = "en"
    TRANSLATIONS = {
        "PROG": "PROGRAM",
        "PROGRAM": "PROGRAM",
        "ALGO": "ALGORITHM",
        "ALGORITHM": "ALGORITHM",
        "PROC": "PROCEDURE",
        "PROCEDURE": "PROCEDURE",
        "FUNC": "FUNCTION",
        "FUNCTION": "FUNCTION",
        "FN": "FUNCTION",
        "CLASS": "CLASS",
    }

    def op_translate(toktype):

        def _op_translate(lexer, match):
            kw = match.group(1).upper()
            kwtrans = lexer.TRANSLATIONS.get(kw, kw)
            yield match.start(), toktype, kwtrans

        return _op_translate

    tokens = {
        "root": [
            (r"\n", Whitespace),
            (r"\\\n", Text),
            (r"(?i)\\("
             r"(?:prog(?:ram)?)"
             r"|(?:algo(?:rithm)?)"
             r"|(?:proc(?:edure)?)"
             r"|(?:func(?:tion)?|(?:fn))"
             r"|(?:class)"
             r")\b",
             op_translate(Keyword)),
            include("expr"),
        ],
        "expr": [
            include("py-strings"),
            include("py-numbers"),
        ]
    }


class PseudocodeLexer_DE(PseudocodeLexer):

    name = "PseudocodeDE"
    aliases = ["pseudocode-de", "pseudo-de", "algorithm-de", "algo-de"]
    filenames = ["*.algo-de", "*.pseudocode-de"]

    LANG = "de"
    TRANSLATIONS = {
        "PROG": "PROGRAMM",
        "PROGRAM": "PROGRAMM",
        "ALGO": "ALGORITHMUS",
        "ALGORITHM": "ALGORITHM",
        "PROC": "PROZEDUR",
        "PROCEDURE": "PROZEDUR",
        "FUNC": "FUNKTION",
        "FUNCTION": "FUNKTION",
        "FN": "FUNKTION",
        "CLASS": "KLASSE",
    }


class PseudocodeLexer_FR(PseudocodeLexer):

    name = "PseudocodeFR"
    aliases = ["pseudocode-fr", "pseudo-fr", "algorithm-fr", "algo-fr"]
    filenames = ["*.algo-fr", "*.pseudocode-fr"]

    LANG = "de"
    TRANSLATIONS = {
        "PROG": "PROGRAMME",
        "PROGRAM": "PROGRAMME",
        "ALGO": "ALGORITHME",
        "ALGORITHM": "ALGORITHME",
        "PROC": "PROCÉDURE",
        "PROCEDURE": "PROCÉDURE",
        "FUNC": "FONCTION",
        "FUNCTION": "FOUNCTION",
        "FN": "FONCTION",
        "CLASS": "CLASSE",
    }