Skip to content

Commit a8383d6

Browse files
committed
bpo-40517: Implement syntax highlighting support for ASDL
1 parent 846d8b2 commit a8383d6

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

Diff for: Doc/conf.py

+2-1
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 = '''

Diff for: Doc/library/ast.rst

+1-1
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

Diff for: Doc/tools/extensions/asdl_highlight.py

+51
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)