Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-40517: Implement syntax highlighting support for ASDL #19967

Merged
merged 3 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
# ---------------------

extensions = ['sphinx.ext.coverage', 'sphinx.ext.doctest',
'pyspecific', 'c_annotations', 'escape4chm']
'pyspecific', 'c_annotations', 'escape4chm',
'asdl_highlight']


doctest_global_setup = '''
Expand Down
2 changes: 1 addition & 1 deletion Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Abstract Grammar
The abstract grammar is currently defined as follows:

.. literalinclude:: ../../Parser/Python.asdl
:language: none
:language: asdl


Node classes
Expand Down
51 changes: 51 additions & 0 deletions Doc/tools/extensions/asdl_highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import sys
sys.path.append(os.path.abspath("../Parser/"))

from pygments.lexer import RegexLexer, bygroups, include, words
from pygments.token import (Comment, Generic, Keyword, Name, Operator,
Punctuation, Text)

from asdl import builtin_types
from sphinx.highlighting import lexers

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|\?\s|\s)(\w+)",
bygroups(Name.Builtin.Pseudo, Operator, Name),
),
(words(builtin_types), Name.Builtin),
(r"attributes", Name.Builtin),
(
_name + _text_ws + "(=)",
bygroups(Name, Text, Operator),
),
(_name, Name.Class),
(r"\|", Operator),
(r"{|}|\(|\)", Punctuation),
(r".", Text),
],
}


def setup(app):
lexers["asdl"] = ASDLLexer()
return {'version': '1.0', 'parallel_read_safe': True}