Skip to content
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
Small tweaks
  • Loading branch information
AA-Turner committed Mar 12, 2025
commit e591450ed853e138b48e55883c183fe441f11b6e
24 changes: 15 additions & 9 deletions Doc/tools/extensions/grammar_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,29 @@ def make_grammar_snippet(

group_name = options['group']
node_location = self.get_location()
production_nodes = []
production_node = None
current_lines = []
production_node = addnodes.production()
production_nodes = [production_node]
for source_line in content:
# Start a new production_node if there's text in the first
# column of the line. (That means a new rule is starting.)
if production_node is None or not source_line[:1].isspace():
production_node = addnodes.production(source_line)
if not source_line[:1].isspace():
# set the raw source of the previous production
production_node.rawsource = '\n'.join(current_lines)
current_lines.clear()
# start a new production_node
production_node = addnodes.production()
production_nodes.append(production_node)
else:
production_node.rawsource += '\n' + source_line
current_lines.append(source_line)
self.add_production_line(
production_node,
source_line,
group_name=group_name,
location=node_location,
)
# set the raw source of the final production
production_node.rawsource = '\n'.join(current_lines)

node = addnodes.productionlist(
'',
*production_nodes,
Expand All @@ -100,9 +107,8 @@ def add_production_line(
for match in self.grammar_re.finditer(source_line):
# Handle text between matches
if match.start() > last_pos:
production_node += nodes.Text(
source_line[last_pos : match.start()]
)
unmatched_text = source_line[last_pos : match.start()]
production_node += nodes.Text(unmatched_text)
last_pos = match.end()

# Handle matches
Expand Down