Ich schreibe einen scanner für das SCons build system. Da SCons auf python 1.5.2 aufbaut will ich dies nicht untergraben in dem ich code für höhere versionen schreibe. Es wäre toll wenn jemand den folgenden Code studieren und falls sinnvoll Verbeseerungsvorschläge unterbreiten könnte.
Danke
Code: Alles auswählen
import re
def flatten(l):
result = []
for el in l:
if hasattr(el, "__iter__") and not isinstance(el, basestring):
result.extend(flatten(el))
else:
result.append(el)
return result
def tokenize(l):
result = []
for el in l:
result += el.split(', ')
return result
module_string = open('TestProg.mod').read();
import_re = re.compile(r'^IMPORT\s+([^;]*)|^FROM\s+([^\s]*)', re.M)
imports = import_re.findall(module_string)
imports = flatten(imports)
imports = filter(lambda x: x != '', imports)
imports = tokenize(imports)
imports = map(lambda x: x.strip(), imports)
imports = map(lambda x: x + '.def', imports)
print imports
Code: Alles auswählen
MODULE TestProg;
IMPORT ModulA;
IMPORT ModulB, ModulC;
FROM ModulD IMPORT FunctionA;
FROM ModulE IMPORT FunctionB, FunctionC;
IMPORT ModulF ;
IMPORT
ModulG , ModulH,
ModulI ;
FROM ModulJ IMPORT FunctionD , FunctionE ;
BEGIN
END TestProg.
Code: Alles auswählen
['ModulA.def', 'ModulB.def', 'ModulC.def', 'ModulD.def', 'ModulE.def', 'ModulF.def', 'ModulG.def', 'ModulH.def', 'ModulI.def', 'ModulJ.def']
Wenn das IMPORT eines FROM IMPORT statements in einer neuen Zeile steht wird es als IMPORT interpretiert. Eine Idee?