Extendable Enum like Type?
On 07/19/2019 01:23 AM, Antoon Pardon wrote:
> I don't seem to have made myself clear. The grammar with its Terminals
> and NonTerminals is read in from a file. The program doesn't know what
> they will be.
> For the moment what I am thinking about is something as follows:
> grammar = LoadGrammer(GrammarFile)
> EnumList = ['class NonTerminal(Enum):\n']
> for Index, NonTerminal in enumerate(grammar.NonTerminals):
> EnumList.append(' %s = %d\n' % (NonTerminal, Index))
> exec(''.join(EnumList), ..., ...)
> The problem with this approach is that I can't use these values until
> the whole grammer is loaded. I would prefer some way to add values
> during the loading of the grammar.
Ah, you want `extend_enum` from the aenum library:
non_terminal = ....
index = ....
extend_enum(NonTerminal, non_terminal, index)