def parseRoboDoc(lines,inFile): info={ 'special':{},'order':[],'trash':[],'name':[],'brief':[], 'date':[],'author':[],'note':[],'todo':[],'params':[],'bug':[], 'literature':[],'history':[],'see':[],'pname':[],'version':[], 'warning':[],'origLines':[]} sAtt=info['brief'] transl={ 'NAME':'name','FUNCTION':'brief','AUTHOR':'author', 'CREATION DATE':'date','DESCRIPTION':'brief','PURPOSE':'brief', 'INPUTS':'params','ARGUMENTS':'params','PARAMETERS':'params', 'ATTRIBUTES':'params','BUGS':'bug','TODO':'todo', 'NOTE':'note','NOTES':'note','LITERATURE':'literature', 'REFERENCES':'literature','OUTPUTS':'params','AUTHORS':'author', 'AURHOR':'author','WARNINGS':'warning','WARNING':'warning', 'MODIFICATION HISTORY':'history','HISTORY':'history', 'SEE ALSO':'see','SOURCE':'trash','SYNOPSIS':'trash'} roboDocRe=re.compile(" *!!?(.*)") headerRe=re.compile(r" *!!\*+[cdfhmstuv]?\** *(?P<name>[a-zA-Z_0-9/]+) *(?:\[(?P<version>[0-9.a-zA-Z_ ]+)\])?") labelRe=re.compile(" +(?P<label>NAME|COPYRIGHT|USAGE|FUNCTION|DESCRIPTION|PURPOSE|AUTHOR|CREATION DATE|MODIFICATION HISTORY|HISTORY|INPUTS|ARGUMENTS|OPTIONS|PARAMETERS|SWITCHES|OUTPUT|SYNOPSIS|SIDE EFFECTS|RESULT|RETURN VALUE|EXAMPLE|NOTES?|WARNINGS?|ERROR|DIAGNOSTICS|BUGS|TODO|IDEAS|PORTABILITY|SEE ALSO|METHODS|ATTRIBUTES|SOURCE|LITERATURE|TAGS|USED BY|[A-Z]+ ?[A-Z]* ?[A-Z]*) *$") fluffRe=re.compile(r" *([-+*#!= ]{3,})? *$") preprocessorRe=re.compile(r" *#") m=headerRe.match(lines[0]) if m: if m.group('version'): info['version'].append(m.group('version')) if m.group('name'): info['pname'].append(m.group('name')) info['origLines'].append(lines[0]) lines=lines[1:] while 1: for line in lines: info['origLines'].extend(lines) m=roboDocRe.match(line) if not m: raise SyntaxError('unexpectly out of robodoc') l=m.groups()[0]+'\n' m2=labelRe.match(l) if m2: label=m2.group('label') if transl.has_key(label): sAtt=info[transl[label]] info['order'].append(transl[label]) else: if info['order'] and info['order'][-1]=='author': # print ('treating "%s"as author'%(label)) sAtt.append(l) else: label=label.title() info['order'].append(label) print ('WARNING, found odd label "%s", added to special'%(label)) if info['special'].has_key(label): sAtt=info['special'][label] else: sAtt=[] info['special'][label]=sAtt else: m=fluffRe.match(l) if m: info['trash'].append(l) if len(sAtt) and not sAtt[-1].isspace(): sAtt.append('\n') else: sAtt.append(l.replace('@author','')) while 1: (jline,comments,lines)=readFortranLine(inFile) if not lines: break if jline!=None and jline!="" and not jline.isspace() or preprocessorRe.match(lines[0]): break if roboDocRe.match(lines[0]): break else: for l in lines: if not l.isspace(): print "rmv",repr(l) info['origLines'].extend(lines) if not lines: break if jline!=None and jline!="" and not jline.isspace() or preprocessorRe.match(lines[0]): break if not info['name'] and info['pname']: info['name'].append(os.path.basename(info['pname'][0])) return (jline,comments,lines,info)
def parseRoboDoc(lines, inFile): info = { 'special': {}, 'order': [], 'trash': [], 'name': [], 'brief': [], 'date': [], 'author': [], 'note': [], 'todo': [], 'params': [], 'bug': [], 'literature': [], 'history': [], 'see': [], 'pname': [], 'version': [], 'warning': [], 'origLines': [] } sAtt = info['brief'] transl = { 'NAME': 'name', 'FUNCTION': 'brief', 'AUTHOR': 'author', 'CREATION DATE': 'date', 'DESCRIPTION': 'brief', 'PURPOSE': 'brief', 'INPUTS': 'params', 'ARGUMENTS': 'params', 'PARAMETERS': 'params', 'ATTRIBUTES': 'params', 'BUGS': 'bug', 'TODO': 'todo', 'NOTE': 'note', 'NOTES': 'note', 'LITERATURE': 'literature', 'REFERENCES': 'literature', 'OUTPUTS': 'params', 'AUTHORS': 'author', 'AURHOR': 'author', 'WARNINGS': 'warning', 'WARNING': 'warning', 'MODIFICATION HISTORY': 'history', 'HISTORY': 'history', 'SEE ALSO': 'see', 'SOURCE': 'trash', 'SYNOPSIS': 'trash' } roboDocRe = re.compile(" *!!?(.*)") headerRe = re.compile( r" *!!\*+[cdfhmstuv]?\** *(?P<name>[a-zA-Z_0-9/]+) *(?:\[(?P<version>[0-9.a-zA-Z_ ]+)\])?" ) labelRe = re.compile( " +(?P<label>NAME|COPYRIGHT|USAGE|FUNCTION|DESCRIPTION|PURPOSE|AUTHOR|CREATION DATE|MODIFICATION HISTORY|HISTORY|INPUTS|ARGUMENTS|OPTIONS|PARAMETERS|SWITCHES|OUTPUT|SYNOPSIS|SIDE EFFECTS|RESULT|RETURN VALUE|EXAMPLE|NOTES?|WARNINGS?|ERROR|DIAGNOSTICS|BUGS|TODO|IDEAS|PORTABILITY|SEE ALSO|METHODS|ATTRIBUTES|SOURCE|LITERATURE|TAGS|USED BY|[A-Z]+ ?[A-Z]* ?[A-Z]*) *$" ) fluffRe = re.compile(r" *([-+*#!= ]{3,})? *$") preprocessorRe = re.compile(r" *#") m = headerRe.match(lines[0]) if m: if m.group('version'): info['version'].append(m.group('version')) if m.group('name'): info['pname'].append(m.group('name')) info['origLines'].append(lines[0]) lines = lines[1:] while 1: for line in lines: info['origLines'].extend(lines) m = roboDocRe.match(line) if not m: raise SyntaxError('unexpectly out of robodoc') l = m.groups()[0] + '\n' m2 = labelRe.match(l) if m2: label = m2.group('label') if transl.has_key(label): sAtt = info[transl[label]] info['order'].append(transl[label]) else: if info['order'] and info['order'][-1] == 'author': # print ('treating "%s"as author'%(label)) sAtt.append(l) else: label = label.title() info['order'].append(label) print( 'WARNING, found odd label "%s", added to special' % (label)) if info['special'].has_key(label): sAtt = info['special'][label] else: sAtt = [] info['special'][label] = sAtt else: m = fluffRe.match(l) if m: info['trash'].append(l) if len(sAtt) and not sAtt[-1].isspace(): sAtt.append('\n') else: sAtt.append(l.replace('@author', '')) while 1: (jline, comments, lines) = readFortranLine(inFile) if not lines: break if jline != None and jline != "" and not jline.isspace( ) or preprocessorRe.match(lines[0]): break if roboDocRe.match(lines[0]): break else: for l in lines: if not l.isspace(): print "rmv", repr(l) info['origLines'].extend(lines) if not lines: break if jline != None and jline != "" and not jline.isspace( ) or preprocessorRe.match(lines[0]): break if not info['name'] and info['pname']: info['name'].append(os.path.basename(info['pname'][0])) return (jline, comments, lines, info)
if __name__=='__main__': inFile=file(sys.argv[1]) outF=file(sys.argv[1][:-1]+'f90','w') roboDocRe=re.compile(" *!!") # fluffRe=re.compile(r" *![+=* ]+$") basicFluffRe=re.compile(r" *! *\** *\*{20,} *$") basicFluff2Re=re.compile(r" *!! *\** *\** *$") separatePreRe=re.compile(r" *(?:(?:end +)?(?:(?P<module>module)|program) +[a-zA-Z]|type +[a-zA-Z]|(?:recursive +|pure +|elemental +)*(?:subroutine|function) +[a-zA-Z])",re.IGNORECASE) #separatePostRe=re.compile(r" *end +(?:subroutine|function|type|module)",re.IGNORECASE) bodyRe=re.compile(r" *(?P<end>end +)(?:type *(?![ ()])|subroutine|function).") isspace=1 didDoc=0 inBody=0 didModule=0 while 1: (jline,comments,lines)=readFortranLine(inFile) if not lines: break m=bodyRe.match(lines[0]) if m: if m.group('end'): inBody=None else: inBody=bodyRe.group() if roboDocRe.match(lines[0]): if basicFluff2Re.match(lines[0]): continue (jline,comments,lines,info)=parseRoboDoc(lines,inFile) output = StringIO.StringIO() didDocAtt=printRoboDoc(info,output,isspace) doc=output.getvalue() if not doc:
roboDocRe = re.compile(" *!!") # fluffRe=re.compile(r" *![+=* ]+$") basicFluffRe = re.compile(r" *! *\** *\*{20,} *$") basicFluff2Re = re.compile(r" *!! *\** *\** *$") separatePreRe = re.compile( r" *(?:(?:end +)?(?:(?P<module>module)|program) +[a-zA-Z]|type +[a-zA-Z]|(?:recursive +|pure +|elemental +)*(?:subroutine|function) +[a-zA-Z])", re.IGNORECASE) #separatePostRe=re.compile(r" *end +(?:subroutine|function|type|module)",re.IGNORECASE) bodyRe = re.compile( r" *(?P<end>end +)(?:type *(?![ ()])|subroutine|function).") isspace = 1 didDoc = 0 inBody = 0 didModule = 0 while 1: (jline, comments, lines) = readFortranLine(inFile) if not lines: break m = bodyRe.match(lines[0]) if m: if m.group('end'): inBody = None else: inBody = bodyRe.group() if roboDocRe.match(lines[0]): if basicFluff2Re.match(lines[0]): continue (jline, comments, lines, info) = parseRoboDoc(lines, inFile) output = StringIO.StringIO() didDocAtt = printRoboDoc(info, output, isspace) doc = output.getvalue() if not doc: