Mercurial > hgrepos > Python > libs > pygments-lexer-pseudocode2
comparison docs/lexer-algpseudocode.rst @ 169:3c517c22df9c
Much more details and refinements in the docs
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 11 May 2026 10:39:39 +0200 |
| parents | bff8b900713a |
| children | ad80fcbf7b47 |
comparison
equal
deleted
inserted
replaced
| 168:bff8b900713a | 169:3c517c22df9c |
|---|---|
| 33 .. code-block:: algpseudocode-de | 33 .. code-block:: algpseudocode-de |
| 34 | 34 |
| 35 \PROGRAM {The Pseudoprogram} \IS | 35 \PROGRAM {The Pseudoprogram} \IS |
| 36 | 36 |
| 37 \END PROGRAM {The Pseudoprogram} | 37 \END PROGRAM {The Pseudoprogram} |
| 38 | |
| 39 | |
| 40 States | |
| 41 ====== | |
| 38 | 42 |
| 39 The AlgPseudocode lexer and its language variants AlgPseudocodeDE and | 43 The AlgPseudocode lexer and its language variants AlgPseudocodeDE and |
| 40 AlgPseudocodeFR basically work in three states: `default`, | 44 AlgPseudocodeFR basically work in three states: `default`, |
| 41 `expression` and `text`. | 45 `expression` and `text`. |
| 42 | 46 |
| 78 - ``\REM`` and ``\REMARK`` for remarks (aka comments) | 82 - ``\REM`` and ``\REMARK`` for remarks (aka comments) |
| 79 | 83 |
| 80 - :ref:`explicit-token-types` | 84 - :ref:`explicit-token-types` |
| 81 | 85 |
| 82 | 86 |
| 83 .. rubric:: Some Examples | |
| 84 | |
| 85 A synthetic example with many features: | |
| 86 | |
| 87 .. literalinclude:: examples/example-1.pseudocode | |
| 88 :language: algpseudocode | |
| 89 :lines: 2- | |
| 90 | |
| 91 With a customized `AlgPseudocodeLexer` and its `no_end` | |
| 92 option set to ``True``. | |
| 93 | |
| 94 .. literalinclude:: examples/example-1.pseudocode | |
| 95 :language: NoEndAlgPseudocode | |
| 96 :lines: 2- | |
| 97 | |
| 98 This is Wikipedia's description of *Dinic's Algorithm* | |
| 99 (see https://en.wikipedia.org/wiki/Dinic%27s_algorithm): | |
| 100 | |
| 101 .. literalinclude:: examples/algorithm-dinic.description | |
| 102 :language: algpseudocode | |
| 103 :lines: 2- | |
| 104 | |
| 105 This is Wikipedia's pseudocode of the *Ford–Fulkerson Algorithm* | |
| 106 (see https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm): | |
| 107 | |
| 108 .. literalinclude:: examples/algorithm-ford-fulkerson.pseudocode | |
| 109 :language: algpseudocode | |
| 110 :lines: 2- | |
| 111 | |
| 112 This is Wikipedia's pseudocode of the *Edmonds–Karp Algorithm* | |
| 113 (see https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm) | |
| 114 with a custom lexer that skip all ``ENDxxx`` keywords: | |
| 115 | |
| 116 .. literalinclude:: examples/algorithm-edmonds-karp.pseudocode | |
| 117 :language: NoEndAlgPseudocode | |
| 118 :lines: 2- | |
| 119 | |
| 120 And now the *Edmonds–Karp Algorithm* with french keywords: | |
| 121 | |
| 122 .. literalinclude:: examples/algorithm-edmonds-karp.pseudocode | |
| 123 :language: algpseudocode-fr | |
| 124 :lines: 2- | |
| 125 | |
| 126 And again the *Edmonds–Karp Algorithm* with german keywords: | |
| 127 | |
| 128 .. literalinclude:: examples/algorithm-edmonds-karp.pseudocode | |
| 129 :language: algpseudocode-de | |
| 130 :lines: 2- | |
| 131 | |
| 132 More details you will find :ref:`here <details-algpseudocode>`. | |
| 133 | |
| 134 | |
| 135 .. _details-algpseudocode: | |
| 136 | |
| 137 Lexer Options | 87 Lexer Options |
| 138 ============= | 88 ============= |
| 139 | 89 |
| 140 .. describe:: prohibit_raiseonerror_filter | 90 .. describe:: prohibit_raiseonerror_filter |
| 141 | 91 |
| 154 **Type:** :py:class:`bool` | 104 **Type:** :py:class:`bool` |
| 155 | 105 |
| 156 **Default:** `False` | 106 **Default:** `False` |
| 157 | 107 |
| 158 If ``True`` all the ``\ENDxxx`` commands will be skipped and yield | 108 If ``True`` all the ``\ENDxxx`` commands will be skipped and yield |
| 159 nothing. | 109 no output. |
| 160 | 110 |
| 161 .. describe:: gets | 111 .. describe:: gets |
| 162 | 112 |
| 163 **Type:** :py:class:`str` or :py:obj:`None` | 113 **Type:** :py:class:`str` or :py:obj:`None` |
| 164 | 114 |
| 282 Runs of dots ``.``, ``..``, ``...``, ``....``, ... are handled | 232 Runs of dots ``.``, ``..``, ``...``, ``....``, ... are handled |
| 283 properly in expressions and yield a punctuation token. | 233 properly in expressions and yield a punctuation token. |
| 284 They are not replaced by corresponding Unicode symbols. | 234 They are not replaced by corresponding Unicode symbols. |
| 285 | 235 |
| 286 | 236 |
| 287 Keywords | 237 Commands |
| 288 ======== | 238 ======== |
| 289 | |
| 290 Explicit Keywords | |
| 291 ----------------- | |
| 292 | 239 |
| 293 - Start with a backslash character ``\`` | 240 - Start with a backslash character ``\`` |
| 294 - Case-insensitive | 241 - Case-insensitive |
| 242 - Yield mostly to :py:class:`pygments.Token.Keyword` | |
| 295 - Translated if a translation is found | 243 - Translated if a translation is found |
| 296 | 244 - Depending on the command---may have required or optional parameters |
| 297 Parameter handling is as follows: | 245 |
| 298 | 246 Parameter handling is as follows: |
| 299 - Parameters are enclosed in curly braces ``{`` and ``}`` | 247 |
| 300 - Escaping within the braces is possible using the backslash ``\`` | 248 * Parameters are enclosed in curly braces ``{`` and ``}`` |
| 301 - Parameters are separated from the keyword/command by a (possibly empty) run | 249 * Escaping within the braces is possible using the backslash ``\`` as |
| 302 of space or TAB characters. | 250 escape character |
| 303 This is true for required and optional parameters. | 251 * Parameters are separated from the keyword/command by a (possibly empty) run |
| 304 | 252 of space or TAB characters. |
| 305 .. todo:: Escaping | 253 This is true for required and optional parameters. |
| 306 | 254 |
| 307 A single backslash is a Generic.Error token | 255 .. todo:: Escaping |
| 308 | 256 |
| 309 | 257 A single backslash yields a Generic.Error token when in |
| 310 With Required Parameters | 258 `default` and `expression` states. |
| 311 ~~~~~~~~~~~~~~~~~~~~~~~~ | 259 |
| 260 | |
| 261 Commands With Required Parameters | |
| 262 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| 312 | 263 |
| 313 .. code-block:: algpseudocode | 264 .. code-block:: algpseudocode |
| 314 | 265 |
| 315 \TEXT{\PROGRAM {A Program\} or \PROG {A Program\}} \PROGRAM {A Program} | 266 \TEXT{\PROGRAM {A Program\} or \PROG {A Program\}} \PROGRAM {A Program} |
| 316 \TEXT{\ALGORITHM{An Algorithm\} or \ALGO{An Algorithm\}} \ALGORITHM{An Algorithm} | 267 \TEXT{\ALGORITHM{An Algorithm\} or \ALGO{An Algorithm\}} \ALGORITHM{An Algorithm} |
| 339 \TEXT{\CALL{a function\}(p1, p2)} \CALL{a function}(p1, p2) | 290 \TEXT{\CALL{a function\}(p1, p2)} \CALL{a function}(p1, p2) |
| 340 | 291 |
| 341 \TEXT{\NAME{an entity name\}} \NAME{an entity name} | 292 \TEXT{\NAME{an entity name\}} \NAME{an entity name} |
| 342 | 293 |
| 343 | 294 |
| 344 With Optional Parameters | 295 Commands With Optional Parameters |
| 345 ~~~~~~~~~~~~~~~~~~~~~~~~ | 296 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 346 | 297 |
| 347 Some ``END``-keywords have optional parameters: | 298 Some ``END``-commands have optional parameters: |
| 348 | 299 |
| 349 .. code-block:: algpseudocode | 300 .. code-block:: algpseudocode |
| 350 | 301 |
| 351 \TEXT{\ENDPROGRAM \ENDPROG} \ENDPROGRAM | 302 \TEXT{\ENDPROGRAM \ENDPROG} \ENDPROGRAM |
| 352 \TEXT{\ENDALGORITHM \ENDALGO} \ENDALGORITHM | 303 \TEXT{\ENDALGORITHM \ENDALGO} \ENDALGORITHM |
| 358 | 309 |
| 359 .. code-block:: algpseudocode | 310 .. code-block:: algpseudocode |
| 360 | 311 |
| 361 \TEXT{\CLASS{Foo Bar Class\} ... \END CLASS {Foo Bar Class\}} \TEXT{yields} \CLASS{Foo Bar Class} ... \END CLASS {Foo Bar Class} | 312 \TEXT{\CLASS{Foo Bar Class\} ... \END CLASS {Foo Bar Class\}} \TEXT{yields} \CLASS{Foo Bar Class} ... \END CLASS {Foo Bar Class} |
| 362 | 313 |
| 363 .. seealso:: Syntax variants: `END-Keywords`_ | 314 \TEXT{\CLASS{Foo Bar Class\} ... \END CLASS} \TEXT{yields} \CLASS{Foo Bar Class} ... \END CLASS |
| 364 | 315 |
| 365 | 316 .. seealso:: For other syntax variants concerning `END` see also section |
| 366 Without Parameters | 317 `END-Commands`_. |
| 367 ~~~~~~~~~~~~~~~~~~ | 318 |
| 368 | 319 |
| 369 "Normal" Keywords | 320 Commands Without Parameters |
| 321 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| 322 | |
| 323 "Normal" Commands | |
| 370 ''''''''''''''''' | 324 ''''''''''''''''' |
| 371 | 325 |
| 372 .. code-block:: algpseudocode | 326 .. code-block:: algpseudocode |
| 373 | 327 |
| 374 \TEXT{\IF} \IF | 328 \TEXT{\IF} \IF |
| 402 ``\REMARK`` or ``\REM`` is special: all characters to the end of the | 356 ``\REMARK`` or ``\REM`` is special: all characters to the end of the |
| 403 line are taken as comment; curly braces are not needed---in fact: | 357 line are taken as comment; curly braces are not needed---in fact: |
| 404 they are interpreted to be part of the comment. | 358 they are interpreted to be part of the comment. |
| 405 | 359 |
| 406 | 360 |
| 407 END-Keywords | 361 END-Commands |
| 408 '''''''''''' | 362 '''''''''''' |
| 409 | 363 |
| 410 The separator character can be empty, a run of ASCII spaces, a run of TAB characters, | 364 The separator character can be empty, a run of ASCII spaces, a run of |
| 411 a single underscore ``_`` or a single hyphen ``-`` like: | 365 TAB characters, a single underscore ``_`` or a single hyphen ``-`` |
| 366 like: | |
| 412 | 367 |
| 413 ``\ENDIF``, ``\END IF``, ``\END-IF``, ``\END_IF`` or ``\END IF`` | 368 ``\ENDIF``, ``\END IF``, ``\END-IF``, ``\END_IF`` or ``\END IF`` |
| 414 | 369 |
| 415 | 370 |
| 416 .. code-block:: algpseudocode | 371 .. code-block:: algpseudocode |
| 425 | 380 |
| 426 \text{\END_IF} \END_IF \rem a single underscore | 381 \text{\END_IF} \END_IF \rem a single underscore |
| 427 | 382 |
| 428 \text{\END IF} \END IF \rem a single TAB character | 383 \text{\END IF} \END IF \rem a single TAB character |
| 429 | 384 |
| 430 The list of END-keywords (here always just with ``-`` as separator): | 385 The list of END-commands (here always just with ``-`` as separator): |
| 431 | 386 |
| 432 .. code-block:: algpseudocode | 387 .. code-block:: algpseudocode |
| 433 | 388 |
| 434 \text{\END-PROGRAM \END-PROG} \END-PROGRAM | 389 \text{\END-PROGRAM \END-PROG} \END-PROGRAM |
| 435 \text{\END-ALGORITHM \END-ALGO} \END-ALGORITHM | 390 \text{\END-ALGORITHM \END-ALGO} \END-ALGORITHM |
| 440 \text{\END-WHILE} \END-WHILE | 395 \text{\END-WHILE} \END-WHILE |
| 441 \text{\END-FOR} \END-FOR | 396 \text{\END-FOR} \END-FOR |
| 442 \text{\END-FORALL} \END-FORALL | 397 \text{\END-FORALL} \END-FORALL |
| 443 \text{\END-LOOP} \END-LOOP | 398 \text{\END-LOOP} \END-LOOP |
| 444 | 399 |
| 400 .. note:: The output of END-commands can be suppressed by setting the | |
| 401 lexer option ``no_end`` to ``True``. | |
| 402 | |
| 445 | 403 |
| 446 Names and Entities | 404 Names and Entities |
| 447 ================== | 405 ================== |
| 448 | 406 |
| 449 In an expression context all other words are interpreted as entity | 407 In an expression context all other words are interpreted as entity |
| 464 \TEXT{\NAME{entity-name 3\}} \NAME{entity-name 3} | 422 \TEXT{\NAME{entity-name 3\}} \NAME{entity-name 3} |
| 465 | 423 |
| 466 \TEXT{München} München | 424 \TEXT{München} München |
| 467 | 425 |
| 468 \TEXT{Genève} Genève | 426 \TEXT{Genève} Genève |
| 427 | |
| 469 | 428 |
| 470 .. _explicit-token-types: | 429 .. _explicit-token-types: |
| 471 | 430 |
| 472 Explicit Token Types | 431 Explicit Token Types |
| 473 ==================== | 432 ==================== |
| 573 Lexer *instances* that are added to | 532 Lexer *instances* that are added to |
| 574 :py:data:`sphinx.highlighting.lexers` somehow are taken as is by | 533 :py:data:`sphinx.highlighting.lexers` somehow are taken as is by |
| 575 Sphinx and are not augmented with any default filters. | 534 Sphinx and are not augmented with any default filters. |
| 576 | 535 |
| 577 For older Sphinx versions your mileage may vary. | 536 For older Sphinx versions your mileage may vary. |
| 537 | |
| 538 | |
| 539 Some Examples | |
| 540 ============= | |
| 541 | |
| 542 A synthetic example with many features: | |
| 543 | |
| 544 .. literalinclude:: examples/example-1.pseudocode | |
| 545 :language: algpseudocode | |
| 546 :lines: 2- | |
| 547 | |
| 548 With a customized `AlgPseudocodeLexer` and its `no_end` | |
| 549 option set to ``True``. | |
| 550 | |
| 551 .. literalinclude:: examples/example-1.pseudocode | |
| 552 :language: NoEndAlgPseudocode | |
| 553 :lines: 2- | |
| 554 | |
| 555 This is Wikipedia's description of *Dinic's Algorithm* | |
| 556 (see https://en.wikipedia.org/wiki/Dinic%27s_algorithm): | |
| 557 | |
| 558 .. literalinclude:: examples/algorithm-dinic.description | |
| 559 :language: algpseudocode | |
| 560 :lines: 2- | |
| 561 | |
| 562 This is Wikipedia's pseudocode of the *Ford–Fulkerson Algorithm* | |
| 563 (see https://en.wikipedia.org/wiki/Ford%E2%80%93Fulkerson_algorithm): | |
| 564 | |
| 565 .. literalinclude:: examples/algorithm-ford-fulkerson.pseudocode | |
| 566 :language: algpseudocode | |
| 567 :lines: 2- | |
| 568 | |
| 569 This is Wikipedia's pseudocode of the *Edmonds–Karp Algorithm* | |
| 570 (see https://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm) | |
| 571 with a custom lexer that skip all ``ENDxxx`` keywords: | |
| 572 | |
| 573 .. literalinclude:: examples/algorithm-edmonds-karp.pseudocode | |
| 574 :language: NoEndAlgPseudocode | |
| 575 :lines: 2- | |
| 576 | |
| 577 And now the *Edmonds–Karp Algorithm* with french keywords: | |
| 578 | |
| 579 .. literalinclude:: examples/algorithm-edmonds-karp.pseudocode | |
| 580 :language: algpseudocode-fr | |
| 581 :lines: 2- | |
| 582 | |
| 583 And again the *Edmonds–Karp Algorithm* with german keywords: | |
| 584 | |
| 585 .. literalinclude:: examples/algorithm-edmonds-karp.pseudocode | |
| 586 :language: algpseudocode-de | |
| 587 :lines: 2- |
