Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
changeset 57:e8f4af9e20a8
Some "ENDxxx" commands
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 27 Apr 2026 16:56:57 +0200 |
| parents | 661461fb4dfc |
| children | be065e5c8042 |
| files | pygments_lexer_pseudocode2/algpseudocode.py tests/test_algpseudo.py |
| diffstat | 2 files changed, 114 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/pygments_lexer_pseudocode2/algpseudocode.py Mon Apr 27 12:37:27 2026 +0200 +++ b/pygments_lexer_pseudocode2/algpseudocode.py Mon Apr 27 16:56:57 2026 +0200 @@ -15,6 +15,7 @@ import re +import pygments.util from pygments.lexer import include, bygroups from pygments.token import (Comment, Keyword, Name, Text, Whitespace) @@ -53,6 +54,19 @@ "FN": "FUNCTION", "CLASS": "CLASS", } + END_TRANSLATIONS = { + "PROG": "END OF PROGRAM", + "PROGRAM": "END OF PROGRAM", + "ALGO": "END OF ALGORITHM", + "ALGORITHM": "END OF ALGORITHM", + "PROC": "END OF PROCEDURE", + "PROCEDURE": "END OF PROCEDURE", + "FUNC": "END OF FUNCTION", + "FUNCTION": "END OF FUNCTION", + "FN": "END OF FUNCTION", + "CLASS": "END OF CLASS", + } + DEFAULT_END_PREFIX = "END OF " SYMBOLS = { "REMARK": "▷", # U+25B7: Unicode 1.0 (Arrows) "Remark": "▷", @@ -75,6 +89,19 @@ return _op_translate + def op_opt_end_translate(toktype): + + def _op_end_translate(lexer, match, ctx=None): + if not lexer.no_end: + kw = match.group().upper() + yield (match.start(), + toktype, + lexer.END_TRANSLATIONS.get( + kw, + lexer.DEFAULT_END_PREFIX + kw)) + + return _op_end_translate + def op_symbol(toktype): def _op_symbol(lexer, match, ctx=None): @@ -102,6 +129,26 @@ r")(\s*)(\{)", bygroups(op_translate(Keyword), Whitespace, Name.Entity), "entity-name"), + # ENDxxx keywords with optional entity name + # with name + (r"(?i)\\end(?:_|-)?(" + r"(?:prog(?:ram)?)" + r"|(?:algo(?:rithm)?)" + r"|(?:proc(?:edure)?)" + r"|(?:func(?:tion)?)" + r"|(?:class)" + r")(\s*)(\{)", + bygroups(op_opt_end_translate(Keyword), Whitespace, Name.Entity), + "entity-name"), + # without name + (r"(?i)\\end(?:_|-)?(" + r"(?:prog(?:ram)?)" + r"|(?:algo(?:rithm)?)" + r"|(?:proc(?:edure)?)" + r"|(?:func(?:tion)?)" + r"|(?:class)" + r")((\s*)(\{))?", + bygroups(op_opt_end_translate(Keyword))), include("expr"), (r"\s+", Text), ], @@ -118,6 +165,11 @@ ] } + def __init__(self, **options): + val = pygments.util.get_bool_opt(options, "no_end", default=False) + self.no_end = val + LexBase.__init__(self, **options) + class AlgPseudocodeLexer_DE(AlgPseudocodeLexer): @@ -139,6 +191,20 @@ "FN": "FUNKTION", "CLASS": "KLASSE", }) + END_TRANSLATIONS = AlgPseudocodeLexer.END_TRANSLATIONS.copy() + END_TRANSLATIONS.update({ + "PROG": "ENDE DES PROGRAMMS", + "PROGRAM": "ENDE VON PROGRAMMS", + "ALGO": "ENDE DES ALGORITHMUS", + "ALGORITHM": "ENDE DES ALGORITHMUS", + "PROC": "ENDE DER PROZEDUR", + "PROCEDURE": "ENDE DER PROZEDUR", + "FUNC": "ENDE DER FUNKTION", + "FUNCTION": "ENDE DER FUNKTION", + "FN": "ENDE DER FUNKTION", + "CLASS": "ENDE DER KLASSE", + }) + DEFAULT_END_PREFIX = "ENDE VON " class AlgPseudocodeLexer_FR(AlgPseudocodeLexer): @@ -161,3 +227,17 @@ "FN": "FONCTION", "CLASS": "CLASSE", }) + END_TRANSLATIONS = AlgPseudocodeLexer.END_TRANSLATIONS.copy() + END_TRANSLATIONS.update({ + "PROG": "FIN DE PROGRAMME", + "PROGRAM": "FIN DE PROGRAMME", + "ALGO": "FIN D'ALGORITHME", + "ALGORITHM": "FIN D'ALGORITHME", + "PROC": "FIN DE PROCÉDURE", + "PROCEDURE": "FIN DE PROCÉDURE", + "FUNC": "FIN DE FONCTION", + "FUNCTION": "FIN DE FOUNCTION", + "FN": "FIN DE FONCTION", + "CLASS": "FIN DE CLASSE", + }) + DEFAULT_END_PREFIX = "FIN DE "
--- a/tests/test_algpseudo.py Mon Apr 27 12:37:27 2026 +0200 +++ b/tests/test_algpseudo.py Mon Apr 27 16:56:57 2026 +0200 @@ -152,6 +152,24 @@ ], pygments.lex("\\PROC {the name}", self.lexer)) + def test_endproc(self): + self.assertTokenStreamEqualComplete( + [("Keyword", "END OF PROCEDURE"), + ("Text.Whitespace", "\n"), + ], + pygments.lex("\\END-PROCEDURE", self.lexer)) + + def test_endproc_with_entityname(self): + self.assertTokenStreamEqualComplete( + [("Keyword", "END OF PROCEDURE"), + ("Text.Whitespace", " "), + ("Name.Entity", "{"), + ("Name.Entity", "the procedure name"), + ("Name.Entity", "}"), + ("Text.Whitespace", "\n"), + ], + pygments.lex("\\ENDPROCEDURE {the procedure name}", self.lexer)) + def test_proc_de(self): lexer = pygments.lexers.load_lexer_from_file( ALGLEXERFILENAME, "AlgPseudocodeLexer_DE") @@ -228,6 +246,22 @@ ], pygments.lex("\\CLASS {\\n\\}}", lexer)) + def test_class_de_with_noend_option(self): + lexer = pygments.lexers.load_lexer_from_file( + ALGLEXERFILENAME, "AlgPseudocodeLexer_DE", no_end="True") + self.assertTokenStreamEqualComplete( + [("Keyword", "KLASSE"), + ("Text.Whitespace", " "), + ("Name.Entity", "{"), + ("Name.Entity", "\\"), + ("Name.Entity", "n"), + ("Name.Entity", "\\}"), + ("Name.Entity", "}"), + ("Text.Whitespace", "\n"), + ("Text.Whitespace", "\n"), + ], + pygments.lex("\\CLASS {\\n\\}}\n\\ENDCLASS", lexer)) + def test_remark_1(self): self.assertTokenStreamEqualComplete( [("Comment.Single", "▷"),
