]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40517: Implement syntax highlighting support for ASDL (#19928)
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>
Wed, 6 May 2020 05:24:39 +0000 (08:24 +0300)
committerGitHub <noreply@github.com>
Wed, 6 May 2020 05:24:39 +0000 (22:24 -0700)
Doc/conf.py
Doc/library/ast.rst
Doc/tools/extensions/asdl_highlight.py [new file with mode: 0644]

index 32db34344a70a104141fbe928b7c6e111965b758..12d74ea24ce4aca139b5f2a1120b365d488e7fa0 100644 (file)
@@ -14,7 +14,8 @@ sys.path.append(os.path.abspath('includes'))
 # ---------------------
 
 extensions = ['sphinx.ext.coverage', 'sphinx.ext.doctest',
-              'pyspecific', 'c_annotations', 'escape4chm']
+              'pyspecific', 'c_annotations', 'escape4chm',
+              'asdl_highlight']
 
 
 doctest_global_setup = '''
index fc04114949c0c36a1e6e1d84a9881f8d526c140c..6c6ad01b842c8ea9d271153ec903c174dfe051ba 100644 (file)
@@ -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 (file)
index 0000000..9b003e9
--- /dev/null
@@ -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.Variable, Generic.Strong, Name.Tag),
+            ),
+            (words(builtin_types), Keyword.Type),
+            (r"attributes", Name.Builtin),
+            (
+                _name + _text_ws + "(=)",
+                bygroups(Name.Variable, 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}