pygments-main / docs / src / tokens.txt

Full commit
.. -*- mode: rst -*-

Builtin Tokens

Inside the `pygments.token` module, there is a special object called `Token`
that is used to create token types.

You can create a new token type by accessing an attribute of `Token`:

.. sourcecode:: pycon

    >>> from pygments.token import Token
    >>> Token.String
    >>> Token.String is Token.String

Note that tokens are singletons so you can use the ``is`` operator for comparing
token types.

As of Pygments 0.7 you can also use the ``in`` operator to perform set tests:

.. sourcecode:: pycon

    >>> from pygments.token import Comment
    >>> Comment.Single in Comment
    >>> Comment in Comment.Multi

This can be useful in `filters`_ and if you write lexers on your own without
using the base lexers.

You can also split a token type into a hierarchy, and get the parent of it:

.. sourcecode:: pycon

    >>> String.split()
    [Token, Token.Literal, Token.Literal.String]
    >>> String.parent

In principle, you can create an unlimited number of token types but nobody can
guarantee that a style would define style rules for a token type. Because of
that, Pygments proposes some global token types defined in the
`pygments.token.STANDARD_TYPES` dict.

For some tokens aliases are already defined:

.. sourcecode:: pycon

    >>> from pygments.token import String
    >>> String

Inside the `pygments.token` module the following aliases are defined:

============= ============================ ====================================
`Text`        `Token.Text`                 for any type of text data
`Whitespace`  `Token.Text.Whitespace`      for specially highlighted whitespace
`Error`       `Token.Error`                represents lexer errors
`Other`       `Token.Other`                special token for data not
                                           matched by a parser (e.g. HTML
                                           markup in PHP code)
`Keyword`     `Token.Keyword`              any kind of keywords
`Name`        `Token.Name`                 variable/function names
`Literal`     `Token.Literal`              Any literals
`String`      `Token.Literal.String`       string literals
`Number`      `Token.Literal.Number`       number literals
`Operator`    `Token.Operator`             operators (``+``, ``not``...)
`Punctuation` `Token.Punctuation`          punctuation (``[``, ``(``...)
`Comment`     `Token.Comment`              any kind of comments
`Generic`     `Token.Generic`              generic tokens (have a look at
                                           the explanation below)
============= ============================ ====================================

The `Whitespace` token type is new in Pygments 0.8. It is used only by the
`VisibleWhitespaceFilter` currently.

Normally you just create token types using the already defined aliases. For each
of those token aliases, a number of subtypes exists (excluding the special tokens
`Token.Text`, `Token.Error` and `Token.Other`)

The `is_token_subtype()` function in the `pygments.token` module can be used to
test if a token type is a subtype of another (such as `Name.Tag` and `Name`).
(This is the same as ``Name.Tag in Name``. The overloaded `in` operator was newly
introduced in Pygments 0.7, the function still exists for backwards

With Pygments 0.7, it's also possible to convert strings to token types (for example
if you want to supply a token from the command line):

.. sourcecode:: pycon

    >>> from pygments.token import String, string_to_tokentype
    >>> string_to_tokentype("String")
    >>> string_to_tokentype("Token.Literal.String")
    >>> string_to_tokentype(String)

Keyword Tokens

    For any kind of keyword (especially if it doesn't match any of the
    subtypes of course).

    For keywords that are constants (e.g. ``None`` in future Python versions).

    For keywords used for variable declaration (e.g. ``var`` in some programming
    languages like JavaScript).

    For keywords used for namespace declarations (e.g. ``import`` in Python and
    Java and ``package`` in Java).

    For keywords that aren't really keywords (e.g. ``None`` in old Python

    For reserved keywords.

    For builtin types that can't be used as identifiers (e.g. ``int``,
    ``char`` etc. in C).

Name Tokens

    For any name (variable names, function names, classes).

    For all attributes (e.g. in HTML tags).

    Builtin names; names that are available in the global namespace.

    Builtin names that are implicit (e.g. ``self`` in Ruby, ``this`` in Java).

    Class names. Because no lexer can know if a name is a class or a function
    or something else this token is meant for class declarations.

    Token type for constants. In some languages you can recognise a token by the
    way it's defined (the value after a ``const`` keyword for example). In
    other languages constants are uppercase by definition (Ruby).

    Token type for decorators. Decorators are synatic elements in the Python
    language. Similar syntax elements exist in C# and Java.

    Token type for special entities. (e.g. `` `` in HTML).

    Token type for exception names (e.g. ``RuntimeError`` in Python). Some languages
    define exceptions in the function signature (Java). You can highlight
    the name of that exception using this token then.

    Token type for function names.

    Token type for label names (e.g. in languages that support ``goto``).

    Token type for namespaces. (e.g. import paths in Java/Python), names following
    the ``module``/``namespace`` keyword in other languages.

    Other names. Normally unused.

    Tag names (in HTML/XML markup or configuration files).

    Token type for variables. Some languages have prefixes for variable names
    (PHP, Ruby, Perl). You can highlight them using this token.

    same as `Name.Variable` but for class variables (also static variables).

    same as `Name.Variable` but for global variables (used in Ruby, for

    same as `Name.Variable` but for instance variables.


    For any literal (if not further defined).

    for date literals (e.g. ``42d`` in Boo).

    For any string literal.

    Token type for strings enclosed in backticks.

    Token type for single characters (e.g. Java, C).

    Token type for documentation strings (for example Python).

    Double quoted strings.

    Token type for escape sequences in strings.

    Token type for "heredoc" strings (e.g. in Ruby or Perl).

    Token type for interpolated parts in strings (e.g. ``#{foo}`` in Ruby).

    Token type for any other strings (for example ``%q{foo}`` string constructs
    in Ruby).

    Token type for regular expression literals (e.g. ``/foo/`` in JavaScript).

    Token type for single quoted strings.

    Token type for symbols (e.g. ``:foo`` in LISP or Ruby).

    Token type for any number literal.

    Token type for float literals (e.g. ``42.0``).

    Token type for hexadecimal number literals (e.g. ``0xdeadbeef``).

    Token type for integer literals (e.g. ``42``).

    Token type for long integer literals (e.g. ``42L`` in Python).

    Token type for octal literals.


    For any punctuation operator (e.g. ``+``, ``-``).

    For any operator that is a word (e.g. ``not``).


*New in Pygments 0.7.*

    For any punctuation which is not an operator (e.g. ``[``, ``(``...)


    Token type for any comment.

    Token type for multiline comments.

    Token type for preprocessor comments (also ``<?php``/``<%`` constructs).

    Token type for comments that end at the end of a line (e.g. ``# foo``).

    Special data in comments. For example code tags, author and license
    information, etc.

Generic Tokens

Generic tokens are for special lexers like the `DiffLexer` that doesn't really
highlight a programming language but a patch file.

    A generic, unstyled token. Normally you don't use this token type.

    Marks the token value as deleted.

    Marks the token value as emphasized.

    Marks the token value as an error message.

    Marks the token value as headline.

    Marks the token value as inserted.

    Marks the token value as program output (e.g. for python cli lexer).

    Marks the token value as command prompt (e.g. bash lexer).

    Marks the token value as bold (e.g. for rst lexer).

    Marks the token value as subheadline.

    Marks the token value as a part of an error traceback.

.. _filters: filters.txt