Metadata section treats all subitems equal, and does not enforce their appearance in the grammar. I think it needs to be rewritten completely. I give it a try, I come back to this. (soon)
Important criteria are, items of metadata should not have a enforced order. Some items are however obligatory and some are optionally.
Sorry, I mix up something (was working on a similar problem in another project).
I have to look at it again, the problem is indeed that the grammar allows duplication, which should not be, as I originally described.
I think you are right, it is a weakness of Antlr that it cannot easily prevent duplicates in a construction like this where there is no order.
It is doable when there are two or three possibilities, but when there are 6, the number of possibilities gets very high, and post-processing seems the best way. Maybe a comment in the grammar to warn people that they need to do that.
I have solved it like this
In top of the grammar
private Set<String> metadataKeys = new HashSet<String>();
metaDataTagAdlVersion '=' VERSION_ID
notifyErrorListeners(String.format("Error adding \"%s\"; appears more then once", $metaDataTagAdlVersion.text));
And then repeat it at every metadata-key processing in the grammar file. It tries to add the metadata key to a Set, but if this fails, because the key is already there, it adds an error to the errorlistener
I think you risk making the grammar too complex. The ADL Workbench does 6 phases of processing:
parse => generate raw AOM structure
post -parse adjustments on AOM structure (e.g. type replacements found in AOM profile)
phase 1 validate
phase 2 validate
phase 3 validate
post compile actions
This means that each phase is relatively easy to code and understand. At the moment, the meta-data validation is done in phase 1 validation.
It is a matter of opinion. Antlr supports embedded post-processing.
Against is that it becomes target-language depending. Pro is that it collects the parser-errors in the parser-error-stack, so that the parser checks and delivers all attributes to build the AOM-structure
I agree that the grammar code looks a mess, but that is very often the case with grammar code.
I have a good editor, that helps a lot, IntelliJ with the Antlr-plugin, not perfect, but quite good.