The set of the invoking module and all invoked modules is collected, including modules in turn invoked by those modules. These modules are going to be concatenated, but any name clashes are resolved first.
If any two invoked modules contain the definition of a rule of the same name, one of the rules is renamed:
If either of the pair is a rule that is not used in any other of the set of modules (whether shared or not), then that rule is renamed.
If both are used in other modules, then if one of the pair is a rule defined by the original invoking module, the other is renamed; otherwise either may be renamed.
A rule is renamed by generating a new unique name, different from all other rule names in the set of modules:
If the rule is defined with a naming
(i.e.
it has a name and an alias), the rule is redefined with a
naming
consisting of the new unique name
and the existing alias.
If the rule is defined using just a name (i.e. without an
alias), the rule is redefined with a naming
formed of the new unique name as name, and the old name as
alias.
All applications of the old name in the module grammar, and any of the other modules that use that rule are replaced with the new name.
Once all naming conflicts are resolved, all invoked modules are
appended to the invoking module, with the uses
and shares
specifications removed.
What these rules ensure is that:
there are no name clashes in the resulting grammar;
the original invoking grammar is not changed in any way, so that error messages about that grammar are given in terms the author expects;
changes within a module are preferred over changes that extend to other modules;
any resulting serialisation remains the same.