Skip to content

Commit da2bb69

Browse files
cdce8phugovk
andauthored
Infra: Add ASDL lexer from cpython (#5220)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 28581cc commit da2bb69

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎pep_sphinx_extensions/LICENCE.rst‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
The files in this directory are placed in the public domain or under the
2-
CC0-1.0-Universal license, whichever is more permissive.
1+
Unless otherwise stated, the files in this directory are placed in the
2+
public domain or under the CC0-1.0-Universal license, whichever is more
3+
permissive.
4+
5+
Some files are based on work from external projects. Check the individual
6+
file headers for specific licensing details. In particular:
7+
- lexers/asdl_lexer.py
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# The code in this file is based on:
2+
# https://github.com/python/cpython/blob/v3.15.2/Doc/tools/extensions/lexers/asdl_lexer.py
3+
4+
# Licensed under the Python Software Foundation License v2
5+
# For the full license text, see:
6+
# https://github.com/python/cpython/blob/v3.15.2/LICENSE
7+
8+
from pygments.lexer import RegexLexer, bygroups, include
9+
from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text
10+
11+
12+
class ASDLLexer(RegexLexer):
13+
name = "ASDL"
14+
aliases = ["asdl"]
15+
filenames = ["*.asdl"]
16+
_name = r"([^\W\d]\w*)"
17+
_text_ws = r"(\s*)"
18+
19+
tokens = {
20+
"ws": [
21+
(r"\n", Text),
22+
(r"\s+", Text),
23+
(r"--.*?$", Comment.Singleline),
24+
],
25+
"root": [
26+
include("ws"),
27+
(
28+
r"(module)" + _text_ws + _name,
29+
bygroups(Keyword, Text, Name.Tag),
30+
),
31+
(
32+
r"(\w+)([\?\*]*\s)(\w+)",
33+
bygroups(Name.Builtin.Pseudo, Operator, Name),
34+
),
35+
# Keep in line with ``builtin_types`` from Parser/asdl.py.
36+
# ASDL's 4 builtin types are
37+
# constant, identifier, int, string
38+
("constant|identifier|int|string", Name.Builtin),
39+
(r"attributes", Name.Builtin),
40+
(
41+
_name + _text_ws + "(=)",
42+
bygroups(Name, Text, Operator),
43+
),
44+
(_name, Name.Class),
45+
(r"\|", Operator),
46+
(r"{|}|\(|\)", Punctuation),
47+
(r".", Text),
48+
],
49+
}
50+
51+
52+
def register():
53+
return [ASDLLexer]

‎peps/pep-0823.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ do not support assignments themselves and thus the context will always be
465465
expression nodes. It is set to ``1`` if an expression is the topmost
466466
node in a group, ``0`` otherwise.
467467

468-
::
468+
.. code-block:: ASDL
469469
470470
expr = ...
471471
| Attribute(expr value, identifier attr, expr_context ctx)

‎peps/pep-0824.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ AST changes
218218
A new ``Coalesce`` operator is added to ``boolop`` for use in ``BoolOp``
219219
nodes.
220220

221-
::
221+
.. code-block:: ASDL
222222
223223
expr = BoolOp(boolop op, expr* values)
224224
| ...
@@ -284,7 +284,7 @@ A new ``BoolAssign`` AST node is added. Similar to ``AugAssign``,
284284
it stores a ``target`` and ``value`` expression as well as
285285
the boolean operator ``Coalesce``.
286286

287-
::
287+
.. code-block:: ASDL
288288
289289
stmt = ...
290290
| AugAssign(expr target, operator op, expr value)

0 commit comments

Comments
 (0)