#(C) 2007 by Wolfgang Fellger <wolfgangfellger@gmx.de>
import re

#Checks an ada source file (given as string) against various elements of
#the styleguide. Returns either True or a string stating what's wrong.
def doCheckFile(Data):
    #Check for tabs
    if "\t" in Data:
        return 'Tab character(s) found'
    
    #Check for too long lines
    m = re.search("^.{80,}$", Data, re.M)
    if m != None:
        return "Line too long: \n %s" % m.group()

    #Check for (obviously) wrong indentation (must be multiple of 2)
    for m in re.finditer(r"\n( *)\S.*", Data):
        if (len(m.group(1)) % 2) != 0:
            return "Indentation not multiple of 2:" + m.group()

    #Check for wrongly aligned comments:
    #Find all comments and leading whitespace at next line (but ignore
    # completely blank lines)
    for m in re.finditer(r"\n( *)--.*", Data):
        nextline = re.match(r'( *)\S.*', Data[m.end()+1:])
        if nextline and len(m.group(1)) != len(nextline.group(1)):
            return "Mis-aligned comment found:%s\n%s" % (m.group(), nextline.group())

    #Remove comments and strings for further processing
    Data = re.sub(re.compile("--.*$", re.M), "", Data)
    Data = re.sub(re.compile('".*?"', re.M), '"(string)"', Data)

    #Check for opening brackets without spaces
    m = re.search("^.*?([A-Za-z0-9]\().*?$", Data, re.M)
    if m != None:
        return "Styleguide violation (opening bracket): \n" + m.group()

    #Check for spaces behind ',', ':' and various other operators
    m = re.search('.*?[,:=&>+][A-Za-z0-9"(].*', Data, re.M)
    if m != None:
        return "Styleguide violation (space required): \n" + m.group()

    #Check for parameter modifiers
    m = re.search("^ +(procedure|function) [A-Za-z0-9_]+ \(.*?: [^io].*", Data, re.M)
    if m != None:
        return "Styleguide violation (in/out modifier required): \n" + m.group()

    return True

