Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 34:1f741934205e | 35:d9a3551a1038 |
|---|---|
| 6 r"""A pseudocode lexer along the lines of CTAN's algpseudocode or | 6 r"""A pseudocode lexer along the lines of CTAN's algpseudocode or |
| 7 algpseudocodex. | 7 algpseudocodex. |
| 8 | 8 |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 __all__ = ["PseudocodeLexer"] | 11 __all__ = ["PseudocodeLexer", "PseudocodeLexer_DE"] |
| 12 | 12 |
| 13 | 13 |
| 14 import re | 14 import re |
| 15 | 15 |
| 16 from pygments.lexer import include | 16 from pygments.lexer import include |
| 17 from pygments.token import (Text, Whitespace) | 17 from pygments.token import (Keyword, Text, Whitespace) |
| 18 | 18 |
| 19 # | 19 # |
| 20 # Relative imports do not work with pygments.lexers.load_lexer_from_file() | 20 # Relative imports do not work with pygments.lexers.load_lexer_from_file() |
| 21 # in all of our supported Python releases. | 21 # in all of our supported Python releases. |
| 22 # | 22 # |
| 36 aliases = ["pseudocode", "pseudo", "algorithm", "algo"] | 36 aliases = ["pseudocode", "pseudo", "algorithm", "algo"] |
| 37 filenames = ["*.algo", "*.pseudocode"] | 37 filenames = ["*.algo", "*.pseudocode"] |
| 38 mimetypes = [] | 38 mimetypes = [] |
| 39 flags = re.MULTILINE | 39 flags = re.MULTILINE |
| 40 | 40 |
| 41 LANG = "en" | |
| 42 TRANSLATIONS = { | |
| 43 "PROG": "PROGRAM", | |
| 44 "PROGRAM": "PROGRAM", | |
| 45 "ALGO": "ALGORITHM", | |
| 46 "ALGORITHM": "ALGORITHM", | |
| 47 "PROC": "PROCEDURE", | |
| 48 "PROCEDURE": "PROCEDURE", | |
| 49 "FUNC": "FUNCTION", | |
| 50 "FUNCTION": "FUNCTION", | |
| 51 "FN": "FUNCTION", | |
| 52 "CLASS": "CLASS", | |
| 53 } | |
| 54 | |
| 55 def op_translate(toktype): | |
| 56 | |
| 57 def _op_translate(lexer, match): | |
| 58 kw = match.group(1).upper() | |
| 59 kwtrans = lexer.TRANSLATIONS.get(kw, kw) | |
| 60 yield match.start(), toktype, kwtrans | |
| 61 | |
| 62 return _op_translate | |
| 63 | |
| 41 tokens = { | 64 tokens = { |
| 42 "root": [ | 65 "root": [ |
| 43 (r"\n", Whitespace), | 66 (r"\n", Whitespace), |
| 44 (r"\\\n", Text), | 67 (r"\\\n", Text), |
| 68 (r"(?i)\\(" | |
| 69 r"(?:prog(?:ram)?)" | |
| 70 r"|(?:algo(?:rithm)?)" | |
| 71 r"|(?:proc(?:edure)?)" | |
| 72 r"|(?:func(?:tion)?|(?:fn))" | |
| 73 r"|(?:class)" | |
| 74 r")\b", | |
| 75 op_translate(Keyword)), | |
| 45 include("expr"), | 76 include("expr"), |
| 46 ], | 77 ], |
| 47 "expr": [ | 78 "expr": [ |
| 48 include("py-strings"), | 79 include("py-strings"), |
| 49 include("py-numbers"), | 80 include("py-numbers"), |
| 50 ] | 81 ] |
| 51 } | 82 } |
| 83 | |
| 84 | |
| 85 class PseudocodeLexer_DE(PseudocodeLexer): | |
| 86 | |
| 87 name = "PseudocodeDE" | |
| 88 aliases = ["pseudocode-de", "pseudo-de", "algorithm-de", "algo-de"] | |
| 89 filenames = ["*.algo-de", "*.pseudocode-de"] | |
| 90 | |
| 91 LANG = "de" | |
| 92 TRANSLATIONS = { | |
| 93 "PROG": "PROGRAMM", | |
| 94 "PROGRAM": "PROGRAMM", | |
| 95 "ALGO": "ALGORITHMUS", | |
| 96 "ALGORITHM": "ALGORITHM", | |
| 97 "PROC": "PROZEDUR", | |
| 98 "PROCEDURE": "PROZEDUR", | |
| 99 "FUNC": "FUNKTION", | |
| 100 "FUNCTION": "FUNKTION", | |
| 101 "FN": "FUNKTION", | |
| 102 "CLASS": "KLASSE", | |
| 103 } | |
| 104 | |
| 105 | |
| 106 class PseudocodeLexer_FR(PseudocodeLexer): | |
| 107 | |
| 108 name = "PseudocodeFR" | |
| 109 aliases = ["pseudocode-fr", "pseudo-fr", "algorithm-fr", "algo-fr"] | |
| 110 filenames = ["*.algo-fr", "*.pseudocode-fr"] | |
| 111 | |
| 112 LANG = "de" | |
| 113 TRANSLATIONS = { | |
| 114 "PROG": "PROGRAMME", | |
| 115 "PROGRAM": "PROGRAMME", | |
| 116 "ALGO": "ALGORITHME", | |
| 117 "ALGORITHM": "ALGORITHME", | |
| 118 "PROC": "PROCÉDURE", | |
| 119 "PROCEDURE": "PROCÉDURE", | |
| 120 "FUNC": "FONCTION", | |
| 121 "FUNCTION": "FOUNCTION", | |
| 122 "FN": "FONCTION", | |
| 123 "CLASS": "CLASSE", | |
| 124 } |
