blob: 4936e9aa08f43d0215430600746f245510369da5 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`token` --- Constants used with Python parse trees
2=======================================================
3
4.. module:: token
5 :synopsis: Constants representing terminal nodes of the parse tree.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
Raymond Hettingera1993682011-01-27 01:20:32 +00009**Source code:** :source:`Lib/token.py`
10
11--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
13This module provides constants which represent the numeric values of leaf nodes
14of the parse tree (terminal tokens). Refer to the file :file:`Grammar/Grammar`
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
Georg Brandleec2d762010-10-17 09:46:11 +000019The module also provides a mapping from numeric codes to names and some
20functions. The functions mirror definitions in the Python C header files.
Georg Brandl116aa622007-08-15 14:28:22 +000021
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
Georg Brandleec2d762010-10-17 09:46:11 +000045The token constants are:
46
Serhiy Storchaka8ac65812018-12-22 11:18:40 +020047.. include:: token-list.inc
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +010048
Serhiy Storchaka5cefb6c2017-06-06 18:43:35 +030049The 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
Guido van Rossumdcfcd142019-01-31 03:40:27 -080072.. data:: TYPE_COMMENT
73
74 Token value indicating that a type comment was recognized. Such
75 tokens are only produced when :func:`ast.parse()` is invoked with
76 ``type_comments=True``.
77
78
Serhiy Storchaka5cefb6c2017-06-06 18:43:35 +030079.. versionchanged:: 3.5
Jelle Zijlstraac317702017-10-05 20:24:46 -070080 Added :data:`AWAIT` and :data:`ASYNC` tokens.
Serhiy Storchaka5cefb6c2017-06-06 18:43:35 +030081
82.. versionchanged:: 3.7
83 Added :data:`COMMENT`, :data:`NL` and :data:`ENCODING` tokens.
Jelle Zijlstraac317702017-10-05 20:24:46 -070084
85.. versionchanged:: 3.7
86 Removed :data:`AWAIT` and :data:`ASYNC` tokens. "async" and "await" are
87 now tokenized as :data:`NAME` tokens.
Guido van Rossumdcfcd142019-01-31 03:40:27 -080088
89.. versionchanged:: 3.8
90 Added :data:`TYPE_COMMENT`.