Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
Raymond Hettinger | a199368 | 2011-01-27 01:20:32 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/token.py` |
| 10 | |
| 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
| 13 | This module provides constants which represent the numeric values of leaf nodes |
| 14 | of the parse tree (terminal tokens). Refer to the file :file:`Grammar/Grammar` |
| 15 | in the Python distribution for the definitions of the names in the context of |
| 16 | the language grammar. The specific numeric values which the names map to may |
| 17 | change between Python versions. |
| 18 | |
Georg Brandl | eec2d76 | 2010-10-17 09:46:11 +0000 | [diff] [blame] | 19 | The module also provides a mapping from numeric codes to names and some |
| 20 | functions. The functions mirror definitions in the Python C header files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Georg Brandl | eec2d76 | 2010-10-17 09:46:11 +0000 | [diff] [blame] | 45 | The token constants are: |
| 46 | |
Serhiy Storchaka | 8ac6581 | 2018-12-22 11:18:40 +0200 | [diff] [blame] | 47 | .. include:: token-list.inc |
Albert-Jan Nijburg | fc354f0 | 2017-05-31 15:00:21 +0100 | [diff] [blame] | 48 | |
Serhiy Storchaka | 5cefb6c | 2017-06-06 18:43:35 +0300 | [diff] [blame] | 49 | The following token type values aren't used by the C tokenizer but are needed for |
| 50 | the :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 Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 72 | .. 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 Storchaka | 5cefb6c | 2017-06-06 18:43:35 +0300 | [diff] [blame] | 79 | .. versionchanged:: 3.5 |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 80 | Added :data:`AWAIT` and :data:`ASYNC` tokens. |
Serhiy Storchaka | 5cefb6c | 2017-06-06 18:43:35 +0300 | [diff] [blame] | 81 | |
| 82 | .. versionchanged:: 3.7 |
| 83 | Added :data:`COMMENT`, :data:`NL` and :data:`ENCODING` tokens. |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 84 | |
| 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 Rossum | dcfcd14 | 2019-01-31 03:40:27 -0800 | [diff] [blame] | 88 | |
| 89 | .. versionchanged:: 3.8 |
| 90 | Added :data:`TYPE_COMMENT`. |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 91 | Added :data:`AWAIT` and :data:`ASYNC` tokens back (they're needed |
| 92 | to support parsing older Python versions for :func:`ast.parse` with |
| 93 | ``feature_version`` set to 6 or lower). |