Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Align the grammar documentation with Python's actual grammar #127833

Open
Labels
docsDocumentation in the Doc dir
@encukou

Description

@encukou

Documentation

The current documentation of Python syntax (the later chapters of thelanguage reference) uses hand-maintainedproduction lists, like this:

A)

compound_stmt ::=  if_stmt                   | while_stmt                   | for_stmt                   | try_stmt                   | with_stmt                   | match_stmt                   | funcdef                   | classdef                   | async_with_stmt                   | async_for_stmt                   | async_funcdefsuite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENTstatement     ::=  stmt_list NEWLINE | compound_stmtstmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

There is no mechanism to ensure that these are in sync with theactual grammar, and they inevitably do get out of sync.
See some of the“docs” issues mentioning “grammar”.

It's not easy to write an automatic tool to keep them in sync, because wedo want to elide some details -- the parser rules,unnecessary lookaheads, cuts, etc. But, it'spossible to write it, and we wrote aproof of concept, which will need to be rewritten, tuned, and reviewed. Before introducing it, I'd like to go through all the docs, correct the existing documentation, bring it closer to what a tool could generate, and discuss what theideal presentation would look like. That needs to be a manual process, and it will also need to touch theprose that's next to the grammar snippets.

As a first step, I propose an update to the tooling, which brings thepresentation a bit closer to thepython.gram syntax.

From theexisting ReST source, we can get this:

B)

compound_stmt: if_stmt               | while_stmt               | for_stmt               | try_stmt               | with_stmt               | match_stmt               | funcdef               | classdef               | async_with_stmt               | async_for_stmt               | async_funcdefsuite:         stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENTstatement:     stmt_list NEWLINE | compound_stmtstmt_list:     simple_stmt (";" simple_stmt)* [";"]

Since Sphinx hard-codes theproductionlist formatting (the::= symbol and the aligning), we'll need to override theproductionlist directive to achieve this.

Then, by changing the ReST and using a different directive, we can get to something like:

C)

compound_stmt:    | if_stmt    | while_stmt    | for_stmt    | try_stmt    | with_stmt    | match_stmt    | funcdef    | classdef    | async_with_stmt    | async_for_stmt    | async_funcdefsuite:    | stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENTstatement:    | stmt_list NEWLINE | compound_stmtstmt_list:    | simple_stmt (";" simple_stmt)* [";"]

I propose to go from A) to B) at once (by overridingproductionlist), and from B) to C) gradually, while also updating the content (including changing rule names to match the grammar, and adjusting/reorganizing nearby prose).
I think that the B) and C) styles are similar enough that mixing them in a single version of the docs should not be jarring.

By the way, one additional benefit of a custom directive is that we can add syntax highlighting. (Ideally, with support from the theme.) I think that making strings stand out makes the listings more readable:

image

Linked PRs

Other related PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp