-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-40939: Add the new grammar to the grammar specification documentation #19969
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de4d919
bpo-40334: Add the new grammar to the grammar specification documenta…
pablogsal 22e6c71
Update Doc/tools/extensions/peg_highlight.py
pablogsal c83284e
Tweak text intro to the grammar
gvanrossum 27f7f00
Suppress all rules named invalid_* or incorrect_*
gvanrossum 266a99a
Now we can remove Grammar/Grammar
gvanrossum 13aa70d
More conservative suppression of lookaheads
gvanrossum 7705e02
Lengthen intro for grammar.rst
gvanrossum bac8971
Fix typo and comma
gvanrossum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
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 | ||
used to generate the CPython parser (see :source:`Grammar/python.gram`). | ||
The version here omits details related to code generation and | ||
error recovery. | ||
|
||
.. literalinclude:: ../../Grammar/Grammar | ||
The notation is a mixture of `EBNF | ||
<https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form>`_ | ||
and `PEG <https://en.wikipedia.org/wiki/Parsing_expression_grammar>`_. | ||
In particular, ``&`` followed by a symbol, token or parenthesized | ||
group indicates a positive lookahead (i.e., is required to match but | ||
not consumed), while ``!`` indicates a negative lookahead (i.e., is | ||
required _not_ to match). We use the ``|`` separator to mean PEG's | ||
"ordered choice" (written as ``/`` in traditional PEG grammars). | ||
|
||
.. literalinclude:: ../../Grammar/python.gram | ||
:language: peg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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"(?<=\|\s)(&\w+\s?)", bygroups(None)), | ||
(r"(?<=\|\s)(&'.+'\s?)", bygroups(None)), | ||
(r'(?<=\|\s)(&".+"\s?)', bygroups(None)), | ||
(r"(?<=\|\s)(&\(.+\)\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} |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.