@@ -62,22 +62,29 @@ def make_grammar_snippet(
6262
6363 group_name = options ['group' ]
6464 node_location = self .get_location ()
65- production_nodes = []
66- production_node = None
65+ current_lines = []
66+ production_node = addnodes .production ()
67+ production_nodes = [production_node ]
6768 for source_line in content :
6869 # Start a new production_node if there's text in the first
6970 # column of the line. (That means a new rule is starting.)
70- if production_node is None or not source_line [:1 ].isspace ():
71- production_node = addnodes .production (source_line )
71+ if not source_line [:1 ].isspace ():
72+ # set the raw source of the previous production
73+ production_node .rawsource = '\n ' .join (current_lines )
74+ current_lines .clear ()
75+ # start a new production_node
76+ production_node = addnodes .production ()
7277 production_nodes .append (production_node )
73- else :
74- production_node .rawsource += '\n ' + source_line
78+ current_lines .append (source_line )
7579 self .add_production_line (
7680 production_node ,
7781 source_line ,
7882 group_name = group_name ,
7983 location = node_location ,
8084 )
85+ # set the raw source of the final production
86+ production_node .rawsource = '\n ' .join (current_lines )
87+
8188 node = addnodes .productionlist (
8289 '' ,
8390 * production_nodes ,
@@ -100,9 +107,8 @@ def add_production_line(
100107 for match in self .grammar_re .finditer (source_line ):
101108 # Handle text between matches
102109 if match .start () > last_pos :
103- production_node += nodes .Text (
104- source_line [last_pos : match .start ()]
105- )
110+ unmatched_text = source_line [last_pos : match .start ()]
111+ production_node += nodes .Text (unmatched_text )
106112 last_pos = match .end ()
107113
108114 # Handle matches
0 commit comments