token — Constants used with Python parse trees

Source code:Lib/token.py


This module provides constants which represent the numeric values of leaf nodesof the parse tree (terminal tokens). Refer to the fileGrammar/Grammarin the Python distribution for the definitions of the names in the context ofthe language grammar. The specific numeric values which the names map to maychange between Python versions.

The module also provides a mapping from numeric codes to names and somefunctions. The functions mirror definitions in the Python C header files.

token.tok_name

Dictionary mapping the numeric values of the constants defined in this moduleback to name strings, allowing more human-readable representation of parse treesto be generated.

token.ISTERMINAL(x)

ReturnTrue for terminal token values.

token.ISNONTERMINAL(x)

ReturnTrue for non-terminal token values.

token.ISEOF(x)

ReturnTrue ifx is the marker indicating the end of input.

The token constants are:

token.ENDMARKER
token.NAME
token.NUMBER
token.STRING
token.NEWLINE
token.INDENT
token.DEDENT
token.LPAR
token.RPAR
token.LSQB
token.RSQB
token.COLON
token.COMMA
token.SEMI
token.PLUS
token.MINUS
token.STAR
token.SLASH
token.VBAR
token.AMPER
token.LESS
token.GREATER
token.EQUAL
token.DOT
token.PERCENT
token.LBRACE
token.RBRACE
token.EQEQUAL
token.NOTEQUAL
token.LESSEQUAL
token.GREATEREQUAL
token.TILDE
token.CIRCUMFLEX
token.LEFTSHIFT
token.RIGHTSHIFT
token.DOUBLESTAR
token.PLUSEQUAL
token.MINEQUAL
token.STAREQUAL
token.SLASHEQUAL
token.PERCENTEQUAL
token.AMPEREQUAL
token.VBAREQUAL
token.CIRCUMFLEXEQUAL
token.LEFTSHIFTEQUAL
token.RIGHTSHIFTEQUAL
token.DOUBLESTAREQUAL
token.DOUBLESLASH
token.DOUBLESLASHEQUAL
token.AT
token.ATEQUAL
token.RARROW
token.ELLIPSIS
token.OP
token.ERRORTOKEN
token.N_TOKENS
token.NT_OFFSET

The following token type values aren’t used by the C tokenizer but are needed forthetokenize module.

token.COMMENT

Token value used to indicate a comment.

token.NL

Token value used to indicate a non-terminating newline. TheNEWLINE token indicates the end of a logical line of Python code;NL tokens are generated when a logical line of code is continued overmultiple physical lines.

token.ENCODING

Token value that indicates the encoding used to decode the source bytesinto text. The first token returned bytokenize.tokenize() willalways be anENCODING token.

Changed in version 3.5:AddedAWAIT andASYNC tokens.

Changed in version 3.7:AddedCOMMENT,NL andENCODING tokens.

Changed in version 3.7:RemovedAWAIT andASYNC tokens. “async” and “await” arenow tokenized asNAME tokens.