[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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:

class NonTerminal(Enum):

while reading_grammar:
     non_terminal = ....
     index = ....
     extend_enum(NonTerminal, non_terminal, index)

See also:
- https://stackoverflow.com/a/33680929/208880
- https://stackoverflow.com/a/35899963/208880
- https://stackoverflow.com/a/36918171/208880
- https://stackoverflow.com/a/43004033/208880