diff pygments_lexer_pseudocode2/algpseudocode.py @ 75:711f8d19e27a

New implementation of "STATEMENT" (also aliased to "STATE" and "BLOCK"). Now this needs curly braces.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 29 Apr 2026 12:43:28 +0200
parents c1357674622d
children 27e12387154d
line wrap: on
line diff
--- a/pygments_lexer_pseudocode2/algpseudocode.py	Tue Apr 28 20:27:46 2026 +0200
+++ b/pygments_lexer_pseudocode2/algpseudocode.py	Wed Apr 29 12:43:28 2026 +0200
@@ -16,8 +16,9 @@
 import re
 
 import pygments.util
-from pygments.lexer import include, bygroups
-from pygments.token import (Comment, Keyword, Name, Text, Whitespace)
+from pygments.lexer import bygroups, include, words
+from pygments.token import (Comment, Keyword, Name, Operator, Punctuation,
+                            Text, Whitespace)
 
 #
 # Relative imports do not work with pygments.lexers.load_lexer_from_file()
@@ -73,15 +74,22 @@
     SYMBOL_BLOCK = "◆"             # U+25C6: Unicode 1.0 (Geometric Shapes)
     # SYMBOL_BLOCK = "┃"           # U+2503: Unicode 1.0 (Bow Drawing)
     # SYMBOL_BLOCK = "●"           # U+25CF: Unicode 1.0 (Geometric Shapes)
-    SYMBOL_TEXTSTATEMENT = "■"     # U+25A0: Unicode 1.0 (Geometric Shapes)
+    SYMBOL_TEXTSTATEMENT = "▪"     # U+25AA: Unicode 1.0 (Geometric Shapes)
+    # SYMBOL_TEXTSTATEMENT = "■"   # U+25A0: Unicode 1.0 (Geometric Shapes)
     SYMBOLS = {
+        # Group REMARK
         "REMARK": SYMBOL_REMARK,
         "REM": SYMBOL_REMARK,
+        # Group STATEMENT
+        "STATEMENT": SYMBOL_BLOCK,
+        "STATE": SYMBOL_BLOCK,
         "BLOCK": SYMBOL_BLOCK,
+        # Group TEXTSTATEMENT
         "TEXTSTATEMENT": SYMBOL_TEXTSTATEMENT,
         "TSTATEMENT": SYMBOL_TEXTSTATEMENT,
         "TSTATE": SYMBOL_TEXTSTATEMENT,
-        "TEXT": SYMBOL_TEXTSTATEMENT,
+        "TEXTBLOCK": SYMBOL_TEXTSTATEMENT,
+        "TBLOCK": SYMBOL_TEXTSTATEMENT,
     }
 
     def op_translate(toktype):
@@ -138,10 +146,10 @@
             (r"\n", Whitespace),
             (r"/\*", Comment.Multiline, "multiline-nested-comment"),
             (r"//.*$", Comment.Single),
-            (r"(?i)\\(remark|rem)\b(.*)$",
-             bygroups(op_symbol(Comment.Single), Comment.Single)),
-            (r"(?i)\\(block)\b(.*)$",
-             bygroups(op_symbol(Text), Text)),
+            include("remark"),
+            (r"(?i)\\(block|state(?:ment)?)\s*(\{)",
+             bygroups(op_symbol(Text), LexBase.op_fixed(Whitespace, " ")),
+             "block-expr"),
             (r"\\\n", Text),
             (r"(?i)\\("
              r"(?:prog(?:ram)?)"
@@ -182,6 +190,10 @@
             include("expr"),
             (r"\s+", Text),
         ],
+        "remark": [
+            (r"(?i)\\(remark|rem)\b(.*)$",
+             bygroups(op_symbol(Comment.Single), Comment.Single)),
+        ],
         "entity-name": [      # may be multiline
             (r"[^\\}]+", Name.Entity),
             (r"\\\}", LexBase.op_fixed(Name.Entity, "}")),
@@ -195,17 +207,58 @@
             (r"\}", LexBase.op_ignore, "#pop"),
         ],
         "expr": [
+            include("punctuation"),
             include("py-strings"),
             include("py-numbers"),
-            (r"(?i)\\text\s*\{", LexBase.op_ignore, "expr-text"),
+            (r"(?i)\\text\s*\{", LexBase.op_ignore, "text-in-expr"),
+            include("remark"),
+            include("text-operators"),
+            include("math-builtins"),
             include("py-name"),
         ],
-        "expr-text": [
+        "block-expr": [      # somewhat similar to "root"
+            (r"\}", LexBase.op_ignore, "#pop"),
+            (r"\n", Whitespace),
+            include("expr-in-braces"),
+            (r"\s+", Text),
+        ],
+        "expr-in-braces": [
+            include("punctuation-in-braces"),
+            include("py-strings"),
+            include("py-numbers"),
+            (r"(?i)\\text\s*\{", LexBase.op_ignore, "text-in-expr"),
+            include("remark"),
+            include("text-operators"),
+            include("math-builtins"),
+            include("py-name"),
+        ],
+        "text-in-expr": [
             (r"[^\\}]+", Text),
             (r"\\\}", LexBase.op_fixed(Text, "}")),
             (r"\\", LexBase.op_fixed(Text, "\\")),
             (r"\}", LexBase.op_ignore, "#pop"),
         ],
+        "math-builtins": [
+            (words(("sqrt", "pow", "cos", "sin", "tan", "arcos", "arcsin",
+                    "arctan", "arctan2", "mod", "exp", "ln", "log"),
+                   prefix=r"(?<!\.)",
+                   suffix=r"\b"),
+             Name.Builtin),
+        ],
+        "text-operators": [
+            (words(("in", "is", "and", "or", "xor", "not"),
+                   prefix=r"(?<!\.)",
+                   suffix=r"\b"),
+             Operator.Word),
+        ],
+        "punctuation": [
+            (r"[{}:(),;[\]]", Punctuation),
+        ],
+        "punctuation-in-braces": [
+            # like "punctuation" but needs an escaped curly brace for }
+            (r"\\\}", LexBase.op_fixed(Punctuation, "}")),
+            (r"[{:(),;[\]]", Punctuation),
+        ],
     }
 
     def __init__(self, **options):