Skip to content

Commit d60040b

Browse files
authored
bpo-40517: Implement syntax highlighting support for ASDL (#19928)
1 parent b9c46a2 commit d60040b

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Doc/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
# ---------------------
1515

1616
extensions = ['sphinx.ext.coverage', 'sphinx.ext.doctest',
17-
'pyspecific', 'c_annotations', 'escape4chm']
17+
'pyspecific', 'c_annotations', 'escape4chm',
18+
'asdl_highlight']
1819

1920

2021
doctest_global_setup = '''

Doc/library/ast.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Abstract Grammar
3535
The abstract grammar is currently defined as follows:
3636

3737
.. literalinclude:: ../../Parser/Python.asdl
38-
:language: none
38+
:language: asdl
3939

4040

4141
Node classes
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import os
2+
import sys
3+
sys.path.append(os.path.abspath("../Parser/"))
4+
5+
from pygments.lexer import RegexLexer, bygroups, include, words
6+
from pygments.token import (Comment, Generic, Keyword, Name, Operator,
7+
Punctuation, Text)
8+
9+
from asdl import builtin_types
10+
from sphinx.highlighting import lexers
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.Class),
30+
),
31+
(
32+
r"(\w+)(\*\s|\?\s|\s)(\w+)",
33+
bygroups(Name.Variable, Generic.Strong, Name.Tag),
34+
),
35+
(words(builtin_types), Keyword.Type),
36+
(r"attributes", Name.Builtin),
37+
(
38+
_name + _text_ws + "(=)",
39+
bygroups(Name.Variable, Text, Operator),
40+
),
41+
(_name, Name.Function),
42+
(r"\|", Operator),
43+
(r"{|}|\(|\)", Punctuation),
44+
(r".", Text),
45+
],
46+
}
47+
48+
49+
def setup(app):
50+
lexers["asdl"] = ASDLLexer()
51+
return {'version': '1.0', 'parallel_read_safe': True}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy