Skip to content

Commit

Permalink
Allow production lists to refer to tokens from other production groups.
Browse files Browse the repository at this point in the history
Outside production lists, syntax "`foo:bar`" already makes it possible to
refer to the production "bar" of group "foo". This commit offers the same
feature inside production lists. Similarly to the reference syntax,
prefixing with a tilde prevents the group from being displayed.

This commit also makes it possible to use "`:bar`" to refer to production
"bar" from a production list without a group name. This is especially
useful when one has a main (unnamed) grammar and one or several named
extensions that need to refer to it.
  • Loading branch information
silene committed Sep 28, 2020
1 parent 3f58415 commit 88813d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
23 changes: 16 additions & 7 deletions doc/usage/restructuredtext/directives.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1199,20 +1199,29 @@ the definition of the symbol. There is this directive:
the following definition. If the definition spans multiple lines, each
continuation line must begin with a colon placed at the same column as in
the first line.
Blank lines are not allowed within ``productionlist`` directive arguments.

The definition can contain token names which are marked as interpreted text
(e.g., "``sum ::= `integer` "+" `integer```") -- this generates
cross-references to the productions of these tokens. Outside of the
production list, you can reference to token productions using
:rst:role:`token`.

The *productionGroup* argument to :rst:dir:`productionlist` serves to
distinguish different sets of production lists that belong to different
grammars. Multiple production lists with the same *productionGroup* thus
define rules in the same scope.

Blank lines are not allowed within ``productionlist`` directive arguments.
Inside of the production list, tokens implicitly refer to productions
from the current group. You can refer to the production of another
grammar by prefixing the token with its group name and a colon, e.g,
"``otherGroup:sum``". If the group of the token should not be shown in
the production, it can be prefixed by a tilde, e.g.,
"``~otherGroup:sum``". To refer to a production from an unnamed
grammar, the token should be prefixed by a colon, e.g., "``:sum``".

The definition can contain token names which are marked as interpreted text
(e.g. "``sum ::= `integer` "+" `integer```") -- this generates
cross-references to the productions of these tokens. Outside of the
production list, you can reference to token productions using
:rst:role:`token`.
However, if you have given a *productionGroup* argument you must prefix the
Outside of the production list,
if you have given a *productionGroup* argument you must prefix the
token name in the cross-reference with the group name and a colon,
e.g., "``myGroup:sum``" instead of just "``sum``".
If the group should not be shown in the title of the link either
Expand Down
22 changes: 18 additions & 4 deletions sphinx/domains/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
# RE for option descriptions
option_desc_re = re.compile(r'((?:/|--|-|\+)?[^\s=[]+)(=?\s*.*)')
# RE for grammar tokens
token_re = re.compile(r'`(\w+)`', re.U)
token_re = re.compile(r'`((~?\w*:)?\w+)`', re.U)


class GenericObject(ObjectDescription):
Expand Down Expand Up @@ -459,9 +459,23 @@ def token_xrefs(text: str, productionGroup: str = '') -> List[Node]:
if m.start() > pos:
txt = text[pos:m.start()]
retnodes.append(nodes.Text(txt, txt))
refnode = pending_xref(m.group(1), reftype='token', refdomain='std',
reftarget=productionGroup + m.group(1))
refnode += nodes.literal(m.group(1), m.group(1), classes=['xref'])
token = m.group(1)
if ':' in token:
if token[0] == '~':
_, title = token.split(':')
target = token[1:]
elif token[0] == ':':
title = token[1:]
target = title
else:
title = token
target = token
else:
title = token
target = productionGroup + token
refnode = pending_xref(title, reftype='token', refdomain='std',
reftarget=target)
refnode += nodes.literal(token, title, classes=['xref'])
retnodes.append(refnode)
pos = m.end()
if pos < len(text):
Expand Down

0 comments on commit 88813d5

Please sign in to comment.