The canonical rules for deriving mobile domain names are stored in the MediaWiki config's wgMobileUrlTemplate variable.
Those rules are now mirrored in (1) the Varnish code and (2) our generation script for the wikis canonical data.
The rules will probably change only a few times a decade, so it seems like way too much work to automatically extract the rules. Instead, we should just add a comment to the code stating that the appropriate teams should be notified of changes.