Read C++ enum in python
Mark Tolonen
metolone+gmane at gmail.com
Thu Aug 20 00:01:23 EDT 2009
"Mark Tolonen" <metolone+gmane at gmail.com> wrote in message
news:h6g9ig$vh0$1 at ger.gmane.org...
[snip]
>
> This is what 3rd party library pyparsing is great for:
>
> --------begin code----------
> from pyparsing import *
>
> # sample string with enums and other stuff
> sample = '''
> stuff before
>
> enum hello {
> Zero,
> One,
> Two,
> Three,
> Five=5,
> Six,
> Ten=10
> }
>
> in the middle
>
> enum blah
> {
> alpha,
> beta,
> gamma = 10 ,
> zeta = 50
> }
>
> at the end
> '''
>
> # syntax we don't want to see in the final parse tree
> _lcurl = Suppress('{')
> _rcurl = Suppress('}')
> _equal = Suppress('=')
> _comma = Suppress(',')
> _enum = Suppress('enum')
>
> identifier = Word(alphas,alphanums+'_')
> integer = Word(nums)
>
> enumValue = Group(identifier('name') + Optional(_equal +
> integer('value')))
> enumList = Group(enumValue + ZeroOrMore(_comma + enumValue))
> enum = _enum + identifier('enum') + _lcurl + enumList('list') + _rcurl
>
> # find instances of enums ignoring other syntax
> for item,start,stop in enum.scanString(sample):
> id = 0
> for entry in item.list:
> if entry.value != '':
> id = int(entry.value)
> print '%s_%s = %d' % (item.enum.upper(),entry.name.upper(),id)
> id += 1
> --------------end code------------
>
> Output:
> HELLO_ZERO = 0
> HELLO_ONE = 1
> HELLO_TWO = 2
> HELLO_THREE = 3
> HELLO_FIVE = 5
> HELLO_SIX = 6
> HELLO_TEN = 10
> BLAH_ALPHA = 0
> BLAH_BETA = 1
> BLAH_GAMMA = 10
> BLAH_ZETA = 50
Paul McGuire (pyparsing author) reminded me that:
enum.ignore(cppStyleComment)
before scanString will skip commented out sections as well.
-Mark
More information about the Python-list
mailing list