From b35c16e92354a8d07d55bad01ec39f7e105cad5c Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Tue, 5 May 2020 16:52:06 +0300 Subject: [PATCH 1/3] bpo-40517: Implement syntax highlighting support for ASDL --- Doc/conf.py | 3 +- Doc/library/ast.rst | 2 +- Doc/tools/extensions/asdl_highlight.py | 51 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 Doc/tools/extensions/asdl_highlight.py diff --git a/Doc/conf.py b/Doc/conf.py index 32db34344a70a1..12d74ea24ce4ac 100644 --- a/Doc/conf.py +++ b/Doc/conf.py @@ -14,7 +14,8 @@ # --------------------- extensions = ['sphinx.ext.coverage', 'sphinx.ext.doctest', - 'pyspecific', 'c_annotations', 'escape4chm'] + 'pyspecific', 'c_annotations', 'escape4chm', + 'asdl_highlight'] doctest_global_setup = ''' diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst index fc04114949c0c3..6c6ad01b842c8e 100644 --- a/Doc/library/ast.rst +++ b/Doc/library/ast.rst @@ -35,7 +35,7 @@ Abstract Grammar The abstract grammar is currently defined as follows: .. literalinclude:: ../../Parser/Python.asdl - :language: none + :language: asdl Node classes diff --git a/Doc/tools/extensions/asdl_highlight.py b/Doc/tools/extensions/asdl_highlight.py new file mode 100644 index 00000000000000..713d6d4414f5c2 --- /dev/null +++ b/Doc/tools/extensions/asdl_highlight.py @@ -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.Class), + ), + ( + 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.Function), + (r"\|", Operator), + (r"{|}|\(|\)", Punctuation), + (r".", Text), + ], + } + + +def setup(app): + lexers["asdl"] = ASDLLexer() + return {'version': '1.0', 'parallel_read_safe': True} From 6f68610bbc5c2b473f5137c1c5ff9e423d152892 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Wed, 6 May 2020 23:09:54 +0300 Subject: [PATCH 2/3] boldify class names --- Doc/tools/extensions/asdl_highlight.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/tools/extensions/asdl_highlight.py b/Doc/tools/extensions/asdl_highlight.py index 713d6d4414f5c2..33d44c109c9952 100644 --- a/Doc/tools/extensions/asdl_highlight.py +++ b/Doc/tools/extensions/asdl_highlight.py @@ -26,7 +26,7 @@ class ASDLLexer(RegexLexer): include("ws"), ( r"(module)" + _text_ws + _name, - bygroups(Keyword, Text, Name.Class), + bygroups(Keyword, Text, Name.Entity), ), ( r"(\w+)(\*\s|\?\s|\s)(\w+)", @@ -38,7 +38,7 @@ class ASDLLexer(RegexLexer): _name + _text_ws + "(=)", bygroups(Name, Text, Operator), ), - (_name, Name.Function), + (_name, Name.Class), (r"\|", Operator), (r"{|}|\(|\)", Punctuation), (r".", Text), From 8ff3e69e707b3a781106d0e2058d16b60520d801 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Thu, 7 May 2020 02:44:42 +0300 Subject: [PATCH 3/3] dark boldify module --- Doc/tools/extensions/asdl_highlight.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tools/extensions/asdl_highlight.py b/Doc/tools/extensions/asdl_highlight.py index 33d44c109c9952..7d2ef011c1b766 100644 --- a/Doc/tools/extensions/asdl_highlight.py +++ b/Doc/tools/extensions/asdl_highlight.py @@ -26,7 +26,7 @@ class ASDLLexer(RegexLexer): include("ws"), ( r"(module)" + _text_ws + _name, - bygroups(Keyword, Text, Name.Entity), + bygroups(Keyword, Text, Name.Tag), ), ( r"(\w+)(\*\s|\?\s|\s)(\w+)",