comparison pygments_lexer_pseudocode2/fr_pseudocode.py @ 28:de1f67eff9d5

Move the original French pseudocode lexter into a sub-module. This is to prepare for a new implementation along the lines of CTAN's "algpseudocode" or "algpseudocodex".
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 21 Apr 2026 10:31:28 +0200
parents pygments_lexer_pseudocode2/__init__.py@bf13850f21fb
children 9004ff039497
comparison
equal deleted inserted replaced
27:bf13850f21fb 28:de1f67eff9d5
1 # -*- coding: utf-8 -*-
2 # :-
3 # SPDX-FileCopyrightText: © 2015 Simon Wachter
4 # SPDX-FileCopyrightText: © 2026 Franz Glasner
5 # SPDX-License-Identifier: MIT
6 # :-
7 r"""
8 :Author: Simon Wachter
9 :Author: Franz Glasner
10 :Copyright: © 2015 Simon Wachter
11 :Copyright: © 2026 Franz Glasner
12 :License: MIT License.
13 See :file:`MIT.txt` for details.
14 If you cannot find MIT.txt see
15 <http://opensource.org/licenses/MIT>.
16 """
17
18 __all__ = ["FrPseudocodeLexer"]
19
20
21 import re
22
23 from pygments.lexer import RegexLexer, include
24 from pygments.token import Punctuation, Whitespace, Comment, Operator, Keyword, Name, String, Number
25
26
27 class FrPseudocodeLexer(RegexLexer):
28 '''
29 A Pseudo code (fr) lexer
30 '''
31 name = 'FrPseudocode'
32 aliases = ['fr-pseudocode', 'fr-pseudo', 'fr-algorithm', 'fr-algo']
33 filenames = ['*.fr-algo', '*.fr-pseudocode']
34 mimetypes = []
35 flags = re.MULTILINE
36
37 REPLACEMENTS = {
38 '<=': '≤',
39 '>=': '≥',
40 '<>': '≠',
41 '!=': '≠',
42 '<-': '←',
43 '->': '→',
44 '=>': '⇒',
45 '<->': '↔',
46 '<=>': '⇔',
47 '^': '↑',
48 }
49
50 def op_replace(lexer, match):
51 op = match.group(0)
52 opr = lexer.REPLACEMENTS.get(op)
53 if opr is None:
54 yield match.start(), Operator, op
55 else:
56 yield match.start(), Operator, opr
57
58 def scomment(lexer, match):
59 s = match.group(1).lower().strip()
60 c = Comment
61
62 directives = ['passage par copie', 'passage par valeur', 'passage par référence', 'passage par reference', 'passage par adresse', 've', 'vs', 've/s']
63
64 if s in directives:
65 c = Comment.Special
66
67 yield match.start(), c, match.group(0)
68
69 tokens = {
70 'root': [
71 (r'\/\*.*\*\/', Comment),
72 (r'(\/\/|#).*\n', Comment),
73 (r'\|', Comment),
74 (r'\{(.*)\}', scomment),
75 include('strings'),
76 include('core'),
77 (r'(?i)[a-zéàùçèÉÀÙÇÈ][a-z0-9éàùçèÉÀÙÇÈ_]*', Name.Variable),
78 include('numbers'),
79 (r'[\s]+', Whitespace)
80 ],
81 'core': [ # Statements
82 (r'(?i)\b(debut|début|fin|si|alors|sinon|fin[_ ]si|tant[ _]que|tantque|fin[ _]tantque|faire|répéter'
83 r'repeter|type|structure|fin[ _]structure|fonction|procédure|procedure|retourner|renvoyer|'
84 r'pour|fin[ _]pour|à|déclarations?|juqsque|spécialise|specialise|comporte|super|public|privé|protégé|'
85 r'classe'
86 r')\s*\b', Keyword),
87
88 # Data Types
89 (r'(?i)\b(entiers?|chaines?|chaînes?|réels?|reels?|caractères?|caracteres?|booléens?|'
90 r'booleens?|tableaux?|rien)\s*\b',
91 Keyword.Type),
92
93 (r'(?i)\b(vrai|faux|nil)\s*\b',
94 Name.Constant),
95
96 # Operators
97 (r'(?i)(<->|<=>|<=|>=|<>|!=|<-|->|=>|\^|\*|\+|-|\/|<|>|=|\\\\|mod|←|↑|≤|≥|≠|÷|×|\.\.|\[|\]|\.|non|xou|et|ou)',
98 op_replace),
99
100 (r'(\(|\)|\,|\;|:)',
101 Punctuation),
102
103 #(r'\b(\[(VE|VS|VE/S)\])\s*\b',
104 # Keyword.Declaration),
105
106 # Intrinsics
107 (r'(?i)\b(sqrt|pow|cos|sin|tan|arccos|arcsin|arctan|arctan2|lire|ecrire|écrire|'
108 r'exp|ln|log|détruire|detruire'
109 r')\s*\b', Name.Builtin)
110 ],
111
112 'strings': [
113 (r'"([^"])*"', String.Double),
114 (r"'([^'])*'", String.Single),
115 ],
116 #
117 # This is stolen from the Pygment's Python lexer.
118 #
119 # SPDX-SnippetBegin
120 # SPDX-License-Identifier: BSD-2-Clause
121 # SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team
122 'numbers': [
123 (r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)'
124 r'([eE][+-]?\d(?:_?\d)*)?', Number.Float),
125 (r'\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?', Number.Float),
126 (r'0[oO](?:_?[0-7])+', Number.Oct),
127 (r'0[bB](?:_?[01])+', Number.Bin),
128 (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex),
129 (r'\d(?:_?\d)*', Number.Integer),
130 ],
131 # SPDX-SnippetEnd
132 }