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-40939: Add the new grammar to the grammar specification documentation #19969

Merged
merged 8 commits into from Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/conf.py
Expand Up @@ -15,7 +15,7 @@

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


doctest_global_setup = '''
Expand Down
7 changes: 4 additions & 3 deletions Doc/reference/grammar.rst
@@ -1,7 +1,8 @@
Full Grammar specification
==========================

This is the full Python grammar, as it is read by the parser generator and used
to parse Python source files:
This is the full Python grammar, derived directly from the grammar
gvanrossum marked this conversation as resolved.
Show resolved Hide resolved
used to generate the CPython parser (see :source:`Grammar/python.gram`).

.. literalinclude:: ../../Grammar/Grammar
.. literalinclude:: ../../Grammar/python.gram
:language: peg
79 changes: 79 additions & 0 deletions Doc/tools/extensions/peg_highlight.py
@@ -0,0 +1,79 @@
from pygments.lexer import RegexLexer, bygroups, include
from pygments.token import Comment, Generic, Keyword, Name, Operator, Punctuation, Text

from sphinx.highlighting import lexers


class PEGLexer(RegexLexer):
"""Pygments Lexer for PEG grammar (.gram) files

This lexer strips the following elements from the grammar:

- Meta-tags
- Variable assignments
- Actions
- Lookaheads
- Rule types
- Rule options
- Rules named `invalid_*` or `incorrect_*`
"""

name = "PEG"
aliases = ["peg"]
filenames = ["*.gram"]
_name = r"([^\W\d]\w*)"
_text_ws = r"(\s*)"

tokens = {
"ws": [(r"\n", Text), (r"\s+", Text), (r"#.*$", Comment.Singleline),],
"lookaheads": [
gvanrossum marked this conversation as resolved.
Show resolved Hide resolved
(r"(&\w+\s?)", bygroups(None)),
(r"(&'.+'\s?)", bygroups(None)),
(r'(&".+"\s?)', bygroups(None)),
(r"(&\(.+\)\s?)", bygroups(None)),
(r"(!\w+\s?)", bygroups(None)),
(r"(!'.+'\s?)", bygroups(None)),
(r'(!".+"\s?)', bygroups(None)),
(r"(!\(.+\)\s?)", bygroups(None)),
],
"metas": [
(r"(@\w+ '''(.|\n)+?''')", bygroups(None)),
(r"^(@.*)$", bygroups(None)),
],
"actions": [(r"{(.|\n)+?}", bygroups(None)),],
"strings": [
(r"'\w+?'", Keyword),
(r'"\w+?"', Keyword),
lysnikolaou marked this conversation as resolved.
Show resolved Hide resolved
(r"'\W+?'", Text),
(r'"\W+?"', Text),
],
"variables": [(_name + _text_ws + "(=)", bygroups(None, None, None),),],
"invalids": [
(r"^(\s+\|\s+invalid_\w+\s*\n)", bygroups(None)),
(r"^(\s+\|\s+incorrect_\w+\s*\n)", bygroups(None)),
(r"^(#.*invalid syntax.*(?:.|\n)*)", bygroups(None),),
],
"root": [
include("invalids"),
include("ws"),
include("lookaheads"),
include("metas"),
include("actions"),
include("strings"),
include("variables"),
(r"\b(?!(NULL|EXTRA))([A-Z_]+)\b\s*(?!\()", Text,),
(
r"^\s*" + _name + "\s*" + "(\[.*\])?" + "\s*" + "(\(.+\))?" + "\s*(:)",
bygroups(Name.Function, None, None, Punctuation),
),
(_name, Name.Function),
(r"[\||\.|\+|\*|\?]", Operator),
(r"{|}|\(|\)|\[|\]", Punctuation),
(r".", Text),
],
}


def setup(app):
lexers["peg"] = PEGLexer()
return {"version": "1.0", "parallel_read_safe": True}
206 changes: 0 additions & 206 deletions Grammar/Grammar

This file was deleted.