Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
changeset 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 | bf13850f21fb |
| children | c5fa8ee08059 |
| files | pygments_lexer_pseudocode2/__init__.py pygments_lexer_pseudocode2/fr_pseudocode.py pyproject.toml tests/_tsetup.py tests/test_fr.py |
| diffstat | 5 files changed, 141 insertions(+), 141 deletions(-) [+] |
line wrap: on
line diff
--- a/pygments_lexer_pseudocode2/__init__.py Tue Apr 21 01:22:43 2026 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -# -*- coding: utf-8 -*- -# :- -# SPDX-FileCopyrightText: © 2015 Simon Wachter -# SPDX-FileCopyrightText: © 2026 Franz Glasner -# SPDX-License-Identifier: MIT -# :- -r""" -:Author: Simon Wachter -:Author: Franz Glasner -:Copyright: © 2015 Simon Wachter -:Copyright: © 2026 Franz Glasner -:License: MIT License. - See :file:`MIT.txt` for details. - If you cannot find MIT.txt see - <http://opensource.org/licenses/MIT>. -""" - -__version__ = "2.0.1" - -__all__ = ["PseudocodeLexer2"] - - -import re - -from pygments.lexer import RegexLexer, include -from pygments.token import Punctuation, Whitespace, Comment, Operator, Keyword, Name, String, Number - - -class PseudocodeLexer2(RegexLexer): - ''' - A Pseudo code (fr) lexer - ''' - name = 'Pseudocode' - aliases = ['pseudocode', 'pseudo', 'algorithm', 'algo'] - filenames = ['*.algo', '*.pseudocode'] - mimetypes = [] - flags = re.MULTILINE - - REPLACEMENTS = { - '<=': '≤', - '>=': '≥', - '<>': '≠', - '!=': '≠', - '<-': '←', - '->': '→', - '=>': '⇒', - '<->': '↔', - '<=>': '⇔', - '^': '↑', - } - - def op_replace(lexer, match): - op = match.group(0) - opr = lexer.REPLACEMENTS.get(op) - if opr is None: - yield match.start(), Operator, op - else: - yield match.start(), Operator, opr - - def scomment(lexer, match): - s = match.group(1).lower().strip() - c = Comment - - directives = ['passage par copie', 'passage par valeur', 'passage par référence', 'passage par reference', 'passage par adresse', 've', 'vs', 've/s'] - - if s in directives: - c = Comment.Special - - yield match.start(), c, match.group(0) - - tokens = { - 'root': [ - (r'\/\*.*\*\/', Comment), - (r'(\/\/|#).*\n', Comment), - (r'\|', Comment), - (r'\{(.*)\}', scomment), - include('strings'), - include('core'), - (r'(?i)[a-zéàùçèÉÀÙÇÈ][a-z0-9éàùçèÉÀÙÇÈ_]*', Name.Variable), - include('numbers'), - (r'[\s]+', Whitespace) - ], - 'core': [ # Statements - (r'(?i)\b(debut|début|fin|si|alors|sinon|fin[_ ]si|tant[ _]que|tantque|fin[ _]tantque|faire|répéter' - r'repeter|type|structure|fin[ _]structure|fonction|procédure|procedure|retourner|renvoyer|' - r'pour|fin[ _]pour|à|déclarations?|juqsque|spécialise|specialise|comporte|super|public|privé|protégé|' - r'classe' - r')\s*\b', Keyword), - - # Data Types - (r'(?i)\b(entiers?|chaines?|chaînes?|réels?|reels?|caractères?|caracteres?|booléens?|' - r'booleens?|tableaux?|rien)\s*\b', - Keyword.Type), - - (r'(?i)\b(vrai|faux|nil)\s*\b', - Name.Constant), - - # Operators - (r'(?i)(<->|<=>|<=|>=|<>|!=|<-|->|=>|\^|\*|\+|-|\/|<|>|=|\\\\|mod|←|↑|≤|≥|≠|÷|×|\.\.|\[|\]|\.|non|xou|et|ou)', - op_replace), - - (r'(\(|\)|\,|\;|:)', - Punctuation), - - #(r'\b(\[(VE|VS|VE/S)\])\s*\b', - # Keyword.Declaration), - - # Intrinsics - (r'(?i)\b(sqrt|pow|cos|sin|tan|arccos|arcsin|arctan|arctan2|lire|ecrire|écrire|' - r'exp|ln|log|détruire|detruire' - r')\s*\b', Name.Builtin) - ], - - 'strings': [ - (r'"([^"])*"', String.Double), - (r"'([^'])*'", String.Single), - ], -# -# This is stolen from the Pygment's Python lexer. -# -# SPDX-SnippetBegin -# SPDX-License-Identifier: BSD-2-Clause -# SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team - 'numbers': [ - (r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)' - r'([eE][+-]?\d(?:_?\d)*)?', Number.Float), - (r'\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?', Number.Float), - (r'0[oO](?:_?[0-7])+', Number.Oct), - (r'0[bB](?:_?[01])+', Number.Bin), - (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex), - (r'\d(?:_?\d)*', Number.Integer), - ], -# SPDX-SnippetEnd - }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pygments_lexer_pseudocode2/fr_pseudocode.py Tue Apr 21 10:31:28 2026 +0200 @@ -0,0 +1,132 @@ +# -*- coding: utf-8 -*- +# :- +# SPDX-FileCopyrightText: © 2015 Simon Wachter +# SPDX-FileCopyrightText: © 2026 Franz Glasner +# SPDX-License-Identifier: MIT +# :- +r""" +:Author: Simon Wachter +:Author: Franz Glasner +:Copyright: © 2015 Simon Wachter +:Copyright: © 2026 Franz Glasner +:License: MIT License. + See :file:`MIT.txt` for details. + If you cannot find MIT.txt see + <http://opensource.org/licenses/MIT>. +""" + +__all__ = ["FrPseudocodeLexer"] + + +import re + +from pygments.lexer import RegexLexer, include +from pygments.token import Punctuation, Whitespace, Comment, Operator, Keyword, Name, String, Number + + +class FrPseudocodeLexer(RegexLexer): + ''' + A Pseudo code (fr) lexer + ''' + name = 'FrPseudocode' + aliases = ['fr-pseudocode', 'fr-pseudo', 'fr-algorithm', 'fr-algo'] + filenames = ['*.fr-algo', '*.fr-pseudocode'] + mimetypes = [] + flags = re.MULTILINE + + REPLACEMENTS = { + '<=': '≤', + '>=': '≥', + '<>': '≠', + '!=': '≠', + '<-': '←', + '->': '→', + '=>': '⇒', + '<->': '↔', + '<=>': '⇔', + '^': '↑', + } + + def op_replace(lexer, match): + op = match.group(0) + opr = lexer.REPLACEMENTS.get(op) + if opr is None: + yield match.start(), Operator, op + else: + yield match.start(), Operator, opr + + def scomment(lexer, match): + s = match.group(1).lower().strip() + c = Comment + + directives = ['passage par copie', 'passage par valeur', 'passage par référence', 'passage par reference', 'passage par adresse', 've', 'vs', 've/s'] + + if s in directives: + c = Comment.Special + + yield match.start(), c, match.group(0) + + tokens = { + 'root': [ + (r'\/\*.*\*\/', Comment), + (r'(\/\/|#).*\n', Comment), + (r'\|', Comment), + (r'\{(.*)\}', scomment), + include('strings'), + include('core'), + (r'(?i)[a-zéàùçèÉÀÙÇÈ][a-z0-9éàùçèÉÀÙÇÈ_]*', Name.Variable), + include('numbers'), + (r'[\s]+', Whitespace) + ], + 'core': [ # Statements + (r'(?i)\b(debut|début|fin|si|alors|sinon|fin[_ ]si|tant[ _]que|tantque|fin[ _]tantque|faire|répéter' + r'repeter|type|structure|fin[ _]structure|fonction|procédure|procedure|retourner|renvoyer|' + r'pour|fin[ _]pour|à|déclarations?|juqsque|spécialise|specialise|comporte|super|public|privé|protégé|' + r'classe' + r')\s*\b', Keyword), + + # Data Types + (r'(?i)\b(entiers?|chaines?|chaînes?|réels?|reels?|caractères?|caracteres?|booléens?|' + r'booleens?|tableaux?|rien)\s*\b', + Keyword.Type), + + (r'(?i)\b(vrai|faux|nil)\s*\b', + Name.Constant), + + # Operators + (r'(?i)(<->|<=>|<=|>=|<>|!=|<-|->|=>|\^|\*|\+|-|\/|<|>|=|\\\\|mod|←|↑|≤|≥|≠|÷|×|\.\.|\[|\]|\.|non|xou|et|ou)', + op_replace), + + (r'(\(|\)|\,|\;|:)', + Punctuation), + + #(r'\b(\[(VE|VS|VE/S)\])\s*\b', + # Keyword.Declaration), + + # Intrinsics + (r'(?i)\b(sqrt|pow|cos|sin|tan|arccos|arcsin|arctan|arctan2|lire|ecrire|écrire|' + r'exp|ln|log|détruire|detruire' + r')\s*\b', Name.Builtin) + ], + + 'strings': [ + (r'"([^"])*"', String.Double), + (r"'([^'])*'", String.Single), + ], +# +# This is stolen from the Pygment's Python lexer. +# +# SPDX-SnippetBegin +# SPDX-License-Identifier: BSD-2-Clause +# SPDX-SnippetCopyrightText: Copyright 2006-2023 by the Pygments team + 'numbers': [ + (r'(\d(?:_?\d)*\.(?:\d(?:_?\d)*)?|(?:\d(?:_?\d)*)?\.\d(?:_?\d)*)' + r'([eE][+-]?\d(?:_?\d)*)?', Number.Float), + (r'\d(?:_?\d)*[eE][+-]?\d(?:_?\d)*j?', Number.Float), + (r'0[oO](?:_?[0-7])+', Number.Oct), + (r'0[bB](?:_?[01])+', Number.Bin), + (r'0[xX](?:_?[a-fA-F0-9])+', Number.Hex), + (r'\d(?:_?\d)*', Number.Integer), + ], +# SPDX-SnippetEnd + }
--- a/pyproject.toml Tue Apr 21 01:22:43 2026 +0200 +++ b/pyproject.toml Tue Apr 21 10:31:28 2026 +0200 @@ -34,10 +34,12 @@ ] [project.urls] +# The original homepage of the Pseudo code (fr) lexer homepage = "https://github.com/svvac/pseudocode-pygments-lexer" [project.entry-points.'pygments.lexers'] -pseudocodelexer2 = "pygments_lexer_pseudocode2:PseudocodeLexer2" +# The mostly original and sonewhat extended Pseudocode lexer (fr) +fr_pseudocodelexer = "pygments_lexer_pseudocode2.fr_pseudocode:FrPseudocodeLexer" [tool.setuptools] packages = [
--- a/tests/_tsetup.py Tue Apr 21 01:22:43 2026 +0200 +++ b/tests/_tsetup.py Tue Apr 21 10:31:28 2026 +0200 @@ -16,9 +16,9 @@ PROJECTDIR = os.path.abspath( os.path.normpath(os.path.join(os.path.dirname(__file__), ".."))) sys.path.insert(0, PROJECTDIR) -LEXERFILENAME = os.path.join(PROJECTDIR, - "pygments_lexer_pseudocode2/__init__.py") -LEXERCLASS = "PseudocodeLexer2" +FRLEXERFILENAME = os.path.join(PROJECTDIR, + "pygments_lexer_pseudocode2/fr_pseudocode.py") +FRLEXERCLASS = "FrPseudocodeLexer" TESTSNIPPETSDIR = os.path.join( os.path.abspath(os.path.dirname(__file__)), "snippets")
--- a/tests/test_fr.py Tue Apr 21 01:22:43 2026 +0200 +++ b/tests/test_fr.py Tue Apr 21 10:31:28 2026 +0200 @@ -4,7 +4,7 @@ # SPDX-License-Identifier: MIT # :- -from _tsetup import LEXERFILENAME, LEXERCLASS +from _tsetup import FRLEXERFILENAME, FRLEXERCLASS import unittest @@ -18,14 +18,14 @@ class TestLoading(unittest.TestCase): def test_load(self): - pygments.lexers.load_lexer_from_file(LEXERFILENAME, LEXERCLASS) + pygments.lexers.load_lexer_from_file(FRLEXERFILENAME, FRLEXERCLASS) class TestFrLexer(unittest.TestCase, _testhelper.TokenAssertHelper): def setUp(self): self.lexer = pygments.lexers.load_lexer_from_file( - LEXERFILENAME, LEXERCLASS) + FRLEXERFILENAME, FRLEXERCLASS) def test_types(self): tokens = pygments.lex("huhu", self.lexer)
