import re, os, sys, time

DEBUG = 0

g_pattSpace = re.compile('\s*')
def Indent(indent, data):
    if data[0] == '\n': data = data[1:]
    lines = data.split('\n')
    space = sys.maxint
    for line in lines:
        match = g_pattSpace.match(line)
        if match and match.end() < len(line):
            if match.end() < space:
                space = match.end()
    output = ''
    for line in lines:
        output = output + indent + line.rstrip()[space:] + '\n'
    return output


def CreateFile(filename, format):
    outfile = open(filename, 'w')
    if format == 'c':
        start_comment = '/*'
        middle_comment = ' *'
        end_comment = ' */'
    elif format =='python':
        start_comment = '#'
        middle_comment = '#'
        end_comment = '#'
    else:
        raise Exception('Unknown file format: %s' % format)
    outfile.write('%s\n' % start_comment)
    outfile.write('%s DO NOT EDIT THIS FILE!\n' % middle_comment)
    outfile.write('%s\n' % middle_comment)
    outfile.write('%s Parser generated by BisonGen on %s.\n' %
                  (middle_comment, time.ctime(time.time())))
    outfile.write('%s\n' % end_comment)
    outfile.write('\n')
    return outfile
