def countedArray( expr, intExpr=None ):
arrayExpr = Forward()<EOL>def countFieldParseAction(s,l,t):<EOL><INDENT>n = t[<NUM_LIT:0>]<EOL>arrayExpr << (n and Group(And([expr]*n)) or Group(empty))<EOL>return []<EOL><DEDENT>if intExpr is None:<EOL><INDENT>intExpr = Word(nums).setParseAction(lambda t:int(t[<NUM_LIT:0>]))<EOL><DEDENT>else:<EOL><INDENT>intExpr = intExpr.copy()<EOL><DEDENT>intExpr.setName("<STR_LIT>")<EOL>intExpr.addParseAction(countFieldParseAction, callDuringTry=True)<EOL>return ( intExpr + arrayExpr ).setName('<STR_LIT>' + _ustr(expr) + '<STR_LIT>')<EOL>
Helper to define a counted list of expressions.

    This helper defines a pattern of the form::

        integer expr expr expr...

    where the leading integer tells how many expr expressions follow.
    The matched tokens returns the array of expr tokens as a list - the
    leading count token is suppressed.

    If ``intExpr`` is specified, it should be a pyparsing expression
    that produces an integer value.

    Example::

        countedArray(Word(alphas)).parseString('2 ab cd ef')  # -> ['ab', 'cd']

        # in this parser, the leading integer value is given in binary,
        # '10' indicating that 2 values are in the array
        binaryConstant = Word('01').setParseAction(lambda t: int(t[0], 2))
        countedArray(Word(alphas), intExpr=binaryConstant).parseString('10 ab cd ef')  # -> ['ab', 'cd']