• 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
54   Token value used to indicate a comment.
55
56
57.. data:: NL
58
59   Token value used to indicate a non-terminating newline.  The
60   :data:`NEWLINE` token indicates the end of a logical line of Python code;
61   ``NL`` tokens are generated when a logical line of code is continued over
62   multiple physical lines.
63
64
65.. data:: ENCODING
66
67   Token value that indicates the encoding used to decode the source bytes
68   into text. The first token returned by :func:`tokenize.tokenize` will
69   always be an ``ENCODING`` token.
70
71
72.. data:: TYPE_COMMENT
73   :noindex:
74
75   Token value indicating that a type comment was recognized.  Such
76   tokens are only produced when :func:`ast.parse()` is invoked with
77   ``type_comments=True``.
78
79
80.. versionchanged:: 3.5
81   Added :data:`AWAIT` and :data:`ASYNC` tokens.
82
83.. versionchanged:: 3.7
84   Added :data:`COMMENT`, :data:`NL` and :data:`ENCODING` tokens.
85
86.. versionchanged:: 3.7
87   Removed :data:`AWAIT` and :data:`ASYNC` tokens. "async" and "await" are
88   now tokenized as :data:`NAME` tokens.
89
90.. versionchanged:: 3.8
91   Added :data:`TYPE_COMMENT`, :data:`TYPE_IGNORE`, :data:`COLONEQUAL`.
92   Added :data:`AWAIT` and :data:`ASYNC` tokens back (they're needed
93   to support parsing older Python versions for :func:`ast.parse` with
94   ``feature_version`` set to 6 or lower).
95