55from sphinx import addnodes
66from sphinx .util .docutils import SphinxDirective
77from sphinx .util .nodes import make_id
8+ from sphinx .domains .std import token_xrefs
89
910
1011class GrammarSnippetBase (SphinxDirective ):
@@ -36,11 +37,11 @@ def make_grammar_snippet(self, options, content):
3637 )
3738
3839 grammar_re = re .compile (
39- """
40+ r """
4041 (?P<rule_name>^[a-zA-Z0-9_]+) # identifier at start of line
4142 (?=:) # ... followed by a colon
4243 |
43- [`] (?P<rule_ref>[a-zA-Z0-9_]+)[`] # identifier in backquotes
44+ (?P<rule_ref>`[^\s`]+`) # identifier in backquotes
4445 |
4546 (?P<single_quoted>'[^']*') # string in 'quotes'
4647 |
@@ -65,16 +66,9 @@ def make_grammar_snippet(self, options, content):
6566 }
6667 match groupdict :
6768 case {'rule_name' : name }:
68- literal += self .make_link_to_token (group_name , name )
69- case {'rule_ref' : name }:
70- ref_node = addnodes .pending_xref (
71- name ,
72- reftype = "token" ,
73- refdomain = "std" ,
74- reftarget = f"{ group_name } :{ name } " ,
75- )
76- ref_node += nodes .Text (name )
77- literal += ref_node
69+ literal += self .make_link_target_for_token (group_name , name )
70+ case {'rule_ref' : ref_text }:
71+ literal += token_xrefs (ref_text , group_name )
7872 case {'single_quoted' : name } | {'double_quoted' : name }:
7973 string_node = nodes .inline (classes = ['nb' ])
8074 string_node += nodes .Text (name )
@@ -91,8 +85,8 @@ def make_grammar_snippet(self, options, content):
9185
9286 return [node ]
9387
94- def make_link_to_token (self , group_name , name ):
95- """Return a literal node that links to the given grammar token"""
88+ def make_link_target_for_token (self , group_name , name ):
89+ """Return a literal node which is a link target for the given token"""
9690 name_node = addnodes .literal_strong ()
9791
9892 # Cargo-culted magic to make `name_node` a link target
0 commit comments