comparison docs/lexer-algpseudocode.rst @ 266:50bd1e91b822

Doc enhancements: style, sub-headings, wording, examples, details
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 19 May 2026 19:01:27 +0200
parents e5ea2f955986
children ea19b621081d
comparison
equal deleted inserted replaced
265:e5ea2f955986 266:50bd1e91b822
17 17
18 These lexers are heavily inspired by CTAN’s `Algpseudocodex`_. 18 These lexers are heavily inspired by CTAN’s `Algpseudocodex`_.
19 They recognize all sorts of single- and multi-line comments in addition to 19 They recognize all sorts of single- and multi-line comments in addition to
20 expressions and commands that are inspired by `Algpseudocodex`_. 20 expressions and commands that are inspired by `Algpseudocodex`_.
21 21
22 They may be used in `Sphinx`_ by their aliases. 22 They are used in `Sphinx`_ using their aliases.
23 The code-block: 23 The code-block:
24 24
25 .. code-block:: none 25 .. code-block:: none
26 26
27 .. code-block:: algpseudocode 27 .. code-block:: algpseudocode
61 triple-double-quote, `Python`_ style) 61 triple-double-quote, `Python`_ style)
62 - Numbers (also `Python`_ style) 62 - Numbers (also `Python`_ style)
63 - (Mathematical) operators and symbols 63 - (Mathematical) operators and symbols
64 - ``\TEXT{...}`` 64 - ``\TEXT{...}``
65 65
66 To switch in a text-mode that prohibits automatic expression 66 Used to switch to a text-mode that prohibits automatic expression
67 highlighting. 67 highlighting.
68 68
69 A closing curly brace can be quoted with ``\}`` to not end the 69 A closing curly brace can be quoted with ``\}`` to not end the
70 text mode prematurely. 70 text mode prematurely.
71 71
77 77
78 - Names (`Name.Entity`) 78 - Names (`Name.Entity`)
79 79
80 - :ref:`explicit-token-types` 80 - :ref:`explicit-token-types`
81 81
82 In the `default`-mode it recognizes all sorts of single- and multi-line 82 The `default`-mode is an extension of `expression`:
83 comments in addition to expressions and commands that are inspired by 83 in addition to `expressions` it recognizes all sorts of single- and
84 `Algpseudocodex`_. 84 multi-line comments and commands that are inspired by `Algpseudocodex`_.
85 85
86 In `texts` it recognizes: 86 In `text` context it recognizes:
87 87
88 - ``\EXPR`` or ``\EXPRESSION`` 88 - ``\EXPR`` or ``\EXPRESSION``
89 89
90 To switch to expression-mode. 90 Use to switch to expression-mode.
91 91
92 A closing curly brace can be quoted with ``\}`` to not end the expression 92 A closing curly brace can be quoted with ``\}`` to not end the expression
93 mode prematurely. 93 mode prematurely.
94 94
95 - ``\TEXT`` as nested construct 95 - ``\TEXT`` as nested construct
147 147
148 148
149 Comments 149 Comments
150 ======== 150 ========
151 151
152 - with the ``\REMARK`` or ``\REM`` keywords (this includes a leading symbol) 152 - with the ``\REMARK`` or ``\REM`` keywords
153 (until the end of the line; the output includes a leading symbol,
154 by default ``▷``)
153 - multi-line comments with ``/* ... */``; they can be **nested** 155 - multi-line comments with ``/* ... */``; they can be **nested**
154 - multi-line comments with ``(* ... *)``; they can be **nested** 156 - multi-line comments with ``(* ... *)``; they can be **nested**
155 - single-line comments with ``//`` or ``#`` (until the end of the line) 157 - single-line comments with ``//`` or ``#`` (until the end of the line)
156 158
157 .. code-block:: algpseudocode 159 .. code-block:: algpseudocode
182 184
183 185
184 Literals 186 Literals
185 ======== 187 ========
186 188
187 Strings and numbers as in `Python`_. String prefixes ``r``, ``f`` and ``t`` 189 Strings and numbers as in `Python`_.
188 are not supported -- ``u`` and ``b`` are. 190 String prefixes ``u`` and ``b`` are supported---prefixes
189 191 ``r``, ``f`` and ``t`` are not supported.
190 To yield non-string-delimiting single- and double-quotes you have to escape them 192
191 using ``\'`` or ``\"``. This must be used to typeset something as 193 To have non-string-delimiting single- and double-quotes in the output you
192 :algpseudocode:`f\\'(x) = 0`. 194 have to escape them using ``\'`` or ``\"``.
195 This must be used to typeset something as :algpseudocode:`f\\'(x) = 0`.
193 196
194 .. code-block:: algpseudocode 197 .. code-block:: algpseudocode
195 198
196 0 1234567890 0xdead 0b100001 0o720 2.7 2.7e-54 199 0 1234567890 0xdead 0b100001 0o720 2.7 2.7e-54
197 200
252 Commands 255 Commands
253 ======== 256 ========
254 257
255 - Start with a backslash character ``\`` 258 - Start with a backslash character ``\``
256 - Case-insensitive 259 - Case-insensitive
257 - Yield mostly to :py:class:`pygments.Token.Keyword` 260 - Yield mostly the :py:class:`pygments.token.Token.Keyword` token type
258 - Translated if a translation is found 261 - Translated if a translation is found
259 - Depending on the command---may have required or optional parameters 262 - Depending on the command---may have required or optional parameters
260 263
261 Parameter handling is as follows: 264 Parameter handling is as follows:
262 265
374 377
375 END-Commands 378 END-Commands
376 '''''''''''' 379 ''''''''''''
377 380
378 The separator character can be empty, a run of ASCII spaces, a run of 381 The separator character can be empty, a run of ASCII spaces, a run of
379 TAB characters, a single underscore ``_`` or a single hyphen ``-`` 382 TAB characters, a single underscore ``_`` or a single hyphen ``-``.
380 like: 383
384 All of the following examples are equally valid and result in the
385 same output:
381 386
382 ``\ENDIF``, ``\END IF``, ``\END-IF``, ``\END_IF`` or ``\END IF`` 387 ``\ENDIF``, ``\END IF``, ``\END-IF``, ``\END_IF`` or ``\END IF``
383 388
384 389
385 .. code-block:: algpseudocode 390 .. code-block:: algpseudocode
409 \text{\END-WHILE} \END-WHILE 414 \text{\END-WHILE} \END-WHILE
410 \text{\END-FOR} \END-FOR 415 \text{\END-FOR} \END-FOR
411 \text{\END-FORALL} \END-FORALL 416 \text{\END-FORALL} \END-FORALL
412 \text{\END-LOOP} \END-LOOP 417 \text{\END-LOOP} \END-LOOP
413 418
414 .. note:: The output of END-commands can be suppressed by setting the 419 .. note:: The output of these END-commands can be suppressed by setting the
415 lexer option ``no_end`` to :py:obj:`True`. 420 lexer option ``no_end`` to :py:obj:`True`.
416 421
417 422
418 Names and Entities 423 Names and Entities
419 ================== 424 ==================
443 .. _explicit-token-types: 448 .. _explicit-token-types:
444 449
445 Explicit Token Types 450 Explicit Token Types
446 ==================== 451 ====================
447 452
448 Handle keywords and operators that are not handled by default or change 453 They allow to handle keywords and operators that are not recognized by default.
449 the default handling of some expressions. 454 And they allow the user to explicitely highlight some input text at low-level.
450 455
451 `XX` represents a `value` in the :py:data:`pygments.token.STANDARD_TYPES` 456 `XX` represents a `value` in the :py:data:`pygments.token.STANDARD_TYPES`
452 dict. 457 dict.
453 Its corresponding token type (the associated `key` in this `dict`) is 458 Its corresponding token type (the associated `key` in this `dict`) is
454 used as token type. 459 used as token type.
479 484
480 .. code-block:: algpseudocode 485 .. code-block:: algpseudocode
481 486
482 \text{• \\tt-kc/C} \tt-kc/C \rem C as Keyword.Constant 487 \text{• \\tt-kc/C} \tt-kc/C \rem C as Keyword.Constant
483 \text{• \\tt-ow/∈} \tt-ow/∈ \rem ∈ as Operator.Word 488 \text{• \\tt-ow/∈} \tt-ow/∈ \rem ∈ as Operator.Word
484 \text{• \\ttx-kc{A New Constant Keyword\}} \ttx-kc{A New Constant Keyword} \rem As a new Keyword.Constant 489 \text{• \\ttx-kc{A Constant Keyword\}} \ttx-kc{A Constant Keyword} \rem An explicit Keyword.Constant
485 \text{• \\ttx-nv{A New Variable Name\}} \ttx-nv{A New Variable Name} \rem An explicit Name.Variable 490 \text{• \\ttx-nv{A Variable Name\}} \ttx-nv{A Variable Name} \rem An explicit Name.Variable
491 \text{• \\ttx-ni{An Entity*Name\}} \ttx-ni{An Entity*Name} \rem An explicit Name.Entity
486 \text{• \\ttx-k(∈ ∌)} \ttx-k(∈ ∌) \rem ∈ and ∌ as (ordinary) Keywords 492 \text{• \\ttx-k(∈ ∌)} \ttx-k(∈ ∌) \rem ∈ and ∌ as (ordinary) Keywords
487 \text{• \\ttx-o<∈ ∌>} \ttx-o<∈ ∌> \rem ∈ and ∌ as (ordinary) Operators 493 \text{• \\ttx-o<∈ ∌>} \ttx-o<∈ ∌> \rem ∈ and ∌ as (ordinary) Operators
488 /* 494 /*
489 * The line below has ∈_∌ as (peculiar) function name. 495 * The line below has ∈_∌ as (peculiar) function name.
490 * Their params are automatic (i.e. a normal expression). 496 * Their params are automatic (i.e. a normal expression).
501 */ 507 */
502 \text{• \\ttx-NON-EXISTING[∈_∌](p1, p2)} \ttx-NON_EXISTING[∈_∌](p1, p2) 508 \text{• \\ttx-NON-EXISTING[∈_∌](p1, p2)} \ttx-NON_EXISTING[∈_∌](p1, p2)
503 509
504 .. note:: Explicit token types are **case-sensitive**. 510 .. note:: Explicit token types are **case-sensitive**.
505 511
512 .. note:: Explicit token types work in all `expression` and `text` contexts.
513
506 514
507 .. _escaping-rules: 515 .. _escaping-rules:
508 516
509 Escaping Rules 517 Escaping Rules
510 ============== 518 ==============
582 590
583 591
584 Some Examples 592 Some Examples
585 ============= 593 =============
586 594
587 A synthetic example with many features. 595 .. rubric:: Synthetic Example
596
597 The first example is a synthetic example with many features.
588 598
589 .. only:: builder_html 599 .. only:: builder_html
590 600
591 Its source code is in :download:`examples/example-1.pseudocode`. 601 Its source code is in :download:`examples/example-1.pseudocode`.
592 602
596 606
597 .. literalinclude:: examples/example-1.pseudocode 607 .. literalinclude:: examples/example-1.pseudocode
598 :language: algpseudocode 608 :language: algpseudocode
599 :lines: 2- 609 :lines: 2-
600 610
601 With a customized `AlgPseudocodeLexer` and its `no_end` 611 The highlighted output with a customized `AlgPseudocodeLexer` and its `no_end`
602 option set to :py:obj:`True`. 612 option set to :py:obj:`True`:
603 613
604 .. literalinclude:: examples/example-1.pseudocode 614 .. literalinclude:: examples/example-1.pseudocode
605 :language: NoEndAlgPseudocode 615 :language: NoEndAlgPseudocode
606 :lines: 2- 616 :lines: 2-
607 617
618 .. rubric:: Dinic's Algorithm
619
608 The second example is Wikipedia's description of *Dinic's Algorithm* 620 The second example is Wikipedia's description of *Dinic's Algorithm*
609 (see https://en.wikipedia.org/wiki/Dinic%27s_algorithm). 621 (see https://en.wikipedia.org/wiki/Dinic%27s_algorithm).
610 622
611 .. only:: builder_html 623 .. only:: builder_html
612 624
613 Its source code is in :download:`examples/algorithm-dinic.pseudocode`. 625 Its source code is in :download:`examples/algorithm-dinic.pseudocode`.
614 626
615 .. raw:: latex 627 .. raw:: latex
616 628
617 Its source code can be found at \url{algorithm-dinic.pseudocode} 629 Its source code can be found at \url{algorithm-dinic.pseudocode}.
618 630
619 .. literalinclude:: examples/algorithm-dinic.pseudocode 631 .. literalinclude:: examples/algorithm-dinic.pseudocode
620 :language: algpseudocode 632 :language: algpseudocode
621 :lines: 2- 633 :lines: 2-
622 634
635 .. rubric:: Ford–Fulkerson Algorithm
636
623 The third example is Wikipedia's pseudocode of the *Ford–Fulkerson Algorithm* 637 The third example is Wikipedia's pseudocode of the *Ford–Fulkerson Algorithm*
624 (see https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm). 638 (see https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm).
625 639
626 .. only:: builder_html 640 .. only:: builder_html
627 641
634 648
635 .. literalinclude:: examples/algorithm-ford-fulkerson.pseudocode 649 .. literalinclude:: examples/algorithm-ford-fulkerson.pseudocode
636 :language: algpseudocode 650 :language: algpseudocode
637 :lines: 2- 651 :lines: 2-
638 652
653 .. rubric:: Edmonds–Karp Algorithm
654
639 The fourth example is Wikipedia's pseudocode of the *Edmonds–Karp Algorithm* 655 The fourth example is Wikipedia's pseudocode of the *Edmonds–Karp Algorithm*
640 (see https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm) 656 (see https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm)
641 with a custom lexer which skips all ``ENDxxx`` keywords. 657 with a custom lexer which skips all ``ENDxxx`` keywords.
642 658
643 .. only:: builder_html 659 .. only:: builder_html