diff --git a/pep_sphinx_extensions/LICENCE.rst b/pep_sphinx_extensions/LICENCE.rst index d68de666acb..4f771d4540b 100644 --- a/pep_sphinx_extensions/LICENCE.rst +++ b/pep_sphinx_extensions/LICENCE.rst @@ -1,2 +1,7 @@ -The files in this directory are placed in the public domain or under the -CC0-1.0-Universal license, whichever is more permissive. +Unless otherwise stated, the files in this directory are placed in the +public domain or under the CC0-1.0-Universal license, whichever is more +permissive. + +Some files are based on work from external projects. Check the individual +file headers for specific licensing details. In particular: +- lexers/asdl_lexer.py diff --git a/pep_sphinx_extensions/lexers/asdl_lexer.py b/pep_sphinx_extensions/lexers/asdl_lexer.py new file mode 100644 index 00000000000..62928a86698 --- /dev/null +++ b/pep_sphinx_extensions/lexers/asdl_lexer.py @@ -0,0 +1,53 @@ +# The code in this file is based on: +# https://github.com/python/cpython/blob/v3.15.2/Doc/tools/extensions/lexers/asdl_lexer.py + +# Licensed under the Python Software Foundation License v2 +# For the full license text, see: +# https://github.com/python/cpython/blob/v3.15.2/LICENSE + +from pygments.lexer import RegexLexer, bygroups, include +from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text + + +class ASDLLexer(RegexLexer): + name = "ASDL" + aliases = ["asdl"] + filenames = ["*.asdl"] + _name = r"([^\W\d]\w*)" + _text_ws = r"(\s*)" + + tokens = { + "ws": [ + (r"\n", Text), + (r"\s+", Text), + (r"--.*?$", Comment.Singleline), + ], + "root": [ + include("ws"), + ( + r"(module)" + _text_ws + _name, + bygroups(Keyword, Text, Name.Tag), + ), + ( + r"(\w+)([\?\*]*\s)(\w+)", + bygroups(Name.Builtin.Pseudo, Operator, Name), + ), + # Keep in line with ``builtin_types`` from Parser/asdl.py. + # ASDL's 4 builtin types are + # constant, identifier, int, string + ("constant|identifier|int|string", Name.Builtin), + (r"attributes", Name.Builtin), + ( + _name + _text_ws + "(=)", + bygroups(Name, Text, Operator), + ), + (_name, Name.Class), + (r"\|", Operator), + (r"{|}|\(|\)", Punctuation), + (r".", Text), + ], + } + + +def register(): + return [ASDLLexer] diff --git a/peps/pep-0823.rst b/peps/pep-0823.rst index 78198a86f39..64c39bb1cd4 100644 --- a/peps/pep-0823.rst +++ b/peps/pep-0823.rst @@ -465,7 +465,7 @@ do not support assignments themselves and thus the context will always be expression nodes. It is set to ``1`` if an expression is the topmost node in a group, ``0`` otherwise. -:: +.. code-block:: ASDL expr = ... | Attribute(expr value, identifier attr, expr_context ctx) diff --git a/peps/pep-0824.rst b/peps/pep-0824.rst index c9989050a19..c6cd257133b 100644 --- a/peps/pep-0824.rst +++ b/peps/pep-0824.rst @@ -218,7 +218,7 @@ AST changes A new ``Coalesce`` operator is added to ``boolop`` for use in ``BoolOp`` nodes. -:: +.. code-block:: ASDL expr = BoolOp(boolop op, expr* values) | ... @@ -284,7 +284,7 @@ A new ``BoolAssign`` AST node is added. Similar to ``AugAssign``, it stores a ``target`` and ``value`` expression as well as the boolean operator ``Coalesce``. -:: +.. code-block:: ASDL stmt = ... | AugAssign(expr target, operator op, expr value)