• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!token` --- Constants used with Python parse trees
2========================================================
3
4.. module:: token
5   :synopsis: Constants representing terminal nodes of the parse tree.
6
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
9**Source code:** :source:`Lib/token.py`
10
11--------------
12
13This module provides constants which represent the numeric values of leaf nodes
14of the parse tree (terminal tokens).  Refer to the file :file:`Grammar/Tokens`
15in the Python distribution for the definitions of the names in the context of
16the language grammar.  The specific numeric values which the names map to may
17change between Python versions.
18
19The module also provides a mapping from numeric codes to names and some
20functions.  The functions mirror definitions in the Python C header files.
21
22
23.. data:: tok_name
24
25   Dictionary mapping the numeric values of the constants defined in this module
26   back to name strings, allowing more human-readable representation of parse trees
27   to be generated.
28
29
30.. function:: ISTERMINAL(x)
31
32   Return ``True`` for terminal token values.
33
34
35.. function:: ISNONTERMINAL(x)
36
37   Return ``True`` for non-terminal token values.
38
39
40.. function:: ISEOF(x)
41
42   Return ``True`` if *x* is the marker indicating the end of input.
43
44
45The token constants are:
46
47.. include:: token-list.inc
48
49The following token type values aren't used by the C tokenizer but are needed for
50the :mod:`tokenize` module.
51
52.. data:: COMMENT
53   :noindex:
54
55   Token value used to indicate a comment.
56
57
58.. data:: NL
59   :noindex:
60
61   Token value used to indicate a non-terminating newline.  The
62   :data:`NEWLINE` token indicates the end of a logical line of Python code;
63   ``NL`` tokens are generated when a logical line of code is continued over
64   multiple physical lines.
65
66
67.. data:: ENCODING
68
69   Token value that indicates the encoding used to decode the source bytes
70   into text. The first token returned by :func:`tokenize.tokenize` will
71   always be an ``ENCODING`` token.
72
73
74.. data:: TYPE_COMMENT
75   :noindex:
76
77   Token value indicating that a type comment was recognized.  Such
78   tokens are only produced when :func:`ast.parse` is invoked with
79   ``type_comments=True``.
80
81
82.. data:: EXACT_TOKEN_TYPES
83
84   A dictionary mapping the string representation of a token to its numeric code.
85
86   .. versionadded:: 3.8
87
88
89.. versionchanged:: 3.5
90   Added :data:`!AWAIT` and :data:`!ASYNC` tokens.
91
92.. versionchanged:: 3.7
93   Added :data:`COMMENT`, :data:`NL` and :data:`ENCODING` tokens.
94
95.. versionchanged:: 3.7
96   Removed :data:`!AWAIT` and :data:`!ASYNC` tokens. "async" and "await" are
97   now tokenized as :data:`NAME` tokens.
98
99.. versionchanged:: 3.8
100   Added :data:`TYPE_COMMENT`, :data:`TYPE_IGNORE`, :data:`COLONEQUAL`.
101   Added :data:`!AWAIT` and :data:`!ASYNC` tokens back (they're needed
102   to support parsing older Python versions for :func:`ast.parse` with
103   ``feature_version`` set to 6 or lower).
104
105.. versionchanged:: 3.13
106   Removed :data:`!AWAIT` and :data:`!ASYNC` tokens again.
107
108