comparison pygments_lexer_pseudocode2/pseudocode.py @ 39:a3151d837258

Some basic keywords for programs, algorithms, procedures and functions; also comments (single and multiline) and "remarks"
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 24 Apr 2026 09:44:34 +0200
parents 69522d4cafac
children df08226a6984
comparison
equal deleted inserted replaced
38:69522d4cafac 39:a3151d837258
11 __all__ = ["PseudocodeLexer", "PseudocodeLexer_DE"] 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, bygroups
17 from pygments.token import (Keyword, Text, Whitespace) 17 from pygments.token import (Comment, 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 #
49 "FUNC": "FUNCTION", 49 "FUNC": "FUNCTION",
50 "FUNCTION": "FUNCTION", 50 "FUNCTION": "FUNCTION",
51 "FN": "FUNCTION", 51 "FN": "FUNCTION",
52 "CLASS": "CLASS", 52 "CLASS": "CLASS",
53 } 53 }
54 SYMBOLS = {
55 "REMARK": "▷", # U+25B7: Unicode 1.0 (Arrows)
56 "Remark": "▷",
57 "remark": "▷",
58 "REM": "▷",
59 "Rem": "▷",
60 "rem": "▷",
61 "R": "▷",
62 "r": "▷",
63 "BLOCK": "┃", # U+2503: Unicode 1.0 (Bow Drawing)
64 "Block": "┃",
65 "block": "┃",
66 }
54 67
55 def op_translate(toktype): 68 def op_translate(toktype):
56 69
57 def _op_translate(lexer, match): 70 def _op_translate(lexer, match):
58 kw = match.group(1).upper() 71 kw = match.group(1).upper()
59 yield match.start(), toktype, lexer.TRANSLATIONS.get(kw, kw) 72 yield match.start(), toktype, lexer.TRANSLATIONS.get(kw, kw)
60 73
61 return _op_translate 74 return _op_translate
62 75
76 def op_symbol(toktype):
77
78 def _op_symbol(lexer, match, ctx=None):
79 kw = match.group()
80 yield match.start(), toktype, lexer.SYMBOLS.get(kw, kw)
81
82 return _op_symbol
83
63 tokens = { 84 tokens = {
64 "root": [ 85 "root": [
65 (r"\n", Whitespace), 86 (r"\n", Whitespace),
87 (r"/\*", Comment.Multiline, "multiline-nested-comment"),
88 (r"//.*$", Comment.Single),
89 (r"(?:\\)(REMARK|Remark|remark|REM|Rem|rem|R|r)\b(.*)$",
90 bygroups(op_symbol(Comment.Single), Comment.Single)),
91 (r"(?:\\)(BLOCK|Block|block)\b(.*)$",
92 bygroups(op_symbol(Text), Text)),
66 (r"\\\n", Text), 93 (r"\\\n", Text),
67 (r"(?i)\\(" 94 (r"(?i)\\("
68 r"(?:prog(?:ram)?)" 95 r"(?:prog(?:ram)?)"
69 r"|(?:algo(?:rithm)?)" 96 r"|(?:algo(?:rithm)?)"
70 r"|(?:proc(?:edure)?)" 97 r"|(?:proc(?:edure)?)"
90 LANG = "de" 117 LANG = "de"
91 TRANSLATIONS = { 118 TRANSLATIONS = {
92 "PROG": "PROGRAMM", 119 "PROG": "PROGRAMM",
93 "PROGRAM": "PROGRAMM", 120 "PROGRAM": "PROGRAMM",
94 "ALGO": "ALGORITHMUS", 121 "ALGO": "ALGORITHMUS",
95 "ALGORITHM": "ALGORITHM", 122 "ALGORITHM": "ALGORITHMUS",
96 "PROC": "PROZEDUR", 123 "PROC": "PROZEDUR",
97 "PROCEDURE": "PROZEDUR", 124 "PROCEDURE": "PROZEDUR",
98 "FUNC": "FUNKTION", 125 "FUNC": "FUNKTION",
99 "FUNCTION": "FUNKTION", 126 "FUNCTION": "FUNKTION",
100 "FN": "FUNKTION", 127 "FN": "FUNKTION",