import re

def Generate(outfile, parser):
    write = outfile.write

    write('\n# start condition definitions for the lexer\n')
    for (sc, (type, number)) in parser.lexer.states.items():
        write('%s = %d\n' % (sc, number))
    
    write('\n# the expressions and information for each rule\n')
    write('import re\n')
    write('patterns = {\n')

    initial = parser.lexer.patterns['INITIAL']
    defines = parser.lexer.defines
    count = 0
    actions = []
    for (scope, patterns) in parser.lexer.patterns.items():
        if not parser.lexer.states[scope][0]:
            # An inclusive state, include the unscoped ones as well
            patterns.extend(initial)
        groups = []
        for pattern in patterns:
            key = 'p%02d' % count
            groups.append((key, defines.expand(pattern.expression)))
            if pattern.begin == pattern.token == None:
                action = 'None'
            else:
                action = '(%s, ' % pattern.begin
                if pattern.token:
                    action = action + str(pattern.token)
                action = action + ')'
            actions.append((key, action))
            count = count + 1

        line = '    %s : ' % scope
        if parser.options.get('posix'):
            line = line + '['
            indent = ' '*len(line)
            for (name, expr) in groups:
                line = line + '(%s, re.compile(%s, re.M)),\n' % (repr(name),
                                                           repr(expr))
                write(line)
                line = indent
            line = line + ']'
        else:
            groups = map(lambda group: '(?P<%s>%s)' % group, groups)
            combined = '|'.join(groups)
            line = line + 're.compile(%s, re.M)' % repr(combined)
        write('%s,\n' % line)
    write('}\n')

    write('pattern_actions = {\n')
    for (name, action) in actions:
        write('    %s : %s,\n' % (repr(name), action))
    write('}\n')
    return
