mirror of
https://gitlab.com/scemama/irpf90.git
synced 2024-11-09 15:43:43 +01:00
Improved pased_text
This commit is contained in:
parent
fa48e8966f
commit
e1fb434bc8
@ -265,7 +265,7 @@ def create_irpf90_files():
|
||||
import os
|
||||
if command_line.do_run:
|
||||
def is_irpf90_file(filename):
|
||||
return filename.endswith(".irp.f")
|
||||
return filename.endswith(".irp.f") and filename.startswith("p")
|
||||
result = filter ( is_irpf90_file, os.listdir(os.getcwd()) )
|
||||
return result
|
||||
irpf90_files = create_irpf90_files()
|
||||
|
@ -6,6 +6,7 @@ from variables import variables
|
||||
from preprocessed_text import preprocessed_text
|
||||
from subroutines import subroutines
|
||||
import regexps
|
||||
import error
|
||||
|
||||
def find_variables_in_line(line):
|
||||
assert isinstance(line,Line)
|
||||
@ -17,6 +18,32 @@ def find_variables_in_line(line):
|
||||
result.append(var.name)
|
||||
return result
|
||||
|
||||
def find_subroutine_in_line(line):
|
||||
assert isinstance(line,Call)
|
||||
buffer = regexps.re_string.sub('',line.text)
|
||||
result = None
|
||||
for v in subroutines.keys():
|
||||
sub = subroutines[v]
|
||||
if sub.regexp.search(buffer) is not None:
|
||||
result = sub.name
|
||||
break
|
||||
assert result is not None
|
||||
return result
|
||||
|
||||
def check_touch(vars,main_vars):
|
||||
def fun(main_var):
|
||||
if main_var not in variables:
|
||||
error.fail(line,"Variable %s unknown"%(main_var,))
|
||||
x = variables[main_var]
|
||||
return [main_var]+x.others
|
||||
all_others = flatten( map(fun,main_vars) )
|
||||
all_others.sort()
|
||||
if len(all_others) == len(vars):
|
||||
for x,y in zip(vars,all_others):
|
||||
if x != y:
|
||||
message = "The following entities should be touched:\n"
|
||||
message = "\n".join([message]+map(lambda x: "- %s"%(x,),all_others))
|
||||
error.fail(line,message)
|
||||
|
||||
def get_parsed_text():
|
||||
result = []
|
||||
@ -50,7 +77,52 @@ def get_parsed_text():
|
||||
elif isinstance(line,Provide):
|
||||
l = line.text.split()[1:]
|
||||
varlist += l
|
||||
temp_result.append( (l,line) )
|
||||
temp_result.append( (l,Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
||||
elif isinstance(line,Call):
|
||||
sub = find_subroutine_in_line(line)
|
||||
if subroutines[sub].touches == []:
|
||||
t = Simple_line
|
||||
else:
|
||||
t = Provide_all
|
||||
temp_result.append( ([],t(line.i,line.text,line.filename)) )
|
||||
elif isinstance(line,Free):
|
||||
vars = line.text.split()
|
||||
if len(vars) < 2:
|
||||
error.fail(line,"Syntax error")
|
||||
vars = vars[1:]
|
||||
result.append( ([],Simple_line(line.i,"!%s"%(line.text),line.filename)) )
|
||||
for var in vars:
|
||||
temp_result.append( ([],Simple_line(line.i," call free_%s"%var,
|
||||
line.filename)) )
|
||||
elif isinstance(line,Touch):
|
||||
vars = line.text.split()
|
||||
if len(vars) < 2:
|
||||
error.fail(line,"Syntax error")
|
||||
vars = vars[1:]
|
||||
def fun(x):
|
||||
if x not in variables:
|
||||
error.fail(line,"Variable %s unknown"%(x,))
|
||||
main = variables[x].same_as
|
||||
if main is None:
|
||||
main = x
|
||||
return main
|
||||
main_vars = make_single( map(fun, vars) )
|
||||
check_touch(vars,main_vars)
|
||||
txt = " ".join(vars)
|
||||
result = [ ([],Simple_line(line.i,"!",line.filename)),
|
||||
([],Simple_line(line.i,"! >>> TOUCH %s"%(txt,),line.filename)) ]
|
||||
def fun(x):
|
||||
if x not in variables:
|
||||
error.fail(line,"Variable %s unknown"%(x,))
|
||||
return [ ([],Simple_line(line.i," call touch_%s"%(x,),line.filename)),
|
||||
([],Simple_line(line.i," %s_is_built = .True."%(x,),line.filename)) ]
|
||||
result += map( fun, main_vars )
|
||||
def fun(x):
|
||||
if x not in variables:
|
||||
error.fail(line,"Variable %s unknown"%(x,))
|
||||
return [ ([],Simple_line(line.i," %s_is_built = .True."%(x,),line.filename)) ]
|
||||
result += map( fun, main_vars )
|
||||
result += [ ([],Provide_all(line.i,"! <<< END TOUCH",line.filename)) ]
|
||||
else:
|
||||
l = find_variables_in_line(line)
|
||||
varlist += l
|
||||
|
@ -206,9 +206,7 @@ def form(text):
|
||||
re2 = re.compile(r"^\s*[!#]")
|
||||
re3 = re.compile(r"^\s*[^ 0-9]+")
|
||||
for line in text:
|
||||
if isinstance(line,Empty_line) or \
|
||||
isinstance(line,Doc) or \
|
||||
isinstance(line,Openmp):
|
||||
if type(line) in [ Empty_line, Doc, Openmp ]:
|
||||
pass
|
||||
else:
|
||||
if len(line.text) > 5:
|
||||
@ -237,8 +235,7 @@ def remove_comments(text,form):
|
||||
|
||||
if form == Free_form:
|
||||
for line in text:
|
||||
if isinstance(line,Openmp) or \
|
||||
isinstance(line,Doc):
|
||||
if type(line) in [ Openmp, Doc] :
|
||||
result.append(line)
|
||||
elif isinstance(line,Empty_line):
|
||||
pass
|
||||
@ -252,8 +249,7 @@ def remove_comments(text,form):
|
||||
return result
|
||||
else:
|
||||
for line in text:
|
||||
if isinstance(line,Openmp) or \
|
||||
isinstance(line,Doc):
|
||||
if type(line) in [ Openmp, Doc ]:
|
||||
result.append(line)
|
||||
elif isinstance(line,Empty_line):
|
||||
pass
|
||||
@ -513,8 +509,7 @@ def process_old_style_do(text):
|
||||
def change_matching_enddo(begin,number):
|
||||
for i in range(begin+1,len(text)):
|
||||
line = text[i]
|
||||
if isinstance(line,Continue) or \
|
||||
isinstance(line,Enddo):
|
||||
if type(line) in [Continue,Enddo]:
|
||||
buffer = line.text.split()
|
||||
if buffer[0] == number:
|
||||
text[i] = Enddo(line.i," enddo",line.filename)
|
||||
@ -586,13 +581,9 @@ endif'''
|
||||
def check_begin_end(text):
|
||||
'''Checks x...endx consistence'''
|
||||
|
||||
def filter_line(line):
|
||||
for type in [ Do, Enddo, If, Endif, Begin_provider, End_provider, \
|
||||
Subroutine, Function, End, Begin_doc, End_doc ]:
|
||||
if isinstance(line,type):
|
||||
return True
|
||||
return False
|
||||
|
||||
filter_line = lambda line: type(line) in [ Do, Enddo, If, Endif, \
|
||||
Begin_provider, End_provider, \
|
||||
Subroutine, Function, End, Begin_doc, End_doc ]
|
||||
text = filter(filter_line, text)
|
||||
|
||||
d = { 'do' : Do, 'enddo': Enddo,
|
||||
@ -610,8 +601,7 @@ def check_begin_end(text):
|
||||
level -= 1
|
||||
if level == 0:
|
||||
return True
|
||||
elif isinstance(line,End) or \
|
||||
isinstance(line,End_provider):
|
||||
elif type(line) in [End, End_provider]:
|
||||
break
|
||||
error.fail(text[begin],"Missing 'end%s'"%(x,))
|
||||
|
||||
@ -620,8 +610,7 @@ def check_begin_end(text):
|
||||
line = text[i]
|
||||
if isinstance(line,x):
|
||||
return
|
||||
for t in [ Subroutine, Function, Begin_provider ]:
|
||||
if isinstance(line,t):
|
||||
if type(line) in [ Subroutine, Function, Begin_provider ]:
|
||||
error.fail(text[begin],"Subroutine/Function/Provider is not closed")
|
||||
error.fail(text[begin],"Subroutine/Function/Provider is not closed")
|
||||
|
||||
@ -679,9 +668,7 @@ def move_to_top(text,t):
|
||||
begin = -1
|
||||
for i in range(len(text)):
|
||||
line = text[i]
|
||||
if isinstance(line,Begin_provider) or \
|
||||
isinstance(line,Subroutine) or \
|
||||
isinstance(line,Function):
|
||||
if type(line) in [ Begin_provider, Subroutine, Function ]:
|
||||
begin = i
|
||||
elif isinstance(line,t):
|
||||
text.pop(i)
|
||||
|
@ -10,7 +10,7 @@ class Sub(object):
|
||||
def __init__(self,text):
|
||||
assert isinstance(text,list)
|
||||
assert len(text) > 0
|
||||
assert isinstance(text[0],Subroutine) or isinstance(text[0],Function)
|
||||
assert type(text[0]) in [Subroutine, Function]
|
||||
self.text = text
|
||||
|
||||
############################################################
|
||||
|
20
src/util.py
20
src/util.py
@ -44,8 +44,22 @@ def make_single(l):
|
||||
d[x] = True
|
||||
return d.keys()
|
||||
|
||||
def flatten(l):
|
||||
if isinstance(l,list):
|
||||
result = []
|
||||
for i in range(len(l)):
|
||||
elem = l[i]
|
||||
result += flatten(elem)
|
||||
return result
|
||||
else:
|
||||
return [l]
|
||||
|
||||
if __name__ == '__main__':
|
||||
print build_dim([])
|
||||
print build_dim(['a'])
|
||||
print build_dim(['a','b'])
|
||||
a = 0
|
||||
print flatten(a)
|
||||
a = []
|
||||
print flatten(a)
|
||||
a = [1,2,3,4]
|
||||
print flatten(a)
|
||||
a = [1,2,3,[4,5,6,[7,8,9],10,],11,12,[13,14],15,16]
|
||||
print flatten(a)
|
||||
|
@ -46,8 +46,7 @@ class Variable(object):
|
||||
def others(self):
|
||||
if '_others' not in self.__dict__:
|
||||
result = []
|
||||
def f(l):
|
||||
return isinstance(l,Begin_provider) or isinstance(l,Cont_provider)
|
||||
f = lambda l: type(l) in [Begin_provider, Cont_provider]
|
||||
lines = filter(f, self.text)
|
||||
for line in lines:
|
||||
buffer = line.text.replace(']',',').split(',')
|
||||
@ -128,8 +127,7 @@ class Variable(object):
|
||||
############################################################
|
||||
def line(self):
|
||||
if '_line' not in self.__dict__:
|
||||
def f(l):
|
||||
return isinstance(l,Begin_provider) or isinstance(l,Cont_provider)
|
||||
f = lambda l: type(l) in [Begin_provider, Cont_provider]
|
||||
lines = filter(f, self.text)
|
||||
for line in lines:
|
||||
buffer = line.text.replace(']',',').split(',')
|
||||
|
Loading…
Reference in New Issue
Block a user