Skip to content

gh-127833: Docs: Add a grammar-snippet directive & replace productionlist #127835

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 20 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use Sphinx's token_xrefs function for formatting the tokens
  • Loading branch information
encukou committed Jan 29, 2025
commit 27321430b6affde9140e71f2f18ce9fa1a831b17
22 changes: 8 additions & 14 deletions Doc/tools/extensions/grammar_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sphinx import addnodes
from sphinx.util.docutils import SphinxDirective
from sphinx.util.nodes import make_id
from sphinx.domains.std import token_xrefs


class GrammarSnippetBase(SphinxDirective):
Expand Down Expand Up @@ -36,11 +37,11 @@ def make_grammar_snippet(self, options, content):
)

grammar_re = re.compile(
"""
r"""
(?P<rule_name>^[a-zA-Z0-9_]+) # identifier at start of line
(?=:) # ... followed by a colon
|
[`](?P<rule_ref>[a-zA-Z0-9_]+)[`] # identifier in backquotes
(?P<rule_ref>`[^\s`]+`) # identifier in backquotes
|
(?P<single_quoted>'[^']*') # string in 'quotes'
|
Expand All @@ -65,16 +66,9 @@ def make_grammar_snippet(self, options, content):
}
match groupdict:
case {'rule_name': name}:
literal += self.make_link_to_token(group_name, name)
case {'rule_ref': name}:
ref_node = addnodes.pending_xref(
name,
reftype="token",
refdomain="std",
reftarget=f"{group_name}:{name}",
)
ref_node += nodes.Text(name)
literal += ref_node
literal += self.make_link_target_for_token(group_name, name)
case {'rule_ref': ref_text}:
literal += token_xrefs(ref_text, group_name)
case {'single_quoted': name} | {'double_quoted': name}:
string_node = nodes.inline(classes=['nb'])
string_node += nodes.Text(name)
Expand All @@ -91,8 +85,8 @@ def make_grammar_snippet(self, options, content):

return [node]

def make_link_to_token(self, group_name, name):
"""Return a literal node that links to the given grammar token"""
def make_link_target_for_token(self, group_name, name):
"""Return a literal node which is a link target for the given token"""
name_node = addnodes.literal_strong()

# Cargo-culted magic to make `name_node` a link target
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy