PHP (7.4), 148148 143 bytes
-5 bytes by stealing RegEx idea from @Arnauld.
<?=preg_replace_callback('/[@_]\w+(".*?="|([^"]*"[^"]*")*[^"]*$[@_]\w+)/',fn($m)=>['=>$m[1]?[':'][$m[0][0]>A].($_GET[$w=substr($m[0],1)]?:$_GET[$w]=++$_GET[0]):$m[0],$argv[1]);
Define labels with @
and refer to them with _
.
Captures any @<label>
or _<label>
which aren't inside double quotes ("..."
) and replaces them with a unique number for every unique label, starting from 1
. Also adds the :
when replacing labels that have @
before them.
PHP's global variable $_GET
is used to store and access last used id and unique ids for each label inside the arrow function. $_GET[0]
holds last used id and $_GET[<label>]
holds unique id for <label>
.