'A more general version of getRuleInvocationStack where you can
pass in, for example, a RecognitionException to get it\'s rule
stack trace.  This routine is shared with all recognizers, hence,
static.
TODO: move to a utility class or something; weird having lexer call
this'

def _getRuleInvocationStack(cls, module):

    rules = []
    for frame in reversed(inspect.stack()):
        code = frame[0].f_code
        codeMod = inspect.getmodule(code)
        if (codeMod is None):
            continue
        if (codeMod.__name__ != module):
            continue
        if (code.co_name in ('nextToken', '<module>')):
            continue
        rules.append(code.co_name)
    return rules
