comparison docs/lexer-algpseudocode.rst @ 285:afbca50b7dc1

Implement an alternate syntax for "Explicit Token Types". Uses a generic two-argument syntax and allows escaping of characters using the common excaping rules. For this to work the AlgPseudocodeLexer is now based on Pygment's ExtendedRegexLexer instead of RegexLexer.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 20 May 2026 20:35:37 +0200
parents 1683a10eabb2
children 051c8877ee22
comparison
equal deleted inserted replaced
284:1683a10eabb2 285:afbca50b7dc1
455 ==================== 455 ====================
456 456
457 They allow to handle keywords and operators that are not recognized by default. 457 They allow to handle keywords and operators that are not recognized by default.
458 And they allow the user to explicitely highlight some input text at low-level. 458 And they allow the user to explicitely highlight some input text at low-level.
459 459
460 .. note:: Explicit token types are **case-sensitive**.
461
462 .. note:: Explicit token types work in all `expression` and `text` contexts.
463
464 .. note:: Nested explicit token types are *not supported*.
465
466
467 Current and Recommended Syntax
468 ------------------------------
469
470 The current and recommended use is the ``\ttX{ARG1}{ARG2}`` command.
471
472 This command has two required parameters:
473
474 #. The content of the first argument `ARG1` must be a `value` in the
475 :py:data:`pygments.token.STANDARD_TYPES` dict.
476 Its corresponding token type (the associated `key` in this dictionary)
477 will be used as token type for the token.
478
479 #. The content of the second argument will given the token type of
480 the first parameter.
481
482 Standard `Escaping Rules`_ apply to this argument!
483
484 Examples:
485
486 .. code-block:: algpseudocode
487
488 \text{• \\ttX{kc\}{C\}} \ttX{kc}{C} \rem C as Keyword.Constant
489 \text{• \\ttX{ow\}{∈\}} \ttX{ow}{∈} \rem ∈ as Operator.Word
490 \text{• \\ttX{kc\}{A Constant Keyword\}} \ttX{kc}{A Constant Keyword} \rem An explicit Keyword.Constant
491 \text{• \\ttX{nv\}{A Variable Name\}} \ttX{nv}{A Variable Name} \rem An explicit Name.Variable
492 \text{• \\ttX{ni\}{An Entity*Name\}} \ttX{ni}{An Entity*Name} \rem An explicit Name.Entity
493 \text{• \\ttX{k\}{∈ ∌\}} \ttX{k}{∈ ∌} \rem ∈ and ∌ as (ordinary) Keywords
494 \text{• \\ttX{o\}{∈ ∌\}} \ttX{o}{∈ ∌} \rem ∈ and ∌ as (ordinary) Operators
495 /*
496 * The line below has ∈_∌ as (peculiar) function name.
497 * Their params are automatic (i.e. a normal expression).
498 */
499 \text{• \\ttX{nf\}{∈_∌\}(p1, p2)} \ttX{nf}{∈_∌}(p1, p2)
500 /*
501 * The line below has ∈_∌ as (peculiar) decorator name (as used in Python).
502 * Their params are automatic (i.e. a normal expression).
503 */
504 \text{• \\ttX{nd\}{∈_∌\}(p1, p2)} \ttX{nd}{∈_∌}(p1, p2)
505 /*
506 * Normal emphasis ("strong")
507 */
508 \text{• \\ttX{gs\}{this is strong\}} \ttX{gs}{this is strong}
509 /*
510 * A strong emphasis.
511 * Note that the backslash is a valid delimiter!
512 */
513 \text{• \\ttX{ges\}{A Strong Emphasis!\}} \ttX{ges}{A Strong Emphasis!}
514 /*
515 * Escaping is allowed and needed for the closing brace!
516 * The example token type is a "String".
517 */
518 \text{• \\ttX{s\}{Escaping brace \\\} and backslash \\\\!\}} \ttX{s}{Escaping brace \} and backslash \\!}
519 /*
520 * This is a non-existing token type: you get some generic error markup
521 * with a Generic.Error token and no expansion.
522 */
523 \text{• \\ttX{NON-EXISTING\}{∈_∌\}(p1, p2)} \ttX{NON_EXISTING}{∈_∌}(p1, p2)
524
525
526
527 Old Syntax (Deprecated)
528 -----------------------
529
530 .. deprecated:: 3.0
531 Use `Current and Recommended Syntax`_ instead.
532
533 .. note:: The lower-case ``x`` in ``\ttx-``!
534
460 `XX` represents a `value` in the :py:data:`pygments.token.STANDARD_TYPES` 535 `XX` represents a `value` in the :py:data:`pygments.token.STANDARD_TYPES`
461 dict. 536 dict.
462 Its corresponding token type (the associated `key` in this `dict`) is 537 Its corresponding token type (the associated `key` in this `dict`) is
463 used as token type. 538 used as token type.
464 539
518 * This is a non-existing token type: you get some generic error markup 593 * This is a non-existing token type: you get some generic error markup
519 * with a Generic.Error token and no expansion. 594 * with a Generic.Error token and no expansion.
520 */ 595 */
521 \text{• \\ttx-NON-EXISTING?∈_∌?(p1, p2)} \ttx-NON_EXISTING?∈_∌?(p1, p2) 596 \text{• \\ttx-NON-EXISTING?∈_∌?(p1, p2)} \ttx-NON_EXISTING?∈_∌?(p1, p2)
522 597
523 .. note:: Explicit token types are **case-sensitive**.
524
525 .. note:: Explicit token types work in all `expression` and `text` contexts.
526
527 598
528 .. _escaping-rules: 599 .. _escaping-rules:
529 600
530 Escaping Rules 601 Escaping Rules
531 ============== 602 ==============
538 parameters environment. 609 parameters environment.
539 It must be escaped using ``\}`` if a closing curly brace is part of the 610 It must be escaped using ``\}`` if a closing curly brace is part of the
540 argument content. 611 argument content.
541 612
542 A single backslash yields a :py:class:`pygments.token.Token.Generic.Error` 613 A single backslash yields a :py:class:`pygments.token.Token.Generic.Error`
543 token when in `default` and `expression` states. 614 token when in `default` and `expression` states
615 (and also in `Explicit Token Types`_).
544 Contrary---in `text` contexts a single backslash character that does not 616 Contrary---in `text` contexts a single backslash character that does not
545 introduce a command yields a normal text token. 617 introduce a command yields a normal text token.
546 618
547 In all contexts a backslash that would normally introduce a known command 619 In all contexts a backslash that would normally introduce a known command
548 must be escaped if the content should not recognized as a command. 620 must be escaped if the content should not recognized as a command.