Skip to content

Commit 7431c37

Browse files
AA-Turnerhugovk
andauthored
GH-121970: Combine custom Pygments lexers into a package (#121976)
Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 12c1afa commit 7431c37

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

Doc/conf.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
# ---------------------
1919

2020
extensions = [
21-
'asdl_highlight',
2221
'c_annotations',
2322
'escape4chm',
2423
'glossary_search',
25-
'peg_highlight',
24+
'lexers',
2625
'pyspecific',
2726
'sphinx.ext.coverage',
2827
'sphinx.ext.doctest',
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .asdl_lexer import ASDLLexer
2+
from .peg_lexer import PEGLexer
3+
4+
5+
def setup(app):
6+
# Used for highlighting Parser/Python.asdl in library/ast.rst
7+
app.add_lexer("asdl", ASDLLexer)
8+
# Used for highlighting Grammar/python.gram in reference/grammar.rst
9+
app.add_lexer("peg", PEGLexer)
10+
11+
return {
12+
"version": "1.0",
13+
"parallel_read_safe": True,
14+
"parallel_write_safe": True,
15+
}

Doc/tools/extensions/asdl_highlight.py renamed to Doc/tools/extensions/lexers/asdl_lexer.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import sys
2-
from pathlib import Path
1+
from pygments.lexer import RegexLexer, bygroups, include
2+
from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text
33

4-
CPYTHON_ROOT = Path(__file__).resolve().parent.parent.parent.parent
5-
sys.path.append(str(CPYTHON_ROOT / "Parser"))
6-
7-
from pygments.lexer import RegexLexer, bygroups, include, words
8-
from pygments.token import (Comment, Keyword, Name, Operator,
9-
Punctuation, Text)
10-
11-
from asdl import builtin_types
12-
from sphinx.highlighting import lexers
134

145
class ASDLLexer(RegexLexer):
156
name = "ASDL"
@@ -34,7 +25,10 @@ class ASDLLexer(RegexLexer):
3425
r"(\w+)(\*\s|\?\s|\s)(\w+)",
3526
bygroups(Name.Builtin.Pseudo, Operator, Name),
3627
),
37-
(words(builtin_types), Name.Builtin),
28+
# Keep in line with ``builtin_types`` from Parser/asdl.py.
29+
# ASDL's 4 builtin types are
30+
# constant, identifier, int, string
31+
('constant|identifier|int|string', Name.Builtin),
3832
(r"attributes", Name.Builtin),
3933
(
4034
_name + _text_ws + "(=)",
@@ -46,8 +40,3 @@ class ASDLLexer(RegexLexer):
4640
(r".", Text),
4741
],
4842
}
49-
50-
51-
def setup(app):
52-
lexers["asdl"] = ASDLLexer()
53-
return {'version': '1.0', 'parallel_read_safe': True}

Doc/tools/extensions/peg_highlight.py renamed to Doc/tools/extensions/lexers/peg_lexer.py

-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from pygments.lexer import RegexLexer, bygroups, include
22
from pygments.token import Comment, Keyword, Name, Operator, Punctuation, Text
33

4-
from sphinx.highlighting import lexers
5-
64

75
class PEGLexer(RegexLexer):
86
"""Pygments Lexer for PEG grammar (.gram) files
@@ -79,8 +77,3 @@ class PEGLexer(RegexLexer):
7977
(r".", Text),
8078
],
8179
}
82-
83-
84-
def setup(app):
85-
lexers["peg"] = PEGLexer()
86-
return {"version": "1.0", "parallel_read_safe": True}

0 commit comments

Comments
 (0)